You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2021/10/29 21:00:00 UTC

[jira] [Resolved] (GROOVY-7116) Make owner/thisObject access as static as delegate access

     [ https://issues.apache.org/jira/browse/GROOVY-7116?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Eric Milles resolved GROOVY-7116.
---------------------------------
    Resolution: Fixed

> Make owner/thisObject access as static as delegate access
> ---------------------------------------------------------
>
>                 Key: GROOVY-7116
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7116
>             Project: Groovy
>          Issue Type: Improvement
>          Components: Static compilation
>    Affects Versions: 2.3.7
>         Environment: Java 7u55, Win7x64, IntellJ IDEA 13.1.5 CE
>            Reporter: Jochen Eddelbuettel
>            Assignee: Eric Milles
>            Priority: Major
>
> Consider this sample code:
> {code}
> import groovy.transform.CompileStatic
> @CompileStatic
> class ClassA {
>     String propA = "XYZ"
>     static String statpropA = "UVW"
>     String methodA() {
>         return statpropA
>     }
> }
> @CompileStatic
> class ClassB {
>     String propB = "XYZ"
>     static String statpropB = "UVW"
>     String methodB() {
>         return statpropB
>     }
>     static void main(String... args) {
>         (new ClassB()).test()
>     }
>     void test()
>     {
>         (new ClassA()).with {
>             String result
>             result = propA
>             result = propB
>             result = statpropA
>             result = statpropB
>             result = methodA()
>             result = methodB()
>             propA = result
>             propB = result
>           }
>     }
> }
> {code}
> Decompiling the resulting bytecode for the closure reveals this:
> {code}
>     public Object doCall(Object it)
>     {
>       String result = null;
>       
>       String str1 = ((ClassA)getDelegate()).getPropA();result = str1;
>       Object localObject1 = getProperty("propB");result = (String)ShortTypeHandling.castToString(localObject1);
>       String str2 = ClassA.getStatpropA();result = str2;
>       Object localObject2 = getProperty("statpropB");result = (String)ShortTypeHandling.castToString(localObject2);
>       String str3 = ((ClassA)getDelegate()).methodA();result = str3;
>       String str4 = ((ClassB)ScriptBytecodeAdapter.castToType(getProperty("thisObject"), ClassB.class)).methodB();result = str4;
>       ((ClassA)getDelegate()).setPropA(result);null;
>       String str5 = result;ScriptBytecodeAdapter.setGroovyObjectProperty(str5, _test_closure1.class, this, "propB");return str5;return null;
>     }
> {code}
> While @DelegatesTo enables very nice static results for the delegate, accessing properties of the surrounding thisObject triggers a casade of calls with reflection and dynamic. The calls should be made directly via getThisObject() in full analogy to the calls made via getDelegate(), e.g. ((ClassB)getThisObject()).setPropB(result) for the second assignment.
> The properties and methods of the surrounding class are the ones checked and accepted by the type checker. I don't see any reason, except my potentially endless ingnorance, why those properties and methods shouldn't be called directly then in the static compiler output.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)