public class ClassProxy extends Object
A util which allows you to create class proxies.
This means if you specify a class A, then a class B which extends A will be created and, depending on the given MethodFilter
, some or all non-static non-final methods which are public or protected will be overridden and dispatched to a provided InvocationHandler
.
Modifier and Type | Method and Description |
---|---|
static Object |
callSuper(Object proxy,
Method method,
Object... args)
Calls the method that has been overridden by a proxy method.
|
static Class |
getClass(Class superClass,
boolean createIfNotExists)
Creates a proxy class for a given super class.
|
static Class |
getClass(Class superClass,
MethodFilter filter,
boolean createIfNotExists)
Creates a proxy class for a given super class.
|
static boolean |
isProxy(Class clazz)
Returns
true if the given Class object is a proxy class generated by the ClassProxy class. |
static <T> T |
newInstance(Class<T> superClass,
InvocationHandler handler,
Class[] paramTypes,
Object... params)
Creates a proxy class for a given super class and instantiates it.
|
static <T> T |
newInstance(Class<T> superClass,
MethodFilter filter,
InvocationHandler handler,
Class[] paramTypes,
Object... params)
Creates a proxy class for a given super class and instantiates it.
|
public static boolean isProxy(Class clazz)
Returns true
if the given Class object is a proxy class generated by the ClassProxy class.
clazz
- The Class object to be checked.true
if the given Class object is a proxy class, false
otherwise.public static Object callSuper(Object proxy, Method method, Object... args) throws Throwable
Calls the method that has been overridden by a proxy method.
proxy
- The proxy instance.method
- The method you want to call.args
- The argument to pass to the method.NoSuchMethodException
- If the super method cannot be found.SecurityException
- If the super method cannot be accessed.Throwable
- If the super method throws anything.public static <T> T newInstance(Class<T> superClass, InvocationHandler handler, Class[] paramTypes, Object... params) throws IllegalArgumentException
Creates a proxy class for a given super class and instantiates it.
This basically just calls newInstance(superClass, null, handler, paramTypes, params)
.
superClass
- The class on which you wish to create a proxy.handler
- The InvocationHandler
to which public and protected method calls will be redirected.paramTypes
- An array of Classes
to identify the constructor of the super class you wish to invoke.params
- An array or arguments to pass to the constructor of the super class.IllegalArgumentException
- If the proxy class has not yet been created: See getClass()
.public static <T> T newInstance(Class<T> superClass, MethodFilter filter, InvocationHandler handler, Class[] paramTypes, Object... params) throws IllegalArgumentException
Creates a proxy class for a given super class and instantiates it.
superClass
- The class on which you wish to create a proxy.filter
- The MethodFilter
to use on superClass
.handler
- The InvocationHandler
to which public and protected method calls will be redirected.paramTypes
- An array of Classes
to identify the constructor of the super class you wish to invoke.params
- An array or arguments to pass to the constructor of the super class.IllegalArgumentException
- If the proxy class has not yet been created: See getClass()
.public static Class getClass(Class superClass, boolean createIfNotExists) throws IllegalArgumentException
Creates a proxy class for a given super class.
Creates a new Class
which extends a given super class which redirects all calls to public and protected methods which are non-final and non-static to a given InvocationHandler
.
Abstract methods will be defined and redirected to the InvocationHandler as well.
For every public and protected constructor the super class has, this class will have the same constructor with an InvocationHandler as additional first parameter.
superClass
- The class on which you wish to create a proxy. Has to be public and not final, primitive, an array or an interface.createIfNotExists
- Whether or not to create a proxy class if none exists.createIfNotExists == false
.IllegalArgumentException
- If createIfNotExists
is true
, the class does not exist yet and no valid bytecode can be generated.public static Class getClass(Class superClass, MethodFilter filter, boolean createIfNotExists) throws IllegalArgumentException
Creates a proxy class for a given super class.
Creates a new Class
which extends a given super class which redirects all calls to public and protected methods which are non-final and non-static, on which filter.filterMethod()
returns true
, to a given InvocationHandler
.
Abstract methods will be defined and redirected to the InvocationHandler as well.
For every public and protected constructor the super class has, this class will have the same constructor with an InvocationHandler as additional first parameter.
superClass
- The class on which you wish to create a proxy. Has to be public and not final, primitive, an array or an interface.filter
- The MethodFilter
to use on superClass
.createIfNotExists
- Whether or not to create a proxy class if none exists.createIfNotExists == false
.IllegalArgumentException
- If createIfNotExists
is true
, the class does not exist yet and no valid bytecode can be generated.Copyright © 2013. All Rights Reserved.