Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9075

The exception message should be more clear when a GroovyObject's metaClass is wrong

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.4.16, 3.0.0-alpha-4, 2.5.6
    • Fix Version/s: None
    • Component/s: groovy-runtime
    • Labels:
      None

      Description

      If a GroovyObject's metaClass is overwritten by mistake, the accesses to the instance may cause an exception with unclear message.

       

      We have met the problem too many times and have spent a lot of time on debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

       

      If there is a try-catch (or some other check) in the runtime code and report this problem more clearly, it will save everyone's time to debug. Or, is there any better solution to this problem, eg: check the class type in `MetaClassImpl.setProperty`, `DefaultGroovyMethods.setMetaClass` ?

       

      import groovy.transform.CompileStatic
      
      //@CompileStatic
      class C1 {
          int x
      }
      //@CompileStatic
      class C2 {
          int x
      }
      
      //@CompileStatic
      class TestGroovy {
          static void main(String[] args) {
              def c1 = new C1()
              def c2 = new C2()
              c1.metaClass = c2.metaClass  //eg: org.springframework.beans.BeanUtils.copyProperties
              // or c1.setMetaClass(c2.getMetaClass())
      
              c1.x += 1  // crash here with unclear exception, but c1.setX() works
          }
      }
      
      

       

      /*
      without @CompileStatic
      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(NativeMethodAccessorImpl.java:62)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
         at java.lang.reflect.Method.invoke(Method.java:498)
         at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
         at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
         at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
         at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
         at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)
      
      with @CompileStatic
      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(NativeMethodAccessorImpl.java:62)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
         at java.lang.reflect.Method.invoke(Method.java:498)
         at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
         at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
         at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2726)
         at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3785)
         at C1.setProperty(TestGroovy.groovy)
         at org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:213)
         at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:497)
       */
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                wxiaoguang Xiaoguang Wang
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 2h
                  2h