public final class Reflect extends Object
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.
Modifier and Type | Field and Description |
---|---|
static int |
CONSTRUCTOR_AS_METHOD
This option is used for getting special methods, like finalize()
|
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.
|
static int |
SYSTEM
This option is used for getting special methods, like finalize()
|
Constructor and Description |
---|
Reflect(Class aClass)
Create a Reflect object given a non-null and non-primitive Class.
|
Modifier and Type | Method and Description |
---|---|
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. e.g. it will convert
"I" to "int", "Z" to "boolean" and "Ljava/lang/String;" to java.lang.String
|
protected Constructor defaultConstructor
protected Reflect mySuperClass
public static final int DECLARED
public static final int PUBLIC
public static final int SYSTEM
public static final int CONSTRUCTOR_AS_METHOD
public Reflect(Class aClass)
aClass
- The class for the object which must be non-null and non-primitive.public static Class loadClass(String name)
name
- The name of the class.public static Reflect loadForName(String name)
name
- The name of the class.public static Object newInstance(String name)
name
- The name of the class.public static Reflect getForObject(Object obj)
obj
- The object - should not be null.public static Reflect getForName(String name)
This can only find classes that are not loaded from a class loader.
name
- The name of the class.public static Reflect getForName(String name, Class requestor)
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.
name
- The name of the class.requestor
- A requesting class.public String getClassName()
public Class getReflectedClass()
public String toString()
Object
public Field getField(String name, int options)
name
- The name of the field.options
- Either DECLARED or PUBLIC.public Method getMethod(String nameAndSpecs, int options)
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 or SYSTEMpublic Method getMethod(String name, String specs, int options)
name
- The name of the method.specs
- The specs (parameters) for the method - must be in Java type specification.options
- Either DECLARED or PUBLIC or SYSTEM or CONSTRUCTOR_AS_METHOD.public Constructor getConstructor(String specs, int options)
specs
- The specs (parameters) for the method - must be in Java type specification.options
- Either DECLARED or PUBLIC.public Object newArray(int num)
num
- The length of the array, must be >= 0.public Object newInstance()
public Object newInstance(String constructorSpecs, Wrapper[] parameters)
constructorParameters
- The parameters for the constructor without any brackets.parameters
- protected String getSuperClass()
public int getNumberOfInterfaces()
getNumberOfInterfaces()
public String getInterface(int idx)
idx
- The index of the interface required.public Reflect superClass()
public boolean isBaseClass(String baseName)
public boolean isInstance(Object obj)
public boolean isAssignableFrom(Reflect other)
public boolean isTypeOf(Reflect other)
public boolean isTypeOf(String aType)
public static boolean isTypeOf(String which, String aType)
public static String[] getParameters(String parameterList)
public static String getMethodParameterList(String list)
public static boolean isArray(Object obj)
public static int arrayLength(Object obj)
public static Object newArrayInstance(String wrapperType, int length)
public boolean isArray()
public String getComponentType()
public static String typeToString(String fullType)
public Field[] getFields(int options)
options
- Should be PUBLIC or DECLARED.public Constructor[] getConstructors(int options)
options
- Should be PUBLIC or DECLARED.public Method[] getMethods(int options)
options
- Should be PUBLIC or DECLARED.public static Reflect toReflect(Object objectOrClassOrReflect)
objectOrClassOrReflect
- This can be a Class or a Reflect or any other object whose Reflect will be created.public static Object toNonReflect(Object objectOrClassOrReflect)
objectOrClassOrReflect
- Any object.public static Class toClass(Object objectOrClassOrReflect)
objectOrClassOrReflect
- This can be a Class or a Reflect or any other object whose Class will be created.public static Class getClass(String type, Class requestor)
type
- the Java encoded type.requestor
- an optional requesting class.public static Class primitiveTypeToClass(char primitiveType)
primitiveType
- the single character primitive type (e.g. 'Z' = boolean, 'J' = long, 'V' = void).public static Class typeToClass(String type)
type
- the Java encoded type.public static char getWrapperType(Class c)
public static String getType(Class c)
c
- The class.public static Object bestReference(Object data, Object classOrReflect)
data
- An instance of a class.classOrReflect
- A Class or Reflect object for the class.