From: <one...@us...> - 2002-11-09 01:38:10
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers In directory usw-pr-cvs1:/tmp/cvs-serv6168/hibernate/helpers Modified Files: ReflectHelper.java Log Message: committed Jon Lipsky's patch to allow components with private constructors Index: ReflectHelper.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers/ReflectHelper.java,v retrieving revision 1.32 retrieving revision 1.33 diff -C2 -d -r1.32 -r1.33 *** ReflectHelper.java 28 Oct 2002 19:27:47 -0000 1.32 --- ReflectHelper.java 9 Nov 2002 01:38:05 -0000 1.33 *************** *** 1,227 **** ! //$Id$ ! package cirrus.hibernate.helpers; ! ! import java.beans.*; ! import java.lang.reflect.InvocationTargetException; ! import java.lang.reflect.Member; ! import java.lang.reflect.Method; ! import java.lang.reflect.Modifier; ! ! import cirrus.hibernate.*; ! import cirrus.hibernate.MappingException; ! import cirrus.hibernate.type.*; ! import cirrus.hibernate.type.Type; ! ! ! public final class ReflectHelper { ! ! private static final Class[] OBJECT = new Class[] { Object.class }; ! private static final Method OBJECT_EQUALS; ! static { ! Method eq; ! try { ! eq = Object.class.getMethod("equals", OBJECT); ! } ! catch (Exception e) { ! throw new AssertionFailure("Could not find Object.equals()", e); ! } ! OBJECT_EQUALS = eq; ! } ! ! public static boolean overridesEquals(Class clazz) { ! Method equals; ! try { ! equals = clazz.getMethod("equals", OBJECT); ! } ! catch (NoSuchMethodException nsme) { ! return false; //its an interface so we can't really tell anything... ! } ! return !OBJECT_EQUALS.equals(equals); ! } ! ! ! public static Method getMethod(Class theClass, String methodName) throws MappingException { ! ! if (theClass==Object.class || theClass==null) throw new MappingException( "Could not find a setter" ); // will be swallowed ! ! Method result; ! try { ! result = theClass.getDeclaredMethod(methodName, null); ! } ! catch (Exception e) { ! try { ! return getMethod( theClass.getSuperclass(), methodName ); ! } ! catch (MappingException me) { ! throw new MappingException( "Could not find method " + methodName + " in class " + theClass.getName() ); ! } ! } ! ! if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true); ! return result; ! ! } ! ! public static Method getSetterMethod(Class theClass, String propertyName) throws MappingException { ! ! if (theClass==Object.class || theClass==null) throw new MappingException( "Could not find a setter" ); // will be swallowed ! ! Method result = setter(theClass, propertyName); ! ! if(result==null) { ! try { ! return getSetterMethod( theClass.getSuperclass(), propertyName ); ! } ! catch (MappingException me) { ! throw new MappingException( "Could not find a setter for " + propertyName + " in class " + theClass.getName() ); ! } ! } ! else { ! if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true); ! return result; ! } ! ! } ! ! private static Method setter(Class theClass, String propertyName) { ! ! Method[] methods = theClass.getDeclaredMethods(); ! for (int i=0; i<methods.length; i++) { ! if( ! ( methods[i].getName().length() > 3 ) && ! ( methods[i].getName().startsWith("set") ) ! ) { ! String testStdMethod = Introspector.decapitalize( methods[i].getName().substring(3) ); ! String testOldMethod = methods[i].getName().substring(3); ! if ( ! ( testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName) ) && ! ( methods[i].getParameterTypes().length==1 ) ! ) return methods[i]; ! } ! } ! return null; ! } ! ! public static Method getGetterMethod(Class theClass, String propertyName) throws MappingException { ! ! if (theClass==Object.class || theClass==null) throw new MappingException( "Could not find a getter" ); // will be swallowed ! ! Method result = getter(theClass, propertyName); ! ! if(result==null) { ! try { ! return getGetterMethod( theClass.getSuperclass(), propertyName ); ! } ! catch (MappingException me) { ! throw new MappingException( "Could not find a getter for " + propertyName + " in class " + theClass.getName() ); ! } ! } ! else { ! if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true); ! return result; ! } ! } ! ! private static Method getter(Class theClass, String propertyName) { ! ! Method[] methods = theClass.getDeclaredMethods(); ! for (int i=0; i<methods.length; i++) { ! // only carry on if the method has no parameters ! if(methods[i].getParameterTypes().length==0) { ! ! // try "get" ! if( (methods[i].getName().length() > 3) && methods[i].getName().startsWith("get") ) { ! ! String testStdMethod = Introspector.decapitalize( methods[i].getName().substring(3) ); ! String testOldMethod = methods[i].getName().substring(3); ! if( ! ( testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName) ) && ! methods[i].getParameterTypes().length==0 ! ) return methods[i]; ! ! } ! ! // if not "get" then try "is" ! if( (methods[i].getName().length() > 2) && methods[i].getName().startsWith("is") ) { ! ! String testStdMethod = Introspector.decapitalize( methods[i].getName().substring(2) ); ! String testOldMethod = methods[i].getName().substring(2); ! if( ! ( testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName) ) && ! methods[i].getParameterTypes().length==0 ! ) return methods[i]; ! } ! } ! } ! return null; ! } ! ! public static Type reflectedPropertyType(Class theClass, String name) throws MappingException { ! return TypeFactory.hueristicType( getGetterMethod(theClass, name).getReturnType().getName() ); ! } ! ! public static Class classForName(String name) throws ClassNotFoundException { ! try { ! return Thread.currentThread().getContextClassLoader().loadClass(name); ! } ! catch (Exception e) { ! return Class.forName(name); ! } ! } ! ! public static void set(Method method, Object target, Object value, Class clazz, String propertyName) throws HibernateException { ! try { ! method.invoke( target, new Object[] { value } ); ! } ! catch (NullPointerException npe) { ! throw new PropertyAccessException(npe, "NullPointerException occurred inside", true, clazz, propertyName); ! } ! catch (InvocationTargetException ite) { ! throw new PropertyAccessException(ite, "Exception occurred inside", true, clazz, propertyName); ! } ! catch (IllegalAccessException iae) { ! throw new PropertyAccessException(iae, "IllegalAccessException occurred while calling", true, clazz, propertyName); ! //cannot occur ! } ! catch (IllegalArgumentException iae) { ! throw new PropertyAccessException(iae, "IllegalArgumentException occurred while calling", true, clazz, propertyName); ! } ! } ! ! public static Object get(Method method, Object target, Class clazz, String propertyName) throws HibernateException { ! try { ! return method.invoke(target, null); ! } ! catch (InvocationTargetException ite) { ! throw new PropertyAccessException(ite, "Exception occurred inside", false, clazz, propertyName); ! } ! catch (IllegalAccessException iae) { ! throw new PropertyAccessException(iae, "IllegalAccessException occurred while calling", false, clazz, propertyName); ! //cannot occur ! } ! catch (IllegalArgumentException iae) { ! throw new PropertyAccessException(iae, "IllegalArgumentException occurred calling", false, clazz, propertyName); ! } ! } ! ! public static boolean isPublic(Class clazz, Member member) { ! return Modifier.isPublic( member.getModifiers() ) && Modifier.isPublic( clazz.getModifiers() ); ! } ! ! public static Object getConstantValue(String name) { ! Class clazz; ! try { ! clazz = classForName( StringHelper.qualifier(name) ); ! } ! catch(ClassNotFoundException cnfe) { ! return null; ! } ! try { ! return clazz.getField( StringHelper.unqualify(name) ).get(null); ! } ! catch (Exception e) { ! return null; ! } ! } ! ! } --- 1,254 ---- ! //$Id$ ! package cirrus.hibernate.helpers; ! ! import java.beans.*; ! import java.lang.reflect.Constructor; ! import java.lang.reflect.InvocationTargetException; ! import java.lang.reflect.Member; ! import java.lang.reflect.Method; ! import java.lang.reflect.Modifier; ! ! import cirrus.hibernate.*; ! import cirrus.hibernate.MappingException; ! import cirrus.hibernate.type.*; ! import cirrus.hibernate.type.Type; ! ! ! public final class ReflectHelper { ! ! private static final Class[] NO_CLASSES = new Class[0]; ! private static final Class[] OBJECT = new Class[] { Object.class }; ! private static final Method OBJECT_EQUALS; ! ! static { ! Method eq; ! try { ! eq = Object.class.getMethod("equals", OBJECT); ! } ! catch (Exception e) { ! throw new AssertionFailure("Could not find Object.equals()", e); ! } ! OBJECT_EQUALS = eq; ! } ! ! public static boolean overridesEquals(Class clazz) { ! Method equals; ! try { ! equals = clazz.getMethod("equals", OBJECT); ! } ! catch (NoSuchMethodException nsme) { ! return false; //its an interface so we can't really tell anything... ! } ! return !OBJECT_EQUALS.equals(equals); ! } ! ! ! public static Method getMethod(Class theClass, String methodName) throws MappingException { ! ! if (theClass==Object.class || theClass==null) throw new MappingException( "Could not find a setter" ); // will be swallowed ! ! Method result; ! try { ! result = theClass.getDeclaredMethod(methodName, null); ! } ! catch (Exception e) { ! try { ! return getMethod( theClass.getSuperclass(), methodName ); ! } ! catch (MappingException me) { ! throw new MappingException( "Could not find method " + methodName + " in class " + theClass.getName() ); ! } ! } ! ! if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true); ! return result; ! ! } ! ! public static Method getSetterMethod(Class theClass, String propertyName) throws MappingException { ! ! if (theClass==Object.class || theClass==null) throw new MappingException( "Could not find a setter" ); // will be swallowed ! ! Method result = setter(theClass, propertyName); ! ! if(result==null) { ! try { ! return getSetterMethod( theClass.getSuperclass(), propertyName ); ! } ! catch (MappingException me) { ! throw new MappingException( "Could not find a setter for " + propertyName + " in class " + theClass.getName() ); ! } ! } ! else { ! if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true); ! return result; ! } ! ! } ! ! private static Method setter(Class theClass, String propertyName) { ! ! Method[] methods = theClass.getDeclaredMethods(); ! for (int i=0; i<methods.length; i++) { ! if( ! ( methods[i].getName().length() > 3 ) && ! ( methods[i].getName().startsWith("set") ) ! ) { ! String testStdMethod = Introspector.decapitalize( methods[i].getName().substring(3) ); ! String testOldMethod = methods[i].getName().substring(3); ! if ( ! ( testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName) ) && ! ( methods[i].getParameterTypes().length==1 ) ! ) return methods[i]; ! } ! } ! return null; ! } ! ! public static Method getGetterMethod(Class theClass, String propertyName) throws MappingException { ! ! if (theClass==Object.class || theClass==null) throw new MappingException( "Could not find a getter" ); // will be swallowed ! ! Method result = getter(theClass, propertyName); ! ! if(result==null) { ! try { ! return getGetterMethod( theClass.getSuperclass(), propertyName ); ! } ! catch (MappingException me) { ! throw new MappingException( "Could not find a getter for " + propertyName + " in class " + theClass.getName() ); ! } ! } ! else { ! if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true); ! return result; ! } ! } ! ! private static Method getter(Class theClass, String propertyName) { ! ! Method[] methods = theClass.getDeclaredMethods(); ! for (int i=0; i<methods.length; i++) { ! // only carry on if the method has no parameters ! if(methods[i].getParameterTypes().length==0) { ! ! // try "get" ! if( (methods[i].getName().length() > 3) && methods[i].getName().startsWith("get") ) { ! ! String testStdMethod = Introspector.decapitalize( methods[i].getName().substring(3) ); ! String testOldMethod = methods[i].getName().substring(3); ! if( ! ( testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName) ) && ! methods[i].getParameterTypes().length==0 ! ) return methods[i]; ! ! } ! ! // if not "get" then try "is" ! if( (methods[i].getName().length() > 2) && methods[i].getName().startsWith("is") ) { ! ! String testStdMethod = Introspector.decapitalize( methods[i].getName().substring(2) ); ! String testOldMethod = methods[i].getName().substring(2); ! if( ! ( testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName) ) && ! methods[i].getParameterTypes().length==0 ! ) return methods[i]; ! } ! } ! } ! return null; ! } ! ! public static Type reflectedPropertyType(Class theClass, String name) throws MappingException { ! return TypeFactory.hueristicType( getGetterMethod(theClass, name).getReturnType().getName() ); ! } ! ! public static Class classForName(String name) throws ClassNotFoundException { ! try { ! return Thread.currentThread().getContextClassLoader().loadClass(name); ! } ! catch (Exception e) { ! return Class.forName(name); ! } ! } ! ! public static void set(Method method, Object target, Object value, Class clazz, String propertyName) throws HibernateException { ! try { ! method.invoke( target, new Object[] { value } ); ! } ! catch (NullPointerException npe) { ! throw new PropertyAccessException(npe, "NullPointerException occurred inside", true, clazz, propertyName); ! } ! catch (InvocationTargetException ite) { ! throw new PropertyAccessException(ite, "Exception occurred inside", true, clazz, propertyName); ! } ! catch (IllegalAccessException iae) { ! throw new PropertyAccessException(iae, "IllegalAccessException occurred while calling", true, clazz, propertyName); ! //cannot occur ! } ! catch (IllegalArgumentException iae) { ! throw new PropertyAccessException(iae, "IllegalArgumentException occurred while calling", true, clazz, propertyName); ! } ! } ! ! public static Object get(Method method, Object target, Class clazz, String propertyName) throws HibernateException { ! try { ! return method.invoke(target, null); ! } ! catch (InvocationTargetException ite) { ! throw new PropertyAccessException(ite, "Exception occurred inside", false, clazz, propertyName); ! } ! catch (IllegalAccessException iae) { ! throw new PropertyAccessException(iae, "IllegalAccessException occurred while calling", false, clazz, propertyName); ! //cannot occur ! } ! catch (IllegalArgumentException iae) { ! throw new PropertyAccessException(iae, "IllegalArgumentException occurred calling", false, clazz, propertyName); ! } ! } ! ! public static boolean isPublic(Class clazz, Member member) { ! return Modifier.isPublic( member.getModifiers() ) && Modifier.isPublic( clazz.getModifiers() ); ! } ! ! public static Object getConstantValue(String name) { ! Class clazz; ! try { ! clazz = classForName( StringHelper.qualifier(name) ); ! } ! catch(ClassNotFoundException cnfe) { ! return null; ! } ! try { ! return clazz.getField( StringHelper.unqualify(name) ).get(null); ! } ! catch (Exception e) { ! return null; ! } ! } ! ! public static Constructor getDefaultConstructor(Class clazz) throws MappingException { ! ! if (isAbstractClass(clazz)) return null; ! ! try { ! Constructor constructor = clazz.getDeclaredConstructor(NO_CLASSES); ! if (!isPublic(clazz, constructor)) { ! constructor.setAccessible(true); ! } ! return constructor; ! } catch (NoSuchMethodException nme) { ! throw new MappingException( ! "Object class " + clazz.getName() + ! " must declare a default (no-argument) constructor" ! ); ! } ! ! } ! ! public static boolean isAbstractClass(Class clazz) { ! int modifier = clazz.getModifiers(); ! return (Modifier.isAbstract(modifier) || Modifier.isInterface(modifier)); ! } ! ! } |