Package org.codehaus.groovy.ast.expr

Examples of org.codehaus.groovy.ast.expr.ConstantExpression


        if (statement.getExpression() instanceof BinaryExpression) {
            BinaryExpression bexp = (BinaryExpression) statement.getExpression();
            if (bexp.getLeftExpression() instanceof MethodCallExpression) {
                MethodCallExpression mce = (MethodCallExpression) bexp.getLeftExpression();
                if (mce.getMethod() instanceof ConstantExpression) {
                    ConstantExpression methodName = (ConstantExpression) mce.getMethod();
                    if("where".equals(methodName.getValue()) || "and".equals(methodName.getValue())){
                        addError("If you are using parentheses with " + methodName.getValue() + " keyword wrap the whole expression in parentheses like '" + methodName.getValue() + "((a == 1 || b > 2) && c =~ d)'", statement);
                    }
                }
            }
        }
        super.visitExpressionStatement(statement);
View Full Code Here


        super.visitExpressionStatement(statement);
    }

    @Override public void visitMethodCallExpression(MethodCallExpression call) {
        if (call.getMethod() instanceof ConstantExpression) {
            ConstantExpression name = (ConstantExpression) call.getMethod();
            if("where".equals(name.getValue()) || "and".equals(name.getValue()) || "or".equals(name.getValue()) || "select".equals(name.getValue())){
                if (call.getArguments() instanceof TupleExpression) {
                    TupleExpression args = (TupleExpression) call.getArguments();
                    List<Expression> transformed = new ArrayList<Expression>();
                    for (Expression expression : args.getExpressions()) {
                        transformed.add(ExpressionToMethodCallsTransformer.INSTANCE.transform(expression));
View Full Code Here

        if (!isDesiredType(objectExp)) { return false; }

        if (!(call.getMethod() instanceof ConstantExpression)) { return false; }

        ConstantExpression method = (ConstantExpression) call.getMethod();

        if (!isDesiredMethodName(method.getValue())) { return false; }

        if (call.getArguments() instanceof TupleExpression) {
            TupleExpression args = (TupleExpression) call.getArguments();
            return args.getExpression(args.getExpressions().size()-1) instanceof ClosureExpression;
        }
View Full Code Here

  public static void addLogField(ClassNode classNode, String logName) {
    FieldNode logVariable = new FieldNode(LOG_PROPERTY,
                        Modifier.STATIC | Modifier.PRIVATE,
                        new ClassNode(Log.class),
                        classNode,
                        new MethodCallExpression(new ClassExpression(new ClassNode(LogFactory.class)), "getLog", new ArgumentListExpression(new ConstantExpression(logName))));

    classNode.addField(logVariable);
  }
View Full Code Here

                new BooleanExpression(
                        new BinaryExpression(
                                new FieldExpression(
                                        new FieldNode("bar", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)),
                                Token.newSymbol("==", 0, 0),
                                new ConstantExpression("abc")));

        Statement trueStatement =
                new ExpressionStatement(
                        new BinaryExpression(
                                new FieldExpression(
                                        new FieldNode("result", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)),
                                Token.newSymbol("=", 0, 0),
                                new ConstantExpression("worked")));

        Statement falseStatement = createPrintlnStatement(new ConstantExpression("false"));

        IfStatement statement = new IfStatement(expression, trueStatement, falseStatement);
        classNode.addMethod(new MethodNode("ifDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statement));

        Class fooClass = loadClass(classNode);
View Full Code Here

    public void testMethods() throws Exception {
        ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
        classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));

        Statement statementA = new ReturnStatement(new ConstantExpression("calledA"));
        Statement statementB = new ReturnStatement(new ConstantExpression("calledB"));
        Statement emptyStatement = new BlockStatement();

        classNode.addMethod(new MethodNode("a", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementA));
        classNode.addMethod(new MethodNode("b", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementB));
View Full Code Here

    private Statement throwAssertionFailedError(AnnotationNode annotationNode) {
        ThrowStatement throwStatement = new ThrowStatement(
                new ConstructorCallExpression(ASSERTION_FAILED_ERROR_TYPE,
                        new ArgumentListExpression(
                                new ConstantExpression("Method is marked with @NotYetImplemented but passes unexpectedly"))));

        throwStatement.setSourcePosition(annotationNode);

        return throwStatement;
    }
View Full Code Here

    @Test
    public void testVisit_Contract() {
        try {
            ASTNode[] badInput = new ASTNode[]{
                    new ConstantExpression("sample"),
                    new EmptyExpression()
            };
            new SingletonASTTransformation().visit(badInput, null);
            Assert.fail("Contract Failure");
        } catch (Error e) {
View Full Code Here

            ArgumentListExpression argumentListExpression = (ArgumentListExpression) arguments;
            List<Expression> args = argumentListExpression.getExpressions();

            for (Expression arg : args) {
                if (arg instanceof ConstantExpression) {
                    ConstantExpression expression = (ConstantExpression) arg;
                    Object o = expression.getValue();

                    if (o instanceof String) {
                        out.print("(String)null");
                    } else {
                        out.print(expression.getText());
                    }
                } else {
                    ClassNode type = getConstructorArgumentType(arg, node);
                    printDefaultValue(out, type);
                }
View Full Code Here

            if (!className.endsWith(".class")) {
                out.print(".class");
            }
        } else {
            if (re instanceof ConstantExpression) {
                ConstantExpression ce = (ConstantExpression) re;
                Object value = ce.getValue();
                if (ClassHelper.STRING_TYPE.equals(ce.getType())) {
                    out.print(formatString((String)value));
                } else if (ClassHelper.char_TYPE.equals(ce.getType()) || ClassHelper.Character_TYPE.equals(ce.getType())) {
                    out.print(formatChar(value.toString()));
                } else if (ClassHelper.long_TYPE.equals(ce.getType())) {
                    out.print("" + value + "L");
                } else if (ClassHelper.float_TYPE.equals(ce.getType())) {
                    out.print("" + value + "f");
                } else if (ClassHelper.double_TYPE.equals(ce.getType())) {
                    out.print("" + value + "d");
                } else {
                    out.print(re.getText());
                }
            } else {
View Full Code Here

TOP

Related Classes of org.codehaus.groovy.ast.expr.ConstantExpression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.