1 25 package com.sun.el.lang; 26 27 import java.io.Externalizable ; 28 import java.io.IOException ; 29 import java.io.ObjectInput ; 30 import java.io.ObjectOutput ; 31 import java.lang.reflect.Method ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import javax.el.FunctionMapper; 36 37 import com.sun.el.util.ReflectionUtil; 38 39 43 public class FunctionMapperImpl extends FunctionMapper implements 44 Externalizable { 45 46 private static final long serialVersionUID = 1L; 47 48 protected Map functions = null; 49 50 56 public Method resolveFunction(String prefix, String localName) { 57 if (this.functions != null) { 58 Function f = (Function) this.functions.get(prefix + ":" + localName); 59 return f.getMethod(); 60 } 61 return null; 62 } 63 64 public void addFunction(String prefix, String localName, Method m) { 65 if (this.functions == null) { 66 this.functions = new HashMap (); 67 } 68 Function f = new Function(prefix, localName, m); 69 synchronized (this) { 70 this.functions.put(prefix+":"+localName, f); 71 } 72 } 73 74 79 public void writeExternal(ObjectOutput out) throws IOException { 80 out.writeObject(this.functions); 81 } 82 83 88 public void readExternal(ObjectInput in) throws IOException , 89 ClassNotFoundException { 90 this.functions = (Map ) in.readObject(); 91 } 92 93 public static class Function implements Externalizable { 94 95 protected transient Method m; 96 protected String owner; 97 protected String name; 98 protected String [] types; 99 protected String prefix; 100 protected String localName; 101 102 105 public Function(String prefix, String localName, Method m) { 106 if (localName == null) { 107 throw new NullPointerException ("LocalName cannot be null"); 108 } 109 if (m == null) { 110 throw new NullPointerException ("Method cannot be null"); 111 } 112 this.prefix = prefix; 113 this.localName = localName; 114 this.m = m; 115 } 116 117 public Function() { 118 } 120 121 126 public void writeExternal(ObjectOutput out) throws IOException { 127 128 out.writeUTF((this.prefix != null) ? this.prefix : ""); 129 out.writeUTF(this.localName); 130 131 if (this.owner != null) { 132 out.writeUTF(this.owner); 133 } else { 134 out.writeUTF(this.m.getDeclaringClass().getName()); 135 } 136 if (this.name != null) { 137 out.writeUTF(this.name); 138 } else { 139 out.writeUTF(this.m.getName()); 140 } 141 if (this.types != null) { 142 out.writeObject(this.types); 143 } else { 144 out.writeObject(ReflectionUtil.toTypeNameArray(this.m.getParameterTypes())); 145 } 146 } 147 148 153 public void readExternal(ObjectInput in) throws IOException , 154 ClassNotFoundException { 155 156 this.prefix = in.readUTF(); 157 if ("".equals(this.prefix)) this.prefix = null; 158 this.localName = in.readUTF(); 159 this.owner = in.readUTF(); 160 this.name = in.readUTF(); 161 this.types = (String []) in.readObject(); 162 } 163 164 public Method getMethod() { 165 if (this.m == null) { 166 try { 167 Class t = Class.forName(this.owner); 168 Class [] p = ReflectionUtil.toTypeArray(this.types); 169 this.m = t.getMethod(this.name, p); 170 } catch (Exception e) { 171 e.printStackTrace(); 172 } 173 } 174 return this.m; 175 } 176 177 public boolean matches(String prefix, String localName) { 178 if (this.prefix != null) { 179 if (prefix == null) return false; 180 if (!this.prefix.equals(prefix)) return false; 181 } 182 return this.localName.equals(localName); 183 } 184 185 188 public boolean equals(Object obj) { 189 if (obj instanceof Function) { 190 return this.hashCode() == obj.hashCode(); 191 } 192 return false; 193 } 194 195 198 public int hashCode() { 199 return (this.prefix + this.localName).hashCode(); 200 } 201 } 202 203 } 204 | Popular Tags |