public final class ReflectionUtilities extends Object
Modifier and Type | Field and Description |
---|---|
static String |
REGEXP
This is used to split Java classnames.
|
Modifier and Type | Method and Description |
---|---|
static Class<?> |
boxClass(Class<?> c)
Given a primitive type, return its boxed representation.
|
static Iterable<Type> |
classAndAncestors(Type c)
Given a Type, return all of the classes it extends and interfaces it implements (including the class itself).
|
static Class<?> |
classForName(String name,
ClassLoader loader)
Lookup the provided name using the provided classloader.
|
static String |
getFullName(Field f)
Return the full name of the provided field.
|
static String |
getFullName(Type name)
Return the full name of the raw type of the provided Type.
|
static Type |
getInterfaceTarget(Class<?> iface,
Type type)
This method takes a class called clazz that *directly* implements a generic interface or generic class, iface.
|
static Type |
getNamedParameterTargetOrNull(Class<?> clazz) |
static Class<?> |
getRawClass(Type clazz)
Coerce a Type into a Class.
|
static String |
getSimpleName(Type name)
Get the simple name of the class.
|
static boolean |
isCoercable(Class<?> to,
Class<?> from)
Check to see if one class can be coerced into another.
|
public static final String REGEXP
public static Class<?> boxClass(Class<?> c)
Examples:
boxClass(int.class) -> Integer.class
boxClass(String.class) -> String.class
c
- The class to be boxed.public static Iterable<Type> classAndAncestors(Type c)
Examples:
Integer.class -> {Integer.class, Number.class, Object.class}
T -> Object
? -> Object
HashSet<T> -> {HashSet<T>, Set<T>, Collection<T>, Object}
FooEventHandler -> {FooEventHandler, EventHandler<Foo>, Object}
public static boolean isCoercable(Class<?> to, Class<?> from)
If both classes are numeric types, then this method returns true iff the conversion will not result in a loss of precision.
TODO: Float and double are currently coercible to int and long. This is a bug.
public static Class<?> classForName(String name, ClassLoader loader) throws ClassNotFoundException
ClassNotFoundException
public static String getSimpleName(Type name)
name
- public static String getFullName(Type name)
Examples:
java.lang.String.class -> "java.lang.String"
Set<String> -> "java.util.Set" // such types can occur as constructor arguments, for example
name
- public static String getFullName(Field f)
Example:
Set<X> { int size; } -> java.util.Set.size
public static Type getInterfaceTarget(Class<?> iface, Type type) throws IllegalArgumentException
TODO This is only tested for interfaces, and the type parameters associated with method arguments.
TODO Not sure what we should do in the face of deeply nested generics (eg: Set<Set<String>
)
TODO Recurse up the class hierarchy in case there are intermediate interfaces
iface
- A generic interface; we're looking up it's first (and only) parameter.type
- A type that is more specific than clazz, or clazz if no such type is available.IllegalArgumentException
- if clazz does not directly implement iface.public static Type getNamedParameterTargetOrNull(Class<?> clazz) throws ClassHierarchyException
clazz
- Name<T>
, null otherwiseBindException
- If clazz's definition incorrectly uses Name or @NamedParameterClassHierarchyException
public static Class<?> getRawClass(Type clazz)
Examples:
java.util.Set<String> -> java.util.Set
? extends T -> Object
T -> Object
? -> Object
Copyright © 2016 The Apache Software Foundation. All rights reserved.