org.codehaus.groovy.ast.DynamicVariable Java Examples

The following examples show how to use org.codehaus.groovy.ast.DynamicVariable. 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: StaticVerifier.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitVariableExpression(VariableExpression ve) {
    if (ve.getAccessedVariable() instanceof DynamicVariable && (ve.isInStaticContext() || inSpecialConstructorCall) && !inClosure) {
        // GROOVY-5687: interface constants not visible to implementing sub-class in static context
        if (methodNode != null && methodNode.isStatic()) {
            FieldNode fieldNode = getDeclaredOrInheritedField(methodNode.getDeclaringClass(), ve.getName());
            if (fieldNode != null && fieldNode.isStatic()) {
                return;
            }
        }
        addError("Apparent variable '" + ve.getName() + "' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:\n" +
                "You attempted to reference a variable in the binding or an instance variable from a static context.\n" +
                "You misspelled a classname or statically imported field. Please check the spelling.\n" +
                "You attempted to use a method '" + ve.getName() + "' but left out brackets in a place not allowed by the grammar.", ve);
    }
}
 
Example #2
Source File: MapConstructorASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static ClassCodeExpressionTransformer makeMapTypedArgsTransformer() {
    return new ClassCodeExpressionTransformer() {
        @Override
        public Expression transform(Expression exp) {
            if (exp instanceof ClosureExpression) {
                ClosureExpression ce = (ClosureExpression) exp;
                ce.getCode().visit(this);
            } else if (exp instanceof VariableExpression) {
                VariableExpression ve = (VariableExpression) exp;
                if ("args".equals(ve.getName()) && ve.getAccessedVariable() instanceof DynamicVariable) {
                    VariableExpression newVe = varX(param(MAP_TYPE, "args"));
                    newVe.setSourcePosition(ve);
                    return newVe;
                }
            }
            return exp.transformExpression(this);
        }

        @Override
        protected SourceUnit getSourceUnit() {
            return null;
        }
    };
}
 
Example #3
Source File: BinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 6 votes vote down vote up
private boolean doAssignmentToLocalVariable(final String method, final BinaryExpression binExp) {
    Expression left = binExp.getLeftExpression();
    if (left instanceof VariableExpression) {
        VariableExpression ve = (VariableExpression) left;
        Variable v = ve.getAccessedVariable();
        if (v instanceof DynamicVariable) return false;
        if (v instanceof PropertyExpression) return false;
        /* field and declaration we don't return false */
    } else {
        return false;
    }

    evaluateBinaryExpression(method, binExp);
    controller.getOperandStack().dup();
    controller.getCompileStack().pushLHS(true);
    binExp.getLeftExpression().visit(controller.getAcg());
    controller.getCompileStack().popLHS();

    return true;
}
 
Example #4
Source File: ProcessVariableRenamer.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visitVariableExpression(final VariableExpression expression) {
    final Variable accessedVar = expression.getAccessedVariable();
    // look for dynamic variables since the parameters already have the
    // new names, the actual references to the parameters are using the
    // old names
    if (accessedVar instanceof DynamicVariable) {
        final String newName = findReplacement(accessedVar.getName());
        if (newName != null) {
            ReplaceEdit replaceEdit = new ReplaceEdit(expression.getStart(), expression.getLength(), newName);
            try {
                edits.addChild(replaceEdit);
            }catch (MalformedTreeException e) {
                BonitaStudioLog.error(e);
            }
        }
    }
}
 
Example #5
Source File: TaskDefinitionScriptTransformer.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private boolean isDynamicVar(Expression expression) {
    if (!(expression instanceof VariableExpression)) {
        return false;
    }
    VariableExpression variableExpression = (VariableExpression) expression;
    return variableExpression.getAccessedVariable() instanceof DynamicVariable;
}
 
Example #6
Source File: TaskDefinitionScriptTransformer.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private boolean isDynamicVar(Expression expression) {
    if (!(expression instanceof VariableExpression)) {
        return false;
    }
    VariableExpression variableExpression = (VariableExpression) expression;
    return variableExpression.getAccessedVariable() instanceof DynamicVariable;
}
 
Example #7
Source File: VariableScopeVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void checkVariableContextAccess(final Variable variable, final Expression expression) {
    if (variable.isInStaticContext() || !currentScope.isInStaticContext()) return;

    addError(variable.getName() + " is declared in a dynamic context, but you tried to access it from a static context.", expression);

    // declare a static variable to be able to continue the check
    currentScope.putDeclaredVariable(new DynamicVariable(variable.getName(), currentScope.isInStaticContext()));
}
 
Example #8
Source File: VariableScopeVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethodCallExpression(final MethodCallExpression expression) {
    if (expression.isImplicitThis() && expression.getMethod() instanceof ConstantExpression) {
        ConstantExpression methodNameConstant = (ConstantExpression) expression.getMethod();
        String methodName = methodNameConstant.getText();

        if (methodName == null) {
            throw new GroovyBugError("method name is null");
        }

        Variable variable = findVariableDeclaration(methodName);
        if (variable != null && !(variable instanceof DynamicVariable)) {
            checkVariableContextAccess(variable, expression);
        }

        if (variable instanceof VariableExpression || variable instanceof Parameter) {
            VariableExpression object = new VariableExpression(variable);
            object.setSourcePosition(methodNameConstant);
            expression.setObjectExpression(object);
            ConstantExpression method = new ConstantExpression("call");
            method.setSourcePosition(methodNameConstant); // important for GROOVY-4344
            expression.setImplicitThis(false);
            expression.setMethod(method);
        }
    }
    super.visitMethodCallExpression(expression);
}
 
Example #9
Source File: TaskDefinitionScriptTransformer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private boolean isDynamicVar(Expression expression) {
    if (!(expression instanceof VariableExpression)) {
        return false;
    }
    VariableExpression variableExpression = (VariableExpression) expression;
    return variableExpression.getAccessedVariable() instanceof DynamicVariable;
}
 
Example #10
Source File: TaskDefinitionScriptTransformer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private boolean isDynamicVar(Expression expression) {
    if (!(expression instanceof VariableExpression)) {
        return false;
    }
    VariableExpression variableExpression = (VariableExpression) expression;
    return variableExpression.getAccessedVariable() instanceof DynamicVariable;
}
 
Example #11
Source File: ElementUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static ElementKind getKind(AstPath path, BaseDocument doc, int caret) {
    ASTNode node = path.leaf();
    ASTNode leafParent = path.leafParent();

    if ((node instanceof ClassNode) ||
        (node instanceof ClassExpression) ||
        (node instanceof CatchStatement) ||
        (node instanceof AnnotationNode) ||
        (node instanceof ConstructorCallExpression) ||
        FindTypeUtils.isCaretOnClassNode(path, doc, caret)) {
        return ElementKind.CLASS;
    } else if ((node instanceof MethodNode)) {
        if ("<init>".equals(((MethodNode) node).getName())) { // NOI18N
            return ElementKind.CONSTRUCTOR;
        }
        return ElementKind.METHOD;
    } else if ((node instanceof ConstantExpression) && (leafParent instanceof MethodCallExpression)) {
        return ElementKind.METHOD;
    } else if (node instanceof FieldNode) {
        return ElementKind.FIELD;
    } else if (node instanceof PropertyNode) {
        return ElementKind.PROPERTY;
    } else if (node instanceof VariableExpression) {
        Variable variable = ((VariableExpression) node).getAccessedVariable();
        if (variable instanceof DynamicVariable) {
            // Not sure now if this is 100% correct, but if we have VariableExpression
            // like "Book^mark.get()" the accessed variable Bookmark (which is the type
            // name) is marked as DynamicVariable and in that case we want to return
            // different ElementKind in oposite to usage of 'normal' variables
            return ElementKind.CLASS;
        }
        return ElementKind.VARIABLE;
    } else if (node instanceof Parameter) {
        return ElementKind.VARIABLE;
    } else if (node instanceof DeclarationExpression) {
        return ElementKind.VARIABLE;
    } else if ((node instanceof ConstantExpression) && (leafParent instanceof PropertyExpression)) {
        return ElementKind.VARIABLE;
    } else if (node instanceof PackageNode) {
        return ElementKind.PACKAGE;
    }
    return ElementKind.OTHER;
}
 
Example #12
Source File: MarkupBuilderCodeTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public Expression transform(final Expression exp) {
    if (exp instanceof BinaryExpression) {
        return transformBinaryExpression((BinaryExpression) exp);
    }
    if (exp instanceof MethodCallExpression) {
        return transformMethodCall((MethodCallExpression) exp);
    }
    if (exp instanceof ClosureExpression) {
        ClosureExpression cl = (ClosureExpression) exp;
        cl.getCode().visit(this);
        return cl;
    }
    if (exp instanceof VariableExpression) {
        VariableExpression var = (VariableExpression) exp;
        if (var.getAccessedVariable() instanceof DynamicVariable) {
            MethodCallExpression callGetModel = new MethodCallExpression(
                    new VariableExpression("this"),
                    "getModel",
                    ArgumentListExpression.EMPTY_ARGUMENTS
            );
            callGetModel.setImplicitThis(true);
            callGetModel.setSourcePosition(exp);
            String varName = var.getName();
            if ("model".equals(varName) || "unescaped".equals(varName)) {
                return callGetModel;
            }
            MethodCallExpression mce = new MethodCallExpression(
                    callGetModel,
                    "get",
                    new ArgumentListExpression(new ConstantExpression(varName))
            );
            mce.setSourcePosition(exp);
            mce.setImplicitThis(false);
            MethodCallExpression yield = new MethodCallExpression(
                    new VariableExpression("this"),
                    "tryEscape",
                    new ArgumentListExpression(mce)
            );
            yield.setImplicitThis(true);
            yield.setSourcePosition(exp);
            yield.putNodeMetaData(TARGET_VARIABLE, varName);
            return autoEscape?yield:mce;
        }
    }
    return super.transform(exp);
}
 
Example #13
Source File: ResolveVisitor.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected Expression transformVariableExpression(final VariableExpression ve) {
    visitAnnotations(ve);
    Variable v = ve.getAccessedVariable();

    if(!(v instanceof DynamicVariable) && !checkingVariableTypeInDeclaration) {
        /*
         *  GROOVY-4009: when a normal variable is simply being used, there is no need to try to
         *  resolve its type. Variable type resolve should proceed only if the variable is being declared.
         */
        return ve;
    }
    if (v instanceof DynamicVariable) {
        String name = ve.getName();
        ClassNode t = ClassHelper.make(name);
        // asking isResolved here allows to check if a primitive
        // type name like "int" was used to make t. In such a case
        // we have nothing left to do.
        boolean isClass = t.isResolved();
        if (!isClass) {
            // It was no primitive type, so next we see if the name,
            // which is a vanilla name, starts with a lower case letter.
            // In that case we change it to a LowerCaseClass to let the
            // compiler skip the resolving at several places in this class.
            if (Character.isLowerCase(name.charAt(0))) {
                t = new LowerCaseClass(name);
            }
            isClass = resolve(t);
        }
        if (isClass) {
            // the name is a type so remove it from the scoping
            // as it is only a classvariable, it is only in
            // referencedClassVariables, but must be removed
            // for each parentscope too
            for (VariableScope scope = currentScope; scope != null && !scope.isRoot(); scope = scope.getParent()) {
                if (scope.removeReferencedClassVariable(ve.getName()) == null) break;
            }
            return new ClassExpression(t);
        }
    }
    resolveOrFail(ve.getType(), ve);
    ClassNode origin = ve.getOriginType();
    if (origin != ve.getType()) resolveOrFail(origin, ve);
    return ve;
}
 
Example #14
Source File: AbstractTypeCheckingExtension.java    From groovy with Apache License 2.0 4 votes vote down vote up
public boolean isDynamic(VariableExpression var) {
    return var.getAccessedVariable() instanceof DynamicVariable;
}