Brooklyn

brooklyn.util.javalang
[Java] Class Reflections

java.lang.Object
  brooklyn.util.javalang.Reflections

public class Reflections

Reflection utilities ("borrowed" from cloudsoft monterey).

Authors:
aled


Nested Class Summary
static class Reflections.ReflectionAccessException

static class Reflections.ReflectionNotFoundException

 
Method Summary
java.lang.Object Reflections(java.lang.ClassLoader classLoader)

static java.lang.Object cast(java.lang.Object candidate, java.lang.Class type)

static void copyFields(java.lang.Object source, java.lang.Object target)

copies all fields from the source to target; very little compile-time safety checking, so use with care

static java.lang.reflect.Constructor findCallabaleConstructor(java.lang.Class clazz, java.lang.Object[] args)

Returns a constructor that accepts the given arguments, or null if no such constructor is accessible.

static java.lang.reflect.Field findField(java.lang.Class clazz, java.lang.String name)

static java.lang.reflect.Method findMethod(java.lang.Class clazz, java.lang.String name, java.lang.Class... parameterTypes)

static java.util.List findPublicFieldsOrderedBySuper(java.lang.Class clazz)

static java.util.List findPublicMethodsOrderedBySuper(java.lang.Class clazz)

static java.lang.Class findSuperType(java.lang.Object impl, java.lang.String typeName)

static java.lang.StackTraceElement getCaller()

deprecated:
since 0.6.0; this is sloppy, and rarely does the right thing.

java.lang.ClassLoader getClassLoader()

static java.util.Set getInterfacesIncludingClassAncestors(java.lang.Class clazz)

whereas Class.getInterfaces() only returns interfaces directly implemented by a class, this walks the inheritance hierarchy to include interfaces implemented by superclass/ancestors; (note it does not include superinterfaces)

java.net.URL getResource(java.lang.String r)

finds the resource in the classloader, if it exists; inserts or replaces leading slash as necessary (i believe it should _not_ have one, but there is some inconsistency)

static java.lang.Class inferSubbest(java.lang.Class c1, java.lang.Class c2)

convenience for casting the given candidate to the given type (without any coercion, and allowing candidate to be null)

static java.lang.reflect.Field inferSubbestField(java.lang.reflect.Field f1, java.lang.reflect.Field f2)

Gets the field that is in the sub-class; or null if one field does not come from a sub-class of the other field's class

static java.lang.reflect.Method inferSubbestMethod(java.lang.reflect.Method m1, java.lang.reflect.Method m2)

Gets the class that is in the sub-class; or null if neither is a sub-class of the other.

static Optional invokeConstructorWithArgs(java.lang.Class clazz, java.lang.Object[] argsArray)

Invokes a suitable constructor, supporting varargs and primitives

static Optional invokeConstructorWithArgs(java.lang.Class clazz, java.lang.Object[] argsArray, boolean setAccessible)

Invokes a suitable constructor, supporting varargs and primitives, additionally supporting setAccessible

java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object obj, java.lang.Object... argValues)

@throws ReflectionAccessException If invocation failed due to illegal access or the invoked method failed

static Optional invokeMethodWithArgs(java.lang.Object clazzOrInstance, java.lang.String method, java.util.List args)

static Optional invokeMethodWithArgs(java.lang.Object clazzOrInstance, java.lang.String method, java.util.List args, boolean setAccessible)

java.lang.Object invokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... argValues)

java.lang.Class loadClass(java.lang.String classname)

instantiates the given class from its binary name

java.lang.Class loadClassFromCanonicalName(java.lang.String canonicalName)

Loads class given its canonical name format (e.g. com.acme.Foo.Inner), using iterative strategy (trying com.acme.Foo$Inner, then com.acme$Foo$Inner, etc).

java.lang.reflect.Constructor loadConstructor(java.lang.Class clazz, java.lang.Class[] argTypes)

java.lang.Class loadInnerClassNotInheritted(java.lang.String outerClassname, java.lang.String innerClassname)

does not look through ancestors of outer class

java.lang.Class loadInnerClassNotInheritted(java.lang.Class outerClazz, java.lang.String innerClassname)

does not look through ancestors of outer class

static java.lang.Class loadInnerClassPossiblyInheritted(java.lang.Class clazz, java.lang.String nestedPart)

given a nested part, e.g.

java.lang.Object loadInstance(java.lang.String classname, java.lang.Object[] argValues)

java.lang.Object loadInstance(java.lang.String classname, java.lang.Class[] argTypes, java.lang.Object[] argValues)

java.lang.Object loadInstance(java.lang.String classname)

java.lang.Object loadInstance(java.lang.reflect.Constructor constructor, java.lang.Object[] argValues)

java.lang.reflect.Method loadMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] argTypes)

java.lang.reflect.Method loadMethod(java.lang.Class clazz, java.lang.String methodName)

returns the first method matching the given name

java.lang.reflect.Constructor loadSingleConstructor(java.lang.Class clazz)

returns a single constructor in a given class, or throws an exception

java.lang.Object loadStaticField(java.lang.Class clazz, java.lang.String fieldname)

java.lang.Object[] loadStaticFields(java.lang.Class clazz, java.lang.String[] fieldnamesArray, java.lang.Object[] defaults)

static boolean typesMatch(java.lang.Object[] argsArray, java.lang.Class[] parameterTypes)

true iff all args match the corresponding types

static boolean typesMatchUpTo(java.lang.Object[] argsArray, java.lang.Class[] parameterTypes, int lengthRequired)

java.lang.Object updateFromNewClassLoader(java.lang.Object data)

Serialize the given object, then reload using the current class loader; this removes linkages to instances with classes loaded by an older class loader.

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Method Detail

Reflections

public java.lang.Object Reflections(java.lang.ClassLoader classLoader)


cast

* @throws IllegalArgumentException */
public static java.lang.Object cast(java.lang.Object candidate, java.lang.Class type)


copyFields

public static void copyFields(java.lang.Object source, java.lang.Object target)
copies all fields from the source to target; very little compile-time safety checking, so use with care
throws:
IllegalAccessException
throws:
IllegalArgumentException


findCallabaleConstructor

tatic  Constructor findCallabaleConstructor(Class clazz, Object[] args) {
for (Constructor constructor : clazz.getConstructors()) {
public static java.lang.reflect.Constructor findCallabaleConstructor(java.lang.Class clazz, java.lang.Object[] args)
Returns a constructor that accepts the given arguments, or null if no such constructor is accessible.

This does not support varargs.

deprecated:
since 0.6.0 use invokeConstructorWithArgs(Class, Object[])


findField

public static java.lang.reflect.Field findField(java.lang.Class clazz, java.lang.String name)


findMethod

public static java.lang.reflect.Method findMethod(java.lang.Class clazz, java.lang.String name, java.lang.Class... parameterTypes)


findPublicFieldsOrderedBySuper

public static java.util.List findPublicFieldsOrderedBySuper(java.lang.Class clazz)


findPublicMethodsOrderedBySuper

public static java.util.List findPublicMethodsOrderedBySuper(java.lang.Class clazz)


findSuperType

le (toinspect.size() > 0) {
public static java.lang.Class findSuperType(java.lang.Object impl, java.lang.String typeName)


getCaller

ckTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
	    //
public static java.lang.StackTraceElement getCaller()
deprecated:
since 0.6.0; this is sloppy, and rarely does the right thing. fortunately don't think it is used. use methods in StackTraceSimplifier or JavaClassNames


getClassLoader

public java.lang.ClassLoader getClassLoader()


getInterfacesIncludingClassAncestors

public static java.util.Set getInterfacesIncludingClassAncestors(java.lang.Class clazz)
whereas Class.getInterfaces() only returns interfaces directly implemented by a class, this walks the inheritance hierarchy to include interfaces implemented by superclass/ancestors; (note it does not include superinterfaces)


getResource

= null;
		u = cl
public java.net.URL getResource(java.lang.String r)
finds the resource in the classloader, if it exists; inserts or replaces leading slash as necessary (i believe it should _not_ have one, but there is some inconsistency) Will return null if no resource is found.


inferSubbest

public static java.lang.Class inferSubbest(java.lang.Class c1, java.lang.Class c2)
convenience for casting the given candidate to the given type (without any coercion, and allowing candidate to be null)


inferSubbestField

public static java.lang.reflect.Field inferSubbestField(java.lang.reflect.Field f1, java.lang.reflect.Field f2)
Gets the field that is in the sub-class; or null if one field does not come from a sub-class of the other field's class


inferSubbestMethod

public static java.lang.reflect.Method inferSubbestMethod(java.lang.reflect.Method m1, java.lang.reflect.Method m2)
Gets the class that is in the sub-class; or null if neither is a sub-class of the other.


invokeConstructorWithArgs

public static Optional invokeConstructorWithArgs(java.lang.Class clazz, java.lang.Object[] argsArray)
Invokes a suitable constructor, supporting varargs and primitives


invokeConstructorWithArgs

for (Constructor constructor : clazz.getConstructors()) {
public static Optional invokeConstructorWithArgs(java.lang.Class clazz, java.lang.Object[] argsArray, boolean setAccessible)
Invokes a suitable constructor, supporting varargs and primitives, additionally supporting setAccessible


invokeMethod

public java.lang.Object invokeMethod(java.lang.reflect.Method method, java.lang.Object obj, java.lang.Object... argValues)
throws:
ReflectionAccessException If invocation failed due to illegal access or the invoked method failed
throws:
IllegalArgumentException If the arguments were invalid


invokeMethodWithArgs

public static Optional invokeMethodWithArgs(java.lang.Object clazzOrInstance, java.lang.String method, java.util.List args)


invokeMethodWithArgs

public static Optional invokeMethodWithArgs(java.lang.Object clazzOrInstance, java.lang.String method, java.util.List args, boolean setAccessible)


invokeStaticMethod

public java.lang.Object invokeStaticMethod(java.lang.reflect.Method method, java.lang.Object... argValues)


loadClass

public java.lang.Class loadClass(java.lang.String classname)
instantiates the given class from its binary name


loadClassFromCanonicalName

public java.lang.Class loadClassFromCanonicalName(java.lang.String canonicalName)
Loads class given its canonical name format (e.g. com.acme.Foo.Inner), using iterative strategy (trying com.acme.Foo$Inner, then com.acme$Foo$Inner, etc).
throws:
ReflectionNotFoundException


loadConstructor

public java.lang.reflect.Constructor loadConstructor(java.lang.Class clazz, java.lang.Class[] argTypes)


loadInnerClassNotInheritted

public java.lang.Class loadInnerClassNotInheritted(java.lang.String outerClassname, java.lang.String innerClassname)
does not look through ancestors of outer class


loadInnerClassNotInheritted

public java.lang.Class loadInnerClassNotInheritted(java.lang.Class outerClazz, java.lang.String innerClassname)
does not look through ancestors of outer class

uses the classloader set in this class, not in the clazz supplied


loadInnerClassPossiblyInheritted

public static java.lang.Class loadInnerClassPossiblyInheritted(java.lang.Class clazz, java.lang.String nestedPart)
given a nested part, e.g. Inner$VeryInner, this will recurse through clazz.Inner, looking for VeryInner, then looking in each supertype (interface) of clazz for Inner.VeryInner;

so it will find Clazz.Inner.VeryInner wherever in the hierarchy it is defined

(as opposed to ClassLoader which requires Inner.VeryInner to be _declared_ in clazz, not in any supertype

returns null if not found


loadInstance

public java.lang.Object loadInstance(java.lang.String classname, java.lang.Object[] argValues)


loadInstance

public java.lang.Object loadInstance(java.lang.String classname, java.lang.Class[] argTypes, java.lang.Object[] argValues)


loadInstance

public java.lang.Object loadInstance(java.lang.String classname)


loadInstance

public java.lang.Object loadInstance(java.lang.reflect.Constructor constructor, java.lang.Object[] argValues)


loadMethod

public java.lang.reflect.Method loadMethod(java.lang.Class clazz, java.lang.String methodName, java.lang.Class[] argTypes)


loadMethod

public java.lang.reflect.Method loadMethod(java.lang.Class clazz, java.lang.String methodName)
returns the first method matching the given name


loadSingleConstructor

public java.lang.reflect.Constructor loadSingleConstructor(java.lang.Class clazz)
returns a single constructor in a given class, or throws an exception


loadStaticField

public java.lang.Object loadStaticField(java.lang.Class clazz, java.lang.String fieldname)


loadStaticFields

public java.lang.Object[] loadStaticFields(java.lang.Class clazz, java.lang.String[] fieldnamesArray, java.lang.Object[] defaults)


typesMatch

public static boolean typesMatch(java.lang.Object[] argsArray, java.lang.Class[] parameterTypes)
true iff all args match the corresponding types


typesMatchUpTo

public static boolean typesMatchUpTo(java.lang.Object[] argsArray, java.lang.Class[] parameterTypes, int lengthRequired)


updateFromNewClassLoader

public final java.lang.Object updateFromNewClassLoader(java.lang.Object data)
Serialize the given object, then reload using the current class loader; this removes linkages to instances with classes loaded by an older class loader.

(like a poor man's clone)

aka "reconstitute(Object)"


 

Brooklyn Multi-Cloud Application Management Platform
brooklyncentral.github.com. Apache License. © 2012.