Java/Reflection/Type
Содержание
Data type utilities
<source lang="java">
// // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd. // ------------------------------------------------------------------------ // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //
import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.util.HashMap;
/* ------------------------------------------------------------ */ /** TYPE Utilities.
* Provides various static utiltiy methods for manipulating types and their * string representations. * * @since Jetty 4.1 * @author Greg Wilkins (gregw) */
public class TypeUtil {
public static int CR = "\015"; public static int LF = "\012"; /* ------------------------------------------------------------ */ private static final HashMap name2Class=new HashMap(); static { name2Class.put("boolean",java.lang.Boolean.TYPE); name2Class.put("byte",java.lang.Byte.TYPE); name2Class.put("char",java.lang.Character.TYPE); name2Class.put("double",java.lang.Double.TYPE); name2Class.put("float",java.lang.Float.TYPE); name2Class.put("int",java.lang.Integer.TYPE); name2Class.put("long",java.lang.Long.TYPE); name2Class.put("short",java.lang.Short.TYPE); name2Class.put("void",java.lang.Void.TYPE); name2Class.put("java.lang.Boolean.TYPE",java.lang.Boolean.TYPE); name2Class.put("java.lang.Byte.TYPE",java.lang.Byte.TYPE); name2Class.put("java.lang.Character.TYPE",java.lang.Character.TYPE); name2Class.put("java.lang.Double.TYPE",java.lang.Double.TYPE); name2Class.put("java.lang.Float.TYPE",java.lang.Float.TYPE); name2Class.put("java.lang.Integer.TYPE",java.lang.Integer.TYPE); name2Class.put("java.lang.Long.TYPE",java.lang.Long.TYPE); name2Class.put("java.lang.Short.TYPE",java.lang.Short.TYPE); name2Class.put("java.lang.Void.TYPE",java.lang.Void.TYPE); name2Class.put("java.lang.Boolean",java.lang.Boolean.class); name2Class.put("java.lang.Byte",java.lang.Byte.class); name2Class.put("java.lang.Character",java.lang.Character.class); name2Class.put("java.lang.Double",java.lang.Double.class); name2Class.put("java.lang.Float",java.lang.Float.class); name2Class.put("java.lang.Integer",java.lang.Integer.class); name2Class.put("java.lang.Long",java.lang.Long.class); name2Class.put("java.lang.Short",java.lang.Short.class); name2Class.put("Boolean",java.lang.Boolean.class); name2Class.put("Byte",java.lang.Byte.class); name2Class.put("Character",java.lang.Character.class); name2Class.put("Double",java.lang.Double.class); name2Class.put("Float",java.lang.Float.class); name2Class.put("Integer",java.lang.Integer.class); name2Class.put("Long",java.lang.Long.class); name2Class.put("Short",java.lang.Short.class); name2Class.put(null,java.lang.Void.TYPE); name2Class.put("string",java.lang.String.class); name2Class.put("String",java.lang.String.class); name2Class.put("java.lang.String",java.lang.String.class); } /* ------------------------------------------------------------ */ private static final HashMap class2Name=new HashMap(); static { class2Name.put(java.lang.Boolean.TYPE,"boolean"); class2Name.put(java.lang.Byte.TYPE,"byte"); class2Name.put(java.lang.Character.TYPE,"char"); class2Name.put(java.lang.Double.TYPE,"double"); class2Name.put(java.lang.Float.TYPE,"float"); class2Name.put(java.lang.Integer.TYPE,"int"); class2Name.put(java.lang.Long.TYPE,"long"); class2Name.put(java.lang.Short.TYPE,"short"); class2Name.put(java.lang.Void.TYPE,"void"); class2Name.put(java.lang.Boolean.class,"java.lang.Boolean"); class2Name.put(java.lang.Byte.class,"java.lang.Byte"); class2Name.put(java.lang.Character.class,"java.lang.Character"); class2Name.put(java.lang.Double.class,"java.lang.Double"); class2Name.put(java.lang.Float.class,"java.lang.Float"); class2Name.put(java.lang.Integer.class,"java.lang.Integer"); class2Name.put(java.lang.Long.class,"java.lang.Long"); class2Name.put(java.lang.Short.class,"java.lang.Short"); class2Name.put(null,"void"); name2Class.put(java.lang.String.class,"java.lang.String"); } /* ------------------------------------------------------------ */ private static final HashMap class2Value=new HashMap(); static { try { Class[] s ={java.lang.String.class}; class2Value.put(java.lang.Boolean.TYPE, java.lang.Boolean.class.getMethod("valueOf",s)); class2Value.put(java.lang.Byte.TYPE, java.lang.Byte.class.getMethod("valueOf",s)); class2Value.put(java.lang.Double.TYPE, java.lang.Double.class.getMethod("valueOf",s)); class2Value.put(java.lang.Float.TYPE, java.lang.Float.class.getMethod("valueOf",s)); class2Value.put(java.lang.Integer.TYPE, java.lang.Integer.class.getMethod("valueOf",s)); class2Value.put(java.lang.Long.TYPE, java.lang.Long.class.getMethod("valueOf",s)); class2Value.put(java.lang.Short.TYPE, java.lang.Short.class.getMethod("valueOf",s)); class2Value.put(java.lang.Boolean.class, java.lang.Boolean.class.getMethod("valueOf",s)); class2Value.put(java.lang.Byte.class, java.lang.Byte.class.getMethod("valueOf",s)); class2Value.put(java.lang.Double.class, java.lang.Double.class.getMethod("valueOf",s)); class2Value.put(java.lang.Float.class, java.lang.Float.class.getMethod("valueOf",s)); class2Value.put(java.lang.Integer.class, java.lang.Integer.class.getMethod("valueOf",s)); class2Value.put(java.lang.Long.class, java.lang.Long.class.getMethod("valueOf",s)); class2Value.put(java.lang.Short.class, java.lang.Short.class.getMethod("valueOf",s)); } catch(Exception e) { e.printStackTrace(); } } /* ------------------------------------------------------------ */ private static Class[] stringArg = { java.lang.String.class }; /* ------------------------------------------------------------ */ private static int intCacheSize= Integer.getInteger("org.mortbay.util.TypeUtil.IntegerCacheSize",600).intValue(); private static Integer[] integerCache = new Integer[intCacheSize]; private static String[] integerStrCache = new String[intCacheSize]; private static Integer minusOne = new Integer(-1); /* ------------------------------------------------------------ */ /** Class from a canonical name for a type. * @param name A class or type name. * @return A class , which may be a primitive TYPE field.. */ public static Class fromName(String name) { return (Class)name2Class.get(name); } /* ------------------------------------------------------------ */ /** Canonical name for a type. * @param type A class , which may be a primitive TYPE field. * @return Canonical name. */ public static String toName(Class type) { return (String)class2Name.get(type); } /* ------------------------------------------------------------ */ /** Convert String value to instance. * @param type The class of the instance, which may be a primitive TYPE field. * @param value The value as a string. * @return The value as an Object. */ public static Object valueOf(Class type, String value) { try { if (type.equals(java.lang.String.class)) return value; Method m = (Method)class2Value.get(type); if (m!=null) return m.invoke(null,new Object[] {value}); if (type.equals(java.lang.Character.TYPE) || type.equals(java.lang.Character.class)) return new Character(value.charAt(0)); Constructor c = type.getConstructor(stringArg); return c.newInstance(new Object[] {value}); } catch(NoSuchMethodException e) { // LogSupport.ignore(log,e); } catch(IllegalAccessException e) { // LogSupport.ignore(log,e); } catch(InstantiationException e) { // LogSupport.ignore(log,e); } catch(InvocationTargetException e) { if (e.getTargetException() instanceof Error) throw (Error)(e.getTargetException()); // LogSupport.ignore(log,e); } return null; } /* ------------------------------------------------------------ */ /** Convert String value to instance. * @param type classname or type (eg int) * @param value The value as a string. * @return The value as an Object. */ public static Object valueOf(String type, String value) { return valueOf(fromName(type),value); } /* ------------------------------------------------------------ */ /** Convert int to Integer using cache. */ public static Integer newInteger(int i) { if (i>=0 && i<intCacheSize) { if (integerCache[i]==null) integerCache[i]=new Integer(i); return integerCache[i]; } else if (i==-1) return minusOne; return new Integer(i); } /* ------------------------------------------------------------ */ /** Convert int to String using cache. */ public static String toString(int i) { if (i>=0 && i<intCacheSize) { if (integerStrCache[i]==null) integerStrCache[i]=Integer.toString(i); return integerStrCache[i]; } else if (i==-1) return "-1"; return Integer.toString(i); } /* ------------------------------------------------------------ */ /** Convert long to String using cache. */ public static String toString(long i) { if (i>=0 && i<intCacheSize) { if (integerStrCache[(int)i]==null) integerStrCache[(int)i]=Long.toString(i); return integerStrCache[(int)i]; } else if (i==-1) return "-1"; return Long.toString(i); }
/* ------------------------------------------------------------ */ /** Parse an int from a substring. * Negative numbers are not handled. * @param s String * @param offset Offset within string * @param length Length of integer or -1 for remainder of string * @param base base of the integer * @exception NumberFormatException */ public static int parseInt(String s, int offset, int length, int base) throws NumberFormatException { int value=0; if (length<0) length=s.length()-offset; for (int i=0;i<length;i++) { char c=s.charAt(offset+i); int digit=c-"0"; if (digit<0 || digit>=base || digit>=10) { digit=10+c-"A"; if (digit<10 || digit>=base) digit=10+c-"a"; } if (digit<0 || digit>=base) throw new NumberFormatException(s.substring(offset,offset+length)); value=value*base+digit; } return value; } /* ------------------------------------------------------------ */ /** Parse an int from a byte array of ascii characters. * Negative numbers are not handled. * @param b byte array * @param offset Offset within string * @param length Length of integer or -1 for remainder of string * @param base base of the integer * @exception NumberFormatException */ public static int parseInt(byte[] b, int offset, int length, int base) throws NumberFormatException { int value=0; if (length<0) length=b.length-offset; for (int i=0;i<length;i++) { char c=(char)(0xff&b[offset+i]); int digit=c-"0"; if (digit<0 || digit>=base || digit>=10) { digit=10+c-"A"; if (digit<10 || digit>=base) digit=10+c-"a"; } if (digit<0 || digit>=base) throw new NumberFormatException(new String(b,offset,length)); value=value*base+digit; } return value; } /* ------------------------------------------------------------ */ public static byte[] parseBytes(String s, int base) { byte[] bytes=new byte[s.length()/2]; for (int i=0;i<s.length();i+=2) bytes[i/2]=(byte)TypeUtil.parseInt(s,i,2,base); return bytes; } /* ------------------------------------------------------------ */ public static String toString(byte[] bytes, int base) { StringBuffer buf = new StringBuffer(); for (int i=0;i<bytes.length;i++) { int bi=0xff&bytes[i]; int c="0"+(bi/base)%base; if (c>"9") c= "a"+(c-"0"-10); buf.append((char)c); c="0"+bi%base; if (c>"9") c= "a"+(c-"0"-10); buf.append((char)c); } return buf.toString(); } /* ------------------------------------------------------------ */ /** * @param b An ASCII encoded character 0-9 a-f A-F * @return The byte value of the character 0-16. */ public static byte convertHexDigit( byte b ) { if ((b >= "0") && (b <= "9")) return (byte)(b - "0"); if ((b >= "a") && (b <= "f")) return (byte)(b - "a" + 10); if ((b >= "A") && (b <= "F")) return (byte)(b - "A" + 10); return 0; } /* ------------------------------------------------------------ */ public static String toHexString(byte[] b) { StringBuffer buf = new StringBuffer(); for (int i=0;i<b.length;i++) { int bi=0xff&b[i]; int c="0"+(bi/16)%16; if (c>"9") c= "A"+(c-"0"-10); buf.append((char)c); c="0"+bi%16; if (c>"9") c= "a"+(c-"0"-10); buf.append((char)c); } return buf.toString(); } /* ------------------------------------------------------------ */ public static String toHexString(byte[] b,int offset,int length) { StringBuffer buf = new StringBuffer(); for (int i=offset;i<offset+length;i++) { int bi=0xff&b[i]; int c="0"+(bi/16)%16; if (c>"9") c= "A"+(c-"0"-10); buf.append((char)c); c="0"+bi%16; if (c>"9") c= "a"+(c-"0"-10); buf.append((char)c); } return buf.toString(); } /* ------------------------------------------------------------ */ public static byte[] fromHexString(String s) { if (s.length()%2!=0) throw new IllegalArgumentException(s); byte[] array = new byte[s.length()/2]; for (int i=0;i<array.length;i++) { int b = Integer.parseInt(s.substring(i*2,i*2+2),16); array[i]=(byte)(0xff&b); } return array; } public static void dump(Class c) { System.err.println("Dump: "+c); dump(c.getClassLoader()); } public static void dump(ClassLoader cl) { System.err.println("Dump Loaders:"); while(cl!=null) { System.err.println(" loader "+cl); cl = cl.getParent(); } } /* ------------------------------------------------------------ */ public static byte[] readLine(InputStream in) throws IOException { byte[] buf = new byte[256]; int i=0; int loops=0; int ch=0; while (true) { ch=in.read(); if (ch<0) break; loops++; // skip a leading LF"s if (loops==1 && ch==LF) continue; if (ch==CR || ch==LF) break; if (i>=buf.length) { byte[] old_buf=buf; buf=new byte[old_buf.length+256]; System.arraycopy(old_buf, 0, buf, 0, old_buf.length); } buf[i++]=(byte)ch; } if (ch==-1 && i==0) return null; // skip a trailing LF if it exists if (ch==CR && in.available()>=1 && in.markSupported()) { in.mark(1); ch=in.read(); if (ch!=LF) in.reset(); } byte[] old_buf=buf; buf=new byte[i]; System.arraycopy(old_buf, 0, buf, 0, i); return buf; }
}
</source>
Is Type Compatible
<source lang="java">
// //$Id: IntrospectionUtil.java 1540 2007-01-19 12:24:10Z janb $ //Copyright 2006 Mort Bay Consulting Pty. Ltd. //------------------------------------------------------------------------ //Licensed under the Apache License, Version 2.0 (the "License"); //you may not use this file except in compliance with the License. //You may obtain a copy of the License at //http://www.apache.org/licenses/LICENSE-2.0 //Unless required by applicable law or agreed to in writing, software //distributed under the License is distributed on an "AS IS" BASIS, //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //See the License for the specific language governing permissions and //limitations under the License. //
import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; public class Utils {
public static boolean isTypeCompatible(Class formalType, Class actualType, boolean strict) { if (formalType == null && actualType != null) return false; if (formalType != null && actualType == null) return false; if (formalType == null && actualType == null) return true; if (strict) return formalType.equals(actualType); else return formalType.isAssignableFrom(actualType); }
}
</source>
Returns an array of Type objects representing the actual type arguments to targetType used by clazz
<source lang="java">
import java.util.Map; import java.util.HashMap; import java.util.Collection; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.HashSet; import java.util.SortedSet; import java.util.TreeSet; import java.util.Queue; import java.util.LinkedList; import java.util.SortedMap; import java.util.TreeMap; import java.util.Arrays; import java.util.concurrent.ConcurrentHashMap; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import static java.lang.reflect.Modifier.isPublic; import java.beans.PropertyDescriptor; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.IntrospectionException; /**
* Common utilty methods that are useful when working with reflection. * * @author Tim Fennell */
public class ReflectUtil {
/** A cache of property descriptors by class and property name */ private static Map<Class<?>, Map<String, PropertyDescriptor>> propertyDescriptors = new ConcurrentHashMap<Class<?>, Map<String, PropertyDescriptor>>(); /** Static helper class, shouldn"t be constructed. */ private ReflectUtil() {} /** * Holds a map of commonly used interface types (mostly collections) to a class that * implements the interface and will, by default, be instantiated when an instance * of the interface is needed. */ protected static final Map<Class<?>,Class<?>> interfaceImplementations = new HashMap<Class<?>,Class<?>>(); /** * Holds a map of primitive type to the default value for that primitive type. Isn"t it * odd that there"s no way to get this programmatically from the Class objects? */ protected static final Map<Class<?>,Object> primitiveDefaults = new HashMap<Class<?>,Object>(); static { interfaceImplementations.put(Collection.class, ArrayList.class); interfaceImplementations.put(List.class, ArrayList.class); interfaceImplementations.put(Set.class, HashSet.class); interfaceImplementations.put(SortedSet.class, TreeSet.class); interfaceImplementations.put(Queue.class, LinkedList.class); interfaceImplementations.put(Map.class, HashMap.class); interfaceImplementations.put(SortedMap.class, TreeMap.class); primitiveDefaults.put(Boolean.TYPE, false); primitiveDefaults.put(Character.TYPE, "\0"); primitiveDefaults.put(Byte.TYPE, new Byte("0")); primitiveDefaults.put(Short.TYPE, new Short("0")); primitiveDefaults.put(Integer.TYPE, new Integer(0)); primitiveDefaults.put(Long.TYPE, new Long(0l)); primitiveDefaults.put(Float.TYPE, new Float(0f)); primitiveDefaults.put(Double.TYPE, new Double(0.0)); } /** * The set of method that annotation classes inherit, and should be avoided when * toString()ing an annotation class. */ private static final Set<String> INHERITED_ANNOTATION_METHODS = new HashSet(Arrays.asList("toString", "equals", "hashCode", "annotationType")); /** * Looks up the default implementing type for the supplied interface. This is done * based on a static map of known common interface types and implementing classes. * * @param iface an interface for which an implementing class is needed * @return a Class object representing the implementing type, or null if one is * not found */ public static Class<?> getImplementingClass(Class<?> iface) { return interfaceImplementations.get(iface); } /** * Attempts to determine an implementing class for the interface provided and instantiate * it using a default constructor. * * @param interfaceType an interface (or abstract class) to make an instance of * @return an instance of the interface type supplied * @throws InstantiationException if no implementation type has been configured * @throws IllegalAccessException if thrown by the JVM during class instantiation */ @SuppressWarnings("unchecked") public static <T> T getInterfaceInstance(Class<T> interfaceType) throws InstantiationException, IllegalAccessException { Class impl = getImplementingClass(interfaceType); if (impl == null) { throw new InstantiationException( "Stripes needed to instantiate a property who"s declared type as an " + "interface (which obviously cannot be instantiated. The interface is not " + "one that Stripes is aware of, so no implementing class was known. The " + "interface type was: "" + interfaceType.getName() + "". To fix this " + "you"ll need to do one of three things. 1) Change the getter/setter methods " + "to use a concrete type so that Stripes can instantiate it. 2) in the bean"s " + "setContext() method pre-instantiate the property so Stripes doesn"t have to. " + "3) Bug the Stripes author ;) If the interface is a JDK type it can easily be " + "fixed. If not, if enough people ask, a generic way to handle the problem " + "might get implemented."); } else { return (T) impl.newInstance(); } } /** * Utility method used to load a class. Any time that Stripes needs to load of find a * class by name it uses this method. As a result any time the classloading strategy * needs to change it can be done in one place! Currently uses * {@code Thread.currentThread().getContextClassLoader().loadClass(String)}. * * @param name the fully qualified (binary) name of the class to find or load * @return the Class object representing the class * @throws ClassNotFoundException if the class cannot be loaded */ @SuppressWarnings("unchecked") // this allows us to assign without casting public static Class findClass(String name) throws ClassNotFoundException { return Thread.currentThread().getContextClassLoader().loadClass(name); } /***
A better (more concise) toString method for annotation types that yields a String * that should look more like the actual usage of the annotation in a class. The String produced * is similar to that produced by calling toString() on the annotation directly, with the * following differences:
**
-
*
- Uses the classes simple name instead of it"s fully qualified name. *
- Only outputs attributes that are set to non-default values.
**
If, for some unforseen reason, an exception is thrown within this method it will be * caught and the return value will be {@code ann.toString()}. * * @param ann the annotation to convert to a human readable String * @return a human readable String form of the annotation and it"s attributes */ public static String toString(Annotation ann) { try { Class<? extends Annotation> type = ann.annotationType(); StringBuilder builder = new StringBuilder(128); builder.append("@"); builder.append(type.getSimpleName()); boolean appendedAnyParameters = false; Method[] methods = type.getMethods(); for (Method method : methods) { if (!INHERITED_ANNOTATION_METHODS.contains(method.getName())) { Object defaultValue = method.getDefaultValue(); Object actualValue = method.invoke(ann); // If we have arrays, they have to be treated a little differently Object[] defaultArray = null, actualArray = null; if ( Object[].class.isAssignableFrom(method.getReturnType()) ) { defaultArray = (Object[]) defaultValue; actualArray = (Object[]) actualValue; } // Only print an attribute if it isn"t set to the default value if ( (defaultArray != null && !Arrays.equals(defaultArray, actualArray)) || (defaultArray == null && !actualValue.equals(defaultValue)) ) { if (appendedAnyParameters) { builder.append(", "); } else { builder.append("("); } builder.append(method.getName()); builder.append("="); if (actualArray != null) { builder.append( Arrays.toString(actualArray) ); } else { builder.append(actualValue); } appendedAnyParameters = true; } } } if (appendedAnyParameters) { builder.append(")"); } return builder.toString(); } catch (Exception e) { return ann.toString(); } } /** * Fetches all methods of all access types from the supplied class and super * classes. Methods that have been overridden in the inheritance hierarchy are * only returned once, using the instance lowest down the hierarchy. * * @param clazz the class to inspect * @return a collection of methods */ public static Collection<Method> getMethods(Class<?> clazz) { Collection<Method> found = new ArrayList<Method>(); while (clazz != null) { for (Method m1 : clazz.getDeclaredMethods()) { boolean overridden = false; for (Method m2 : found) { if ( m2.getName().equals(m1.getName()) && Arrays.deepEquals(m1.getParameterTypes(), m2.getParameterTypes())) { overridden = true; break; } } if (!overridden) found.add(m1); } clazz = clazz.getSuperclass(); } return found; } /** * Fetches all fields of all access types from the supplied class and super * classes. Fieldss that have been overridden in the inheritance hierarchy are * only returned once, using the instance lowest down the hierarchy. * * @param clazz the class to inspect * @return a collection of fields */ public static Collection<Field> getFields(Class<?> clazz) { Map<String,Field> fields = new HashMap<String, Field>(); while (clazz != null) { for (Field field : clazz.getDeclaredFields()) { if ( !fields.containsKey(field.getName()) ) { fields.put(field.getName(), field); } } clazz = clazz.getSuperclass(); } return fields.values(); } /** * Fetches the property descriptor for the named property of the supplied class. To * speed things up a cache is maintained of propertyName to PropertyDescriptor for * each class used with this method. If there is no property with the specified name, * returns null. * * @param clazz the class who"s properties to examine * @param property the String name of the property to look for * @return the PropertyDescriptor or null if none is found with a matching name */ public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String property) { Map<String,PropertyDescriptor> pds = propertyDescriptors.get(clazz); if (pds == null) { try { BeanInfo info = Introspector.getBeanInfo(clazz); PropertyDescriptor[] descriptors = info.getPropertyDescriptors(); pds = new HashMap<String, PropertyDescriptor>(); for (PropertyDescriptor descriptor : descriptors) { pds.put(descriptor.getName(), descriptor); } propertyDescriptors.put(clazz, pds); } catch (IntrospectionException ie) { throw new RuntimeException("Could not examine class "" + clazz.getName() + "" using Introspector.getBeanInfo() to determine property information.", ie); } } return pds.get(property); } /** * <p>Attempts to find an accessible version of the method passed in, where accessible * is defined as the method itself being public and the declaring class being public. * Mostly useful as a workaround to the situation when * {@link PropertyDescriptor#getReadMethod()} and/or * {@link java.beans.PropertyDescriptor#getWriteMethod()} returns methods that are not * accessible (usually due to public implementations of interface methods in private * classes).
**
Checks the method passed in and if it already meets these criteria it is returned * immediately. In general this leads to very little performance overhead
**
If the method does not meet the criteria then the class" interfaces are scanned * for a matching method. If one is not found, then the class" superclass hierarchy * is searched. Finally, if no matching method can be found the original method is * returned.
* * @param m a method that may or may not be accessible * @return either an accessible version of the same method, or the method passed in if * an accessible version cannot be found */ public static Method findAccessibleMethod(final Method m) { // If the passed in method is accessible, then just give it back. if (isPublic(m.getModifiers()) && isPublic(m.getDeclaringClass().getModifiers())) return m; if (m.isAccessible()) return m; final Class<?> clazz = m.getDeclaringClass(); final String name = m.getName(); final Class<?>[] ptypes = m.getParameterTypes(); // Else, loop through the interfaces for the declaring class, looking for a // public version of the method that we can call for (Class<?> iface : clazz.getInterfaces()) { try { Method m2 = iface.getMethod(name, ptypes); if (m2.isAccessible()) return m2; if (isPublic(iface.getModifiers()) && isPublic(m2.getModifiers())) return m2; } catch (NoSuchMethodException nsme) { /* Not Unexpected. */ } } // Else loop through the superclasses looking for a public method Class<?> c = clazz.getSuperclass(); while (c != null) { try { Method m2 = c.getMethod(name, ptypes); if (m2.isAccessible()) return m2; if (isPublic(c.getModifiers()) && isPublic(m2.getModifiers())) return m2; } catch (NoSuchMethodException nsme) { /* Not Unexpected. */ } c = c.getSuperclass(); } // If we haven"t found anything at this point, just give up! return m; }
/** * Looks for an instance (i.e. non-static) public field with the matching name and * returns it if one exists. If no such field exists, returns null. * * @param clazz the clazz who"s fields to examine * @param property the name of the property/field to look for * @return the Field object or null if no matching field exists */ public static Field getField(Class<?> clazz, String property) { try { Field field = clazz.getField(property); return !Modifier.isStatic(field.getModifiers()) ? field : null; } catch (NoSuchFieldException nsfe) { return null; } } /** * Returns an appropriate default value for the class supplied. Mirrors the defaults used * when the JVM initializes instance variables. * * @param clazz the class for which to find the default value * @return null for non-primitive types and an appropriate wrapper instance for primitives */ public static Object getDefaultValue(Class<?> clazz) { if (clazz.isPrimitive()) { return primitiveDefaults.get(clazz); } else { return null; } } /** * Returns a set of all interfaces implemented by class supplied. This includes all * interfaces directly implemented by this class as well as those implemented by * superclasses or interface superclasses. * * @param clazz * @return all interfaces implemented by this class */ public static Set<Class<?>> getImplementedInterfaces(Class<?> clazz) { Set<Class<?>> interfaces = new HashSet<Class<?>>(); if (clazz.isInterface()) interfaces.add(clazz); while (clazz != null) { for (Class<?> iface : clazz.getInterfaces()) interfaces.addAll(getImplementedInterfaces(iface)); clazz = clazz.getSuperclass(); } return interfaces; } /** * Returns an array of Type objects representing the actual type arguments * to targetType used by clazz. * * @param clazz the implementing class (or subclass) * @param targetType the implemented generic class or interface * @return an array of Type objects or null */ public static Type[] getActualTypeArguments(Class<?> clazz, Class<?> targetType) { Set<Class<?>> classes = new HashSet<Class<?>>(); classes.add(clazz); if (targetType.isInterface()) classes.addAll(getImplementedInterfaces(clazz)); Class<?> superClass = clazz.getSuperclass(); while (superClass != null) { classes.add(superClass); superClass = superClass.getSuperclass(); } for (Class<?> search : classes) { for (Type type : (targetType.isInterface() ? search.getGenericInterfaces() : new Type[] { search.getGenericSuperclass() })) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; if (targetType.equals(parameterizedType.getRawType())) return parameterizedType.getActualTypeArguments(); } } } return null; }
}
</source>
Returns true if type is a iterable type
<source lang="java">
// $Id: ReflectionHelper.java 16271 2009-04-07 20:20:12Z hardy.ferentschik $ /*
- JBoss, Home of Professional Open Source
- Copyright 2008, Red Hat Middleware LLC, and individual contributors
- by the @authors tag. See the copyright.txt in the distribution for a
- full listing of individual contributors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- /
import java.beans.Introspector; import java.lang.annotation.Annotation; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.WildcardType; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Map;
/**
* Some reflection utility methods. * * @author Hardy Ferentschik */
public class ReflectionHelper {
/** * @param type the type to check. * * @return Returnstrue
iftype
is a iterable type,false
otherwise. */ public static boolean isIterable(Type type) { if ( type instanceof Class && isIterableClass( ( Class ) type ) ) { return true; } if ( type instanceof ParameterizedType ) { return isIterable( ( ( ParameterizedType ) type ).getRawType() ); } if ( type instanceof WildcardType ) { Type[] upperBounds = ( ( WildcardType ) type ).getUpperBounds(); return upperBounds.length != 0 && isIterable( upperBounds[0] ); } return false; } /** * Checks whether the specified class parameter is an instance of a collection class. * * @param clazzClass
to check. * * @returntrue
isclazz
is instance of a collection class,false
otherwise. */ private static boolean isIterableClass(Class<?> clazz) { List<Class<?>> classes = new ArrayList<Class<?>>(); computeClassHierarchy( clazz, classes ); return classes.contains( Iterable.class ); } /** * Get all superclasses and interfaces recursively. * * @param clazz The class to start the search with. * @param classes List of classes to which to add all found super classes and interfaces. */ private static void computeClassHierarchy(Class<?> clazz, List<Class<?>> classes) { for ( Class current = clazz; current != null; current = current.getSuperclass() ) { if ( classes.contains( current ) ) { return; } classes.add( current ); for ( Class currentInterface : current.getInterfaces() ) { computeClassHierarchy( currentInterface, classes ); } } }
}
</source>
TYPE Utilities
<source lang="java">
// // $Id: TypeUtil.java,v 1.12 2004/05/09 20:33:04 gregwilkins Exp $ // Copyright 2002-2004 Mort Bay Consulting Pty. Ltd. // ------------------------------------------------------------------------ // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //
import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap;
/* ------------------------------------------------------------ */ /** TYPE Utilities.
* Provides various static utiltiy methods for manipulating types and their * string representations. * * @since Jetty 4.1 * @version $Revision: 1.12 $ * @author Greg Wilkins (gregw) */
public class TypeUtil {
/* ------------------------------------------------------------ */ private static final HashMap name2Class=new HashMap(); static { name2Class.put("boolean",java.lang.Boolean.TYPE); name2Class.put("byte",java.lang.Byte.TYPE); name2Class.put("char",java.lang.Character.TYPE); name2Class.put("double",java.lang.Double.TYPE); name2Class.put("float",java.lang.Float.TYPE); name2Class.put("int",java.lang.Integer.TYPE); name2Class.put("long",java.lang.Long.TYPE); name2Class.put("short",java.lang.Short.TYPE); name2Class.put("void",java.lang.Void.TYPE); name2Class.put("java.lang.Boolean.TYPE",java.lang.Boolean.TYPE); name2Class.put("java.lang.Byte.TYPE",java.lang.Byte.TYPE); name2Class.put("java.lang.Character.TYPE",java.lang.Character.TYPE); name2Class.put("java.lang.Double.TYPE",java.lang.Double.TYPE); name2Class.put("java.lang.Float.TYPE",java.lang.Float.TYPE); name2Class.put("java.lang.Integer.TYPE",java.lang.Integer.TYPE); name2Class.put("java.lang.Long.TYPE",java.lang.Long.TYPE); name2Class.put("java.lang.Short.TYPE",java.lang.Short.TYPE); name2Class.put("java.lang.Void.TYPE",java.lang.Void.TYPE); name2Class.put("java.lang.Boolean",java.lang.Boolean.class); name2Class.put("java.lang.Byte",java.lang.Byte.class); name2Class.put("java.lang.Character",java.lang.Character.class); name2Class.put("java.lang.Double",java.lang.Double.class); name2Class.put("java.lang.Float",java.lang.Float.class); name2Class.put("java.lang.Integer",java.lang.Integer.class); name2Class.put("java.lang.Long",java.lang.Long.class); name2Class.put("java.lang.Short",java.lang.Short.class); name2Class.put("Boolean",java.lang.Boolean.class); name2Class.put("Byte",java.lang.Byte.class); name2Class.put("Character",java.lang.Character.class); name2Class.put("Double",java.lang.Double.class); name2Class.put("Float",java.lang.Float.class); name2Class.put("Integer",java.lang.Integer.class); name2Class.put("Long",java.lang.Long.class); name2Class.put("Short",java.lang.Short.class); name2Class.put(null,java.lang.Void.TYPE); name2Class.put("string",java.lang.String.class); name2Class.put("String",java.lang.String.class); name2Class.put("java.lang.String",java.lang.String.class); } /* ------------------------------------------------------------ */ private static final HashMap class2Name=new HashMap(); static { class2Name.put(java.lang.Boolean.TYPE,"boolean"); class2Name.put(java.lang.Byte.TYPE,"byte"); class2Name.put(java.lang.Character.TYPE,"char"); class2Name.put(java.lang.Double.TYPE,"double"); class2Name.put(java.lang.Float.TYPE,"float"); class2Name.put(java.lang.Integer.TYPE,"int"); class2Name.put(java.lang.Long.TYPE,"long"); class2Name.put(java.lang.Short.TYPE,"short"); class2Name.put(java.lang.Void.TYPE,"void"); class2Name.put(java.lang.Boolean.class,"java.lang.Boolean"); class2Name.put(java.lang.Byte.class,"java.lang.Byte"); class2Name.put(java.lang.Character.class,"java.lang.Character"); class2Name.put(java.lang.Double.class,"java.lang.Double"); class2Name.put(java.lang.Float.class,"java.lang.Float"); class2Name.put(java.lang.Integer.class,"java.lang.Integer"); class2Name.put(java.lang.Long.class,"java.lang.Long"); class2Name.put(java.lang.Short.class,"java.lang.Short"); class2Name.put(null,"void"); name2Class.put(java.lang.String.class,"java.lang.String"); } /* ------------------------------------------------------------ */ private static final HashMap class2Value=new HashMap(); static { try { Class[] s ={java.lang.String.class}; class2Value.put(java.lang.Boolean.TYPE, java.lang.Boolean.class.getMethod("valueOf",s)); class2Value.put(java.lang.Byte.TYPE, java.lang.Byte.class.getMethod("valueOf",s)); class2Value.put(java.lang.Double.TYPE, java.lang.Double.class.getMethod("valueOf",s)); class2Value.put(java.lang.Float.TYPE, java.lang.Float.class.getMethod("valueOf",s)); class2Value.put(java.lang.Integer.TYPE, java.lang.Integer.class.getMethod("valueOf",s)); class2Value.put(java.lang.Long.TYPE, java.lang.Long.class.getMethod("valueOf",s)); class2Value.put(java.lang.Short.TYPE, java.lang.Short.class.getMethod("valueOf",s)); class2Value.put(java.lang.Boolean.class, java.lang.Boolean.class.getMethod("valueOf",s)); class2Value.put(java.lang.Byte.class, java.lang.Byte.class.getMethod("valueOf",s)); class2Value.put(java.lang.Double.class, java.lang.Double.class.getMethod("valueOf",s)); class2Value.put(java.lang.Float.class, java.lang.Float.class.getMethod("valueOf",s)); class2Value.put(java.lang.Integer.class, java.lang.Integer.class.getMethod("valueOf",s)); class2Value.put(java.lang.Long.class, java.lang.Long.class.getMethod("valueOf",s)); class2Value.put(java.lang.Short.class, java.lang.Short.class.getMethod("valueOf",s)); } catch(Exception e) { } } /* ------------------------------------------------------------ */ private static Class[] stringArg = { java.lang.String.class }; /* ------------------------------------------------------------ */ private static int intCacheSize= Integer.getInteger("org.mortbay.util.TypeUtil.IntegerCacheSize",600).intValue(); private static Integer[] integerCache = new Integer[intCacheSize]; private static String[] integerStrCache = new String[intCacheSize]; private static Integer minusOne = new Integer(-1); /* ------------------------------------------------------------ */ /** Class from a canonical name for a type. * @param name A class or type name. * @return A class , which may be a primitive TYPE field.. */ public static Class fromName(String name) { return (Class)name2Class.get(name); } /* ------------------------------------------------------------ */ /** Canonical name for a type. * @param type A class , which may be a primitive TYPE field. * @return Canonical name. */ public static String toName(Class type) { return (String)class2Name.get(type); } /* ------------------------------------------------------------ */ /** Convert String value to instance. * @param type The class of the instance, which may be a primitive TYPE field. * @param value The value as a string. * @return The value as an Object. */ public static Object valueOf(Class type, String value) { try { if (type.equals(java.lang.String.class)) return value; Method m = (Method)class2Value.get(type); if (m!=null) return m.invoke(null,new Object[] {value}); if (type.equals(java.lang.Character.TYPE) || type.equals(java.lang.Character.class)) return new Character(value.charAt(0)); Constructor c = type.getConstructor(stringArg); return c.newInstance(new Object[] {value}); } catch(InvocationTargetException e) { if (e.getTargetException() instanceof Error) throw (Error)(e.getTargetException()); } return null; } /* ------------------------------------------------------------ */ /** Convert String value to instance. * @param type classname or type (eg int) * @param value The value as a string. * @return The value as an Object. */ public static Object valueOf(String type, String value) { return valueOf(fromName(type),value); } /* ------------------------------------------------------------ */ /** Convert int to Integer using cache. */ public static Integer newInteger(int i) { if (i>=0 && i<intCacheSize) { if (integerCache[i]==null) integerCache[i]=new Integer(i); return integerCache[i]; } else if (i==-1) return minusOne; return new Integer(i); } /* ------------------------------------------------------------ */ /** Convert int to String using cache. */ public static String toString(int i) { if (i>=0 && i<intCacheSize) { if (integerStrCache[i]==null) integerStrCache[i]=Integer.toString(i); return integerStrCache[i]; } else if (i==-1) return "-1"; return Integer.toString(i); }
/* ------------------------------------------------------------ */ /** Parse an int from a substring. * Negative numbers are not handled. * @param s String * @param offset Offset within string * @param length Length of integer or -1 for remainder of string * @param base base of the integer * @exception NumberFormatException */ public static int parseInt(String s, int offset, int length, int base) throws NumberFormatException { int value=0; if (length<0) length=s.length()-offset; for (int i=0;i<length;i++) { char c=s.charAt(offset+i); int digit=c-"0"; if (digit<0 || digit>=base || digit>=10) { digit=10+c-"A"; if (digit<10 || digit>=base) digit=10+c-"a"; } if (digit<0 || digit>=base) throw new NumberFormatException(s.substring(offset,offset+length)); value=value*base+digit; } return value; } /* ------------------------------------------------------------ */ public static byte[] parseBytes(String s, int base) { byte[] bytes=new byte[s.length()/2]; for (int i=0;i<s.length();i+=2) bytes[i/2]=(byte)TypeUtil.parseInt(s,i,2,base); return bytes; } /* ------------------------------------------------------------ */ public static String toString(byte[] bytes, int base) { StringBuffer buf = new StringBuffer(); for (int i=0;i<bytes.length;i++) { int bi=0xff&bytes[i]; int c="0"+(bi/base)%base; if (c>"9") c= "a"+(c-"0"-10); buf.append((char)c); c="0"+bi%base; if (c>"9") c= "a"+(c-"0"-10); buf.append((char)c); } return buf.toString(); } /* ------------------------------------------------------------ */ /** * @param b An ASCII encoded character 0-9 a-f A-F * @return The byte value of the character 0-16. */ public static byte convertHexDigit( byte b ) { if ((b >= "0") && (b <= "9")) return (byte)(b - "0"); if ((b >= "a") && (b <= "f")) return (byte)(b - "a" + 10); if ((b >= "A") && (b <= "F")) return (byte)(b - "A" + 10); return 0; } /* ------------------------------------------------------------ */ public static String toHexString(byte[] b) { StringBuffer buf = new StringBuffer(); for (int i=0;i<b.length;i++) { int bi=0xff&b[i]; int c="0"+(bi/16)%16; if (c>"9") c= "A"+(c-"0"-10); buf.append((char)c); c="0"+bi%16; if (c>"9") c= "a"+(c-"0"-10); buf.append((char)c); } return buf.toString(); } /* ------------------------------------------------------------ */ public static String toHexString(byte[] b,int offset,int length) { StringBuffer buf = new StringBuffer(); for (int i=offset;i<offset+length;i++) { int bi=0xff&b[i]; int c="0"+(bi/16)%16; if (c>"9") c= "A"+(c-"0"-10); buf.append((char)c); c="0"+bi%16; if (c>"9") c= "a"+(c-"0"-10); buf.append((char)c); } return buf.toString(); } /* ------------------------------------------------------------ */ public static byte[] fromHexString(String s) { if (s.length()%2!=0) throw new IllegalArgumentException(s); byte[] array = new byte[s.length()/2]; for (int i=0;i<array.length;i++) { int b = Integer.parseInt(s.substring(i*2,i*2+2),16); array[i]=(byte)(0xff&b); } return array; } public static void dump(Class c) { System.err.println("Dump: "+c); dump(c.getClassLoader()); } public static void dump(ClassLoader cl) { System.err.println("Dump Loaders:"); while(cl!=null) { System.err.println(" loader "+cl); cl = cl.getParent(); } }
}
</source>