[Snmap-developer] SNMAP/src/net/sf/snmap/util ReflectHelper.java,1.2,1.3
Status: Planning
Brought to you by:
arden
|
From: arden l. <ar...@us...> - 2006-01-19 04:20:26
|
Update of /cvsroot/snmap/SNMAP/src/net/sf/snmap/util In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4476/src/net/sf/snmap/util Modified Files: ReflectHelper.java Log Message: Index: ReflectHelper.java =================================================================== RCS file: /cvsroot/snmap/SNMAP/src/net/sf/snmap/util/ReflectHelper.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** ReflectHelper.java 13 Jan 2006 03:26:04 -0000 1.2 --- ReflectHelper.java 19 Jan 2006 04:20:14 -0000 1.3 *************** *** 7,100 **** import net.sf.snmap.MappingException; import net.sf.snmap.PropertyNotFoundException; import net.sf.snmap.property.Getter; public final class ReflectHelper { - private static final Class[] NO_CLASSES = new Class[0]; ! /** ! * Check input class could be instantiable. ! * ! * @param clazz -- ! * The Class for checking. ! * @return -- The result. ! */ ! public static boolean isInstantiable(Class clazz) { ! int modifier = clazz.getModifiers(); ! return !(Modifier.isAbstract(modifier) || Modifier ! .isInterface(modifier)); ! } ! /** ! * Checking input Class and Member is public. ! * ! * @param clazz -- ! * The Class for checking. ! * @param member -- ! * The Member for checking. ! * @return -- The result. ! */ ! public static boolean isPublic(Class clazz, Member member) { ! return Modifier.isPublic(member.getModifiers()) ! && Modifier.isPublic(clazz.getModifiers()); ! } ! /** ! * Get Default Constructor. ! * ! * @param clazz -- ! * The input Class. ! * @return -- The Constructor. ! * @throws PropertyNotFoundException ! */ ! public static Constructor getDefaultConstructor(Class clazz) ! throws PropertyNotFoundException { ! if (!isInstantiable(clazz)) ! return null; ! try { ! Constructor constructor = clazz.getDeclaredConstructor(NO_CLASSES); ! if (!isPublic(clazz, constructor)) { ! constructor.setAccessible(true); ! } ! return constructor; ! } catch (NoSuchMethodException nme) { ! throw new PropertyNotFoundException("Object class " ! + clazz.getName() ! + " must declare a default (no-argument) constructor"); ! } ! } ! ! public static Class classForName(String name) throws ClassNotFoundException { ! try { ! ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); ! if(contextClassLoader!=null) { ! return contextClassLoader.loadClass(name); ! } else { ! return Class.forName(name); ! } ! } ! catch (Exception e) { ! return Class.forName(name); ! } ! } ! ! public static Class reflectedPropertyClass(String className, String name) throws MappingException { ! try { ! Class clazz = ReflectHelper.classForName(className); ! return getter(clazz, name).getReturnType(); ! } ! catch (ClassNotFoundException cnfe) { ! throw new MappingException("class " + className + " not found while looking for property: " + name, cnfe); ! } ! } ! ! private static Getter getter(Class clazz, String name) throws MappingException { ! try { ! return BASIC_PROPERTY_ACCESSOR.getGetter(clazz, name); ! } ! catch (PropertyNotFoundException pnfe) { ! return DIRECT_PROPERTY_ACCESSOR.getGetter(clazz, name); ! } ! } } --- 7,109 ---- import net.sf.snmap.MappingException; import net.sf.snmap.PropertyNotFoundException; + import net.sf.snmap.property.BasicPropertyAccessor; + import net.sf.snmap.property.DirectPropertyAccessor; import net.sf.snmap.property.Getter; + import net.sf.snmap.property.PropertyAccessor; public final class ReflectHelper { ! private static final PropertyAccessor BASIC_PROPERTY_ACCESSOR = new BasicPropertyAccessor(); ! private static final PropertyAccessor DIRECT_PROPERTY_ACCESSOR = new DirectPropertyAccessor(); ! private static final Class[] NO_CLASSES = new Class[0]; ! /** ! * Check input class could be instantiable. ! * ! * @param clazz -- ! * The Class for checking. ! * @return -- The result. ! */ ! public static boolean isInstantiable(Class clazz) { ! int modifier = clazz.getModifiers(); ! return !(Modifier.isAbstract(modifier) || Modifier ! .isInterface(modifier)); ! } ! /** ! * Checking input Class and Member is public. ! * ! * @param clazz -- ! * The Class for checking. ! * @param member -- ! * The Member for checking. ! * @return -- The result. ! */ ! public static boolean isPublic(Class clazz, Member member) { ! return Modifier.isPublic(member.getModifiers()) ! && Modifier.isPublic(clazz.getModifiers()); ! } ! ! /** ! * Get Default Constructor. ! * ! * @param clazz -- ! * The input Class. ! * @return -- The Constructor. ! * @throws PropertyNotFoundException ! */ ! public static Constructor getDefaultConstructor(Class clazz) ! throws PropertyNotFoundException { ! ! if (!isInstantiable(clazz)) ! return null; ! ! try { ! Constructor constructor = clazz.getDeclaredConstructor(NO_CLASSES); ! if (!isPublic(clazz, constructor)) { ! constructor.setAccessible(true); ! } ! return constructor; ! } catch (NoSuchMethodException nme) { ! throw new PropertyNotFoundException("Object class " ! + clazz.getName() ! + " must declare a default (no-argument) constructor"); ! } ! } ! ! public static Class classForName(String name) throws ClassNotFoundException { ! try { ! ClassLoader contextClassLoader = Thread.currentThread() ! .getContextClassLoader(); ! if (contextClassLoader != null) { ! return contextClassLoader.loadClass(name); ! } else { ! return Class.forName(name); ! } ! } catch (Exception e) { ! return Class.forName(name); ! } ! } ! ! public static Class reflectedPropertyClass(String className, String name) ! throws MappingException { ! try { ! Class clazz = ReflectHelper.classForName(className); ! return getter(clazz, name).getReturnType(); ! } catch (ClassNotFoundException cnfe) { ! throw new MappingException("class " + className ! + " not found while looking for property: " + name, cnfe); ! } ! } ! ! private static Getter getter(Class clazz, String name) ! throws MappingException { ! try { ! return BASIC_PROPERTY_ACCESSOR.getGetter(clazz, name); ! } catch (PropertyNotFoundException pnfe) { ! return DIRECT_PROPERTY_ACCESSOR.getGetter(clazz, name); ! } ! } } |