[Snmap-developer] SNMAP/src/net/sf/snmap/property Setter.java,NONE,1.1 BasicPropertyAccessor.java,NO
Status: Planning
Brought to you by:
arden
|
From: arden l. <ar...@us...> - 2006-01-13 03:25:55
|
Update of /cvsroot/snmap/SNMAP/src/net/sf/snmap/property In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7932/src/net/sf/snmap/property Added Files: Setter.java BasicPropertyAccessor.java Getter.java PropertyAccessor.java Log Message: --- NEW FILE: Getter.java --- //$Id$ package net.sf.snmap.property; import java.io.Serializable; import java.lang.reflect.Method; import net.sf.snmap.SnmapException; /** * Gets values of a particular property * * @author Gavin King */ public interface Getter extends Serializable { /** * Get the property value from the given instance . * @param owner The instance containing the value to be retreived. * @return The extracted value. * @throws HibernateException */ public Object get(Object owner) throws SnmapException; /** * Get the declared Java type */ public Class getReturnType(); /** * Optional operation (return null) */ public String getMethodName(); /** * Optional operation (return null) */ public Method getMethod(); } --- NEW FILE: BasicPropertyAccessor.java --- //$Id$ package net.sf.snmap.property; import java.beans.Introspector; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import net.sf.snmap.PropertyAccessException; import net.sf.snmap.PropertyNotFoundException; import net.sf.snmap.SnmapException; import net.sf.snmap.util.ReflectHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Accesses property values via a get/set pair, which may be nonpublic. * The default (and recommended strategy). * @author Gavin King */ public class BasicPropertyAccessor implements PropertyAccessor { private static final Log log = LogFactory.getLog(BasicPropertyAccessor.class); public static final class BasicSetter implements Setter { private static final long serialVersionUID = 3986040833426548238L; private Class clazz; private final transient Method method; private final String propertyName; private BasicSetter(Class clazz, Method method, String propertyName) { this.clazz=clazz; this.method=method; this.propertyName=propertyName; } public void set(Object target, Object value) throws SnmapException { try { method.invoke( target, new Object[] { value } ); } catch (NullPointerException npe) { if ( value==null && method.getParameterTypes()[0].isPrimitive() ) { throw new PropertyAccessException(npe, "Null value 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) { if ( value==null && method.getParameterTypes()[0].isPrimitive() ) { throw new PropertyAccessException(iae, "Null value was assigned to a property of primitive type", true, clazz, propertyName); } else { log.error( "IllegalArgumentException in class: " + clazz.getName() + ", setter method of property: " + propertyName ); log.error( "expected type: " + method.getParameterTypes()[0].getName() + ", actual value: " + ( value==null ? null : value.getClass().getName() ) ); throw new PropertyAccessException(iae, "IllegalArgumentException occurred while calling", true, clazz, propertyName); } } } public Method getMethod() { return method; } public String getMethodName() { return method.getName(); } Object readResolve() { return createSetter(clazz, propertyName); } public String toString() { return "BasicSetter(" + clazz.getName() + '.' + propertyName + ')'; } } public static final class BasicGetter implements Getter { private static final long serialVersionUID = 3952031236835264141L; private Class clazz; private final transient Method method; private final String propertyName; private BasicGetter(Class clazz, Method method, String propertyName) { this.clazz=clazz; this.method=method; this.propertyName=propertyName; } public Object get(Object target) throws SnmapException { try { return method.invoke(target, ((Object[])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) { log.error( "IllegalArgumentException in class: " + clazz.getName() + ", getter method of property: " + propertyName ); throw new PropertyAccessException(iae, "IllegalArgumentException occurred calling", false, clazz, propertyName); } } public Object getForInsert(Object target) { return get( target ); } public Class getReturnType() { return method.getReturnType(); } public Method getMethod() { return method; } public String getMethodName() { return method.getName(); } public String toString() { return "BasicGetter(" + clazz.getName() + '.' + propertyName + ')'; } Object readResolve() { return createGetter(clazz, propertyName); } } public Setter getSetter(Class theClass, String propertyName) throws PropertyNotFoundException { return createSetter(theClass, propertyName); } private static Setter createSetter(Class theClass, String propertyName) throws PropertyNotFoundException { BasicSetter result = getSetterOrNull(theClass, propertyName); if (result==null) { throw new PropertyNotFoundException( "Could not find a setter for property " + propertyName + " in class " + theClass.getName() ); } return result; } private static BasicSetter getSetterOrNull(Class theClass, String propertyName) { if (theClass==Object.class || theClass==null) return null; Method method = setterMethod(theClass, propertyName); if (method!=null) { if ( !ReflectHelper.isPublic(theClass, method) ) method.setAccessible(true); return new BasicSetter(theClass, method, propertyName); } else { BasicSetter setter = getSetterOrNull( theClass.getSuperclass(), propertyName ); if (setter==null) { Class[] interfaces = theClass.getInterfaces(); for ( int i=0; setter==null && i<interfaces.length; i++ ) { setter=getSetterOrNull( interfaces[i], propertyName ); } } return setter; } } private static Method setterMethod(Class theClass, String propertyName) { BasicGetter getter = getGetterOrNull(theClass, propertyName); Class returnType = (getter==null) ? null : getter.getReturnType(); Method[] methods = theClass.getDeclaredMethods(); Method potentialSetter = null; for (int i=0; i<methods.length; i++) { String methodName = methods[i].getName(); if ( methods[i].getParameterTypes().length==1 && methodName.startsWith("set") ) { String testStdMethod = Introspector.decapitalize( methodName.substring(3) ); String testOldMethod = methodName.substring(3); if ( testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName) ) { potentialSetter = methods[i]; if ( returnType==null || methods[i].getParameterTypes()[0].equals(returnType) ) return potentialSetter; } } } return potentialSetter; } public Getter getGetter(Class theClass, String propertyName) throws PropertyNotFoundException { return createGetter(theClass, propertyName); } public static Getter createGetter(Class theClass, String propertyName) throws PropertyNotFoundException { BasicGetter result = getGetterOrNull(theClass, propertyName); if (result==null) { throw new PropertyNotFoundException( "Could not find a getter for " + propertyName + " in class " + theClass.getName() ); } return result; } private static BasicGetter getGetterOrNull(Class theClass, String propertyName) { if (theClass==Object.class || theClass==null) return null; Method method = getterMethod(theClass, propertyName); if (method!=null) { if ( !ReflectHelper.isPublic(theClass, method) ) method.setAccessible(true); return new BasicGetter(theClass, method, propertyName); } else { BasicGetter getter = getGetterOrNull( theClass.getSuperclass(), propertyName ); if (getter==null) { Class[] interfaces = theClass.getInterfaces(); for ( int i=0; getter==null && i<interfaces.length; i++ ) { getter=getGetterOrNull( interfaces[i], propertyName ); } } return getter; } } private static Method getterMethod(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 ) { String methodName = methods[i].getName(); // try "get" if( methodName.startsWith("get") ) { String testStdMethod = Introspector.decapitalize( methodName.substring(3) ); String testOldMethod = methodName.substring(3); if( testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName) ) return methods[i]; } // if not "get" then try "is" /*boolean isBoolean = methods[i].getReturnType().equals(Boolean.class) || methods[i].getReturnType().equals(boolean.class);*/ if( methodName.startsWith("is") ) { String testStdMethod = Introspector.decapitalize( methodName.substring(2) ); String testOldMethod = methodName.substring(2); if( testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName) ) return methods[i]; } } } return null; } } --- NEW FILE: PropertyAccessor.java --- package net.sf.snmap.property; import net.sf.snmap.PropertyNotFoundException; /** * Abstracts the notion of a "property". Defines a strategy for accessing the * value of an attribute. * @author Gavin King */ public interface PropertyAccessor { /** * Create a "getter" for the named attribute */ public Getter getGetter(Class theClass, String propertyName) throws PropertyNotFoundException; /** * Create a "setter" for the named attribute */ public Setter getSetter(Class theClass, String propertyName) throws PropertyNotFoundException; } --- NEW FILE: Setter.java --- package net.sf.snmap.property; import java.io.Serializable; import java.lang.reflect.Method; import net.sf.snmap.SnmapException; /** * Sets values to a particular property. * * @author Gavin King */ public interface Setter extends Serializable { /** * Set the property value from the given instance * * @param target The instance upon which to set the given value. * @param value The value to be set on the target. * @param factory The session factory from which this request originated. * @throws HibernateException */ public void set(Object target, Object value) throws SnmapException; /** * Optional operation (return null) */ public String getMethodName(); /** * Optional operation (return null) */ public Method getMethod(); } |