Search in sources :

Example 21 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project groovy by apache.

the class Java5 method configureAnnotation.

public void configureAnnotation(AnnotationNode node) {
    ClassNode type = node.getClassNode();
    List<AnnotationNode> annotations = type.getAnnotations();
    for (AnnotationNode an : annotations) {
        configureAnnotationFromDefinition(an, node);
    }
    configureAnnotationFromDefinition(node, node);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode)

Example 22 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project ratpack by ratpack.

the class ScriptEngine method createClassLoader.

private GroovyClassLoader createClassLoader(final Path scriptPath) {
    final CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
    if (!scriptBaseClass.equals(Script.class)) {
        compilerConfiguration.setScriptBaseClass(scriptBaseClass.getName());
    }
    compilerConfiguration.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {

        @Override
        public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
            if (staticCompile) {
                classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
            }
            classNode.addAnnotation(new AnnotationNode(new ClassNode(InheritConstructors.class)));
            if (scriptPath != null) {
                AnnotationNode scriptPathAnnotation = new AnnotationNode(new ClassNode(ScriptPath.class));
                scriptPathAnnotation.addMember("value", new ConstantExpression(scriptPath.toUri().toString()));
                classNode.addAnnotation(scriptPathAnnotation);
            }
        }
    });
    return new GroovyClassLoader(parentLoader, compilerConfiguration) {

        @Override
        protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
            return new CompilationUnit(config, source, this) {

                {
                    verifier = new Verifier() {

                        @Override
                        public void visitClass(ClassNode node) {
                            if (node.implementsInterface(ClassHelper.GENERATED_CLOSURE_Type)) {
                                AnnotationNode lineNumberAnnotation = new AnnotationNode(LINE_NUMBER_CLASS_NODE);
                                lineNumberAnnotation.addMember("value", new ConstantExpression(node.getLineNumber(), true));
                                node.addAnnotation(lineNumberAnnotation);
                            }
                            super.visitClass(node);
                        }
                    };
                }
            };
        }
    };
}
Also used : Script(groovy.lang.Script) ClassNode(org.codehaus.groovy.ast.ClassNode) InheritConstructors(groovy.transform.InheritConstructors) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) CompileStatic(groovy.transform.CompileStatic) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) CodeSource(java.security.CodeSource) Verifier(org.codehaus.groovy.classgen.Verifier) GroovyClassLoader(groovy.lang.GroovyClassLoader) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) CompilationCustomizer(org.codehaus.groovy.control.customizers.CompilationCustomizer)

Example 23 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project hale by halestudio.

the class VertexEntityTransformation method processClass.

/**
 * Process a class and make it a vertex entity class.
 *
 * @param clazz the class node
 */
private void processClass(ClassNode clazz) {
    // check if class already was processed (has field v)
    if (clazz.getField("v") != null) {
        return;
    }
    // find all classes annotated with @ODocumentEntity
    List<AnnotationNode> entityAnnotations = clazz.getAnnotations(VERTEX_ENTITY_CLASS);
    if (entityAnnotations != null && !entityAnnotations.isEmpty()) {
        Expression entityName = entityAnnotations.get(0).getMember("value");
        Expression typeProperty = entityAnnotations.get(0).getMember("typeProperty");
        if (typeProperty == null) {
            // default value if none given
            typeProperty = new ConstantExpression("_type");
        }
        Expression superEntityName = null;
        FieldNode vertexField = null;
        FieldNode graphField = null;
        ClassNode superClass = clazz.getSuperClass();
        if (superClass != null) {
            List<AnnotationNode> superAnnotations = superClass.getAnnotations(VERTEX_ENTITY_CLASS);
            if (superAnnotations != null && !superAnnotations.isEmpty()) {
                // super class is also a vertex entity
                superEntityName = superAnnotations.get(0).getMember("value");
                // super class must be processed first
                processClass(superClass);
                // use fields from super class
                vertexField = clazz.getField("v");
                graphField = clazz.getField("g");
            } else {
                superClass = null;
            }
        }
        // add the vertex field
        if (vertexField == null) {
            vertexField = clazz.addField("v", Modifier.PROTECTED, VERTEX_CLASS, null);
        }
        // add the graph field
        if (graphField == null) {
            graphField = clazz.addField("g", Modifier.PROTECTED, GRAPH_CLASS, null);
        }
        // add constructor
        clazz.addConstructor(buildVertexGraphConstructor(vertexField, graphField, superClass, typeProperty, entityName));
        Map<String, Expression> initialExpressions = new HashMap<>();
        // get all non-static properties
        List<PropertyNode> properties = AbstractASTTransformUtil.getInstanceProperties(clazz);
        List<PropertyNode> newProperties = new ArrayList<>();
        for (PropertyNode property : properties) {
            // collect initial expressions for create function
            if (property.getField().getInitialExpression() != null) {
                initialExpressions.put(property.getName(), property.getInitialExpression());
            }
            // add static findByX method
            clazz.addMethod(buildFindByMethod(clazz, entityName, typeProperty, property.getName(), property.getType()));
            // add static findUniqueByX method
            clazz.addMethod(buildGetByMethod(clazz, entityName, typeProperty, property.getName(), property.getType()));
            // update property
            property.setGetterBlock(createGetter(property.getName(), vertexField, property.getType(), property.getField().getInitialExpression()));
            property.setSetterBlock(createSetter(property.getName(), vertexField));
            newProperties.add(property);
        }
        // readd updated properties
        for (PropertyNode property : newProperties) {
            readdProperty(clazz, property);
        }
        // add the vertex getter
        // 
        clazz.addMethod(// 
        "getV", // 
        Modifier.PUBLIC, // 
        VERTEX_CLASS, new Parameter[0], new ClassNode[0], new ReturnStatement(new FieldExpression(vertexField)));
        // add the graph getter
        // 
        clazz.addMethod(// 
        "getG", // 
        Modifier.PUBLIC, // 
        GRAPH_CLASS, new Parameter[0], new ClassNode[0], new ReturnStatement(new FieldExpression(graphField)));
        // add the id getter
        clazz.addMethod("getId", Modifier.PUBLIC, ClassHelper.OBJECT_TYPE, new Parameter[0], new ClassNode[0], new ReturnStatement(new MethodCallExpression(new FieldExpression(vertexField), "getId", new ArgumentListExpression())));
        // add delete method
        clazz.addMethod(buildDeleteMethod(vertexField, graphField));
        // add static create method
        clazz.addMethod(buildCreateMethod(clazz, entityName, initialExpressions));
        // add static findAll method
        clazz.addMethod(buildFindAllMethod(clazz, entityName, typeProperty));
        // add static getById method
        clazz.addMethod(buildGetByIdMethod(clazz));
        // add static initGraph method
        clazz.addMethod(buildInitGraphMethod(entityName, superEntityName));
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) HashMap(java.util.HashMap) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArrayList(java.util.ArrayList) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Example 24 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project ratpack by ratpack.

the class GroovySnippetExecuter method execute.

@Override
public void execute(TestCodeSnippet snippet) throws Exception {
    CompilerConfiguration config = new CompilerConfiguration();
    config.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CONVERSION) {

        @Override
        public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
            if (compileStatic) {
                classNode.addAnnotation(new AnnotationNode(new ClassNode(CompileStatic.class)));
            }
        }
    });
    ClassLoader classLoader = new URLClassLoader(new URL[] {}, getClass().getClassLoader());
    GroovyShell groovyShell = new GroovyShell(classLoader, new Binding(), config);
    List<String> importsAndSnippet = extractImports(snippet.getSnippet());
    String imports = importsAndSnippet.get(0);
    String snippetMinusImports = fixture.transform(importsAndSnippet.get(1));
    String fullSnippet = imports + fixture.pre() + snippetMinusImports + fixture.post();
    Script script;
    try {
        script = groovyShell.parse(fullSnippet, snippet.getClassName());
    } catch (MultipleCompilationErrorsException e) {
        Message error = e.getErrorCollector().getError(0);
        if (error instanceof SyntaxErrorMessage) {
            // noinspection ThrowableResultOfMethodCallIgnored
            System.out.println(snippet.getSnippet());
            throw new CompileException(e, ((SyntaxErrorMessage) error).getCause().getLine());
        } else {
            throw e;
        }
    }
    ClassLoader previousContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(groovyShell.getClassLoader());
        fixture.around(script::run);
    } finally {
        Thread.currentThread().setContextClassLoader(previousContextClassLoader);
    }
}
Also used : Binding(groovy.lang.Binding) ClassNode(org.codehaus.groovy.ast.ClassNode) Script(groovy.lang.Script) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) CompileStatic(groovy.transform.CompileStatic) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) GroovyShell(groovy.lang.GroovyShell) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) CompilationCustomizer(org.codehaus.groovy.control.customizers.CompilationCustomizer)

Example 25 with AnnotationNode

use of org.codehaus.groovy.ast.AnnotationNode in project grails-core by grails.

the class MixinTransformation method visit.

public void visit(ASTNode[] astNodes, SourceUnit source) {
    if (!(astNodes[0] instanceof AnnotationNode) || !(astNodes[1] instanceof AnnotatedNode)) {
        throw new RuntimeException("Internal error: wrong types: $node.class / $parent.class");
    }
    AnnotatedNode parent = (AnnotatedNode) astNodes[1];
    AnnotationNode node = (AnnotationNode) astNodes[0];
    if (!MY_TYPE.equals(node.getClassNode()) || !(parent instanceof ClassNode)) {
        return;
    }
    ClassNode classNode = (ClassNode) parent;
    String cName = classNode.getName();
    if (classNode.isInterface()) {
        throw new RuntimeException("Error processing interface '" + cName + "'. " + MY_TYPE_NAME + " not allowed for interfaces.");
    }
    ListExpression values = getListOfClasses(node);
    weaveMixinsIntoClass(classNode, values);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Aggregations

AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)248 ClassNode (org.codehaus.groovy.ast.ClassNode)158 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)78 Expression (org.codehaus.groovy.ast.expr.Expression)68 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)64 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)60 MethodNode (org.codehaus.groovy.ast.MethodNode)57 FieldNode (org.codehaus.groovy.ast.FieldNode)54 ArrayList (java.util.ArrayList)53 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)49 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)48 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)44 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)36 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)35 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)30 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)30 Parameter (org.codehaus.groovy.ast.Parameter)29 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)29 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)29 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)28