From: <one...@us...> - 2002-11-30 08:19:02
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers In directory sc8-pr-cvs1:/tmp/cvs-serv13167/hibernate/helpers Modified Files: ReflectHelper.java Log Message: introduced Getter and Setter classes and improved an exception message setting null to a primitive property Index: ReflectHelper.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers/ReflectHelper.java,v retrieving revision 1.34 retrieving revision 1.35 diff -C2 -d -r1.34 -r1.35 *** ReflectHelper.java 26 Nov 2002 03:35:42 -0000 1.34 --- ReflectHelper.java 30 Nov 2002 08:18:59 -0000 1.35 *************** *** 21,24 **** --- 21,98 ---- private static final Method OBJECT_EQUALS; + public static final class Setter { + private Class clazz; + private final Method method; + private final String propertyName; + + private Setter(Class clazz, Method method, String propertyName) { + this.clazz=clazz; + this.method=method; + this.propertyName=propertyName; + } + + public void set(Object target, Object value) throws HibernateException { + try { + method.invoke( target, new Object[] { value } ); + } + catch (NullPointerException npe) { + if ( value==null && method.getParameterTypes()[0].isPrimitive() ) { + throw new PropertyAccessException(npe, "Null values was assigned to a property of primitive type", true, clazz, propertyName); + } + else { + throw new PropertyAccessException(npe, "NullPointerException occurred while calling", 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 final class Getter { + private Class clazz; + private final Method method; + private final String propertyName; + + private Getter(Class clazz, Method method, String propertyName) { + this.clazz=clazz; + this.method=method; + this.propertyName=propertyName; + } + + public Object get(Object target) 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 Class getReturnType() { + return method.getReturnType(); + } + + public Method getMethod() { + return method; + } + + } + static { Method eq; *************** *** 66,70 **** } ! 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 --- 140,144 ---- } ! public static Setter getSetter(Class theClass, String propertyName) throws MappingException { if (theClass==Object.class || theClass==null) throw new MappingException( "Could not find a setter" ); // will be swallowed *************** *** 74,78 **** if(result==null) { try { ! return getSetterMethod( theClass.getSuperclass(), propertyName ); } catch (MappingException me) { --- 148,152 ---- if(result==null) { try { ! return getSetter( theClass.getSuperclass(), propertyName ); } catch (MappingException me) { *************** *** 82,86 **** else { if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true); ! return result; } --- 156,160 ---- else { if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true); ! return new Setter(theClass, result, propertyName); } *************** *** 106,110 **** } ! 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 --- 180,184 ---- } ! public static Getter getGetter(Class theClass, String propertyName) throws MappingException { if (theClass==Object.class || theClass==null) throw new MappingException( "Could not find a getter" ); // will be swallowed *************** *** 114,118 **** if(result==null) { try { ! return getGetterMethod( theClass.getSuperclass(), propertyName ); } catch (MappingException me) { --- 188,192 ---- if(result==null) { try { ! return getGetter( theClass.getSuperclass(), propertyName ); } catch (MappingException me) { *************** *** 122,126 **** else { if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true); ! return result; } } --- 196,200 ---- else { if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true); ! return new Getter(theClass, result, propertyName); } } *************** *** 161,165 **** public static Type reflectedPropertyType(Class theClass, String name) throws MappingException { ! return TypeFactory.hueristicType( getGetterMethod(theClass, name).getReturnType().getName() ); } --- 235,239 ---- public static Type reflectedPropertyType(Class theClass, String name) throws MappingException { ! return TypeFactory.hueristicType( getGetter(theClass, name).getReturnType().getName() ); } *************** *** 170,208 **** 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); } } --- 244,247 ---- |