Java Code Examples for org.codehaus.groovy.ast.expr.ConstructorCallExpression#isSpecialCall()

The following examples show how to use org.codehaus.groovy.ast.expr.ConstructorCallExpression#isSpecialCall() . 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: GroovyVirtualSourceProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void genConstructor(ClassNode clazz, ConstructorNode constructorNode, PrintWriter out) {
    // <netbeans>
    if (constructorNode.isSynthetic()) {
        return;
    }
    // </netbeans>
    // printModifiers(out, constructorNode.getModifiers());

    out.print("public "); // temporary hack

    out.print(clazz.getNameWithoutPackage());

    printParams(constructorNode, out);

    ConstructorCallExpression constrCall = getConstructorCallExpression(constructorNode);
    if (constrCall == null || !constrCall.isSpecialCall()) {
        out.println(" {}");
    } else {
        out.println(" {");

        genSpecialConstructorArgs(out, constructorNode, constrCall);

        out.println("}");
    }
}
 
Example 2
Source File: ConstructorNodeUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Return the first statement from the constructor code if it is a call to super or this, otherwise null.
 *
 * @param code
 * @return the first statement if a special call or null
 */
public static ConstructorCallExpression getFirstIfSpecialConstructorCall(Statement code) {
    if (code == null) return null;

    if (code instanceof BlockStatement) {
        final BlockStatement block = (BlockStatement) code;
        final List<Statement> statementList = block.getStatements();
        if (statementList.isEmpty()) return null;
        // handle blocks of blocks
        return getFirstIfSpecialConstructorCall(statementList.get(0));
    }

    if (!(code instanceof ExpressionStatement)) return null;

    Expression expression = ((ExpressionStatement) code).getExpression();
    if (!(expression instanceof ConstructorCallExpression)) return null;
    ConstructorCallExpression cce = (ConstructorCallExpression) expression;
    if (cce.isSpecialCall()) return cce;
    return null;
}
 
Example 3
Source File: StaticImportVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected Expression transformConstructorCallExpression(ConstructorCallExpression cce) {
    inSpecialConstructorCall = cce.isSpecialCall();
    Expression expression = cce.getArguments();
    if (expression instanceof TupleExpression) {
        TupleExpression tuple = (TupleExpression) expression;
        if (tuple.getExpressions().size() == 1) {
            expression = tuple.getExpression(0);
            if (expression instanceof NamedArgumentListExpression) {
                NamedArgumentListExpression namedArgs = (NamedArgumentListExpression) expression;
                List<MapEntryExpression> entryExpressions = namedArgs.getMapEntryExpressions();
                for (int i = 0; i < entryExpressions.size(); i++) {
                    entryExpressions.set(i, (MapEntryExpression) transformMapEntryExpression(entryExpressions.get(i), cce.getType()));
                }
            }
        }
    }
    Expression ret = cce.transformExpression(this);
    inSpecialConstructorCall = false;
    return ret;
}
 
Example 4
Source File: StaticVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstructorCallExpression(ConstructorCallExpression cce) {
    boolean oldIsSpecialConstructorCall = inSpecialConstructorCall;
    inSpecialConstructorCall |= cce.isSpecialCall();
    super.visitConstructorCallExpression(cce);
    inSpecialConstructorCall = oldIsSpecialConstructorCall;
}
 
Example 5
Source File: ConstructorNode.java    From groovy with Apache License 2.0 5 votes vote down vote up
public boolean firstStatementIsSpecialConstructorCall() {
    Statement code = getFirstStatement();
    if (!(code instanceof ExpressionStatement)) return false;

    Expression expression = ((ExpressionStatement) code).getExpression();
    if (!(expression instanceof ConstructorCallExpression)) return false;
    ConstructorCallExpression cce = (ConstructorCallExpression) expression;
    return cce.isSpecialCall();
}
 
Example 6
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstructorCallExpression(final ConstructorCallExpression call) {
    onLineNumber(call, "visitConstructorCallExpression: \"" + call.getType().getName() + "\":");
    if (call.isSpecialCall()) {
        controller.getInvocationWriter().writeSpecialConstructorCall(call);
        return;
    }
    controller.getInvocationWriter().writeInvokeConstructor(call);
    controller.getAssertionWriter().record(call);
}
 
Example 7
Source File: VariableScopeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstructorCallExpression(final ConstructorCallExpression expression) {
    boolean oldInSpecialCtorFlag = inSpecialConstructorCall;
    inSpecialConstructorCall |= expression.isSpecialCall();
    super.visitConstructorCallExpression(expression);
    inSpecialConstructorCall = oldInSpecialCtorFlag;

    if (!expression.isUsingAnonymousInnerClass()) return;

    pushState();
    InnerClassNode innerClass = (InnerClassNode) expression.getType();
    innerClass.setVariableScope(currentScope);
    currentScope.setClassScope(innerClass);
    currentScope.setInStaticContext(false);
    for (MethodNode method : innerClass.getMethods()) {
        Parameter[] parameters = method.getParameters();
        if (parameters.length == 0) {
            parameters = null; // null means no implicit "it"
        }
        visitClosureExpression(new ClosureExpression(parameters, method.getCode()));
    }

    for (FieldNode field : innerClass.getFields()) {
        Expression initExpression = field.getInitialExpression();
        pushState(field.isStatic());
        if (initExpression != null) {
            if (initExpression.isSynthetic() && initExpression instanceof VariableExpression
                    && ((VariableExpression) initExpression).getAccessedVariable() instanceof Parameter) {
                // GROOVY-6834: accessing a parameter which is not yet seen in scope
                popState();
                continue;
            }
            initExpression.visit(this);
        }
        popState();
    }

    for (Statement initStatement : innerClass.getObjectInitializerStatements()) {
        initStatement.visit(this);
    }
    markClosureSharedVariables();
    popState();
}