From: <one...@us...> - 2002-11-26 03:36:14
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers In directory sc8-pr-cvs1:/tmp/cvs-serv20166/cirrus/hibernate/helpers Modified Files: IdentityMap.java JoinedIterator.java PropertiesHelper.java ReflectHelper.java Log Message: fixed broken line-endings and added a test Index: IdentityMap.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers/IdentityMap.java,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** IdentityMap.java 10 Aug 2002 02:18:53 -0000 1.18 --- IdentityMap.java 26 Nov 2002 03:35:41 -0000 1.19 *************** *** 140,144 **** } return set; ! } } --- 140,144 ---- } return set; ! } } Index: JoinedIterator.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers/JoinedIterator.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** JoinedIterator.java 28 May 2002 15:09:25 -0000 1.2 --- JoinedIterator.java 26 Nov 2002 03:35:42 -0000 1.3 *************** *** 32,36 **** iterators[current].remove(); } ! } --- 32,36 ---- iterators[current].remove(); } ! } Index: PropertiesHelper.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers/PropertiesHelper.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PropertiesHelper.java 2 Nov 2002 14:32:12 -0000 1.2 --- PropertiesHelper.java 26 Nov 2002 03:35:42 -0000 1.3 *************** *** 1,17 **** ! //$Id$ ! package cirrus.hibernate.helpers; ! ! import java.util.HashMap; ! import java.util.Map; ! import java.util.Properties; ! import java.util.StringTokenizer; ! ! ! public final class PropertiesHelper { ! public static boolean getBoolean(String property, Properties properties) { return Boolean.valueOf( properties.getProperty(property) ).booleanValue(); ! } ! public static int getInt(String property, Properties properties, int defaultValue) { String propValue = properties.getProperty(property); --- 1,17 ---- ! //$Id$ ! package cirrus.hibernate.helpers; ! ! import java.util.HashMap; ! import java.util.Map; ! import java.util.Properties; ! import java.util.StringTokenizer; ! ! ! public final class PropertiesHelper { ! public static boolean getBoolean(String property, Properties properties) { return Boolean.valueOf( properties.getProperty(property) ).booleanValue(); ! } ! public static int getInt(String property, Properties properties, int defaultValue) { String propValue = properties.getProperty(property); *************** *** 19,24 **** } ! ! public static Integer getInteger(String property, Properties properties) { String propValue = properties.getProperty(property); --- 19,24 ---- } ! ! public static Integer getInteger(String property, Properties properties) { String propValue = properties.getProperty(property); *************** *** 26,56 **** } ! public static Map toMap(String property, String delim, Properties properties) { ! Map map = new HashMap(); ! String propValue = properties.getProperty(property); ! if (propValue!=null) { ! StringTokenizer tokens = new StringTokenizer(propValue, delim); ! while ( tokens.hasMoreTokens() ) { ! map.put( ! tokens.nextToken(), ! tokens.hasMoreElements() ? tokens.nextToken() : "" ! ); ! } ! } ! return map; ! } ! ! public static String[] toStringArray(String property, String delim, Properties properties) { ! return toStringArray( properties.getProperty(property), delim ); } ! ! public static String[] toStringArray(String propValue, String delim) { ! if (propValue!=null) { ! return StringHelper.split(delim, propValue); ! } ! else { ! return new String[0]; ! } } ! ! } --- 26,56 ---- } ! public static Map toMap(String property, String delim, Properties properties) { ! Map map = new HashMap(); ! String propValue = properties.getProperty(property); ! if (propValue!=null) { ! StringTokenizer tokens = new StringTokenizer(propValue, delim); ! while ( tokens.hasMoreTokens() ) { ! map.put( ! tokens.nextToken(), ! tokens.hasMoreElements() ? tokens.nextToken() : "" ! ); ! } ! } ! return map; } ! ! public static String[] toStringArray(String property, String delim, Properties properties) { ! return toStringArray( properties.getProperty(property), delim ); } ! ! public static String[] toStringArray(String propValue, String delim) { ! if (propValue!=null) { ! return StringHelper.split(delim, propValue); ! } ! else { ! return new String[0]; ! } ! } ! ! } Index: ReflectHelper.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers/ReflectHelper.java,v retrieving revision 1.33 retrieving revision 1.34 diff -C2 -d -r1.33 -r1.34 *** ReflectHelper.java 9 Nov 2002 01:38:05 -0000 1.33 --- ReflectHelper.java 26 Nov 2002 03:35:42 -0000 1.34 *************** *** 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)); ! } ! ! } --- 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)); ! } ! ! } |