Search in sources :

Example 66 with ClassExpression

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

the class DelegateAsyncTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        throw new GroovyBugError("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
    }
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode annotationNode = (AnnotationNode) nodes[0];
    if (parent instanceof ClassNode) {
        Expression value = annotationNode.getMember("value");
        if (value instanceof ClassExpression) {
            ClassNode targetApi = value.getType().getPlainNodeReference();
            ClassNode classNode = (ClassNode) parent;
            final String fieldName = '$' + Introspector.decapitalize(targetApi.getNameWithoutPackage());
            FieldNode fieldNode = classNode.getField(fieldName);
            if (fieldNode == null) {
                fieldNode = new FieldNode(fieldName, Modifier.PRIVATE, targetApi, classNode, new ConstructorCallExpression(targetApi, NO_ARGS));
                classNode.addField(fieldNode);
            }
            applyDelegateAsyncTransform(classNode, targetApi, fieldName);
        }
    } else if (parent instanceof FieldNode) {
        FieldNode fieldNode = (FieldNode) parent;
        ClassNode targetApi = fieldNode.getType().getPlainNodeReference();
        ClassNode classNode = fieldNode.getOwner();
        applyDelegateAsyncTransform(classNode, targetApi, fieldNode.getName());
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) 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) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 67 with ClassExpression

use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy-core by groovy.

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 || memberValue instanceof VariableExpression) {
        // assume must be static class field or enum value or class that Java can resolve
        val = ((Expression) memberValue).getText();
    } 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 68 with ClassExpression

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

the class MacroInvocationTrap method handleTargetMethodCallExpression.

@Override
protected boolean handleTargetMethodCallExpression(MethodCallExpression macroCall) {
    final ClosureExpression closureExpression = getClosureArgument(macroCall);
    if (closureExpression == null) {
        return true;
    }
    if (closureExpression.getParameters() != null && closureExpression.getParameters().length > 0) {
        addError("Macro closure arguments are not allowed", closureExpression);
        return true;
    }
    final MapExpression mapExpression = buildSubstitutionMap(closureExpression);
    String source = convertClosureToSource(closureExpression);
    BlockStatement closureBlock = (BlockStatement) closureExpression.getCode();
    Boolean asIs = false;
    TupleExpression macroArguments = getMacroArguments(macroCall);
    if (macroArguments == null) {
        return true;
    }
    List<Expression> macroArgumentsExpressions = macroArguments.getExpressions();
    if (macroArgumentsExpressions.size() == 2 || macroArgumentsExpressions.size() == 3) {
        Expression asIsArgumentExpression = macroArgumentsExpressions.get(macroArgumentsExpressions.size() - 2);
        if ((asIsArgumentExpression instanceof ConstantExpression)) {
            ConstantExpression asIsConstantExpression = (ConstantExpression) asIsArgumentExpression;
            if (!(asIsConstantExpression.getValue() instanceof Boolean)) {
                addError("AsIs argument value should be boolean", asIsConstantExpression);
                return true;
            }
            asIs = (Boolean) asIsConstantExpression.getValue();
        }
    }
    macroArgumentsExpressions.remove(macroArgumentsExpressions.size() - 1);
    macroArgumentsExpressions.add(new ConstantExpression(source));
    macroArgumentsExpressions.add(mapExpression);
    macroArgumentsExpressions.add(new ClassExpression(ClassHelper.makeWithoutCaching(MacroBuilder.getMacroValue(closureBlock, asIs).getClass(), false)));
    macroCall.setObjectExpression(new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(MacroBuilder.class, false)), "INSTANCE"));
    macroCall.setSpreadSafe(false);
    macroCall.setSafe(false);
    macroCall.setImplicitThis(false);
    return true;
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) 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) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) MacroBuilder(org.codehaus.groovy.macro.runtime.MacroBuilder) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 69 with ClassExpression

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

the class AnnotationVisitor method transformInlineConstants.

private Expression transformInlineConstants(Expression exp) {
    if (exp instanceof PropertyExpression) {
        PropertyExpression pe = (PropertyExpression) exp;
        if (pe.getObjectExpression() instanceof ClassExpression) {
            ClassExpression ce = (ClassExpression) pe.getObjectExpression();
            ClassNode type = ce.getType();
            if (type.isEnum() || !type.isResolved())
                return exp;
            try {
                Field field = type.getTypeClass().getField(pe.getPropertyAsString());
                if (field != null && Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) {
                    return new ConstantExpression(field.get(null));
                }
            } catch (Exception e) {
            // ignore, leave property expression in place and we'll report later
            }
        }
    } else if (exp instanceof ListExpression) {
        ListExpression le = (ListExpression) exp;
        ListExpression result = new ListExpression();
        for (Expression e : le.getExpressions()) {
            result.addExpression(transformInlineConstants(e));
        }
        return result;
    }
    return exp;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) Field(java.lang.reflect.Field) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 70 with ClassExpression

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

the class AnnotationVisitor method visitExpression.

protected void visitExpression(String attrName, Expression attrExp, ClassNode attrType) {
    if (attrType.isArray()) {
        // check needed as @Test(attr = {"elem"}) passes through the parser
        if (attrExp instanceof ListExpression) {
            ListExpression le = (ListExpression) attrExp;
            visitListExpression(attrName, le, attrType.getComponentType());
        } else if (attrExp instanceof ClosureExpression) {
            addError("Annotation list attributes must use Groovy notation [el1, el2]", attrExp);
        } else {
            // treat like a singleton list as per Java
            ListExpression listExp = new ListExpression();
            listExp.addExpression(attrExp);
            if (annotation != null) {
                annotation.setMember(attrName, listExp);
            }
            visitExpression(attrName, listExp, attrType);
        }
    } else if (ClassHelper.isPrimitiveType(attrType)) {
        visitConstantExpression(attrName, getConstantExpression(attrExp, attrType), ClassHelper.getWrapper(attrType));
    } else if (ClassHelper.STRING_TYPE.equals(attrType)) {
        visitConstantExpression(attrName, getConstantExpression(attrExp, attrType), ClassHelper.STRING_TYPE);
    } else if (ClassHelper.CLASS_Type.equals(attrType)) {
        if (!(attrExp instanceof ClassExpression || attrExp instanceof ClosureExpression)) {
            addError("Only classes and closures can be used for attribute '" + attrName + "'", attrExp);
        }
    } else if (attrType.isDerivedFrom(ClassHelper.Enum_Type)) {
        if (attrExp instanceof PropertyExpression) {
            visitEnumExpression(attrName, (PropertyExpression) attrExp, attrType);
        } else {
            addError("Expected enum value for attribute " + attrName, attrExp);
        }
    } else if (isValidAnnotationClass(attrType)) {
        if (attrExp instanceof AnnotationConstantExpression) {
            visitAnnotationExpression(attrName, (AnnotationConstantExpression) attrExp, attrType);
        } else {
            addError("Expected annotation of type '" + attrType.getName() + "' for attribute " + attrName, attrExp);
        }
    } else {
        addError("Unexpected type " + attrType.getName(), attrExp);
    }
}
Also used : AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Aggregations

ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)97 Expression (org.codehaus.groovy.ast.expr.Expression)59 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)57 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)55 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)52 ClassNode (org.codehaus.groovy.ast.ClassNode)48 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)41 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)40 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)37 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)33 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)31 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)27 MethodNode (org.codehaus.groovy.ast.MethodNode)18 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)17 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)17 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)17 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)16 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)16 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)16 FieldNode (org.codehaus.groovy.ast.FieldNode)15