groovy git commit: Refine "GROOVY-8543: Support setting compileStatic by default via system properties"

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

groovy git commit: Refine "GROOVY-8543: Support setting compileStatic by default via system properties"

Daniel.Sun
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X e89cf7ba7 -> 289cd1b90


Refine "GROOVY-8543: Support setting compileStatic by default via system properties"

Use `CompilationCustomizer` to enable compileStatic

(cherry picked from commit ae74e1d)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/289cd1b9
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/289cd1b9
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/289cd1b9

Branch: refs/heads/GROOVY_2_6_X
Commit: 289cd1b904c39d10a95b63b51f4fca0ab3fc3e16
Parents: e89cf7b
Author: sunlan <[hidden email]>
Authored: Fri Apr 13 16:59:45 2018 +0800
Committer: sunlan <[hidden email]>
Committed: Fri Apr 13 17:19:38 2018 +0800

----------------------------------------------------------------------
 .../groovy/control/CompilerConfiguration.java   | 51 ++++++++++++++++++++
 .../apache/groovy/parser/antlr4/AstBuilder.java | 27 +----------
 2 files changed, 52 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/289cd1b9/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java b/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java
index 28ac3e0..293d8da 100644
--- a/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java
+++ b/src/main/java/org/codehaus/groovy/control/CompilerConfiguration.java
@@ -19,6 +19,11 @@
 package org.codehaus.groovy.control;
 
 import org.apache.groovy.util.Maps;
+import org.codehaus.groovy.ast.AnnotationNode;
+import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
+import org.codehaus.groovy.ast.ClassHelper;
+import org.codehaus.groovy.ast.ClassNode;
+import org.codehaus.groovy.classgen.GeneratorContext;
 import org.codehaus.groovy.control.customizers.CompilationCustomizer;
 import org.codehaus.groovy.control.io.NullWriter;
 import org.codehaus.groovy.control.messages.WarningMessage;
@@ -194,6 +199,8 @@ public class CompilerConfiguration {
 
     private final List<CompilationCustomizer> compilationCustomizers = new LinkedList<CompilationCustomizer>();
 
+    private static final boolean COMPILE_STATIC_BY_DEFAULT = Boolean.getBoolean("groovy.compile.static.by.default");
+
     /**
      * Sets a list of global AST transformations which should not be loaded even if they are
      * defined in META-INF/org.codehaus.groovy.transform.ASTTransformation files. By default,
@@ -935,4 +942,48 @@ public class CompilerConfiguration {
 
         return indyEnabled;
     }
+
+    private void enableCompileStaticByDefault() {
+        if (!COMPILE_STATIC_BY_DEFAULT) {
+            return;
+        }
+
+        compilationCustomizers.add(
+                new CompilationCustomizer(CompilePhase.CONVERSION) {
+                    @Override
+                    public void call(final SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
+                        for (ClassNode cn : source.getAST().getClasses()) {
+                            new ClassCodeVisitorSupport() {
+                                @Override
+                                public void visitClass(ClassNode node) {
+                                    enableCompileStatic(node);
+                                }
+
+                                private void enableCompileStatic(ClassNode classNode) {
+                                    if (!classNode.getAnnotations(ClassHelper.make(GROOVY_TRANSFORM_COMPILE_STATIC)).isEmpty()) {
+                                        return;
+                                    }
+
+                                    if (!classNode.getAnnotations(ClassHelper.make(GROOVY_TRANSFORM_COMPILE_DYNAMIC)).isEmpty()) {
+                                        return;
+                                    }
+
+                                    classNode.addAnnotation(new AnnotationNode(ClassHelper.make(GROOVY_TRANSFORM_COMPILE_STATIC)));
+                                }
+
+                                @Override
+                                protected SourceUnit getSourceUnit() {
+                                    return source;
+                                }
+
+                                private static final String GROOVY_TRANSFORM_COMPILE_STATIC = "groovy.transform.CompileStatic";
+                                private static final String GROOVY_TRANSFORM_COMPILE_DYNAMIC = "groovy.transform.CompileDynamic";
+                            }.visitClass(cn);
+                        }
+                    }
+                }
+        );
+
+    }
+    { enableCompileStaticByDefault(); }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/289cd1b9/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index 95243c0..17eb878 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -1202,8 +1202,6 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
         classNode.putNodeMetaData(CLASS_NAME, className);
         classNode.setSyntheticPublic(syntheticPublic);
 
-        enableCompileStaticByDefault(classNode);
-
         if (asBoolean(ctx.TRAIT())) {
             attachTraitAnnotation(classNode);
         }
@@ -1274,26 +1272,6 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
         return classNode;
     }
 
-    private void enableCompileStaticByDefault(ClassNode classNode) {
-        if (!COMPILE_STATIC_BY_DEFAULT) {
-            return;
-        }
-
-        if (!classNode.getAnnotations(ClassHelper.make(GROOVY_TRANSFORM_COMPILE_STATIC)).isEmpty()) {
-            return;
-        }
-
-        if (!classNode.getAnnotations(ClassHelper.make(GROOVY_TRANSFORM_COMPILE_DYNAMIC)).isEmpty()) {
-            return;
-        }
-
-        attachCompileStaticAnnotation(classNode);
-    }
-
-    private void attachCompileStaticAnnotation(ClassNode classNode) {
-        attachAnnotation(classNode, GROOVY_TRANSFORM_COMPILE_STATIC);
-    }
-
     private void attachTraitAnnotation(ClassNode classNode) {
         attachAnnotation(classNode, GROOVY_TRANSFORM_TRAIT);
     }
@@ -4864,8 +4842,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
 
     private static final String PACKAGE_INFO = "package-info";
     private static final String PACKAGE_INFO_FILE_NAME = PACKAGE_INFO + ".groovy";
-    private static final String GROOVY_TRANSFORM_COMPILE_STATIC = "groovy.transform.CompileStatic";
-    private static final String GROOVY_TRANSFORM_COMPILE_DYNAMIC = "groovy.transform.CompileDynamic";
+
     private static final String GROOVY_TRANSFORM_TRAIT = "groovy.transform.Trait";
     private static final Set<String> PRIMITIVE_TYPE_SET = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("boolean", "char", "byte", "short", "int", "long", "float", "double")));
     private static final Logger LOGGER = Logger.getLogger(AstBuilder.class.getName());
@@ -4893,6 +4870,4 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
     private static final String ENCLOSING_INSTANCE_EXPRESSION = "_ENCLOSING_INSTANCE_EXPRESSION";
 
     private static final String CLASS_NAME = "_CLASS_NAME";
-
-    private static final boolean COMPILE_STATIC_BY_DEFAULT = Boolean.getBoolean("groovy.compile.static.by.default");
 }