org.qedeq.base.utility
Class YodaUtility

java.lang.Object
  extended by org.qedeq.base.utility.YodaUtility

public abstract class YodaUtility
extends java.lang.Object

We learned so much from the great Jedi master. Using the force we can get and set private fields of arbitrary objects. We can even execute private methods...

Author:
Michael Meyling

Method Summary
static java.lang.Object executeMethod(java.lang.Object obj, java.lang.String name, java.lang.Class[] parameterTypes, java.lang.Object[] parameter)
          This method executes a method on an object or one of its super instances (even if it is private).
static java.lang.Object executeMethod(java.lang.String clazzName, java.lang.String name, java.lang.Class[] parameterTypes, java.lang.Object[] parameter)
          This method executes a static class method (even if it is private).
static boolean existsMethod(java.lang.Class clazz, java.lang.String name, java.lang.Class[] parameterTypes)
          Analyze if a class or one of its super classes contains a given method.
static boolean existsMethod(java.lang.String clazz, java.lang.String name, java.lang.Class[] parameterTypes)
          Analyze if a class or one of its super classes contains a given method.
static java.lang.reflect.Field getField(java.lang.Object obj, java.lang.String name)
          Get field of given name in given object.
static java.lang.Object getFieldValue(java.lang.Object obj, java.lang.String name)
          This method returns the contents of an object variable .The class hierarchy is recursively searched to find such a field (even if it is private).
static void setFieldContent(java.lang.Object obj, java.lang.String name, java.lang.Object value)
          This method sets the contents of an object variable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

existsMethod

public static boolean existsMethod(java.lang.String clazz,
                                   java.lang.String name,
                                   java.lang.Class[] parameterTypes)
Analyze if a class or one of its super classes contains a given method.

Example: you can test with YodaUtility.existsMethod("java.net.URLConnection", "setConnectTimeout", new Class[] {Integer.TYPE} with JDK 1.4.2 and if you run it with a 1.5 JRE or higher then it will be successfully executed.

Parameters:
clazz - Class to analyze.
name - Method name.
parameterTypes - Parameter types.
Returns:
Does the class (or one of its super classes) have such a method?

existsMethod

public static boolean existsMethod(java.lang.Class clazz,
                                   java.lang.String name,
                                   java.lang.Class[] parameterTypes)
Analyze if a class or one of its super classes contains a given method.

Example: you can test with YodaUtility.existsMethod(URLConnection.class, "setConnectTimeout", new Class[] {Integer.TYPE} with JDK 1.4.2 and if you run it with a 1.5 JRE or higher then it will be successfully executed.

Parameters:
clazz - Class to analyze.
name - Method name.
parameterTypes - Parameter types.
Returns:
Does the class (or one of its super classes) have such a method?

executeMethod

public static java.lang.Object executeMethod(java.lang.Object obj,
                                             java.lang.String name,
                                             java.lang.Class[] parameterTypes,
                                             java.lang.Object[] parameter)
                                      throws java.lang.NoSuchMethodException,
                                             java.lang.reflect.InvocationTargetException
This method executes a method on an object or one of its super instances (even if it is private).

Example: you can compile YodaUtility.executeMethod((URLConnection) httpConnection, "setConnectTimeout", new Class[] {Integer.TYPE}, new Object[] { new Integer(100)}); with JDK 1.4.2 and if you run it with a 1.5 JRE or higher then it will be successfully executed.

Parameters:
obj - Object.
name - Method name.
parameterTypes - Parameter types.
parameter - Parameter values.
Returns:
Execution result.
Throws:
java.lang.NoSuchMethodException - Method not found.
java.lang.reflect.InvocationTargetException - Wrapped exception.

executeMethod

public static java.lang.Object executeMethod(java.lang.String clazzName,
                                             java.lang.String name,
                                             java.lang.Class[] parameterTypes,
                                             java.lang.Object[] parameter)
                                      throws java.lang.NoSuchMethodException,
                                             java.lang.reflect.InvocationTargetException
This method executes a static class method (even if it is private).

Example: you can compile YodaUtility.executeMethod( "java.util.concurrent.locks.LockSupport", "park", new Class[0], new Object[0]); with JDK 1.4.2 and if you run it with a 1.5 JRE or higher then it will be successfully executed.

Parameters:
clazzName - Name of class.
name - Name of static method to execute.
parameterTypes - Parameter types.
parameter - Parameter values.
Returns:
Execution result.
Throws:
java.lang.NoSuchMethodException - Method not found.
java.lang.reflect.InvocationTargetException - Wrapped exception.

getFieldValue

public static java.lang.Object getFieldValue(java.lang.Object obj,
                                             java.lang.String name)
                                      throws java.lang.NoSuchFieldException
This method returns the contents of an object variable .The class hierarchy is recursively searched to find such a field (even if it is private).

Parameters:
obj - Object.
name - Variable name.
Returns:
Contents of variable.
Throws:
java.lang.NoSuchFieldException - Variable of given name was not found.

setFieldContent

public static void setFieldContent(java.lang.Object obj,
                                   java.lang.String name,
                                   java.lang.Object value)
                            throws java.lang.NoSuchFieldException
This method sets the contents of an object variable. The class hierarchy is recursively searched to find such a field (even if it is private).

Parameters:
obj - Object.
name - Variable name.
value - Value to set.
Throws:
java.lang.NoSuchFieldException - Variable of given name was not found.

getField

public static java.lang.reflect.Field getField(java.lang.Object obj,
                                               java.lang.String name)
                                        throws java.lang.NoSuchFieldException
Get field of given name in given object. The class hierarchy is recursively searched to find such a field (even if it is private).

Parameters:
obj - Object to work on.
name - Search this field.
Returns:
Found field.
Throws:
java.lang.NoSuchFieldException - Field with name name was not found.


Copyright © 2014. All Rights Reserved.