Search in sources :

Example 56 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 57 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 58 with ConstantExpression

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

the class StaticImportVisitor method transformMethodCallExpression.

protected Expression transformMethodCallExpression(MethodCallExpression mce) {
    Expression args = transform(mce.getArguments());
    Expression method = transform(mce.getMethod());
    Expression object = transform(mce.getObjectExpression());
    boolean isExplicitThisOrSuper = false;
    boolean isExplicitSuper = false;
    if (object instanceof VariableExpression) {
        VariableExpression ve = (VariableExpression) object;
        isExplicitThisOrSuper = !mce.isImplicitThis() && (ve.isThisExpression() || ve.isSuperExpression());
        isExplicitSuper = ve.isSuperExpression();
    }
    if (mce.isImplicitThis() || isExplicitThisOrSuper) {
        if (mce.isImplicitThis()) {
            Expression ret = findStaticMethodImportFromModule(method, args);
            if (ret != null) {
                setSourcePosition(ret, mce);
                return ret;
            }
            if (method instanceof ConstantExpression && !inLeftExpression) {
                // could be a closure field
                String methodName = (String) ((ConstantExpression) method).getValue();
                ret = findStaticFieldOrPropAccessorImportFromModule(methodName);
                if (ret != null) {
                    ret = new MethodCallExpression(ret, "call", args);
                    setSourcePosition(ret, mce);
                    return ret;
                }
            }
        } else if (currentMethod != null && currentMethod.isStatic() && isExplicitSuper) {
            MethodCallExpression ret = new MethodCallExpression(new ClassExpression(currentClass.getSuperClass()), method, args);
            setSourcePosition(ret, mce);
            return ret;
        }
        if (method instanceof ConstantExpression) {
            ConstantExpression ce = (ConstantExpression) method;
            Object value = ce.getValue();
            if (value instanceof String) {
                boolean foundInstanceMethod = false;
                String methodName = (String) value;
                boolean inInnerClass = isInnerClass(currentClass);
                if (currentMethod != null && !currentMethod.isStatic()) {
                    if (currentClass.hasPossibleMethod(methodName, args)) {
                        foundInstanceMethod = true;
                    }
                }
                boolean lookForPossibleStaticMethod = !methodName.equals("call");
                lookForPossibleStaticMethod &= !foundInstanceMethod;
                lookForPossibleStaticMethod |= inSpecialConstructorCall;
                lookForPossibleStaticMethod &= !inInnerClass;
                if (!inClosure && lookForPossibleStaticMethod && (hasPossibleStaticMethod(currentClass, methodName, args, true)) || hasPossibleStaticProperty(currentClass, methodName)) {
                    StaticMethodCallExpression smce = new StaticMethodCallExpression(currentClass, methodName, args);
                    setSourcePosition(smce, mce);
                    return smce;
                }
                if (!inClosure && inInnerClass && inSpecialConstructorCall && mce.isImplicitThis() && !foundInstanceMethod) {
                    if (currentClass.getOuterClass().hasPossibleMethod(methodName, args)) {
                        object = new PropertyExpression(new ClassExpression(currentClass.getOuterClass()), new ConstantExpression("this"));
                    } else if (hasPossibleStaticMethod(currentClass.getOuterClass(), methodName, args, true) || hasPossibleStaticProperty(currentClass.getOuterClass(), methodName)) {
                        StaticMethodCallExpression smce = new StaticMethodCallExpression(currentClass.getOuterClass(), methodName, args);
                        setSourcePosition(smce, mce);
                        return smce;
                    }
                }
            }
        }
    }
    MethodCallExpression result = new MethodCallExpression(object, method, args);
    result.setSafe(mce.isSafe());
    result.setImplicitThis(mce.isImplicitThis());
    result.setSpreadSafe(mce.isSpreadSafe());
    result.setMethodTarget(mce.getMethodTarget());
    // GROOVY-6757
    result.setGenericsTypes(mce.getGenericsTypes());
    setSourcePosition(result, mce);
    return result;
}
Also used : StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) 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) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 59 with ConstantExpression

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

the class AnnotationConstantsVisitor method transformConstantExpression.

private static Expression transformConstantExpression(Expression val, ClassNode returnType) {
    ClassNode returnWrapperType = ClassHelper.getWrapper(returnType);
    if (val instanceof ConstantExpression) {
        Expression result = revertType(val, returnWrapperType);
        if (result != null) {
            return result;
        }
        return val;
    }
    if (val instanceof CastExpression) {
        CastExpression castExp = (CastExpression) val;
        Expression castee = castExp.getExpression();
        if (castee instanceof ConstantExpression) {
            if (ClassHelper.getWrapper(castee.getType()).isDerivedFrom(returnWrapperType)) {
                return castee;
            }
            Expression result = revertType(castee, returnWrapperType);
            if (result != null) {
                return result;
            }
            return castee;
        }
    }
    return val;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 60 with ConstantExpression

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

the class OperandStack method pushDynamicName.

public void pushDynamicName(Expression name) {
    if (name instanceof ConstantExpression) {
        ConstantExpression ce = (ConstantExpression) name;
        Object value = ce.getValue();
        if (value instanceof String) {
            pushConstant(ce);
            return;
        }
    }
    new CastExpression(ClassHelper.STRING_TYPE, name).visit(controller.getAcg());
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Aggregations

ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)157 Expression (org.codehaus.groovy.ast.expr.Expression)92 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)88 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)73 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)72 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)67 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)59 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)55 ClassNode (org.codehaus.groovy.ast.ClassNode)51 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)43 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)42 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)41 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)35 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)33 Statement (org.codehaus.groovy.ast.stmt.Statement)33 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)31 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)29