Jadex 0.96-beta1

jadex.util
Class SReflect

java.lang.Object
  extended by jadex.util.SReflect

public class SReflect
extends Object

This class provides several useful static reflection methods.


Constructor Summary
SReflect()
           
 
Method Summary
static Class classForName(String name)
          Extension for Class.forName(), because primitive types are not supported.
static Class classForName0(String name)
          Extension for Class.forName(), because primitive types are not supported.
static Class classForName0(String name, boolean initialize)
          Extension for Class.forName(), because primitive types are not supported.
static void clearClassCache()
          Clear the classcache, e.g. when the classpath has changed.
static Object convertWrappedValue(Object value, Class clazz)
          Convert a value to the correct wrapped type.
static Object createInnerClassInstance(Object object, String name, Class type)
          Create a new instance of an inner class.
static Class findClass(String clname, String[] imports)
          Find a class.
static Class findClass0(String clname, String[] imports)
          Find a class.
static Class[] getAnonymousInnerClasses(Class clazz)
          Workaround for java leaveout.
static Field getCachedField(Class clazz, String name)
          Get a cached field.
static String getClassName(Class clazz)
          Beautifies names of arrays (eg 'String[]' instead of '[LString;').
static Object getDeclared(Object o, String fieldname)
          Get the declared object, doesnt cumulate through superclasses.
static Field getField(Class clazz, String name)
          Get a field of the class, or any of it's superclasses.
static String getInnerClassName(Class clazz)
          Get inner class name.
static Iterator getIterator(Object collection)
          Get an iterator for an arbitrary collection object.
static Method getMethod(Class clazz, String name, Class[] types)
          Get a method of the class.
static Method[] getMethods(Class clazz, String name)
          Get public method(s) of the class by name.
static String getPackageName(Class clazz)
          Get the package of a class.
static String getUnqualifiedClassName(Class clazz)
          Get unqualified class name.
static Class getWrappedType(Class clazz)
          Get the wrapped type.
static boolean instanceOf(Object o, Class c)
          Is an object instanceof a class or its superclasses.
static boolean instanceOf(Object o, String c)
          Is an object instanceof a class or its superclasses.
static boolean isBasicType(Class clazz)
          Is basic type.
static boolean isSupertype(Class clazz1, Class clazz2)
          Check if a class is a supertype of, or the same as another class.
static int[] matchArgumentTypes(Class[] argtypes, Class[][] paramtypes)
          Match the given argument types to a set of parameter type arrays.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SReflect

public SReflect()
Method Detail

getWrappedType

public static Class getWrappedType(Class clazz)
Get the wrapped type. This method converts basic types such as boolean or int to the object types Boolean, Integer.

Parameters:
clazz - The basic class.
Returns:
The wrapped type, return clazz when it is no basic type.

isBasicType

public static boolean isBasicType(Class clazz)
Is basic type.

Returns:
True, if the class is a basic type.

classForName0

public static Class classForName0(String name)
Extension for Class.forName(), because primitive types are not supported. Uses static cache to speed up lookup.

Parameters:
name - The class name.
Returns:
The class, or null if not found.

classForName0

public static Class classForName0(String name,
                                  boolean initialize)
Extension for Class.forName(), because primitive types are not supported. Uses static cache to speed up lookup.

Parameters:
name - The class name.
Returns:
The class, or null if not found.

classForName

public static Class classForName(String name)
                          throws ClassNotFoundException
Extension for Class.forName(), because primitive types are not supported. Uses static cache to speed up lookup.

Parameters:
name - The class name.
Returns:
The class.
Throws:
ClassNotFoundException

createInnerClassInstance

public static Object createInnerClassInstance(Object object,
                                              String name,
                                              Class type)
Create a new instance of an inner class. Requires a public no arg constructer.

Parameters:
object - The enclosing object.
name - The inner class' name.
type - The inner class' required type, or null.

getAnonymousInnerClasses

public static Class[] getAnonymousInnerClasses(Class clazz)
Workaround for java leaveout.

Returns:
The anonymous inner classes of a class.

getClassName

public static String getClassName(Class clazz)
Beautifies names of arrays (eg 'String[]' instead of '[LString;').

Returns:
The beautified name of a class.

getUnqualifiedClassName

public static String getUnqualifiedClassName(Class clazz)
Get unqualified class name. Also beautifies names of arrays (eg 'String[]' instead of '[LString;').

Returns:
The unqualified (without package) name of a class.

getInnerClassName

public static String getInnerClassName(Class clazz)
Get inner class name.

Returns:
The inner class's name (without declaring class).

getPackageName

public static String getPackageName(Class clazz)
Get the package of a class.

Returns:
The name of the package.

getField

public static Field getField(Class clazz,
                             String name)
Get a field of the class, or any of it's superclasses. Unlike Class.getField(String), this will also return nonpublic fields (except when running in netscape :-( ).

Parameters:
clazz - The class to search.
name - The name of the field to search for.
Returns:
The field (or null if not found).

getCachedField

public static Field getCachedField(Class clazz,
                                   String name)
                            throws NoSuchFieldException
Get a cached field.

Parameters:
clazz - The clazz.
name - The name.
Returns:
The field.
Throws:
NoSuchFieldException

getDeclared

public static Object getDeclared(Object o,
                                 String fieldname)
Get the declared object, doesnt cumulate through superclasses.

Parameters:
o - The object.
fieldname - The name of the array.

getMethod

public static Method getMethod(Class clazz,
                               String name,
                               Class[] types)
Get a method of the class. Unlike Class.getMethod(String, Class[]), this uses the methodcache.

Parameters:
clazz - The class to search.
name - The name of the method to search for.
types - The parameter types.
Returns:
The method (or null if not found).

getMethods

public static Method[] getMethods(Class clazz,
                                  String name)
Get public method(s) of the class by name.

Parameters:
clazz - The class to search.
name - The name of the method to search for.
Returns:
The method(s).

findClass

public static Class findClass(String clname,
                              String[] imports)
                       throws ClassNotFoundException
Find a class. When the class name is not fully qualified, the list of imported packages is searched for the class.

Parameters:
clname - The class name.
imports - The comma separated list of imported packages.
Throws:
ClassNotFoundException - when the class is not found in the imports.

findClass0

public static Class findClass0(String clname,
                               String[] imports)
Find a class. Also supports basic types and arrays. When the class name is not fully qualified, the list of imported packages is searched for the class.

Parameters:
clname - The class name.
imports - The comma separated list of imported packages.
Returns:
null, when the class is not found in the imports.

matchArgumentTypes

public static int[] matchArgumentTypes(Class[] argtypes,
                                       Class[][] paramtypes)
Match the given argument types to a set of parameter type arrays. The argument type array may contain null values, when an argument type is unknown. For convenience, the length of a parameter type array does not have to equal the argument array length. (although it will of course never match). The method returns the indices of the matching parameter type arrays. An empty array is returned, if no match is found. The returned matches are sorted by quality (best match first).

Parameters:
argtypes - The array of argument types.
paramtypes - The array of parameter type arrays.
Returns:
The indices of the matching parameter type arrays.

isSupertype

public static boolean isSupertype(Class clazz1,
                                  Class clazz2)
Check if a class is a supertype of, or the same as another class. Maps basic types to wrapped types, and respects the basic type hierarchy.

Parameters:
clazz1 - The assumed supertype.
clazz2 - The assumed subtype.
Returns:
True, if clazz1 is a supertype of, or the same as clazz2.

convertWrappedValue

public static Object convertWrappedValue(Object value,
                                         Class clazz)
Convert a value to the correct wrapped type. Assumes that the conversion is possible.

Parameters:
value - The value.
clazz - The target clazz.
Returns:
The converted value.
See Also:
isSupertype(Class, Class)

getIterator

public static Iterator getIterator(Object collection)
Get an iterator for an arbitrary collection object. Supports iterators, enumerations, java.util.Collections, java.util.Maps, arrays. Null is converted to empty iterator.

Parameters:
collection - The collection object.
Returns:
An iterator over the collection.
Throws:
IllegalArgumentException - when argument is not one of (Iterator, Enumeration, Collection, Map, Array).

instanceOf

public static boolean instanceOf(Object o,
                                 Class c)
Is an object instanceof a class or its superclasses.

Parameters:
o - The object.
c - The class.
Returns:
True, when o is instance of class c.

instanceOf

public static boolean instanceOf(Object o,
                                 String c)
Is an object instanceof a class or its superclasses.

Parameters:
o - The object.
c - The class.
Returns:
True, when o is instance of class c.

clearClassCache

public static void clearClassCache()
Clear the classcache, e.g. when the classpath has changed.


Jadex 0.96-beta1

Submit a bug or feature
For further API reference and developer documentation, see the Jadex User Guide and the Jadex Tutorial. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, and working code examples.

Copyright (C) 2002-2007 Lars Braubach, Alexander Pokahr - University of Hamburg. Use is subject to license terms.