Package org.plumelib.reflection
Class ReflectionP
java.lang.Object
org.plumelib.reflection.ReflectionP
Utility functions related to reflection, Class, Method, ClassLoader, and classpath.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddToClasspath(String dir) Add the directory to the system classpath.static Class<?> classForName(@ClassGetName String className) LikeClass.forName(String): given a string representing a non-array class, returns the Class.static StringReturns the classpath as a multi-line string.static Class<?> defineClassFromFile(@BinaryName String className, String pathname) Converts the bytes in a file into an instance of class Class, and resolves (links) the class.static @ClassGetSimpleName StringfullyQualifiedNameToSimpleName(@FullyQualifiedName String qualifiedName) Returns the simple unqualified class name that corresponds to the specified fully qualified name.static @Nullable ObjectgetPrivateField(Object o, String fieldName) Reads the given field, which may be private.static booleanReturns true iff sub is a subtype of sup.static <T> @Nullable Class<T> leastUpperBound(@PolyMustCall @PolyNull Object[] objects) Returns the least upper bound of the classes of the given objects.static <T> @Nullable Class<T> leastUpperBound(@Nullable Class<T>[] classes) Returns the least upper bound of all the given classes.static <T> @Nullable Class<T> leastUpperBound(@Nullable Class<T> a, @Nullable Class<T> b) Returns the least upper bound of the given classes.static <T> @Nullable Class<T> leastUpperBound(List<? extends @Nullable Object> objects) Returns the least upper bound of the classes of the given objects.static MethodmethodForName(@BinaryName String classname, String methodname, Class<?>[] params) Given a class name and a method name in that class, return the method.static MethodmethodForName(String method) Given a method signature, return the method.static StringnameWithoutPackage(Class<?> c) Returns the class name, including outer classes but without the package.static voidsetFinalField(Object o, String fieldName, @Interned Object value) Sets the given field, which may be final and/or private.
-
Method Details
-
isSubtype
Returns true iff sub is a subtype of sup. If sub == sup, then sub is considered a subtype of sup and this method returns true.- Parameters:
sub- class to test for being a subtypesup- class to test for being a supertype- Returns:
- true iff sub is a subtype of sup
-
classForName
LikeClass.forName(String): given a string representing a non-array class, returns the Class. UnlikeClass.forName(String), the argument may be a primitive type or a fully-qualified name (in addition to a binary name).If the given name can't be found, this method changes the last '.' to a dollar sign ($) and tries again, repeating with successive dots (from right to left) until the class is found or no dots remain. This accounts for inner classes that are incorrectly passed in fully-qualified format instead of binary format.
Recall the rather odd specification for
Class.forName(String): the argument is a binary name for non-arrays, but a field descriptor for arrays. This method uses the same rules, but additionally handles primitive types and, for non-arrays, fully-qualified names.- Parameters:
className- name of the class- Returns:
- the Class corresponding to className
- Throws:
ClassNotFoundException- if the class is not found
-
fullyQualifiedNameToSimpleName
public static @ClassGetSimpleName String fullyQualifiedNameToSimpleName(@FullyQualifiedName String qualifiedName) Returns the simple unqualified class name that corresponds to the specified fully qualified name. For example, if qualifiedName is java.lang.String, String will be returned.- Parameters:
qualifiedName- the fully-qualified name of a class- Returns:
- the simple unqualified name of the class
-
nameWithoutPackage
Returns the class name, including outer classes but without the package. Uses "." as the separator between outer and inner classes, as in Java source code.- Parameters:
c- a class- Returns:
- the class name, including outer classes but without the package
-
defineClassFromFile
public static Class<?> defineClassFromFile(@BinaryName String className, String pathname) throws FileNotFoundException, IOException Converts the bytes in a file into an instance of class Class, and resolves (links) the class. LikeClassLoader.defineClass(String,byte[],int,int), but takes a file name rather than an array of bytes as an argument, and also resolves (links) the class.- Parameters:
className- the name of the class to define, or null if not knownpathname- the pathname of a .class file- Returns:
- a Java Object corresponding to the Class defined in the .class file
- Throws:
FileNotFoundException- if the file cannot be foundIOException- if there is trouble reading the file- See Also:
-
addToClasspath
Add the directory to the system classpath.- Parameters:
dir- directory to add to the system classpath
-
classpathToString
Returns the classpath as a multi-line string.- Returns:
- the classpath as a multi-line string
-
methodForName
public static Method methodForName(String method) throws ClassNotFoundException, NoSuchMethodException Given a method signature, return the method.Example calls are:
UtilP.methodForName( "org.plumelib.reflection.ReflectionP.methodForName" +"(java.lang.String, java.lang.String, java.lang.Class[])") UtilP.methodForName("org.plumelib.reflection.ReflectionP.methodForName" +"(java.lang.String,java.lang.String,java.lang.Class[])") UtilP.methodForName("java.lang.Math.min(int,int)")- Parameters:
method- a method signature- Returns:
- the method corresponding to the given signature
- Throws:
ClassNotFoundException- if the class is not foundNoSuchMethodException- if the method is not found
-
methodForName
public static Method methodForName(@BinaryName String classname, String methodname, Class<?>[] params) throws ClassNotFoundException, NoSuchMethodException Given a class name and a method name in that class, return the method.- Parameters:
classname- class in which to find the methodmethodname- the method nameparams- the parameters of the method- Returns:
- the method named classname.methodname with parameters params
- Throws:
ClassNotFoundException- if the class is not foundNoSuchMethodException- if the method is not found
-
setFinalField
public static void setFinalField(Object o, String fieldName, @Interned Object value) throws NoSuchFieldException Sets the given field, which may be final and/or private. Restores the field's original accessibility.- Parameters:
o- object in which to set the field; null iff the field is staticfieldName- name of field to setvalue- new value of field; may be null iff the field is nullable- Throws:
NoSuchFieldException- if the field does not exist in the object
-
getPrivateField
public static @Nullable Object getPrivateField(Object o, String fieldName) throws NoSuchFieldException Reads the given field, which may be private. Restores the field's original accessibility.- Parameters:
o- object in which to get the fieldfieldName- name of field to get- Returns:
- value of field
- Throws:
NoSuchFieldException- if the field does not exist in the object
-
leastUpperBound
Returns the least upper bound of the given classes.- Type Parameters:
T- the (inferred) least upper bound of the two arguments- Parameters:
a- a classb- a class- Returns:
- the least upper bound of the two classes, or null if both are null
-
leastUpperBound
Returns the least upper bound of all the given classes.- Type Parameters:
T- the (inferred) least upper bound of the arguments- Parameters:
classes- an array of classes- Returns:
- the least upper bound of all the given classes, or null if the array is empty or all its elements are null
-
leastUpperBound
Returns the least upper bound of the classes of the given objects.- Type Parameters:
T- the (inferred) least upper bound of the arguments- Parameters:
objects- an array of objects- Returns:
- the least upper bound of the classes of the given objects, or null if the array is empty or all its elements are null
-
leastUpperBound
Returns the least upper bound of the classes of the given objects.- Type Parameters:
T- the (inferred) least upper bound of the arguments- Parameters:
objects- a list of objects- Returns:
- the least upper bound of the classes of the given objects, or null if the list is empty or all its elements are null
-