#13 Performance Optimization for Reflect.getObjectFieldValue()

open
nobody
None
5
2006-09-08
2006-09-08
Anonymous
No

/**
* This is a static instance of a zero length
Object array, that is frequently used
* in getter method calls
*/
private static final Object[]
zeroLengthObjectArray = new Object[0];

......

/**
*/
public static Object getObjectFieldValue( Object
object, String fieldName )
throws UtilEvalError, ReflectError
{
if ( object instanceof This )
return ((This)
object).namespace.getVariable( fieldName );
else {
try {
return getFieldValue(
object.getClass
(), object, fieldName, false/*onlystatic*/);
} catch ( ReflectError e ) {
// no field, try
property acces

// FIXME: THIS WAS
KIND OF INEFFICIENT, FIRST hasObjectPropertyGetter()
// CHECKS THE CURRENT
OBJECT, AND IF IT IS SUCCESSFUL, THE ENTIRE CLASS
HIERARCHY
// IS TRAVERSED TO
FIND A "MOST SPECIFIC MATCH" FOR A ZERO-ARGUMENT
METHOD !!!!
// if (
hasObjectPropertyGetter( object.getClass(),
fieldName ) )
// return
getObjectProperty( object, fieldName );
// else
// throw e;
try {
String
accessorName = accessorName( "get", fieldName );
Method method
= object.getClass().getMethod( accessorName,
zeroLengthClassArray );
return
invokeMethod( method, object, null );
} catch (
NoSuchMethodException exc ) {
} catch (
InvocationTargetException exc ) {
} catch (
SecurityException exc ) {
} catch ( ReflectError
exc ) {
} catch ( Exception
exc ) {}
try {
String
accessorName = accessorName( "is", fieldName );
Method method
= object.getClass().getMethod( accessorName,
zeroLengthClassArray );
if (
method.getReturnType() == Boolean.TYPE ) {
return
invokeMethod( method, object, null );
}
} catch (
NoSuchMethodException exc ) {
} catch (
InvocationTargetException exc ) {
} catch (
SecurityException exc ) {
} catch ( ReflectError
exc ) {
} catch ( Exception
exc ) {}
// FIXME: END OF
OPTIMIZATION
throw e;
}
}
}

Discussion