Announcement Announcement Module
Collapse
No announcement yet.
Error "object is not an instance of declaring class" with Aspects in OSGi Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error "object is not an instance of declaring class" with Aspects in OSGi

    Hi all,

    Firstly, I hope that this is the correct forum for my question. I am having problems with using Spring AOP when deploying code in OSGi bundles. Here is the explanation (I would try to whittle down my actual code to the smallest size possible):

    I have two interfaces called NodeConfig and ValidatorAspect.
    Code:
    public interface NodeConfig {
        void add();
    }
    
    public interface ValidatorAspect {
        void validate(JoinPoint joinPoint);
    }
    As the name suggests, ValidatorAspect implements an advice method and aspects are configured in XML as follows:
    Code:
        <aop:config>
          <aop:aspect ref="validatorAspect">
            <aop:before pointcut="execution(* a.b.c.NodeConfig.*(..))"
                        method="validate" />
          </aop:aspect>
        </aop:config>
    Now, when both the above interfaces are deployed in the same bundle and when a third bundle calls NodeConfig.add(), aspect works just fine with NodeConfig.add() being properly wrapped by the advice.

    How ever, when I deploy ValidatorAspect in a separate bundle (because I want to use same aspect from multiple bundles), I get the following error when NodeConfig.add() is called:

    Code:
    Caused by: org.springframework.aop.AopInvocationException: Mismatch on arguments to advice method [public final void $Proxy212.validate(org.aspectj.lang.JoinPoint) throws java.lang.Exception]; pointcut expression [[email protected]]; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
            at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:624)
            at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:603)
            at org.springframework.aop.aspectj.AspectJMethodBeforeAdvice.before(AspectJMethodBeforeAdvice.java:39)
    ........
    Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    I am using Spring framework 3.0.6 and Virgo-tomcat-server 3.0.2 for this test. Can any one suggest what maybe at issue here? It appears that when advice and the object being advised are in separate bundles, Spring AOP is running into some problems.

    I do hope that I have described the problem in sufficient detail but if there are any further questions, please feel free to ask.

    Thanks,
    Raghu

  • #2
    Something is not clear in your code. You say ValidatorAspect is an interface, but then you say "ValidatorAspect implements an advice method". Interfaces DO NOT implement anything, they ARE implemented by implementation classes. And, anyway, you don't "implement advice methods": rather, your aspect is made up by your pointcut, and the advice code that is to be weaved at the joinpoints determined by the pointcut. There is no interface to be involved here; I guess that's probably the source of your problems.
    In short, don't use interfaces for aspects. Your aspect's gotta be a class that doesn't implement a thing.

    Comment


    • #3
      Sorry for confusion about the terminology. Obviously, I had a class that implemented ValidatorAspect. As to interface being the problem here, I did try removing the interface from the picture and directly exported the implentation class as an OSGi service. This got rid of the "object is not an instance of declaring class" error but resulted in another one. I will post about it separately as I don't have the environment handy right now.

      Thanks,
      Raghu

      Comment


      • #4
        With OSGi - in the case of Eclipse Virgo (formerly Spring dm Server), you have to use LTW (load time weaving). See: http://static.springsource.org/sprin...tml#aop-aj-ltw

        Which means you have to use pure AspectJ, not Spring AOP.

        Also see: AspectJ in Action, 2nd Edition http://manning.com/laddad2/ Chapter 9 describes LTW in Spring.

        Regards,
        Gordon Dickens

        twitter.com/gdickens
        linkedin.com/in/gordondickens
        Blog: technophile.gordondickens.com

        Comment


        • #5
          Hi Gordon,

          Thanks for the pointer. I will take a look.

          Thanks,
          Raghu

          Comment

          Working...
          X