[ 
https://issues.apache.org/jira/browse/GROOVY-9075?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16907871#comment-16907871
 ] 

Daniel Sun commented on GROOVY-9075:
------------------------------------

You mean a better error message like the following one?

{code:java}
object[C2] is not an instance of declaring class[C1]
{code}



> The exception message should be more clear when a GroovyObject's metaClass is 
> wrong
> -----------------------------------------------------------------------------------
>
>                 Key: GROOVY-9075
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9075
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.4.16, 3.0.0-alpha-4, 2.5.6
>            Reporter: Xiaoguang Wang
>            Priority: Major
>
> 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` ?
>  
> {code:java}
> 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
>     }
> }
> {code}
>  
> {code:java}
> /*
> 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)
>  */
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)

Reply via email to