Java Code Examples for org.codehaus.groovy.ast.expr.ListExpression#addExpression()

The following examples show how to use org.codehaus.groovy.ast.expr.ListExpression#addExpression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExpressionUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Given a list of constants, transform each item in the list.
 *
 * @param origList the list to transform
 * @param attrType the target type
 * @return the transformed list or the original if nothing was changed
 */
public static Expression transformListOfConstants(final ListExpression origList, final ClassNode attrType) {
    ListExpression newList = new ListExpression();
    boolean changed = false;
    for (Expression e : origList.getExpressions()) {
        try {
            Expression transformed = transformInlineConstants(e, attrType);
            newList.addExpression(transformed);
            if (transformed != e) changed = true;
        } catch(Exception ignored) {
            newList.addExpression(e);
        }
    }
    if (changed) {
        newList.setSourcePosition(origList);
        return newList;
    }
    return origList;
}
 
Example 2
Source File: Java8.java    From groovy with Apache License 2.0 6 votes vote down vote up
private Expression annotationValueToExpression (Object value) {
    if (value == null || value instanceof String || value instanceof Number || value instanceof Character || value instanceof Boolean)
        return new ConstantExpression(value);

    if (value instanceof Class)
        return new ClassExpression(ClassHelper.makeWithoutCaching((Class<?>)value));

    if (value.getClass().isArray()) {
        ListExpression elementExprs = new ListExpression();
        int len = Array.getLength(value);
        for (int i = 0; i != len; ++i)
            elementExprs.addExpression(annotationValueToExpression(Array.get(value, i)));
        return elementExprs;
    }

    return null;
}
 
Example 3
Source File: ExpressionUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * The attribute values of annotations must be primitive, String or Enum constants.
 * In various places, such constants can be seen during type resolution but won't be
 * readily accessible in later phases, e.g. they might be embedded into constructor code.
 * This method transforms constants that would appear in annotations early so they aren't lost.
 * Subsequent processing determines whether they are valid, this method simply retains
 * the constant value as a constant expression.
 *
 * @param exp the original expression
 * @return the converted expression
 */
public static Expression transformInlineConstants(final Expression exp) {
    if (exp instanceof PropertyExpression) {
        PropertyExpression pe = (PropertyExpression) exp;
        if (pe.getObjectExpression() instanceof ClassExpression) {
            ClassExpression ce = (ClassExpression) pe.getObjectExpression();
            ClassNode type = ce.getType();
            FieldNode field = ClassNodeUtils.getField(type, pe.getPropertyAsString());
            if (type.isEnum() && field != null && field.isEnum()) return exp;
            Expression constant = findConstant(field);
            if (constant != null) return constant;
        }
    } else if (exp instanceof BinaryExpression) {
        BinaryExpression be = (BinaryExpression) exp;
        be.setLeftExpression(transformInlineConstants(be.getLeftExpression()));
        be.setRightExpression(transformInlineConstants(be.getRightExpression()));
        return be;
    } else if (exp instanceof ListExpression) {
        ListExpression origList = (ListExpression) exp;
        ListExpression newList = new ListExpression();
        boolean changed = false;
        for (Expression e : origList.getExpressions()) {
            Expression transformed = transformInlineConstants(e);
            newList.addExpression(transformed);
            if (transformed != e) changed = true;
        }
        if (changed) {
            newList.setSourcePosition(origList);
            return newList;
        }
        return origList;
    }

    return exp;
}
 
Example 4
Source File: EnumHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: GeneralUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static ListExpression list2args(final List<?> args) {
    ListExpression result = new ListExpression();
    for (Object o : args) {
        result.addExpression(constX(o));
    }
    return result;
}
 
Example 6
Source File: GeneralUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static ListExpression classList2args(final List<String> args) {
    ListExpression result = new ListExpression();
    for (Object o : args) {
        result.addExpression(classX(ClassHelper.make(o.toString())));
    }
    return result;
}
 
Example 7
Source File: MacroGroovyMethods.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static ListExpression buildSubstitutions(final SourceUnit source, final ASTNode expr) {
    final ListExpression listExpression = new ListExpression();

    ClassCodeVisitorSupport visitor = new ClassCodeVisitorSupport() {
        @Override
        protected SourceUnit getSourceUnit() {
            return null;
        }

        @Override
        public void visitClass(final ClassNode node) {
            super.visitClass(node);
            Iterator<InnerClassNode> it = node.getInnerClasses();
            while (it.hasNext()) {
                InnerClassNode next = it.next();
                visitClass(next);
            }
        }

        @Override
        public void visitMethodCallExpression(MethodCallExpression call) {
            super.visitMethodCallExpression(call);

            if (DOLLAR_VALUE.equals(call.getMethodAsString())) {
                ClosureExpression substitutionClosureExpression = getClosureArgument(source, call);

                if (substitutionClosureExpression == null) {
                    return;
                }

                Statement code = substitutionClosureExpression.getCode();
                if (code instanceof BlockStatement) {
                    ((BlockStatement) code).setVariableScope(null);
                }

                listExpression.addExpression(substitutionClosureExpression);
            }
        }
    };
    if (expr instanceof ClassNode) {
        visitor.visitClass((ClassNode) expr);
    } else {
        expr.visit(visitor);
    }
    return listExpression;
}
 
Example 8
Source File: AnnotationVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
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);
    }
}