You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by kreiger <gi...@git.apache.org> on 2017/02/22 01:32:42 UTC

[GitHub] groovy pull request #502: CompilerConfiguration.setScriptBaseClass with Java...

GitHub user kreiger opened a pull request:

    https://github.com/apache/groovy/pull/502

    CompilerConfiguration.setScriptBaseClass with Java class calls super() instead of super(Binding)

    This test fails because `ModuleNode.setScriptBaseClassFromConfig(ClassNode)`
    calls `.setSuperClass(ClassHelper.make(baseClassName))` on the `scriptDummy` `ClassNode`.
    
     The `ClassNode` created for this script's base class has `.lazyInitDone = true` and `.constructors = null` so when `.getSuperClass().getDeclaredConstructor(SCRIPT_CONTEXT_CTOR)` is called by `ModuleNode.createStatementsClass()`, then `ClassNode.constructors` is set to an empty ArrayList in `ClassNode.getDeclaredConstructors()`
    
    The script constructor is then generated as
    
             Constructor(Binding context) {
                 super();                   // Fields are initialized after the call to super()
                                            // Fields are initialized here
                 setBinding(context);       // Fields are initialized before the call to setBinding(context)
             }
    
    instead of
    
             Constructor(Binding context) {
                 super(context);            // Fields are initialized after the call to super(context)
             }
    
    Fields are initialized between the call to super() and the setBinding(context)
    which means Field initializers don't have access to the Binding context.
    
    This leads to `MissingPropertyException` because we're trying to look up variables from the `new Binding()` created in the default constructor, instead of the binding we passed in.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/kreiger/groovy setscriptbaseclass-calls-wrong-constructor

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/groovy/pull/502.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #502
    
----
commit 764388a34b9f6fbf1c605fbbf02700b8ebed82d8
Author: Christoffer Hammarstr�m <ch...@linuxgods.com>
Date:   2017-02-22T01:18:52Z

    Add tests to prove that calling CompilerConfiguration.setScriptBaseClass with a Java class causes ModuleNode to generate a constructor that prevents Field initialization from Binding context.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request #502: GROOVY-8096 setScriptBaseClass with Java class bre...

Posted by kreiger <gi...@git.apache.org>.
Github user kreiger commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/502#discussion_r102426057
  
    --- Diff: src/test/groovy/lang/BindingScript.java ---
    @@ -0,0 +1,32 @@
    +/*
    + *  Licensed to the Apache Software Foundation (ASF) under one
    + *  or more contributor license agreements.  See the NOTICE file
    + *  distributed with this work for additional information
    + *  regarding copyright ownership.  The ASF licenses this file
    + *  to you under the Apache License, Version 2.0 (the
    + *  "License"); you may not use this file except in compliance
    + *  with the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *  Unless required by applicable law or agreed to in writing,
    + *  software distributed under the License is distributed on an
    + *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + *  KIND, either express or implied.  See the License for the
    + *  specific language governing permissions and limitations
    + *  under the License.
    + */
    +package groovy.lang;
    +
    +/**
    + * A Script which requires a Binding passed in the constructor and disallows calling the default constructor.
    + */
    +public abstract class BindingScript extends Script {
    +    protected BindingScript() {
    --- End diff --
    
    If i remove both constructors the `super()` call just goes to the default constructor in groovy.lang.Script, giving `MissingPropertyException` on trying to access the wrong `Binding context` on field initalization:
    
        groovy.lang.MissingPropertyException: No such property: args for class: TestBindingsInFieldInitializersWithConfigJavaBaseScript
     	    at TestBindingsInFieldInitializersWithConfigJavaBaseScript.<init>(TestBindingsInFieldInitializersWithConfigJavaBaseScript.groovy:2)
    	    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    	    at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:271)



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] groovy pull request #502: CompilerConfiguration.setScriptBaseClass with Java...

Posted by danielsun1106 <gi...@git.apache.org>.
Github user danielsun1106 commented on a diff in the pull request:

    https://github.com/apache/groovy/pull/502#discussion_r102385098
  
    --- Diff: src/test/groovy/lang/BindingScript.java ---
    @@ -0,0 +1,32 @@
    +/*
    + *  Licensed to the Apache Software Foundation (ASF) under one
    + *  or more contributor license agreements.  See the NOTICE file
    + *  distributed with this work for additional information
    + *  regarding copyright ownership.  The ASF licenses this file
    + *  to you under the Apache License, Version 2.0 (the
    + *  "License"); you may not use this file except in compliance
    + *  with the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + *  Unless required by applicable law or agreed to in writing,
    + *  software distributed under the License is distributed on an
    + *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + *  KIND, either express or implied.  See the License for the
    + *  specific language governing permissions and limitations
    + *  under the License.
    + */
    +package groovy.lang;
    +
    +/**
    + * A Script which requires a Binding passed in the constructor and disallows calling the default constructor.
    + */
    +public abstract class BindingScript extends Script {
    +    protected BindingScript() {
    --- End diff --
    
    How about make the default constructor `private`? Developers can get compilation error other than runtime error, then the program will be more robust IMO.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---