1 17 package org.apache.el.util; 18 19 import java.beans.IntrospectionException ; 20 import java.beans.Introspector ; 21 import java.beans.PropertyDescriptor ; 22 import java.lang.reflect.Array ; 23 import java.lang.reflect.Method ; 24 import java.util.Arrays ; 25 26 import javax.el.ELException; 27 import javax.el.MethodNotFoundException; 28 import javax.el.PropertyNotFoundException; 29 30 import org.apache.el.lang.ELSupport; 31 32 33 39 public class ReflectionUtil { 40 41 protected static final String [] EMPTY_STRING = new String [0]; 42 43 protected static final String [] PRIMITIVE_NAMES = new String [] { "boolean", 44 "byte", "char", "double", "float", "int", "long", "short", "void" }; 45 46 protected static final Class [] PRIMITIVES = new Class [] { boolean.class, 47 byte.class, char.class, double.class, float.class, int.class, 48 long.class, short.class, Void.TYPE }; 49 50 53 private ReflectionUtil() { 54 super(); 55 } 56 57 public static Class forName(String name) throws ClassNotFoundException { 58 if (null == name || "".equals(name)) { 59 return null; 60 } 61 Class c = forNamePrimitive(name); 62 if (c == null) { 63 if (name.endsWith("[]")) { 64 String nc = name.substring(0, name.length() - 2); 65 c = Class.forName(nc, true, Thread.currentThread().getContextClassLoader()); 66 c = Array.newInstance(c, 0).getClass(); 67 } else { 68 c = Class.forName(name, true, Thread.currentThread().getContextClassLoader()); 69 } 70 } 71 return c; 72 } 73 74 protected static Class forNamePrimitive(String name) { 75 if (name.length() <= 8) { 76 int p = Arrays.binarySearch(PRIMITIVE_NAMES, name); 77 if (p >= 0) { 78 return PRIMITIVES[p]; 79 } 80 } 81 return null; 82 } 83 84 90 public static Class [] toTypeArray(String [] s) throws ClassNotFoundException { 91 if (s == null) 92 return null; 93 Class [] c = new Class [s.length]; 94 for (int i = 0; i < s.length; i++) { 95 c[i] = forName(s[i]); 96 } 97 return c; 98 } 99 100 105 public static String [] toTypeNameArray(Class [] c) { 106 if (c == null) 107 return null; 108 String [] s = new String [c.length]; 109 for (int i = 0; i < c.length; i++) { 110 s[i] = c[i].getName(); 111 } 112 return s; 113 } 114 115 123 public static Method getMethod(Object base, Object property, 124 Class [] paramTypes) throws MethodNotFoundException { 125 if (base == null || property == null) { 126 throw new MethodNotFoundException(MessageFactory.get( 127 "error.method.notfound", base, property, 128 paramString(paramTypes))); 129 } 130 131 String methodName = (property instanceof String ) ? (String ) property 132 : property.toString(); 133 134 Method method = null; 135 try { 136 method = base.getClass().getMethod(methodName, paramTypes); 137 } catch (NoSuchMethodException nsme) { 138 throw new MethodNotFoundException(MessageFactory.get( 139 "error.method.notfound", base, property, 140 paramString(paramTypes))); 141 } 142 return method; 143 } 144 145 protected static final String paramString(Class [] types) { 146 if (types != null) { 147 StringBuffer sb = new StringBuffer (); 148 for (int i = 0; i < types.length; i++) { 149 sb.append(types[i].getName()).append(", "); 150 } 151 if (sb.length() > 2) { 152 sb.setLength(sb.length() - 2); 153 } 154 return sb.toString(); 155 } 156 return null; 157 } 158 159 166 public static PropertyDescriptor getPropertyDescriptor(Object base, 167 Object property) throws ELException, PropertyNotFoundException { 168 String name = ELSupport.coerceToString(property); 169 PropertyDescriptor p = null; 170 try { 171 PropertyDescriptor [] desc = Introspector.getBeanInfo( 172 base.getClass()).getPropertyDescriptors(); 173 for (int i = 0; i < desc.length; i++) { 174 if (desc[i].getName().equals(name)) { 175 return desc[i]; 176 } 177 } 178 } catch (IntrospectionException ie) { 179 throw new ELException(ie); 180 } 181 throw new PropertyNotFoundException(MessageFactory.get( 182 "error.property.notfound", base, name)); 183 } 184 } 185 | Popular Tags |