Java Code Examples for org.codehaus.groovy.ast.expr.VariableExpression#setAccessedVariable()

The following examples show how to use org.codehaus.groovy.ast.expr.VariableExpression#setAccessedVariable() . 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: MapConstructorASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void doAddConstructor(final ClassNode cNode, final ConstructorNode constructorNode) {
    markAsGenerated(cNode, constructorNode);
    cNode.addConstructor(constructorNode);
    // GROOVY-5814: Immutable is not compatible with @CompileStatic
    Parameter argsParam = null;
    for (Parameter p : constructorNode.getParameters()) {
        if ("args".equals(p.getName())) {
            argsParam = p;
            break;
        }
    }
    if (argsParam != null) {
        final Parameter arg = argsParam;
        ClassCodeVisitorSupport variableExpressionFix = new ClassCodeVisitorSupport() {
            @Override
            protected SourceUnit getSourceUnit() {
                return cNode.getModule().getContext();
            }

            @Override
            public void visitVariableExpression(final VariableExpression expression) {
                super.visitVariableExpression(expression);
                if ("args".equals(expression.getName())) {
                    expression.setAccessedVariable(arg);
                }
            }
        };
        variableExpressionFix.visitConstructor(constructorNode);
    }
}
 
Example 2
Source File: FieldASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void adjustToClassVar(VariableExpression expr) {
    // we only need to check the variable name because the Groovy compiler
    // already fails if a variable with the same name already exists in the scope.
    // this means that a closure cannot shadow a class variable
    expr.setAccessedVariable(fieldNode);
    final VariableScope variableScope = currentClosure.getVariableScope();
    final Iterator<Variable> iterator = variableScope.getReferencedLocalVariablesIterator();
    while (iterator.hasNext()) {
        Variable next = iterator.next();
        if (next.getName().equals(variableName)) iterator.remove();
    }
    variableScope.putReferencedClassVariable(fieldNode);
}
 
Example 3
Source File: VariableScopeVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitVariableExpression(final VariableExpression expression) {
    Variable variable = findVariableDeclaration(expression.getName());
    if (variable == null) return;
    expression.setAccessedVariable(variable);
    checkVariableContextAccess(variable, expression);
}
 
Example 4
Source File: InnerClassCompletionVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void addThisReference(ConstructorNode node) {
    if (!shouldHandleImplicitThisForInnerClass(classNode)) return;

    // add "this$0" field init

    //add this parameter to node
    Parameter[] params = node.getParameters();
    Parameter[] newParams = new Parameter[params.length + 1];
    System.arraycopy(params, 0, newParams, 1, params.length);
    String name = getUniqueName(params, node);

    Parameter thisPara = new Parameter(classNode.getOuterClass().getPlainNodeReference(), name);
    newParams[0] = thisPara;
    node.setParameters(newParams);

    BlockStatement block = getCodeAsBlock(node);
    BlockStatement newCode = block();
    addFieldInit(thisPara, thisField, newCode);
    ConstructorCallExpression cce = getFirstIfSpecialConstructorCall(block);
    if (cce == null) {
        cce = ctorSuperX(new TupleExpression());
        block.getStatements().add(0, stmt(cce));
    }
    if (shouldImplicitlyPassThisPara(cce)) {
        // add thisPara to this(...)
        TupleExpression args = (TupleExpression) cce.getArguments();
        List<Expression> expressions = args.getExpressions();
        VariableExpression ve = varX(thisPara.getName());
        ve.setAccessedVariable(thisPara);
        expressions.add(0, ve);
    }
    if (cce.isSuperCall()) {
        // we have a call to super here, so we need to add
        // our code after that
        block.getStatements().add(1, newCode);
    }
    node.setCode(block);
}
 
Example 5
Source File: ClosureWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void addFieldsAndGettersForLocalVariables(final InnerClassNode answer, final Parameter[] localVariableParams) {
    for (Parameter param : localVariableParams) {
        String paramName = param.getName();
        ClassNode type = param.getType();
        VariableExpression initialValue = new VariableExpression(paramName);
        initialValue.setAccessedVariable(param);
        initialValue.setUseReferenceDirectly(true);
        ClassNode realType = type;
        type = ClassHelper.makeReference();
        param.setType(ClassHelper.makeReference());
        FieldNode paramField = answer.addField(paramName, ACC_PRIVATE | ACC_SYNTHETIC, type, initialValue);
        paramField.setOriginType(ClassHelper.getWrapper(param.getOriginType()));
        paramField.setHolder(true);
        String methodName = Verifier.capitalize(paramName);

        // let's add a getter & setter
        Expression fieldExp = new FieldExpression(paramField);
        markAsGenerated(answer,
            answer.addMethod(
                "get" + methodName,
                ACC_PUBLIC,
                realType.getPlainNodeReference(),
                Parameter.EMPTY_ARRAY,
                ClassNode.EMPTY_ARRAY,
                new ReturnStatement(fieldExp)),
            true);
    }
}
 
Example 6
Source File: ClosureWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitVariableExpression(final VariableExpression expression) {
    Variable v = expression.getAccessedVariable();
    if (v == null) return;
    if (!(v instanceof FieldNode)) return;
    String name = expression.getName();
    FieldNode fn = icn.getDeclaredField(name);
    if (fn != null) { // only overwrite if we find something more specific
        expression.setAccessedVariable(fn);
    }
}
 
Example 7
Source File: GeneralUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static VariableExpression localVarX(final String name) {
    VariableExpression result = varX(name);
    result.setAccessedVariable(result);
    return result;
}
 
Example 8
Source File: GeneralUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static VariableExpression localVarX(final String name, final ClassNode type) {
    VariableExpression result = varX(name, type);
    result.setAccessedVariable(result);
    return result;
}
 
Example 9
Source File: VariableScopeVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void declare(final VariableExpression variable) {
    variable.setInStaticContext(currentScope.isInStaticContext());
    declare(variable, variable);
    variable.setAccessedVariable(variable);
}