|
From: <one...@us...> - 2003-01-13 13:05:47
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers
In directory sc8-pr-cvs1:/tmp/cvs-serv11899/hibernate/helpers
Modified Files:
ReflectHelper.java
Log Message:
fixed bug finding properties inherited by interfaces
added ScrollableResults.isFirst(), isLast()
Index: ReflectHelper.java
===================================================================
RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/helpers/ReflectHelper.java,v
retrieving revision 1.38
retrieving revision 1.39
diff -C2 -d -r1.38 -r1.39
*** ReflectHelper.java 9 Jan 2003 09:40:18 -0000 1.38
--- ReflectHelper.java 13 Jan 2003 13:05:45 -0000 1.39
***************
*** 2,6 ****
package cirrus.hibernate.helpers;
! import java.beans.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
--- 2,6 ----
package cirrus.hibernate.helpers;
! import java.beans.Introspector;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
***************
*** 9,28 ****
import java.lang.reflect.Modifier;
! import cirrus.hibernate.*;
! 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;
!
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;
--- 9,32 ----
import java.lang.reflect.Modifier;
! import cirrus.hibernate.AssertionFailure;
! import cirrus.hibernate.HibernateException;
! import cirrus.hibernate.MappingException;
! import cirrus.hibernate.PropertyAccessException;
! import cirrus.hibernate.PropertyNotFoundException;
import cirrus.hibernate.type.Type;
+ import cirrus.hibernate.type.TypeFactory;
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;
!
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;
***************
*** 30,34 ****
this.propertyName=propertyName;
}
!
public void set(Object target, Object value) throws HibernateException {
try {
--- 34,38 ----
this.propertyName=propertyName;
}
!
public void set(Object target, Object value) throws HibernateException {
try {
***************
*** 51,70 ****
}
catch (IllegalArgumentException iae) {
! if (value == null) {
! throw new PropertyAccessException(iae, "IllegalArgumentException (null value) occurred while calling", true, clazz, propertyName);
! }
! else {
! 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;
--- 55,69 ----
}
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;
***************
*** 72,76 ****
this.propertyName=propertyName;
}
!
public Object get(Object target) throws HibernateException {
try {
--- 71,75 ----
this.propertyName=propertyName;
}
!
public Object get(Object target) throws HibernateException {
try {
***************
*** 88,102 ****
}
}
!
public Class getReturnType() {
return method.getReturnType();
}
!
public Method getMethod() {
return method;
}
!
}
!
static {
Method eq;
--- 87,101 ----
}
}
!
public Class getReturnType() {
return method.getReturnType();
}
!
public Method getMethod() {
return method;
}
!
}
!
static {
Method eq;
***************
*** 109,113 ****
OBJECT_EQUALS = eq;
}
!
public static boolean overridesEquals(Class clazz) {
Method equals;
--- 108,112 ----
OBJECT_EQUALS = eq;
}
!
public static boolean overridesEquals(Class clazz) {
Method equals;
***************
*** 120,172 ****
return !OBJECT_EQUALS.equals(equals);
}
!
!
! public static Method getMethod(Class theClass, String methodName) throws PropertyNotFoundException {
!
! if (theClass==Object.class || theClass==null) throw new PropertyNotFoundException( "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 (PropertyNotFoundException me) {
! throw new PropertyNotFoundException( "Could not find method " + methodName + " in class " + theClass.getName() );
! }
! }
!
! if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true);
return result;
-
}
!
! public static Setter getSetter(Class theClass, String propertyName) throws PropertyNotFoundException {
!
! if (theClass==Object.class || theClass==null) throw new PropertyNotFoundException( "Could not find a setter" ); // will be swallowed
!
! Method result = setter(theClass, propertyName);
!
! if(result==null) {
! try {
! return getSetter( theClass.getSuperclass(), propertyName );
! }
! catch (PropertyNotFoundException me) {
! throw new PropertyNotFoundException( "Could not find a setter for " + propertyName + " in class " + theClass.getName() );
! }
}
else {
! if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true);
! return new Setter(theClass, result, propertyName);
}
!
}
!
! private static Method setter(Class theClass, String propertyName) throws PropertyNotFoundException {
!
! Class returnType = getGetter(theClass, propertyName).getReturnType();
!
Method[] methods = theClass.getDeclaredMethods();
Method potentialSetter = null;
--- 119,156 ----
return !OBJECT_EQUALS.equals(equals);
}
!
! public static Setter getSetter(Class theClass, String propertyName) throws PropertyNotFoundException {
! Setter 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 Setter 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 Setter(theClass, method, propertyName);
}
else {
! Setter 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) {
!
! Class returnType = getGetterOrNull(theClass, propertyName).getReturnType();
!
Method[] methods = theClass.getDeclaredMethods();
Method potentialSetter = null;
***************
*** 183,187 ****
) {
potentialSetter = methods[i];
! if ( methods[i].getParameterTypes()[0].equals(returnType) ) return potentialSetter;
}
}
--- 167,171 ----
) {
potentialSetter = methods[i];
! if ( returnType==null || methods[i].getParameterTypes()[0].equals(returnType) ) return potentialSetter;
}
}
***************
*** 189,223 ****
return potentialSetter;
}
!
public static Getter getGetter(Class theClass, String propertyName) throws PropertyNotFoundException {
!
! if (theClass==Object.class || theClass==null) throw new PropertyNotFoundException( "Could not find a getter" ); // will be swallowed
!
! Method result = getter(theClass, propertyName);
!
! if(result==null) {
! try {
! return getGetter( theClass.getSuperclass(), propertyName );
! }
! catch (PropertyNotFoundException me) {
! throw new PropertyNotFoundException( "Could not find a getter for " + propertyName + " in class " + theClass.getName() );
! }
}
else {
! if ( !ReflectHelper.isPublic(theClass, result) ) result.setAccessible(true);
! return new Getter(theClass, result, propertyName);
}
}
!
! 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);
--- 173,216 ----
return potentialSetter;
}
!
public static Getter getGetter(Class theClass, String propertyName) throws PropertyNotFoundException {
! Getter 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 Getter 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 Getter(theClass, method, propertyName);
}
else {
! Getter 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) {
!
// 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);
***************
*** 226,235 ****
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);
--- 219,228 ----
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);
***************
*** 243,251 ****
return null;
}
!
public static Type reflectedPropertyType(Class theClass, String name) throws MappingException {
return TypeFactory.hueristicType( getGetter(theClass, name).getReturnType().getName() );
}
!
public static Class classForName(String name) throws ClassNotFoundException {
try {
--- 236,244 ----
return null;
}
!
public static Type reflectedPropertyType(Class theClass, String name) throws MappingException {
return TypeFactory.hueristicType( getGetter(theClass, name).getReturnType().getName() );
}
!
public static Class classForName(String name) throws ClassNotFoundException {
try {
***************
*** 256,264 ****
}
}
!
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;
--- 249,257 ----
}
}
!
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;
***************
*** 276,284 ****
}
}
!
public static Constructor getDefaultConstructor(Class clazz) throws PropertyNotFoundException {
!
if (isAbstractClass(clazz)) return null;
!
try {
Constructor constructor = clazz.getDeclaredConstructor(NO_CLASSES);
--- 269,277 ----
}
}
!
public static Constructor getDefaultConstructor(Class clazz) throws PropertyNotFoundException {
!
if (isAbstractClass(clazz)) return null;
!
try {
Constructor constructor = clazz.getDeclaredConstructor(NO_CLASSES);
***************
*** 287,291 ****
}
return constructor;
! } catch (NoSuchMethodException nme) {
throw new PropertyNotFoundException(
"Object class " + clazz.getName() +
--- 280,285 ----
}
return constructor;
! }
! catch (NoSuchMethodException nme) {
throw new PropertyNotFoundException(
"Object class " + clazz.getName() +
***************
*** 293,299 ****
);
}
!
}
!
public static boolean isAbstractClass(Class clazz) {
int modifier = clazz.getModifiers();
--- 287,293 ----
);
}
!
}
!
public static boolean isAbstractClass(Class clazz) {
int modifier = clazz.getModifiers();
|