Repository: groovy
Updated Branches:
  refs/heads/master adf7e9219 -> ae74e1d62


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

Use `CompilationCustomizer` to enable compileStatic


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

Branch: refs/heads/master
Commit: ae74e1d6270518144e45b54a5a8b892a3ce4781f
Parents: adf7e92
Author: sunlan <sun...@apache.org>
Authored: Fri Apr 13 16:59:45 2018 +0800
Committer: sunlan <sun...@apache.org>
Committed: Fri Apr 13 16:59:45 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/ae74e1d6/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 d2d0a38..06a4b1f 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;
@@ -189,6 +194,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,
@@ -934,4 +941,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/ae74e1d6/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 b383f7b..b530c20 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
@@ -1187,8 +1187,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);
         }
@@ -1259,26 +1257,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);
     }
@@ -4767,8 +4745,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());
@@ -4796,6 +4773,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");
 }

Reply via email to