public final class ReflectionPlume
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private static class |
ReflectionPlume.PromiscuousLoader
This static nested class has no purpose but to define defineClassFromFile.
|
Modifier and Type | Field and Description |
---|---|
(package private) static java.util.HashMap<java.lang.String,java.lang.Class<?>[]> |
args_seen
Maps from a comma-delimited string of arg types, such as appears in a method signature, to an
array of Class objects, one for each arg type.
|
private static java.util.HashMap<java.lang.String,java.lang.Class<?>> |
primitiveClasses
Used by
classForName(java.lang.String) . |
private static ReflectionPlume.PromiscuousLoader |
thePromiscuousLoader
A ClassLoader that can call defineClassFromFile.
|
Modifier | Constructor and Description |
---|---|
private |
ReflectionPlume()
This class is a collection of methods; it does not represent anything.
|
Modifier and Type | Method and Description |
---|---|
static void |
addToClasspath(java.lang.String dir)
Add the directory to the system classpath.
|
static java.lang.Class<?> |
classForName(@ClassGetName java.lang.String className)
Like
Class.forName(String) : given a string representing a non-array class, returns the
Class. |
static java.lang.String |
classpathToString()
Returns the classpath as a multi-line string.
|
private static java.lang.String |
classpathToStringJava8Only()
Returns the classpath as a multi-line string.
|
static java.lang.Class<?> |
defineClassFromFile(@BinaryName java.lang.String className,
java.lang.String pathname)
Converts the bytes in a file into an instance of class Class, and resolves (links) the class.
|
static @ClassGetSimpleName java.lang.String |
fullyQualifiedNameToSimpleName(@FullyQualifiedName java.lang.String qualifiedName)
Returns the simple unqualified class name that corresponds to the specified fully qualified
name.
|
static @Nullable java.lang.Object |
getPrivateField(java.lang.Object o,
java.lang.String fieldName)
Reads the given field, which may be private.
|
static boolean |
isSubtype(java.lang.Class<?> sub,
java.lang.Class<?> sup)
Return true iff sub is a subtype of sup.
|
static <T> @Nullable java.lang.Class<T> |
leastUpperBound(@Nullable java.lang.Class<T>[] classes)
Returns the least upper bound of all the given classes.
|
static <T> @Nullable java.lang.Class<T> |
leastUpperBound(@Nullable java.lang.Class<T> a,
@Nullable java.lang.Class<T> b)
Returns the least upper bound of the given classes.
|
static <T> @Nullable java.lang.Class<T> |
leastUpperBound(java.util.List<? extends java.lang.Object> objects)
Returns the least upper bound of the classes of the given objects.
|
static <T> @Nullable java.lang.Class<T> |
leastUpperBound(@PolyNull java.lang.Object[] objects)
Returns the least upper bound of the classes of the given objects.
|
static java.lang.reflect.Method |
methodForName(java.lang.String method)
Given a method signature, return the method.
|
static java.lang.reflect.Method |
methodForName(@BinaryName java.lang.String classname,
java.lang.String methodname,
java.lang.Class<?>[] params)
Given a class name and a method name in that class, return the method.
|
static java.lang.String |
nameWithoutPackage(java.lang.Class<?> c)
Returns the class name, including outer classes but without the package.
|
static void |
setFinalField(java.lang.Object o,
java.lang.String fieldName,
@Interned java.lang.Object value)
Sets the given field, which may be final and/or private.
|
private static java.util.HashMap<java.lang.String,java.lang.Class<?>> primitiveClasses
classForName(java.lang.String)
.private static ReflectionPlume.PromiscuousLoader thePromiscuousLoader
static java.util.HashMap<java.lang.String,java.lang.Class<?>[]> args_seen
private ReflectionPlume()
@Pure public static boolean isSubtype(java.lang.Class<?> sub, java.lang.Class<?> sup)
sub
- class to test for being a subtypesup
- class to test for being a supertypepublic static java.lang.Class<?> classForName(@ClassGetName java.lang.String className) throws java.lang.ClassNotFoundException
Class.forName(String)
: given a string representing a non-array class, returns the
Class. Unlike Class.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. This accounts for inner classes that are incorrectly passed in in fully-qualified format instead of binary format. (It should try multiple dollar signs, not just at the last position.)
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.
className
- name of the classjava.lang.ClassNotFoundException
- if the class is not foundpublic static @ClassGetSimpleName java.lang.String fullyQualifiedNameToSimpleName(@FullyQualifiedName java.lang.String qualifiedName)
qualifiedName
- the fully-qualified name of a classpublic static java.lang.String nameWithoutPackage(java.lang.Class<?> c)
c
- a classpublic static java.lang.Class<?> defineClassFromFile(@BinaryName java.lang.String className, java.lang.String pathname) throws java.io.FileNotFoundException, java.io.IOException
ClassLoader.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.className
- the name of the class to define, or null if not knownpathname
- the pathname of a .class filejava.io.FileNotFoundException
- if the file cannot be foundjava.io.IOException
- if there is trouble reading the fileClassLoader.defineClass(String,byte[],int,int)
public static void addToClasspath(java.lang.String dir)
dir
- directory to add to the system classpathprivate static java.lang.String classpathToStringJava8Only()
public static java.lang.String classpathToString()
public static java.lang.reflect.Method methodForName(java.lang.String method) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.SecurityException
Example calls are:
UtilPlume.methodForName("org.plumelib.reflection.ReflectionPlume.methodForName(java.lang.String, java.lang.String, java.lang.Class[])") UtilPlume.methodForName("org.plumelib.reflection.ReflectionPlume.methodForName(java.lang.String,java.lang.String,java.lang.Class[])") UtilPlume.methodForName("java.lang.Math.min(int,int)")
method
- a method signaturejava.lang.ClassNotFoundException
- if the class is not foundjava.lang.NoSuchMethodException
- if the method is not foundjava.lang.SecurityException
public static java.lang.reflect.Method methodForName(@BinaryName java.lang.String classname, java.lang.String methodname, java.lang.Class<?>[] params) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.SecurityException
classname
- class in which to find the methodmethodname
- the method nameparams
- the parameters of the methodjava.lang.ClassNotFoundException
- if the class is not foundjava.lang.NoSuchMethodException
- if the method is not foundjava.lang.SecurityException
public static void setFinalField(java.lang.Object o, java.lang.String fieldName, @Interned java.lang.Object value) throws java.lang.NoSuchFieldException
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 nullablejava.lang.NoSuchFieldException
- if the field does not exist in the objectpublic static @Nullable java.lang.Object getPrivateField(java.lang.Object o, java.lang.String fieldName) throws java.lang.NoSuchFieldException
o
- object in which to set the fieldfieldName
- name of field to setjava.lang.NoSuchFieldException
- if the field does not exist in the objectpublic static <T> @Nullable java.lang.Class<T> leastUpperBound(@Nullable java.lang.Class<T> a, @Nullable java.lang.Class<T> b)
T
- the (inferred) least upper bound of the two argumentsa
- a classb
- a classpublic static <T> @Nullable java.lang.Class<T> leastUpperBound(@Nullable java.lang.Class<T>[] classes)
T
- the (inferred) least upper bound of the argumentsclasses
- a non-empty list of classespublic static <T> @Nullable java.lang.Class<T> leastUpperBound(@PolyNull java.lang.Object[] objects)
T
- the (inferred) least upper bound of the argumentsobjects
- a list of objectspublic static <T> @Nullable java.lang.Class<T> leastUpperBound(java.util.List<? extends java.lang.Object> objects)
T
- the (inferred) least upper bound of the argumentsobjects
- a non-empty list of objects