Search in sources :

Example 46 with ListExpression

use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.

the class MixinASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotationNode node = (AnnotationNode) nodes[0];
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    final Expression expr = node.getMember("value");
    if (expr == null) {
        return;
    }
    Expression useClasses = null;
    if (expr instanceof ClassExpression) {
        useClasses = expr;
    } else if (expr instanceof ListExpression) {
        ListExpression listExpression = (ListExpression) expr;
        for (Expression ex : listExpression.getExpressions()) {
            if (!(ex instanceof ClassExpression))
                return;
        }
        useClasses = expr;
    }
    if (useClasses == null)
        return;
    if (parent instanceof ClassNode) {
        ClassNode annotatedClass = (ClassNode) parent;
        final Parameter[] noparams = Parameter.EMPTY_ARRAY;
        MethodNode clinit = annotatedClass.getDeclaredMethod("<clinit>", noparams);
        if (clinit == null) {
            clinit = annotatedClass.addMethod("<clinit>", ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.VOID_TYPE, noparams, null, new BlockStatement());
            clinit.setSynthetic(true);
        }
        final BlockStatement code = (BlockStatement) clinit.getCode();
        code.addStatement(stmt(callX(propX(classX(annotatedClass), "metaClass"), "mixin", useClasses)));
    }
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 47 with ListExpression

use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.

the class EnumHelper method addEnumConstant.

public static FieldNode addEnumConstant(ClassNode enumClass, String name, Expression init) {
    int modifiers = PUBLIC_FS | Opcodes.ACC_ENUM;
    if (init != null && !(init instanceof ListExpression)) {
        ListExpression list = new ListExpression();
        list.addExpression(init);
        init = list;
    }
    FieldNode fn = new FieldNode(name, modifiers, enumClass.getPlainNodeReference(), enumClass, init);
    enumClass.addField(fn);
    return fn;
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression)

Example 48 with ListExpression

use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.

the class JavaStubGenerator method getAnnotationValue.

private String getAnnotationValue(Object memberValue) {
    String val = "null";
    if (memberValue instanceof ListExpression) {
        StringBuilder sb = new StringBuilder("{");
        boolean first = true;
        ListExpression le = (ListExpression) memberValue;
        for (Expression e : le.getExpressions()) {
            if (first)
                first = false;
            else
                sb.append(",");
            sb.append(getAnnotationValue(e));
        }
        sb.append("}");
        val = sb.toString();
    } else if (memberValue instanceof ConstantExpression) {
        ConstantExpression ce = (ConstantExpression) memberValue;
        Object constValue = ce.getValue();
        if (constValue instanceof AnnotationNode) {
            StringWriter writer = new StringWriter();
            PrintWriter out = new PrintWriter(writer);
            printAnnotation(out, (AnnotationNode) constValue);
            val = writer.toString();
        } else if (constValue instanceof Number || constValue instanceof Boolean)
            val = constValue.toString();
        else
            val = "\"" + escapeSpecialChars(constValue.toString()) + "\"";
    } else if (memberValue instanceof PropertyExpression) {
        // assume must be static class field or enum value or class that Java can resolve
        val = ((Expression) memberValue).getText();
    } else if (memberValue instanceof VariableExpression) {
        val = ((Expression) memberValue).getText();
        //check for an alias
        ImportNode alias = currentModule.getStaticImports().get(val);
        if (alias != null)
            val = alias.getClassName() + "." + alias.getFieldName();
    } else if (memberValue instanceof ClosureExpression) {
        // annotation closure; replaced with this specific class literal to cover the
        // case where annotation type uses Class<? extends Closure> for the closure's type
        val = "groovy.lang.Closure.class";
    } else if (memberValue instanceof ClassExpression) {
        val = ((Expression) memberValue).getText() + ".class";
    }
    return val;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) StringWriter(java.io.StringWriter) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) 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) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) PrintWriter(java.io.PrintWriter)

Example 49 with ListExpression

use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.

the class Java5 method configureAnnotation.

private void configureAnnotation(AnnotationNode node, Annotation annotation) {
    Class type = annotation.annotationType();
    if (type == Retention.class) {
        Retention r = (Retention) annotation;
        RetentionPolicy value = r.value();
        setRetentionPolicy(value, node);
        node.setMember("value", new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(RetentionPolicy.class, false)), value.toString()));
    } else if (type == Target.class) {
        Target t = (Target) annotation;
        ElementType[] elements = t.value();
        ListExpression elementExprs = new ListExpression();
        for (ElementType element : elements) {
            elementExprs.addExpression(new PropertyExpression(new ClassExpression(ClassHelper.ELEMENT_TYPE_TYPE), element.name()));
        }
        node.setMember("value", elementExprs);
    } else {
        Method[] declaredMethods;
        try {
            declaredMethods = type.getDeclaredMethods();
        } catch (SecurityException se) {
            declaredMethods = new Method[0];
        }
        for (Method declaredMethod : declaredMethods) {
            try {
                Object value = declaredMethod.invoke(annotation);
                Expression valueExpression = annotationValueToExpression(value);
                if (valueExpression == null)
                    continue;
                node.setMember(declaredMethod.getName(), valueExpression);
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }
    }
}
Also used : ElementType(java.lang.annotation.ElementType) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Method(java.lang.reflect.Method) Retention(java.lang.annotation.Retention) RetentionPolicy(java.lang.annotation.RetentionPolicy) InvocationTargetException(java.lang.reflect.InvocationTargetException) Target(java.lang.annotation.Target) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 50 with ListExpression

use of org.codehaus.groovy.ast.expr.ListExpression in project groovy by apache.

the class Java5 method configureAnnotationFromDefinition.

public static void configureAnnotationFromDefinition(AnnotationNode definition, AnnotationNode root) {
    ClassNode type = definition.getClassNode();
    if ("java.lang.annotation.Retention".equals(type.getName())) {
        Expression exp = definition.getMember("value");
        if (!(exp instanceof PropertyExpression))
            return;
        PropertyExpression pe = (PropertyExpression) exp;
        String name = pe.getPropertyAsString();
        RetentionPolicy policy = RetentionPolicy.valueOf(name);
        setRetentionPolicy(policy, root);
    } else if ("java.lang.annotation.Target".equals(type.getName())) {
        Expression exp = definition.getMember("value");
        if (!(exp instanceof ListExpression))
            return;
        ListExpression le = (ListExpression) exp;
        int bitmap = 0;
        for (Expression e : le.getExpressions()) {
            if (!(e instanceof PropertyExpression))
                return;
            PropertyExpression element = (PropertyExpression) e;
            String name = element.getPropertyAsString();
            ElementType value = ElementType.valueOf(name);
            bitmap |= getElementCode(value);
        }
        root.setAllowedTargets(bitmap);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ElementType(java.lang.annotation.ElementType) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) RetentionPolicy(java.lang.annotation.RetentionPolicy)

Aggregations

ListExpression (org.codehaus.groovy.ast.expr.ListExpression)60 Expression (org.codehaus.groovy.ast.expr.Expression)44 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)41 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)37 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)27 ClassNode (org.codehaus.groovy.ast.ClassNode)25 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)19 ArrayList (java.util.ArrayList)16 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)15 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)15 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)14 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)12 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)12 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)11 FieldNode (org.codehaus.groovy.ast.FieldNode)7 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)7 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)6 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)6 AnnotationConstantExpression (org.codehaus.groovy.ast.expr.AnnotationConstantExpression)5 MapEntryExpression (org.codehaus.groovy.ast.expr.MapEntryExpression)5