edu.uci.eecs.doc.cascade.util
Class ReflectionUtilities

java.lang.Object
  extended by edu.uci.eecs.doc.cascade.util.ReflectionUtilities

public final class ReflectionUtilities
extends Object

A collection of helper methods for Java reflection.

Author:
Trevor Harmon

Method Summary
static void clearCache()
          FIXME: This is a quick fix for a problem that occurs when a class is analyzed by Volta, then modified and recompiled, and finally analyzed again (without exiting the VM in which Volta is running).
static List<InstructionHandle> getBytecode(Method method)
          Returns a list of the bytecode instructions for the given method.
static Class getClassForName(String className, List<String> classPath)
          Returns a Class object corresponding to the given class name.
static ClassInfo getClassInfo(Method method, List<String> classPath)
          Creates a JODE ClassInfo object corresponding to the given method.
static ClassLoader getClassLoaderForClassPath(List<String> classPath)
          Returns a ClassLoader that will only load classes only from a given class path.
static Method getMethod(InvokeInstruction invokeInstruction, Method caller, List<String> classPath)
          Returns a handle to the method invoked by the given invoke instruction.
static Method getMethod(MethodInfo methodInfo, ClassInfo classInfo)
          Converts a JODE MethodInfo object to a standard Java method object.
static Method getMethod(String name, List<String> classPath)
          Returns a Method object corresponding to the given method name.
static MethodInfo getMethodInfo(Method method, ClassInfo classInfo)
          Converts a standard Java method object to a JODE MethodInfo object.
static int getMethodSize(Method method)
          Returns the size, in bytes, of the given method.
static String getQualifiedName(Method method)
          Returns a fully qualified human-readble name of the given method.
static String getSignature(Class theClass)
          Given a handle to a Java class, this method returns the class's signature.
static int getSourceCodeLine(Method method, String sourceCode)
          Returns the line in the source code on which the given method was declared.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

clearCache

public static void clearCache()
FIXME: This is a quick fix for a problem that occurs when a class is analyzed by Volta, then modified and recompiled, and finally analyzed again (without exiting the VM in which Volta is running). When this happens, the old class information will still be in the cache, and any subsequent analysis of that class will reflect the old class rather than the newly recompiled one. One way to fix this problem is to simply disable all caching, but the caches are necessary for the correct operation of Volta (see the comments attached to each cache field). It should also be possible to unload the class, perhaps using Java 7's URLClassLoader.close, but I tried this and it seemed to have no effect. Clearing the cache on demand, which is what this method allows, is a temporary workaround until proper class reloading can be implemented.


getSignature

public static String getSignature(Class theClass)
Given a handle to a Java class, this method returns the class's signature. Examples:
 Ljava/lang/String;
 Z
 [Ljava/lang/Object;
 [[I
 
This differs from the standard Class.getName() method, which uses a format that differs slightly from the usual Java signature syntax. Examples:
 java.lang.String
 byte
 [Ljava.lang.Object;
 [[I
 

Parameters:
theClass - a handle to a Java class
Returns:
the signtature of the class
See Also:
Class.getName()

getQualifiedName

public static String getQualifiedName(Method method)
Returns a fully qualified human-readble name of the given method. Example:
java.lang.String.indexOf(java.lang.String,int)

Parameters:
method - a handle to a Java method
Returns:
a string representing the method's name

getMethod

public static Method getMethod(String name,
                               List<String> classPath)
                        throws NoSuchMethodException,
                               ClassNotFoundException
Returns a Method object corresponding to the given method name.

Parameters:
name - the fully qualified name of the method (e.g., java.lang.String.indexOf(java.lang.String,int))
classPath - a list of paths to use when searching for the method's class
Returns:
a method object corresponding to the method name
Throws:
NoSuchMethodException - if the method cannot be found
ClassNotFoundException - if the method's class cannot be found

getMethod

public static Method getMethod(InvokeInstruction invokeInstruction,
                               Method caller,
                               List<String> classPath)
                        throws NoSuchMethodException,
                               ClassNotFoundException
Returns a handle to the method invoked by the given invoke instruction.

Parameters:
invokeInstruction - a BCEL instruction that invokes a method
caller - the method in which the invoke instruction is stored
classPath - a list of paths to use when searching for the method
Returns:
a method object corresponding to the invoked method's name
Throws:
NoSuchMethodException - if the method cannot be found
ClassNotFoundException - if the method's class cannot be found

getClassForName

public static Class getClassForName(String className,
                                    List<String> classPath)
                             throws ClassNotFoundException
Returns a Class object corresponding to the given class name. This method works just like Class.forName(String), but the search is restricted to a custom class path instead of the system's default class path.

Parameters:
className - the name of the class to load (e.g., java.lang.String)
classPath - a list of paths to use when searching for the class
Returns:
a Class object corresponding to the class name
Throws:
ClassNotFoundException - if the class cannot be found
See Also:
Class.forName(String)

getClassLoaderForClassPath

public static ClassLoader getClassLoaderForClassPath(List<String> classPath)
                                              throws MalformedURLException
Returns a ClassLoader that will only load classes only from a given class path.

Parameters:
classPath - a list of paths to which the ClassLoader should limit its search
Returns:
a custom ClassLoader
Throws:
MalformedURLException - if at least one of the paths in the given path list is invalid

getClassInfo

public static ClassInfo getClassInfo(Method method,
                                     List<String> classPath)
Creates a JODE ClassInfo object corresponding to the given method.

Parameters:
method - a handle to a Java method
classPath - a list of paths to use when searching for the method's class
Returns:
a JODE ClassInfo object

getMethodInfo

public static MethodInfo getMethodInfo(Method method,
                                       ClassInfo classInfo)
                                throws NoSuchMethodException
Converts a standard Java method object to a JODE MethodInfo object.

Parameters:
method - a handle to a Java method
classInfo - a JODE ClassInfo object corresponding to the given method
Returns:
a JODE MethodInfo object corresponding to the given method
Throws:
NoSuchMethodException

getMethod

public static Method getMethod(MethodInfo methodInfo,
                               ClassInfo classInfo)
                        throws NoSuchMethodException,
                               ClassNotFoundException
Converts a JODE MethodInfo object to a standard Java method object.

Parameters:
methodInfo - a JODE MethodInfo object
Returns:
a handle to a Java method
Throws:
NoSuchMethodException - if a method reference for the given MethodInfo object could not be obtained
ClassNotFoundException - if the method's class could not be found

getMethodSize

public static int getMethodSize(Method method)
                         throws ClassNotFoundException
Returns the size, in bytes, of the given method. The size is the number of bytes in the method's executable bytecode only. The size of other metadata in the method's Code attribute (e.g., the exception table) is not included in the total.

Returns:
the size of the method's bytecode
Throws:
ClassNotFoundException - if the method's class could not be found

getBytecode

public static List<InstructionHandle> getBytecode(Method method)
                                           throws ClassNotFoundException
Returns a list of the bytecode instructions for the given method.

Parameters:
method - a handle to the method
Returns:
a list of BCEL InstructionHandle objects
Throws:
ClassNotFoundException - if the method's class could not be found

getSourceCodeLine

public static int getSourceCodeLine(Method method,
                                    String sourceCode)
                             throws ClassNotFoundException
Returns the line in the source code on which the given method was declared. (Although Java's LineNumberTable attribute provides information about source code line numbers, it only pertains to code within a method, not the method declaration.) The given method must have been compiled with debugging symbols turned on. Note that this method will fail in some rare cases, such as when a statement annotation is placed on the first statement of the method and has the same name as the method.

Parameters:
method - a handle to the method
sourceCode - the source code in which the method was declared; must be legal code (free of any compiler errors)
Returns:
the line number on which the method was declared
Throws:
ClassNotFoundException - if the class corresponding to the given method cannot be found