groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mirco <mirch...@gmail.com>
Subject Set package name with a CompilationCustomizer
Date Sat, 01 Apr 2017 09:33:50 GMT
Hi all,
I'm trying to create a CompilationCustomizer that automatically inject a
package name into a script executed with the usual GroovyShell.
Below you can find the code to reproduce my failed attempts with the
groovyConsole.
Anyone can suggest me the right way to do such thing?

Thank you


*import org.codehaus.groovy.control.customizers.CompilationCustomizer;*
*import org.codehaus.groovy.ast.ClassHelper;*
*import org.codehaus.groovy.ast.ClassNode;*
*import org.codehaus.groovy.ast.ModuleNode;*
*import org.codehaus.groovy.classgen.GeneratorContext;*
*import org.codehaus.groovy.control.CompilationFailedException;*
*import org.codehaus.groovy.control.CompilePhase;*
*import org.codehaus.groovy.control.SourceUnit;*

*import java.util.*;*

*//import org.codehaus.groovy.syntax.ASTHelper*
*import org.codehaus.groovy.ast.AnnotationNode;*
*import org.codehaus.groovy.ast.PackageNode;*

*public class PackageCustomizer extends CompilationCustomizer {*

*    private String packageName;*

*    public PackageCustomizer() {*
*        super(CompilePhase.CONVERSION);*
*    }*

*    @Override*
*    public void call(final SourceUnit source, final GeneratorContext
context, final ClassNode classNode) throws CompilationFailedException {*
*        final ModuleNode ast = source.getAST();*
*        if (packageName != "" && !ast.hasPackageName()) {
//ast.getPackageName() == null) {*

*            /*ast.setPackageName(packageName)*
*            println ast.getPackageName()*
*            println ast.getPackage()*/*

*            List<AnnotationNode> annotations = new
ArrayList<AnnotationNode>()*
*            PackageNode packageNode = new PackageNode(packageName);*
*            packageNode.addAnnotations(annotations);*
*            ast.setPackage(packageNode)*

*            //ASTHelper helper = new ASTHelper(source, null)*
*            //helper.setPackageName(packageName)*
*        }*
*    }*

*     public PackageCustomizer setPackageName(final String name) {*
*        packageName = name*
*        return this;*
*    }*

*}*

*import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer*
*import org.codehaus.groovy.control.CompilerConfiguration*
*import groovy.transform.Canonical*

*def conf = new CompilerConfiguration()*
*conf.addCompilationCustomizers(new ASTTransformationCustomizer(Canonical))*
*PackageCustomizer packageCustomizer = new PackageCustomizer()*
*packageCustomizer.setPackageName("pippo.pluto.paperino")*
*conf.addCompilationCustomizers(packageCustomizer) *

*def shell = new GroovyShell(conf)*
*shell.evaluate '''*
*    //package pippo.pluto.paperino*

*    class Character {*
*        String name*
*    }*

*    def character = new Character('Pippo')*

*    println Character.getCanonicalName()*
*    println character.toString()*

*    assert character.toString() == 'pippo.pluto.paperino.Character(Pippo)'*
*    assert this.toString() == 'pippo.pluto.paperino.Script1()'*
*'''*

Mime
View raw message