ewe.reflect
Class Reflect

java.lang.Object
  extended byewe.reflect.Reflect

public final class Reflect
extends Object

ewe.reflect.Reflect provides methods for certain Class reflection operations which are not provided by the Class object in Ewe. Note some important differences between Class and Reflect:

1. Reflect.forName() can load classes which are system classes (in the Ewe Library) AND classes which are contained in the ".ewe" files as specified in the command line. However Class.forName() can ONLY load system Classes. Therefore Reflect.forName() is a better way to locate reflection information for your application classes. Neither Reflect.forName() nor Class.forName() can load classes which have been loaded by a ClassLoader object.

2. You cannot use Reflect to represent primitive types - while you can with Class. Attempting to create a Reflect with a Class that represents a primitive type will throw a RuntimeException.


Field Summary
static int DECLARED
          An option for retrieving Fields, Methods and Constructors.
protected  Constructor defaultConstructor
           
protected  Reflect mySuperClass
           
static int PUBLIC
          An option for retrieving Fields, Methods and Constructors.
 
Constructor Summary
Reflect(Class aClass)
          Create a Reflect object given a non-null and non-primitive Class.
 
Method Summary
static int arrayLength(Object obj)
          Gets the length of the array - if obj represents an array type.
static Object bestReference(Object data, Object classOrReflect)
          This returns the best object to get reference info on a particular class.
 Reflect findBaseClass(String baseName)
           
static Class getClass(String type, Class requestor)
          Deprecated. - use typeToClass() instead.
 String getClassName()
          This returns the className in '/' notation.
 String getComponentType()
          Returns the type of the elements of the array if this Reflect represents an array.
 Constructor getConstructor(String specs, int options)
          Look for a Constructor for the class.
 Constructor[] getConstructors(int options)
          Return an array of all the Constructors of the class.
 Field getField(String name, int options)
          Look for a Field in the class - either instance or static.
 Field[] getFields(int options)
          Return an array of all the Fields of the class.
static Reflect getForName(String name)
          Get a Reflect object that represents the specified class name.
static Reflect getForName(String name, Class requestor)
          Get a Reflect object that represents the specified class name.
static Reflect getForObject(Object obj)
          Get a Reflect object that represents this class.
 String getInterface(int idx)
          Get the name of the interface implemented by this class at the specified index.
 Method getMethod(String nameAndSpecs, int options)
          Look for a Method in the class - either instance or static.
 Method getMethod(String name, String specs, int options)
          Look for a Method in the class - either instance or static.
static String getMethodParameterList(String list)
          This returns the string within brackets in a method description.
 Method[] getMethods(int options)
          Return an array of all the Methods of the class.
 int getNumberOfInterfaces()
          Get the number of interfaces implemented by this class.
static String[] getParameters(String parameterList)
          This converts a parameter list (e.g.
 Class getReflectedClass()
          This returns a Class object representing the class being reflected.
protected  String getSuperClass()
           
static String getType(Class c)
          Returns the String encoded type of the class.
static char getWrapperType(Class c)
           
 boolean isArray()
          Returns if this Reflect object represents an array.
static boolean isArray(Object obj)
          Returns if the specified object is an array type.
 boolean isAssignableFrom(Reflect other)
          Determines if the class or interface represented by this Reflect object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Reflect parameter.
 boolean isBaseClass(String baseName)
           
 boolean isInstance(Object obj)
          Check if the specified object parameter is an instance of this reflected class.
 boolean isTypeOf(Reflect other)
          Determines if the class or interface represented by this Reflect object is either the same as, or is a subclass of, or implements, the class or interface represented by the specified Reflect parameter.
 boolean isTypeOf(String aType)
          Determines if the class or interface represented by this Reflect object is either the same as, or is a subclass of, or implements, the class or interface represented by the specified String parameter.
static boolean isTypeOf(String which, String aType)
          Even though this could be done by creating Reference objects, I have made it native for efficiency.
static Class loadClass(String name)
          This attempts to get a Class first using the system and then using any application class loaders.
static Reflect loadForName(String name)
          This attempts to get a Reflect first using the system and then using any application class loaders.
 Object newArray(int num)
          Create a new Array where each element is of the type reflected by this Reflect object.
static Object newArrayInstance(Class c, int length)
           
static Object newArrayInstance(String wrapperType, int length)
          Creates a new array of the specified component type (which should be "I" or "Z" or "Ljava/lang/String;" (etc.)
 Object newInstance()
          Create a new Instance of the Object IF it is not an array - using the default constructor.
static Object newInstance(String name)
          This creates a new instance of a Class given the class name.
 Object newInstance(String constructorSpecs, Wrapper[] parameters)
          Create a new Instance of the Object IF it is not an array - using the constructor with the specified parameters int Java type notation (eg "([BII)V").
static Class primitiveTypeToClass(char primitiveType)
          Convert a Java encoded primitive type to the Class representing that type.
 Reflect superClass()
          Get a Reflect Object representing the Superclass of this Reflect.
static Class toClass(Object objectOrClassOrReflect)
          This converts an Object or a Class or a Reflect into a Class object.
static Object toNonReflect(Object objectOrClassOrReflect)
          If objectOrClassOrReflect is a Class or Reflect object, this will return null, otherwise it will return the objectOrClassOrReflect.
static Reflect toReflect(Object objectOrClassOrReflect)
          This converts an Object or a Class or a Reflect into a Reflect object.
 String toString()
          Return a String representation of this object.
static Class typeToClass(String type)
          Creates a new class from a java encoded type string, searching all the registered class loaders if necessary.
static String typeToString(String fullType)
          This converts a encoded Java type to a printable type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode
 

Field Detail

defaultConstructor

protected Constructor defaultConstructor

mySuperClass

protected Reflect mySuperClass

DECLARED

public static final int DECLARED
An option for retrieving Fields, Methods and Constructors. This can be used OR PUBLIC.

See Also:
Constant Field Values

PUBLIC

public static final int PUBLIC
An option for retrieving Fields, Methods and Constructors. This can be used OR DECLARED.

See Also:
Constant Field Values
Constructor Detail

Reflect

public Reflect(Class aClass)
Create a Reflect object given a non-null and non-primitive Class.

Parameters:
aClass - The class for the object which must be non-null and non-primitive.
Method Detail

loadClass

public static Class loadClass(String name)
This attempts to get a Class first using the system and then using any application class loaders.

Parameters:
name - The name of the class.
Returns:
The resolved Class or null if not found.

loadForName

public static Reflect loadForName(String name)
This attempts to get a Reflect first using the system and then using any application class loaders.

Parameters:
name - The name of the class.
Returns:
The resolved Reflect or null if not found.

newInstance

public static Object newInstance(String name)
This creates a new instance of a Class given the class name. All active ClassLoaders are also used to try to resolve the Class.

Parameters:
name - The name of the class.
Returns:
a new instance of the class or null if it could not be created.

newArrayInstance

public static Object newArrayInstance(Class c,
                                      int length)

getForObject

public static Reflect getForObject(Object obj)
Get a Reflect object that represents this class. Will work for Objects and Arrays.

Parameters:
obj - The object - should not be null.
Returns:
A Reflect object if successful.

getForName

public static Reflect getForName(String name)
Get a Reflect object that represents the specified class name. It is better to call this method rather than Class.forName() because under Java, Class.forName() cannot find classes which are bound in a ".ewe" file - even though it is considered an application class.

This can only find classes that are not loaded from a class loader.

Parameters:
name - The name of the class.
Returns:
A Reflect object if successful - null otherwise.

getForName

public static Reflect getForName(String name,
                                 Class requestor)
Get a Reflect object that represents the specified class name. It is better to call this method rather than Class.forName() because under Java, Class.forName() cannot find classes which are bound in a ".ewe" file - even though it is considered an application class.

If the class is not an application class or system class AND the requestor Class has been loaded via a ClassLoader - then the ClassLoader is checked to see if it can locate the requested class.

Parameters:
name - The name of the class.
requestor - A requesting class.
Returns:
A Reflect object if successful - null otherwise.

getClassName

public String getClassName()
This returns the className in '/' notation.


getReflectedClass

public Class getReflectedClass()
This returns a Class object representing the class being reflected.


toString

public String toString()
Description copied from class: Object
Return a String representation of this object.

Overrides:
toString in class Object
Returns:
a String representing this object.

getField

public Field getField(String name,
                      int options)
Look for a Field in the class - either instance or static.

Parameters:
name - The name of the field.
options - Either DECLARED or PUBLIC.
Returns:
A Field object or null if the field is not found.

getMethod

public Method getMethod(String nameAndSpecs,
                        int options)
Look for a Method in the class - either instance or static.

Parameters:
nameAndSpecs - Must be in Java type specification. For example "aBooleanMethod(ILjava/lang/Object;J)Z" specifies a method named "aBooleanMethod" that takes three parameters: (int, java.lang.Object, long) and returns a boolean value. Void methods will end with a "V".
options - Either DECLARED or PUBLIC.
Returns:
A Method object or null if the method is not found.

getMethod

public Method getMethod(String name,
                        String specs,
                        int options)
Look for a Method in the class - either instance or static.

Parameters:
name - The name of the method.
specs - The specs (parameters) for the method - must be in Java type specification.
options - Either DECLARED or PUBLIC.
Returns:
A Method object or null if the method is not found.

getConstructor

public Constructor getConstructor(String specs,
                                  int options)
Look for a Constructor for the class.

Parameters:
specs - The specs (parameters) for the method - must be in Java type specification.
options - Either DECLARED or PUBLIC.
Returns:
A Constructor object or null if not found.

newArray

public Object newArray(int num)
Create a new Array where each element is of the type reflected by this Reflect object.

Parameters:
num - The length of the array, must be >= 0.
Returns:
A new Array if successful.

newInstance

public Object newInstance()
Create a new Instance of the Object IF it is not an array - using the default constructor. Returns the new Object or null if failed.


newInstance

public Object newInstance(String constructorSpecs,
                          Wrapper[] parameters)
Create a new Instance of the Object IF it is not an array - using the constructor with the specified parameters int Java type notation (eg "([BII)V").

Parameters:
parameters -
Returns:

getSuperClass

protected String getSuperClass()

getNumberOfInterfaces

public int getNumberOfInterfaces()
Get the number of interfaces implemented by this class.

See Also:
getNumberOfInterfaces()

getInterface

public String getInterface(int idx)
Get the name of the interface implemented by this class at the specified index. You can use Reflect.getForName() on the result to create a Reflect object to represent it.

Parameters:
idx - The index of the interface required.
Returns:
The name of the interface implemented by this class at the specified index.

superClass

public Reflect superClass()
Get a Reflect Object representing the Superclass of this Reflect.


isBaseClass

public boolean isBaseClass(String baseName)

findBaseClass

public Reflect findBaseClass(String baseName)

isInstance

public boolean isInstance(Object obj)
Check if the specified object parameter is an instance of this reflected class.


isAssignableFrom

public boolean isAssignableFrom(Reflect other)
Determines if the class or interface represented by this Reflect object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Reflect parameter.


isTypeOf

public boolean isTypeOf(Reflect other)
Determines if the class or interface represented by this Reflect object is either the same as, or is a subclass of, or implements, the class or interface represented by the specified Reflect parameter.


isTypeOf

public boolean isTypeOf(String aType)
Determines if the class or interface represented by this Reflect object is either the same as, or is a subclass of, or implements, the class or interface represented by the specified String parameter. This is done without creating any extra Reflect objects.


isTypeOf

public static boolean isTypeOf(String which,
                               String aType)
Even though this could be done by creating Reference objects, I have made it native for efficiency. It is done by the VM without the creation of any objects. This checks to see if "which" is an instance of, or inherits from or implements "aType".


getParameters

public static String[] getParameters(String parameterList)
This converts a parameter list (e.g. "IILjava/lang/String;") to an array of strings, each representing a parameter.


getMethodParameterList

public static String getMethodParameterList(String list)
This returns the string within brackets in a method description.


isArray

public static boolean isArray(Object obj)
Returns if the specified object is an array type.


arrayLength

public static int arrayLength(Object obj)
Gets the length of the array - if obj represents an array type. It returns -1 if it does not represent an array.


newArrayInstance

public static Object newArrayInstance(String wrapperType,
                                      int length)
Creates a new array of the specified component type (which should be "I" or "Z" or "Ljava/lang/String;" (etc.)


isArray

public boolean isArray()
Returns if this Reflect object represents an array.


getComponentType

public String getComponentType()
Returns the type of the elements of the array if this Reflect represents an array. If it does not represent an array, null will be returned.


typeToString

public static String typeToString(String fullType)
This converts a encoded Java type to a printable type. e.g. it will convert "I" to "int", "Z" to "boolean" and "Ljava/lang/String;" to java.lang.String


getFields

public Field[] getFields(int options)
Return an array of all the Fields of the class.

Parameters:
options - Should be PUBLIC or DECLARED.
Returns:
an array of all the Fields of the class.

getConstructors

public Constructor[] getConstructors(int options)
Return an array of all the Constructors of the class.

Parameters:
options - Should be PUBLIC or DECLARED.
Returns:
an array of all the Constructor of the class.

getMethods

public Method[] getMethods(int options)
Return an array of all the Methods of the class.

Parameters:
options - Should be PUBLIC or DECLARED.
Returns:
an array of all the Methods of the class.

toReflect

public static Reflect toReflect(Object objectOrClassOrReflect)
This converts an Object or a Class or a Reflect into a Reflect object.

Parameters:
objectOrClassOrReflect - This can be a Class or a Reflect or any other object whose Reflect will be created.
Returns:
A Reflect object.

toNonReflect

public static Object toNonReflect(Object objectOrClassOrReflect)
If objectOrClassOrReflect is a Class or Reflect object, this will return null, otherwise it will return the objectOrClassOrReflect.

Parameters:
objectOrClassOrReflect - Any object.
Returns:
If objectOrClassOrReflect is a Class or Reflect object, this will return null, otherwise it will return the objectOrClassOrReflect.

toClass

public static Class toClass(Object objectOrClassOrReflect)
This converts an Object or a Class or a Reflect into a Class object.

Parameters:
objectOrClassOrReflect - This can be a Class or a Reflect or any other object whose Class will be created.
Returns:
A Class object.

getClass

public static Class getClass(String type,
                             Class requestor)
Deprecated. - use typeToClass() instead.

Creates a new class from a java encoded type string. This is more useful than Class.forName() since it can also find classes loaded from .ewe files and also get a Class representing a primitive type.

Parameters:
type - the Java encoded type.
requestor - an optional requesting class.
Returns:
the Class representing the type or null if the type was not found.

primitiveTypeToClass

public static Class primitiveTypeToClass(char primitiveType)
Convert a Java encoded primitive type to the Class representing that type.

Parameters:
primitiveType - the single character primitive type (e.g. 'Z' = boolean, 'J' = long, 'V' = void).
Returns:
the Class representing the primitive type.

typeToClass

public static Class typeToClass(String type)
Creates a new class from a java encoded type string, searching all the registered class loaders if necessary. This is more useful than Class.forName() since it can also find classes loaded from .ewe files and also get a Class representing a primitive type.

Parameters:
type - the Java encoded type.
Returns:
the Class representing the type or null if the type was not found.

getWrapperType

public static char getWrapperType(Class c)

getType

public static String getType(Class c)
Returns the String encoded type of the class. The returned String is a Java encoded type.

Parameters:
c - The class.
Returns:
the Java encoded type.

bestReference

public static Object bestReference(Object data,
                                   Object classOrReflect)
This returns the best object to get reference info on a particular class. if data is null it will return classOrReflect, otherwise it will return data.

Parameters:
data - An instance of a class.
classOrReflect - A Class or Reflect object for the class.
Returns:
the data if it is not null, or the classOrReflect object.