P.Hill said:
Can you tell us why? People do the wierdest things with reflection
and I'm wondering why you'd want to know. As part of some IDE plugin or
ANT
tool it sounds great, but otherwise it sounds a bit odd.
I'll try but it isn't easy in english for me.
I have many classes each exposing some properties (getter/setter
methods) and other methods, some of wich defined in a common interface.
Now I want to attach some common functionality to those properties and
common methods (for example user's permission checking, but not only) so
I have defined a class like this:
public class PermissionChecker {
private Object aObject;
public PermissionChecker(Object obj) {
this.aObject = obj;
}
public vodi setProperty(String propertyName, Object value) {
// Do the permission checking
// ...
PropertyUtils.setProperty(aObject, propertyName, value);
}
public Object getProperty(String propertyName) {
// ...
}
public void doSomething() {
// ...
}
}
I don't know if this is a good solution, but it is the solution we
adopted and I can't change it easily. Consider that usually we accessed
the properties throw reflection and there were few places were we do this.
The problem is that in some code I need to get the functionality of the
PermissionChecker without knowing of it. In that code I access the
object throw some interfaces (implemented by the original class) that
define the properties and the other methods.
I think I can do this with the java.lang.Proxy class.
Consider this code:
public interface MyPropertiesInterface {
public void setName(String name);
public String getName();
}
public interface MyCommonInterface {
public void doSomething();
}
public interface MyInterface
extends MyPropertiesInterface, MyCommonInterface{
}
public class MyClass implements MyInterface { ... }
....
{
MyInterface anObject = ....;
PermissionChecker pcheck = new PermissionChecker(anObject)
invHand = new MyInvocationHandler(pcheck);
MyInterface checkedObject = (MyInterface)Proxy.newProxyInstance(
loader,
new Class[]{ MyInterface.class },
invHand);
// Use the checked object as it was anObject
}
public class MyInvocationHandler
implements java.lang.reflect.InvocationHandler {
PermissionChecker pcheck;
Object anObject;
public MyInvocationHandler(PermissionChecker checker, Object obj) {
pckeck = checker;
anObject = obj;
}
public Object invoke(Object proxy, Method method, Object[] args) {
if(/* method is a property setter, I know how to do */){
// ...
String propertyName = ...;
pckeck.setProperty(propertyName, args[0]);
return null;
}
if(/* method is a property getter, I know how to do */) {
// ...
String propertyName = ...;
return pckeck.getProperty(propertyName);
}
if(/*method belongs to MyCommonInterface, I DON'T KNOW HOW TO DO*/){
// do something special
return ...;
}
method.invoke(anObject, args);
}
}
The problem is in the invoke method. I have to know if the method
belogns to MyMethodsInterface, but I don't know how to do (other than
manually compare the method name and the arguments classes).
Any suggestion?
Andrea Polci