Skip to content

Commit

Permalink
Refine "GROOVY-8543: Support setting compileStatic by default via sys…
Browse files Browse the repository at this point in the history
…tem properties"

Use `CompilationCustomizer` to enable compileStatic
  • Loading branch information
daniellansun committed Apr 13, 2018
1 parent adf7e92 commit ae74e1d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -934,4 +941,48 @@ public boolean isIndyEnabled() {

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(); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -1187,8 +1187,6 @@ public ClassNode visitClassDeclaration(ClassDeclarationContext ctx) {
classNode.putNodeMetaData(CLASS_NAME, className);
classNode.setSyntheticPublic(syntheticPublic);

enableCompileStaticByDefault(classNode);

if (asBoolean(ctx.TRAIT())) {
attachTraitAnnotation(classNode);
}
Expand Down Expand Up @@ -1259,26 +1257,6 @@ public ClassNode visitClassDeclaration(ClassDeclarationContext ctx) {
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);
}
Expand Down Expand Up @@ -4767,8 +4745,7 @@ public List<DeclarationExpression> getDeclarationExpressions() {

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());
Expand Down Expand Up @@ -4796,6 +4773,4 @@ public List<DeclarationExpression> getDeclarationExpressions() {
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");
}

0 comments on commit ae74e1d

Please sign in to comment.