26import java.lang.reflect.InaccessibleObjectException;
27import java.lang.reflect.InvocationTargetException;
28import java.lang.reflect.Method;
30import org.jau.sys.Debug;
36 static final boolean DEBUG =
Debug.
debug(
"Reflection");
38 private static final String asString(
final Class<?>[] argTypes) {
39 final StringBuilder args =
new StringBuilder();
40 if(
null != argTypes) {
41 for (
int i = 0; i < argTypes.length; i++) {
45 args.append(argTypes[i].getName());
48 return args.toString();
58 return null != Class.forName(clazzName,
false , cl);
67 }
catch(
final Throwable t ) {
76 public static final Class<?>
getClass(
final String clazzName,
final boolean initializeClazz,
final ClassLoader cl)
77 throws RuntimeException {
79 return Class.forName(clazzName, initializeClazz, cl);
80 }
catch(
final Throwable t ) {
81 throw new RuntimeException(clazzName +
" not available", t);
88 public static final Method
getMethod(
final Class<?> clazz,
final String methodName,
final Class<?> ... argTypes)
89 throws RuntimeException
92 return clazz.getDeclaredMethod(methodName, argTypes);
93 }
catch (
final Throwable t) {
94 throw new RuntimeException(
"Method: '" + clazz +
"." + methodName +
"(" + asString(argTypes) +
")' not found", t);
101 public static final Method
getMethod(
final String clazzName,
final boolean initializeClazz,
final String methodName,
final Class<?>[] argTypes,
final ClassLoader cl)
102 throws RuntimeException
105 return getMethod(Class.forName(clazzName, initializeClazz, cl), methodName, argTypes);
106 }
catch (
final Throwable t) {
107 throw new RuntimeException(
"Method: '" + clazzName +
"." + methodName +
"(" + asString(argTypes) +
")' not found", t);
118 @SuppressWarnings(
"unchecked")
119 public static final <R> R
callMethod(final Object instance, final Method method, final Object ... args)
120 throws RuntimeException
123 return (R)method.invoke(instance, args);
124 }
catch (
final Exception e) {
126 if (t instanceof InvocationTargetException) {
127 t = ((InvocationTargetException) t).getTargetException();
129 if (t instanceof Error) {
132 if (t instanceof RuntimeException) {
133 throw (RuntimeException) t;
135 throw new RuntimeException(
"calling "+method+
" failed", t);
146 throws RuntimeException
154 public static final <R> R
callStaticMethod(
final String clazzName,
final String methodName,
final Class<?>[] argTypes,
final Object[] args,
final ClassLoader cl)
155 throws RuntimeException
165 public MethodAccessor(
final Class<?> clazz,
final String methodName,
final Class<?> ... argTypes) {
168 }
catch (
final RuntimeException jre) { }
183 throws InaccessibleObjectException, SecurityException
185 m.setAccessible(flag);
195 m.setAccessible(flag);
197 }
catch(
final Throwable t ) {
209 public <R> R callMethod(
final Object instance,
final Object ... args) {
211 throw new RuntimeException(
"Method not available. Instance: "+instance);
222 public <R> R callStaticMethod(
final Object ... args) {
224 throw new RuntimeException(
"Method not available.");
226 return ReflectionUtil.callStaticMethod(m, args);
Convenient Method access class.
void setAccessible(final boolean flag)
See Method#setAccessible(boolean).
boolean setAccessibleSafe(final boolean flag)
See setAccessible(boolean).
MethodAccessor(final Class<?> clazz, final String methodName, final Class<?> ... argTypes)
Check available() before using instance.
boolean available()
Returns true if method is available, otherwise false.
Utility methods to simplify reflection access.
static final< R > R callStaticMethod(final Method method, final Object ... args)
static final Class<?> getClass(final String clazzName, final boolean initializeClazz, final ClassLoader cl)
Loads and returns the class or null.
static final< R > R callStaticMethod(final String clazzName, final String methodName, final Class<?>[] argTypes, final Object[] args, final ClassLoader cl)
static final Method getMethod(final String clazzName, final boolean initializeClazz, final String methodName, final Class<?>[] argTypes, final ClassLoader cl)
static final< R > R callMethod(final Object instance, final Method method, final Object ... args)
static final Method getMethod(final Class<?> clazz, final String methodName, final Class<?> ... argTypes)
static boolean isClassAvailable(final String clazzName, final ClassLoader cl)
Returns true only if the class could be loaded but w/o class initialization.
Helper routines for logging and debugging.
static final boolean debug(final String subcomponent)