org.codehaus.groovy.ast.expr.EmptyExpression Java Examples

The following examples show how to use org.codehaus.groovy.ast.expr.EmptyExpression. 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: PicocliScriptASTTransformation.java    From picocli with Apache License 2.0 6 votes vote down vote up
private void changeBaseScriptTypeFromDeclaration(final SourceUnit source, final DeclarationExpression de, final AnnotationNode node) {
    if (de.isMultipleAssignmentDeclaration()) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with multiple assignment notation.", de);
        return;
    }

    ClassNode cNode = de.getDeclaringClass();
    ClassNode baseScriptType = de.getVariableExpression().getType().getPlainNodeReference();
    if (baseScriptType.isScript()) {
        if (!(de.getRightExpression() instanceof EmptyExpression)) {
            addError("Annotation " + MY_TYPE_NAME + " not supported with variable assignment.", de);
            return;
        }
        de.setRightExpression(new VariableExpression("this"));
    } else {
        baseScriptType = BASE_SCRIPT_TYPE;
    }
    Expression value = node.getMember("value");
    if (value != null) {
        addError("Annotation " + MY_TYPE_NAME + " cannot have member 'value' if used on a declaration.", value);
        return;
    }


    changeBaseScriptType(source, de, cNode, baseScriptType, node);
}
 
Example #2
Source File: SourceURIASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void setScriptURIOnDeclaration(final DeclarationExpression de, final AnnotationNode node) {
    if (de.isMultipleAssignmentDeclaration()) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with multiple assignment notation.", de);
        return;
    }

    if (!(de.getRightExpression() instanceof EmptyExpression)) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with variable assignment.", de);
        return;
    }

    URI uri = getSourceURI(node);

    if (uri == null) {
        addError("Unable to get the URI for the source of this script!", de);
    } else {
        // Set the RHS to '= URI.create("string for this URI")'.
        // That may throw an IllegalArgumentExpression wrapping the URISyntaxException.
        de.setRightExpression(getExpression(uri));
    }
}
 
Example #3
Source File: BaseScriptASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void changeBaseScriptTypeFromDeclaration(final DeclarationExpression de, final AnnotationNode node) {
    if (de.isMultipleAssignmentDeclaration()) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with multiple assignment notation.", de);
        return;
    }

    if (!(de.getRightExpression() instanceof EmptyExpression)) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with variable assignment.", de);
        return;
    }
    Expression value = node.getMember("value");
    if (value != null) {
        addError("Annotation " + MY_TYPE_NAME + " cannot have member 'value' if used on a declaration.", value);
        return;
    }

    ClassNode cNode = de.getDeclaringClass();
    ClassNode baseScriptType = de.getVariableExpression().getType().getPlainNodeReference();
    de.setRightExpression(new VariableExpression("this"));

    changeBaseScriptType(de, cNode, baseScriptType);
}
 
Example #4
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitBytecodeSequence(final BytecodeSequence bytecodeSequence) {
    MethodVisitor mv = controller.getMethodVisitor();
    List<?> sequence = bytecodeSequence.getInstructions();
    int mark = controller.getOperandStack().getStackLength();

    for (Object element : sequence) {
        if (element instanceof EmptyExpression) {
            mv.visitInsn(ACONST_NULL);
        } else if (element instanceof Expression) {
            ((Expression) element).visit(this);
        } else if (element instanceof Statement) {
            ((Statement) element).visit(this);
            mv.visitInsn(ACONST_NULL);
        } else {
            ((BytecodeInstruction) element).visit(mv);
        }
    }

    controller.getOperandStack().remove(mark - controller.getOperandStack().getStackLength());
}
 
Example #5
Source File: LazyASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Expression getInitExpr(ErrorCollecting xform, FieldNode fieldNode) {
    Expression initExpr = fieldNode.getInitialValueExpression();
    fieldNode.setInitialValueExpression(null);

    if (initExpr == null || initExpr instanceof EmptyExpression) {
        if (fieldNode.getType().isAbstract()) {
            xform.addError("You cannot lazily initialize '" + fieldNode.getName() + "' from the abstract class '" +
                    fieldNode.getType().getName() + "'", fieldNode);
        }
        initExpr = ctorX(fieldNode.getType());
    }

    return initExpr;
}
 
Example #6
Source File: FinalVariableAnalyzer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void recordAssignments(BinaryExpression expression, boolean isDeclaration, Expression leftExpression, Expression rightExpression) {
    if (leftExpression instanceof Variable) {
        boolean uninitialized = isDeclaration && rightExpression instanceof EmptyExpression;
        recordAssignment((Variable) leftExpression, isDeclaration, uninitialized, false, expression);
    } else if (leftExpression instanceof TupleExpression) {
        TupleExpression te = (TupleExpression) leftExpression;
        for (Expression next : te.getExpressions()) {
            if (next instanceof Variable) {
                recordAssignment((Variable) next, isDeclaration, false, false, next);
            }
        }
    }
}
 
Example #7
Source File: IndyBinHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
protected void writePostOrPrefixMethod(int op, String method, Expression expression, Expression orig) {
    getController().getInvocationWriter().makeCall(
            orig, EmptyExpression.INSTANCE, 
            new ConstantExpression(method), 
            MethodCallExpression.NO_ARGUMENTS, 
            InvocationWriter.invokeMethod, 
            false, false, false);
}
 
Example #8
Source File: InvokeDynamicWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void coerce(ClassNode from, ClassNode target) {
    ClassNode wrapper = ClassHelper.getWrapper(target);
    makeIndyCall(invokeMethod, EmptyExpression.INSTANCE, false, false, "asType", new ClassExpression(wrapper));
    if (ClassHelper.boolean_TYPE.equals(target) || ClassHelper.Boolean_TYPE.equals(target)) {
        writeIndyCast(ClassHelper.OBJECT_TYPE,target);
    } else {
        BytecodeHelper.doCast(controller.getMethodVisitor(), wrapper);
        controller.getOperandStack().replace(wrapper);
        controller.getOperandStack().doGroovyCast(target);
    }
}
 
Example #9
Source File: StatementWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void visitExpressionOfLoopStatement(final Expression expression) {
    Consumer<Expression> visit = expr -> {
        if (expr instanceof EmptyExpression) return;
        int mark = controller.getOperandStack().getStackLength();
        expr.visit(controller.getAcg());
        controller.getOperandStack().popDownTo(mark);
    };

    if (expression instanceof ClosureListExpression) {
        ((ClosureListExpression) expression).getExpressions().forEach(visit);
    } else {
        visit.accept(expression);
    }
}
 
Example #10
Source File: SingletonASTTransformationTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Test
public void testVisit_Contract() {
    try {
        ASTNode[] badInput = new ASTNode[]{
                new ConstantExpression("sample"),
                EmptyExpression.INSTANCE
        };
        new SingletonASTTransformation().visit(badInput, null);
        Assert.fail("Contract Failure");
    } catch (Error e) {
        Assert.assertTrue("Bad error msg: " + e.getMessage(), e.getMessage().contains("ConstantExpression"));
        Assert.assertTrue("Bad error msg: " + e.getMessage(), e.getMessage().contains("EmptyExpression"));
        Assert.assertTrue("Bad error msg: " + e.getMessage(), e.getMessage().contains("expecting [AnnotationNode, AnnotatedNode]"));
    }
}
 
Example #11
Source File: ASTTransformer.java    From pom-manipulation-ext with Apache License 2.0 5 votes vote down vote up
private void changeBaseScriptTypeFromDeclaration(final DeclarationExpression de, final AnnotationNode node) {
    if (de.isMultipleAssignmentDeclaration()) {
        addError( "Annotation " + getType() + " not supported with multiple assignment notation.", de);
        return;
    }

    if (!(de.getRightExpression() instanceof EmptyExpression)) {
        addError( "Annotation " + getType() + " not supported with variable assignment.", de);
        return;
    }
    Expression value = node.getMember("value");
    if (value != null) {
        addError( "Annotation " + getType() + " cannot have member 'value' if used on a declaration.", value);
        return;
    }

    ClassNode cNode = de.getDeclaringClass();
    ClassNode baseScriptType = de.getVariableExpression().getType().getPlainNodeReference();
    if (baseScriptType.isScript()) {
        if (!(de.getRightExpression() instanceof EmptyExpression)) {
            addError( "Annotation " + getType() + " not supported with variable assignment.", de);
            return;
        }
        de.setRightExpression(new VariableExpression("this"));
    } else {
        if ( type == Type.MAVEN )
        {
            baseScriptType = MAVEN_BASE_SCRIPT_TYPE;
        }
        else
        {
            baseScriptType = GRADLE_BASE_SCRIPT_TYPE;
        }
    }

    changeBaseScriptType(de, cNode, baseScriptType);
}
 
Example #12
Source File: StaticImportVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
private Expression findStaticPropertyAccessorByFullName(ClassNode staticImportType, String accessorMethodName) {
    // anything will do as we only check size == 1
    ArgumentListExpression dummyArgs = new ArgumentListExpression();
    dummyArgs.addExpression(EmptyExpression.INSTANCE);
    return findStaticMethod(staticImportType, accessorMethodName, (inLeftExpression ? dummyArgs : ArgumentListExpression.EMPTY_ARGUMENTS));
}
 
Example #13
Source File: GroovyCodeVisitor.java    From groovy with Apache License 2.0 votes vote down vote up
default void visitEmptyExpression(EmptyExpression expression) {}