1 25 package com.sun.el.util; 26 27 import java.beans.IntrospectionException ; 28 import java.beans.Introspector ; 29 import java.beans.PropertyDescriptor ; 30 import java.lang.reflect.Array ; 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.Modifier ; 33 import java.util.Arrays ; 34 35 import javax.el.ELException; 36 import javax.el.MethodNotFoundException; 37 import javax.el.PropertyNotFoundException; 38 39 import com.sun.el.lang.ELSupport; 40 41 47 public class ReflectionUtil { 48 49 protected static final String [] EMPTY_STRING = new String [0]; 50 51 protected static final String [] PRIMITIVE_NAMES = new String [] { "boolean", 52 "byte", "char", "double", "float", "int", "long", "short", "void" }; 53 54 protected static final Class [] PRIMITIVES = new Class [] { boolean.class, 55 byte.class, char.class, double.class, float.class, int.class, 56 long.class, short.class, Void.TYPE }; 57 58 61 private ReflectionUtil() { 62 super(); 63 } 64 65 public static Class forName(String name) throws ClassNotFoundException { 66 if (null == name || "".equals(name)) { 67 return null; 68 } 69 Class c = forNamePrimitive(name); 70 if (c == null) { 71 if (name.endsWith("[]")) { 72 String nc = name.substring(0, name.length() - 2); 73 c = Class.forName(nc, true, Thread.currentThread().getContextClassLoader()); 74 c = Array.newInstance(c, 0).getClass(); 75 } else { 76 c = Class.forName(name, true, Thread.currentThread().getContextClassLoader()); 77 } 78 } 79 return c; 80 } 81 82 protected static Class forNamePrimitive(String name) { 83 if (name.length() <= 8) { 84 int p = Arrays.binarySearch(PRIMITIVE_NAMES, name); 85 if (p >= 0) { 86 return PRIMITIVES[p]; 87 } 88 } 89 return null; 90 } 91 92 98 public static Class [] toTypeArray(String [] s) throws ClassNotFoundException { 99 if (s == null) 100 return null; 101 Class [] c = new Class [s.length]; 102 for (int i = 0; i < s.length; i++) { 103 c[i] = forName(s[i]); 104 } 105 return c; 106 } 107 108 113 public static String [] toTypeNameArray(Class [] c) { 114 if (c == null) 115 return null; 116 String [] s = new String [c.length]; 117 for (int i = 0; i < c.length; i++) { 118 s[i] = c[i].getName(); 119 } 120 return s; 121 } 122 123 131 public static Method getMethod(Object base, Object property, 132 Class [] paramTypes) throws MethodNotFoundException { 133 if (base == null || property == null) { 134 throw new MethodNotFoundException(MessageFactory.get( 135 "error.method.notfound", base, property, 136 paramString(paramTypes))); 137 } 138 139 String methodName = property.toString(); 140 141 Method method = getMethod(base.getClass(), methodName, paramTypes); 142 if (method == null) { 143 throw new MethodNotFoundException(MessageFactory.get( 144 "error.method.notfound", base, property, 145 paramString(paramTypes))); 146 } 147 return method; 148 } 149 150 157 158 static private Method getMethod(Class cl, String methodName, 159 Class [] paramTypes) { 160 161 Method m = null; 162 try { 163 m = cl.getMethod(methodName, paramTypes); 164 } catch (NoSuchMethodException ex) { 165 return null; 166 } 167 168 Class dclass = m.getDeclaringClass(); 169 if (Modifier.isPublic(dclass.getModifiers())) { 170 return m; 171 } 172 173 for (Class c: dclass.getInterfaces()) { 174 m = getMethod(c, methodName, paramTypes); 175 if (m != null) { 176 return m; 177 } 178 } 179 Class c = dclass.getSuperclass(); 180 if (c != null) { 181 m = getMethod(c, methodName, paramTypes); 182 if (m != null) { 183 return m; 184 } 185 } 186 return null; 187 } 188 189 protected static final String paramString(Class [] types) { 190 if (types != null) { 191 StringBuffer sb = new StringBuffer (); 192 for (int i = 0; i < types.length; i++) { 193 sb.append(types[i].getName()).append(", "); 194 } 195 if (sb.length() > 2) { 196 sb.setLength(sb.length() - 2); 197 } 198 return sb.toString(); 199 } 200 return null; 201 } 202 203 210 public static PropertyDescriptor getPropertyDescriptor(Object base, 211 Object property) throws ELException, PropertyNotFoundException { 212 String name = ELSupport.coerceToString(property); 213 PropertyDescriptor p = null; 214 try { 215 PropertyDescriptor [] desc = Introspector.getBeanInfo( 216 base.getClass()).getPropertyDescriptors(); 217 for (int i = 0; i < desc.length; i++) { 218 if (desc[i].getName().equals(name)) { 219 return desc[i]; 220 } 221 } 222 } catch (IntrospectionException ie) { 223 throw new ELException(ie); 224 } 225 throw new PropertyNotFoundException(MessageFactory.get( 226 "error.property.notfound", base, name)); 227 } 228 } 229 | Popular Tags |