FileDocCategorySizeDatePackage
ReflectHelper.javaAPI DocHibernate 3.2.56044Wed Jul 12 10:40:00 BST 2006org.hibernate.util

ReflectHelper

public final class ReflectHelper extends Object

Fields Summary
private static final org.hibernate.property.PropertyAccessor
BASIC_PROPERTY_ACCESSOR
private static final org.hibernate.property.PropertyAccessor
DIRECT_PROPERTY_ACCESSOR
private static final Class[]
NO_CLASSES
private static final Class[]
OBJECT
private static final Method
OBJECT_EQUALS
private static final Class[]
NO_PARAM
private static final Method
OBJECT_HASHCODE
Constructors Summary
private ReflectHelper()

Methods Summary
public static java.lang.ClassclassForName(java.lang.String name)

		try {
			ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
			if ( contextClassLoader != null ) {
				return contextClassLoader.loadClass(name);
			}
		}
		catch ( Throwable t ) {
		}
		return Class.forName( name );
	
public static java.lang.ClassclassForName(java.lang.String name, java.lang.Class caller)

		try {
			ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
			if ( contextClassLoader != null ) {
				return contextClassLoader.loadClass( name );
			}
		}
		catch ( Throwable e ) {
		}
		return Class.forName( name, true, caller.getClassLoader() );
	
public static java.lang.ObjectgetConstantValue(java.lang.String name)

		Class clazz;
		try {
			clazz = classForName( StringHelper.qualifier( name ) );
		}
		catch ( Throwable t ) {
			return null;
		}
		try {
			return clazz.getField( StringHelper.unqualify( name ) ).get(null);
		}
		catch ( Throwable t ) {
			return null;
		}
	
public static java.lang.reflect.ConstructorgetConstructor(java.lang.Class clazz, org.hibernate.type.Type[] types)

		final Constructor[] candidates = clazz.getConstructors();
		for ( int i=0; i<candidates.length; i++ ) {
			final Constructor constructor = candidates[i];
			final Class[] params = constructor.getParameterTypes();
			if ( params.length==types.length ) {
				boolean found = true;
				for ( int j=0; j<params.length; j++ ) {
					final boolean ok = params[j].isAssignableFrom( types[j].getReturnedClass() ) || (
						types[j] instanceof PrimitiveType &&
						params[j] == ( (PrimitiveType) types[j] ).getPrimitiveClass()
					);
					if (!ok) {
						found = false;
						break;
					}
				}
				if (found) {
					if ( !isPublic(clazz, constructor) ) constructor.setAccessible(true);
					return constructor;
				}
			}
		}
		throw new PropertyNotFoundException( "no appropriate constructor in class: " + clazz.getName() );
	
public static java.lang.reflect.ConstructorgetDefaultConstructor(java.lang.Class clazz)


		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 PropertyNotFoundException(
				"Object class " + clazz.getName() +
				" must declare a default (no-argument) constructor"
			);
		}

	
public static org.hibernate.property.GettergetGetter(java.lang.Class theClass, java.lang.String name)

		return BASIC_PROPERTY_ACCESSOR.getGetter(theClass, name);
	
public static java.lang.reflect.MethodgetMethod(java.lang.Class clazz, java.lang.reflect.Method method)

		try {
			return clazz.getMethod( method.getName(), method.getParameterTypes() );
		}
		catch (Exception e) {
			return null;
		}
	
private static org.hibernate.property.Gettergetter(java.lang.Class clazz, java.lang.String name)

		try {
			return BASIC_PROPERTY_ACCESSOR.getGetter(clazz, name);
		}
		catch (PropertyNotFoundException pnfe) {
			return DIRECT_PROPERTY_ACCESSOR.getGetter(clazz, name);
		}
	
public static booleanisAbstractClass(java.lang.Class clazz)

		int modifier = clazz.getModifiers();
		return Modifier.isAbstract(modifier) || Modifier.isInterface(modifier);
	
public static booleanisFinalClass(java.lang.Class clazz)

		return Modifier.isFinal( clazz.getModifiers() );
	
public static booleanisPublic(java.lang.Class clazz, java.lang.reflect.Member member)

		return Modifier.isPublic( member.getModifiers() ) && Modifier.isPublic( clazz.getModifiers() );
	
public static booleanoverridesEquals(java.lang.Class clazz)

	 
		Method eq;
		Method hash;
		try {
			eq = Object.class.getMethod("equals", OBJECT);
			hash = Object.class.getMethod("hashCode", NO_PARAM);
		}
		catch (Exception e) {
			throw new AssertionFailure("Could not find Object.equals() or Object.hashCode()", e);
		}
		OBJECT_EQUALS = eq;
		OBJECT_HASHCODE = hash;
	
		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 booleanoverridesHashCode(java.lang.Class clazz)

		Method hashCode;
		try {
			hashCode = clazz.getMethod("hashCode", NO_PARAM);
		}
		catch (NoSuchMethodException nsme) {
			return false; //its an interface so we can't really tell anything...
		}
		return !OBJECT_HASHCODE.equals(hashCode);
	
public static java.lang.ClassreflectedPropertyClass(java.lang.String className, java.lang.String name)

		try {
			Class clazz = ReflectHelper.classForName(className);
			return getter(clazz, name).getReturnType();
		}
		catch (ClassNotFoundException cnfe) {
			throw new MappingException("class " + className + " not found while looking for property: " + name, cnfe);
		}