This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY-7304
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY-7304 by this push:
     new 65328e6  GROOVY-7304: static compile private field access and mutate 
methods
65328e6 is described below

commit 65328e6b0db9b44abe44daf7a0874dc06b59eb4b
Author: Eric Milles <[email protected]>
AuthorDate: Tue Jul 7 10:12:14 2020 -0500

    GROOVY-7304: static compile private field access and mutate methods
---
 .../transform/sc/StaticCompilationVisitor.java     |  5 +--
 .../groovy/classgen/asm/sc/bugs/Groovy7304.groovy  | 42 ++++++++++++++++++++++
 2 files changed, 45 insertions(+), 2 deletions(-)

diff --git 
a/src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java 
b/src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java
index baa8a79..16c721d 100644
--- 
a/src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java
+++ 
b/src/main/java/org/codehaus/groovy/transform/sc/StaticCompilationVisitor.java
@@ -23,7 +23,6 @@ import groovy.transform.CompileStatic;
 import groovy.transform.TypeChecked;
 import org.codehaus.groovy.ast.ASTNode;
 import org.codehaus.groovy.ast.AnnotatedNode;
-import org.codehaus.groovy.ast.AnnotationNode;
 import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
 import org.codehaus.groovy.ast.ClassHelper;
 import org.codehaus.groovy.ast.ClassNode;
@@ -274,6 +273,7 @@ public class StaticCompilationVisitor extends 
StaticTypeCheckingVisitor {
                 Expression receiver = fieldNode.isStatic() ? classX(node) : 
varX(param);
                 Statement body = returnS(attrX(receiver, 
constX(fieldNode.getName())));
                 MethodNode accessor = node.addMethod("pfaccess$" + acc, 
modifiers, fieldNode.getOriginType(), new Parameter[]{param}, 
ClassNode.EMPTY_ARRAY, body);
+                accessor.setNodeMetaData(STATIC_COMPILE_NODE, Boolean.TRUE);
                 privateFieldAccessors.put(fieldNode.getName(), accessor);
             }
             if (generateMutator) {
@@ -284,6 +284,7 @@ public class StaticCompilationVisitor extends 
StaticTypeCheckingVisitor {
                 Parameter value = new Parameter(fieldNode.getOriginType(), 
"$value");
                 Statement body = assignS(attrX(receiver, 
constX(fieldNode.getName())), varX(value));
                 MethodNode mutator = node.addMethod("pfaccess$0" + acc, 
modifiers, fieldNode.getOriginType(), new Parameter[]{param, value}, 
ClassNode.EMPTY_ARRAY, body);
+                mutator.setNodeMetaData(STATIC_COMPILE_NODE, Boolean.TRUE);
                 privateFieldMutators.put(fieldNode.getName(), mutator);
             }
         }
@@ -376,8 +377,8 @@ public class StaticCompilationVisitor extends 
StaticTypeCheckingVisitor {
                 if (origGenericsTypes != null) {
                     
bridge.setGenericsTypes(applyGenericsContextToPlaceHolders(genericsSpec, 
origGenericsTypes));
                 }
+                bridge.setNodeMetaData(STATIC_COMPILE_NODE, Boolean.TRUE);
                 privateBridgeMethods.put(method, bridge);
-                bridge.addAnnotation(new 
AnnotationNode(COMPILESTATIC_CLASSNODE));
             }
         }
         if (!privateBridgeMethods.isEmpty()) {
diff --git 
a/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7304.groovy 
b/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7304.groovy
new file mode 100644
index 0000000..13b5222
--- /dev/null
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7304.groovy
@@ -0,0 +1,42 @@
+/*
+ *  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 org.codehaus.groovy.classgen.asm.sc.bugs
+
+import groovy.transform.CompileStatic
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+@CompileStatic
+final class Groovy7304 {
+
+    @Test // bridge methods should also be statically compiled
+    void testShouldGoThroughPrivateBridgeMethod() {
+        assertScript '''
+            class A {
+                private int i = 1
+                @groovy.transform.CompileStatic
+                int m() { new String().with { i++ } }
+            }
+            class B extends A {
+            }
+            assert new B().m() == 1
+        '''
+    }
+}

Reply via email to