Hi,
I have obtained a JdkDynamicAopProxy.
I need to invoke a method from target class via reflection.
I now only method name and parameters, i don't have a "Method object".
How can i do this?
Best regards.
Hi,
I have obtained a JdkDynamicAopProxy.
I need to invoke a method from target class via reflection.
I now only method name and parameters, i don't have a "Method object".
How can i do this?
Best regards.
Reflection tutorial
Btw, I don't think that it's really necessary to get JdkDynamicAopProxy object and explicitly call a method on it.
I obtained this JdkDynamicAopProxy from the bean container.
The target class have @Secured annotation and the bean injected is JdkDynamicAopProxy.
What is it that you're trying to do with the JdkDynamicAopProxy? If you simply want to invoke a method via reflection, you don't need anything Spring-specific. Here's an example:
Here, MyClass is a class that implements a public void printHelloWorld() {}Code:MyClass myClass = new MyClass(); Method method = myClass.getClass().getMethod("printHelloWorld"); method.invoke(myClass);
I have a generic mapping class where i decide what method to call and the target spring bean based on my business logic.
At this point i now the method name who have a generic signature and the spring bean.
When i want to invoke the method i have a proxy to the bean, not the bean itself.
Object bean = ctx.getBean("myBean");
Class c = AopUtils.getTargetClass(bean);
Method m = c.getMethod(methodName, new Class[] { Param1.class, Param2.class });
Object obj =AopUtils.invokeJoinpointUsingReflection(contrlooe r, m, new Object[] { param1Instance, param2Instance });
At this point i receive this error:
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:307)
... 30 more
My target class have a @Secured annotation.
What i am doing wrong?
- Is method that you are trying to call a member of some interface implemented by your class? If so then you need not go for target class - just call it directly on the proxy. If not - then it is the most likely severe design error. Rethink your solution once more.
- From your snippet is absolutely unclear what is "contrlooe r", how you have obtained it and if it has something in common with your target object.
- And to reach your object you may do something like this (as created proxy likely to be instansce of Advised)
Code:static public <T> T getUltimateTarget(T target) { while (target != null && target instanceof Advised) try { target = (T) ((Advised) target).getTargetSource().getTarget(); } catch (Exception e) { ... Do something or rethros } return target; }- And BTW, why you fetch bean from context by name - with some relatively rare exceptions it smells
1. I declared an interface and i make my class implement that interface (correct design)
2. "controller" is the proxy object obtained from spring container
4. i corrected my code
I resolve the problem by obtaining the "method" object from the interface implemented by my class, not from the class.
Thanks for responses.
Best regards.