Search in sources :

Example 26 with ConstantExpression

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

the class Java5 method setMethodDefaultValue.

private static void setMethodDefaultValue(MethodNode mn, Method m) {
    Object defaultValue = m.getDefaultValue();
    ConstantExpression cExp = ConstantExpression.NULL;
    if (defaultValue != null)
        cExp = new ConstantExpression(defaultValue);
    mn.setCode(new ReturnStatement(cExp));
    mn.setAnnotationDefault(true);
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Example 27 with ConstantExpression

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

the class Java5 method annotationValueToExpression.

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;
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Example 28 with ConstantExpression

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

the class GrabAnnotationTransformation method checkForConvenienceForm.

private static void checkForConvenienceForm(AnnotationNode node, boolean exclude) {
    Object val = node.getMember("value");
    if (val == null || !(val instanceof ConstantExpression))
        return;
    Object allParts = ((ConstantExpression) val).getValue();
    if (!(allParts instanceof String))
        return;
    String allstr = (String) allParts;
    // strip off trailing attributes
    boolean done = false;
    while (!done) {
        Matcher attrs = ATTRIBUTES_PATTERN.matcher(allstr);
        if (attrs.find()) {
            String attrName = attrs.group(2);
            String attrValue = attrs.group(3);
            if (attrName == null || attrValue == null)
                continue;
            boolean isBool = GRAB_BOOLEAN.contains(attrName);
            ConstantExpression value = constX(isBool ? Boolean.valueOf(attrValue) : attrValue);
            value.setSourcePosition(node);
            node.addMember(attrName, value);
            int lastSemi = allstr.lastIndexOf(';');
            if (lastSemi == -1) {
                allstr = "";
                break;
            }
            allstr = allstr.substring(0, lastSemi);
        } else {
            done = true;
        }
    }
    if (allstr.contains("#")) {
        // see: http://ant.apache.org/ivy/history/latest-milestone/textual.html
        Matcher m = IVY_PATTERN.matcher(allstr);
        if (!m.find())
            return;
        if (m.group(1) == null || m.group(2) == null)
            return;
        node.addMember("module", constX(m.group(2)));
        node.addMember("group", constX(m.group(1)));
        if (m.group(6) != null)
            node.addMember("conf", constX(m.group(6)));
        if (m.group(4) != null)
            node.addMember("version", constX(m.group(4)));
        else if (!exclude && node.getMember("version") == null)
            node.addMember("version", constX("*"));
        node.getMembers().remove("value");
    } else if (allstr.contains(":")) {
        // assume gradle syntax
        // see: http://www.gradle.org/latest/docs/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
        Map<String, Object> parts = GrapeUtil.getIvyParts(allstr);
        for (String key : parts.keySet()) {
            String value = parts.get(key).toString();
            if (!key.equals("version") || !value.equals("*") || !exclude) {
                node.addMember(key, constX(value));
            }
        }
        node.getMembers().remove("value");
    }
}
Also used : Matcher(java.util.regex.Matcher) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Map(java.util.Map) HashMap(java.util.HashMap)

Example 29 with ConstantExpression

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

the class AbstractASTTransformation method getMemberStringList.

public static List<String> getMemberStringList(AnnotationNode anno, String name) {
    Expression expr = anno.getMember(name);
    if (expr == null) {
        return null;
    }
    if (expr instanceof ListExpression) {
        List<String> list = new ArrayList<String>();
        final ListExpression listExpression = (ListExpression) expr;
        if (isUndefinedMarkerList(listExpression)) {
            return null;
        }
        for (Expression itemExpr : listExpression.getExpressions()) {
            if (itemExpr != null && itemExpr instanceof ConstantExpression) {
                Object value = ((ConstantExpression) itemExpr).getValue();
                if (value != null)
                    list.add(value.toString());
            }
        }
        return list;
    }
    return tokenize(getMemberStringValue(anno, name));
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArrayList(java.util.ArrayList)

Example 30 with ConstantExpression

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

the class JavaStubGenerator method printField.

private void printField(PrintWriter out, FieldNode fieldNode, boolean isInterface) {
    if ((fieldNode.getModifiers() & Opcodes.ACC_PRIVATE) != 0)
        return;
    printAnnotations(out, fieldNode);
    if (!isInterface) {
        printModifiers(out, fieldNode.getModifiers());
    }
    ClassNode type = fieldNode.getType();
    printType(out, type);
    out.print(" ");
    out.print(fieldNode.getName());
    if (isInterface || (fieldNode.getModifiers() & Opcodes.ACC_FINAL) != 0) {
        out.print(" = ");
        Expression valueExpr = fieldNode.getInitialValueExpression();
        if (valueExpr instanceof ConstantExpression) {
            valueExpr = Verifier.transformToPrimitiveConstantIfPossible((ConstantExpression) valueExpr);
        }
        if (valueExpr instanceof ConstantExpression && fieldNode.isStatic() && fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(valueExpr.getType()) && valueExpr.getType().equals(fieldNode.getType())) {
            // GROOVY-5150 : Initialize value with a dummy constant so that Java cross compiles correctly
            if (ClassHelper.STRING_TYPE.equals(valueExpr.getType())) {
                out.print(formatString(valueExpr.getText()));
            } else if (ClassHelper.char_TYPE.equals(valueExpr.getType())) {
                out.print("'" + valueExpr.getText() + "'");
            } else {
                ClassNode constantType = valueExpr.getType();
                out.print('(');
                printType(out, type);
                out.print(") ");
                out.print(valueExpr.getText());
                if (ClassHelper.Long_TYPE.equals(ClassHelper.getWrapper(constantType)))
                    out.print('L');
            }
        } else if (ClassHelper.isPrimitiveType(type)) {
            String val = type == ClassHelper.boolean_TYPE ? "false" : "0";
            out.print("new " + ClassHelper.getWrapper(type) + "((" + type + ")" + val + ")");
        } else {
            out.print("null");
        }
    }
    out.println(";");
}
Also used : 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) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Aggregations

ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)153 Expression (org.codehaus.groovy.ast.expr.Expression)90 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)86 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)71 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)69 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)63 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)58 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)54 ClassNode (org.codehaus.groovy.ast.ClassNode)50 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)41 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)41 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)39 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)39 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)37 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)36 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)34 Statement (org.codehaus.groovy.ast.stmt.Statement)33 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)32 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)31 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)29