groovy
  1. groovy
  2. GROOVY-2583

"object is not an instance of declaring class" thrown invoking a method on Groovy class, wrapped in a Spring proxy

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.5, 1.5.2, 1.5.4
    • Fix Version/s: 1.6-rc-1, 1.5.8, 1.7-beta-1
    • Component/s: None
    • Labels:
      None
    • Environment:
      Groovy 1.5.4
      Spring 2.5
    • Testcase included:
      yes
    • Number of attachments :
      1

      Description

      I suspect this is connected with GROOVY-2006, here is the case:
      I have two Spring-loaded groovy scripts, marked with refresh-check-delay, which loads them as CGLIB proxies. Invoking a method on the proxy instance, I get

      Exception in thread "main" java.lang.IllegalArgumentException: object is not an instance of declaring class
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
              at java.lang.reflect.Method.invoke(Unknown Source)
              at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
              at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:226)
              at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:899)
              at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:740)
              at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:777)
              at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:757)
              at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:167)
              at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethod0(ScriptBytecodeAdapter.java:195)
              at X.doIt(script1.groovy]:5)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
              at java.lang.reflect.Method.invoke(Unknown Source)
              at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:301)
              at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
              at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
              at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
              at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
              at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
              at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
              at $Proxy1.doIt(Unknown Source)
              at TestMe.main(TestMe.java:20)
      
      script1.groovy
      class X implements Doable {
          def prop
          void doIt() {
              println prop.getClass().getName();
              prop.doIt(); 
          }
      }
      
      script2.groovy
      class Y {
          void doIt() { println "OK"; }
      }
      
      Doable.java
      public interface Doable {
          void doIt();
      }
      
      spring.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:lang="http://www.springframework.org/schema/lang"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
             http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">
          
          <lang:groovy id="y" script-source="classpath:groovy/script2.groovy" refresh-check-delay="1000"/>
          <lang:groovy id="x" script-source="classpath:groovy/script1.groovy" refresh-check-delay="1000">
              <lang:property name="prop" ref="y"/>
          </lang:groovy>
      </beans>
      
      TestMe.java
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      public class TestMe {
          public static void main(String[] args) {
              ClassPathXmlApplicationContext ctxt = new ClassPathXmlApplicationContext("spring.xml");
              Doable o = (Doable)ctxt.getBean("x");
              o.doIt();
          }
      }
      

        Activity

        Hide
        Alexander Kleymenov added a comment -

        groovy-1.5.6 works fine if Groovy script implements GroovyInterceptable interface.
        But groovy-1.6-beta-1 fails after bean refresh with
        java.lang.UnsupportedOperationException
        Approach with GroovyScriptFactoryPostProcessor also does not work...
        Please, use attached test to reproduce the problem: tune JAVA_HOME, GROOVY_HOME, and place spring.jar near bat launchers.

        Show
        Alexander Kleymenov added a comment - groovy-1.5.6 works fine if Groovy script implements GroovyInterceptable interface. But groovy-1.6-beta-1 fails after bean refresh with java.lang.UnsupportedOperationException Approach with GroovyScriptFactoryPostProcessor also does not work... Please, use attached test to reproduce the problem: tune JAVA_HOME, GROOVY_HOME, and place spring.jar near bat launchers.
        Hide
        Alexander Kleymenov added a comment -

        Not reproducible on groovy-1.6-beta-2.

        Show
        Alexander Kleymenov added a comment - Not reproducible on groovy-1.6-beta-2.
        Hide
        Guillaume Laforge added a comment -

        Alexander, is it working fine for you too with 1.5.7?

        Show
        Guillaume Laforge added a comment - Alexander, is it working fine for you too with 1.5.7?
        Hide
        Alexander Kleymenov added a comment -

        Guillaume, yes it works on 1.5.7 too!
        This workaround (implementing GroovyInterceptable marker interface to make groovy classes interceptable with Spring AOP) could be really usefull info on the groovy wiki!

        Show
        Alexander Kleymenov added a comment - Guillaume, yes it works on 1.5.7 too! This workaround (implementing GroovyInterceptable marker interface to make groovy classes interceptable with Spring AOP) could be really usefull info on the groovy wiki!
        Hide
        Guillaume Laforge added a comment -

        Thank you everybody for your feedback and reports saying the problem is solved.

        Show
        Guillaume Laforge added a comment - Thank you everybody for your feedback and reports saying the problem is solved.

          People

          • Assignee:
            Unassigned
            Reporter:
            Martin Mavrov
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: