Results 1 to 8 of 8

Thread: JdkDynamicAopProxy

  1. #1

    Default JdkDynamicAopProxy

    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.

  2. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Reflection tutorial

    Btw, I don't think that it's really necessary to get JdkDynamicAopProxy object and explicitly call a method on it.

  3. #3

    Default JdkDynamicAopProxy

    I obtained this JdkDynamicAopProxy from the bean container.
    The target class have @Secured annotation and the bean injected is JdkDynamicAopProxy.

  4. #4
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Ok, but it does not explain why you need to use reflection. Did I miss something?
    Quote Originally Posted by ovidiusnassos View Post
    I obtained this JdkDynamicAopProxy from the bean container.
    The target class have @Secured annotation and the bean injected is JdkDynamicAopProxy.

  5. #5

    Default

    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:

    Code:
    		MyClass myClass = new MyClass();
    		Method method = myClass.getClass().getMethod("printHelloWorld");
    		method.invoke(myClass);
    Here, MyClass is a class that implements a public void printHelloWorld() {}

  6. #6

    Default

    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?

  7. #7
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    1. 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.
    2. 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.
    3. 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;
          }
    4. And BTW, why you fetch bean from context by name - with some relatively rare exceptions it smells

    Quote Originally Posted by Ovidiu Ariton View Post
    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?

  8. #8

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •