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

The following examples show how to use org.codehaus.groovy.ast.expr.VariableExpression#isSuperExpression() . 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: TypeInferenceVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void visitVariableExpression(VariableExpression expression) {
        if (expression.isSuperExpression()) {
            guessedType = expression.getType().getSuperClass();
        }
        if (null != expression.getAccessedVariable()) {
            Variable accessedVariable = expression.getAccessedVariable();

            if (accessedVariable.hasInitialExpression()) {
                Expression initialExpression = expression.getAccessedVariable().getInitialExpression();
                if (initialExpression instanceof ConstantExpression
                        && !initialExpression.getText().equals("null")) { // NOI18N
                    guessedType = ((ConstantExpression) initialExpression).getType();
                } else if (initialExpression instanceof ConstructorCallExpression) {
                    guessedType = ClassHelper.make(((ConstructorCallExpression) initialExpression).getType().getName());
                } else if (initialExpression instanceof MethodCallExpression) {
                    int newOffset = ASTUtils.getOffset(doc, initialExpression.getLineNumber(), initialExpression.getColumnNumber());
                    AstPath newPath = new AstPath(path.root(), newOffset, doc);
                    guessedType = MethodInference.findCallerType(initialExpression, newPath, doc, newOffset);
                } else if (initialExpression instanceof ListExpression) {
                    guessedType = ((ListExpression) initialExpression).getType();
                } else if (initialExpression instanceof MapExpression) {
                    guessedType = ((MapExpression) initialExpression).getType();
                } else if (initialExpression instanceof RangeExpression) {
                    // this should work, but the type is Object - nut sure why
                    // guessedType = ((RangeExpression)initialExpression).getType();
                    guessedType = ClassHelper.makeWithoutCaching(Range.class, true);                
                }
            } else if (accessedVariable instanceof Parameter) {
                Parameter param = (Parameter) accessedVariable;
                guessedType = param.getType();
            }
        } else if (!expression.getType().getName().equals("java.lang.Object")) {
            guessedType = expression.getType();

        }
    super.visitVariableExpression(expression);
}
 
Example 2
Source File: ClassCompletionVerifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void checkSuperOrThisOnLHS(Expression expression) {
    if (!(expression instanceof VariableExpression)) return;
    VariableExpression ve = (VariableExpression) expression;
    if (ve.isThisExpression()) {
        addError("cannot have 'this' as LHS of an assignment", expression);
    } else if (ve.isSuperExpression()) {
        addError("cannot have 'super' as LHS of an assignment", expression);
    }
}
 
Example 3
Source File: StatementMetaTypeChooser.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public ClassNode resolveType(final Expression exp, final ClassNode current) {
    ClassNode type = null;
    if (exp instanceof ClassExpression) { type = exp.getType();
        ClassNode classType = ClassHelper.makeWithoutCaching("java.lang.Class");
        classType.setGenericsTypes(new GenericsType[] {new GenericsType(type)});
        classType.setRedirect(ClassHelper.CLASS_Type);
        return classType;
    }

    OptimizingStatementWriter.StatementMeta meta = exp.getNodeMetaData(OptimizingStatementWriter.StatementMeta.class);
    if (meta != null) type = meta.type;
    if (type != null) return type;

    if (exp instanceof VariableExpression) {
        VariableExpression ve = (VariableExpression) exp;
        if (ve.isClosureSharedVariable()) return ve.getType();
        if (ve.isSuperExpression()) return current.getSuperClass();

        type = ve.getOriginType();
    } else if (exp instanceof Variable) {
        Variable v = (Variable) exp;
        type = v.getOriginType();
    } else {
        type = exp.getType();
    }
    return type.redirect();
}
 
Example 4
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitVariableExpression(final VariableExpression expression) {
    final String variableName = expression.getName();

    if (expression.isThisExpression()) {
        // "this" in static context is Class instance
        if (controller.isStaticMethod() || controller.getCompileStack().isInSpecialConstructorCall()
                || (!controller.getCompileStack().isImplicitThis() && controller.isStaticContext())) {
            ClassNode thisType = controller.getClassNode();
            if (controller.isInGeneratedFunction()) {
                do { thisType = thisType.getOuterClass();
                } while (ClassHelper.isGeneratedFunction(thisType));
            }
            classX(thisType).visit(this);
        } else {
            loadThis(expression);
        }
        return;
    }

    if (expression.isSuperExpression()) {
        // "super" in static context is Class instance
        if (controller.isStaticMethod()) {
            ClassNode superType = controller.getClassNode().getSuperClass();
            classX(superType).visit(this);
        } else {
            loadThis(expression);
        }
        return;
    }

    BytecodeVariable variable = controller.getCompileStack().getVariable(variableName, false);
    if (variable != null) {
        controller.getOperandStack().loadOrStoreVariable(variable, expression.isUseReferenceDirectly());
    } else if (passingParams && controller.isInScriptBody()) {
        MethodVisitor mv = controller.getMethodVisitor();
        mv.visitTypeInsn(NEW, "org/codehaus/groovy/runtime/ScriptReference");
        mv.visitInsn(DUP);
        loadThisOrOwner();
        mv.visitLdcInsn(variableName);
        mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/runtime/ScriptReference", "<init>", "(Lgroovy/lang/Script;Ljava/lang/String;)V", false);
    } else {
        PropertyExpression pexp = thisPropX(true, variableName);
        pexp.getObjectExpression().setSourcePosition(expression);
        pexp.getProperty().setSourcePosition(expression);
        pexp.visit(this);
    }

    if (!controller.getCompileStack().isLHS()) {
        controller.getAssertionWriter().record(expression);
    }
}