Announcement Announcement Module
No announcement yet.
Error "object is not an instance of declaring class" with Aspects in OSGi Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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.
    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:
          <aop:aspect ref="validatorAspect">
            <aop:before pointcut="execution(* a.b.c.NodeConfig.*(..))"
                        method="validate" />
    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:

    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]d]; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
            at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(
            at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(
            at org.springframework.aop.aspectj.AspectJMethodBeforeAdvice.before(
    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(
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    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.


  • #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.


    • #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.



      • #4
        With OSGi - in the case of Eclipse Virgo (formerly Spring dm Server), you have to use LTW (load time weaving). See:

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

        Also see: AspectJ in Action, 2nd Edition Chapter 9 describes LTW in Spring.

        Gordon Dickens


        • #5
          Hi Gordon,

          Thanks for the pointer. I will take a look.