FileDocCategorySizeDatePackage
BasicPropertyAccessor.javaAPI DocHibernate 3.2.59611Sat Jul 16 17:20:48 BST 2005org.hibernate.property

BasicPropertyAccessor

public class BasicPropertyAccessor extends Object implements PropertyAccessor
Accesses property values via a get/set pair, which may be nonpublic. The default (and recommended strategy).
author
Gavin King

Fields Summary
private static final Log
log
Constructors Summary
Methods Summary
public static GettercreateGetter(java.lang.Class theClass, java.lang.String propertyName)

		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 SettercreateSetter(java.lang.Class theClass, java.lang.String propertyName)

		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;
	
public GettergetGetter(java.lang.Class theClass, java.lang.String propertyName)

		return createGetter(theClass, propertyName);
	
private static org.hibernate.property.BasicPropertyAccessor$BasicGettergetGetterOrNull(java.lang.Class theClass, java.lang.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;
		}
	
public SettergetSetter(java.lang.Class theClass, java.lang.String propertyName)

		return createSetter(theClass, propertyName);
	
private static org.hibernate.property.BasicPropertyAccessor$BasicSettergetSetterOrNull(java.lang.Class theClass, java.lang.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 java.lang.reflect.MethodgetterMethod(java.lang.Class theClass, java.lang.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;
	
private static java.lang.reflect.MethodsetterMethod(java.lang.Class theClass, java.lang.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;