Java Code Examples for org.codehaus.groovy.ast.expr.PropertyExpression#getPropertyAsString()

The following examples show how to use org.codehaus.groovy.ast.expr.PropertyExpression#getPropertyAsString() . 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: VariableScopeVisitor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addExpressionOccurrences(PropertyExpression expression) {
    final Expression property = expression.getProperty();
    final String nodeAsString = expression.getPropertyAsString();
    
    if (nodeAsString != null) {
        if (leaf instanceof Variable && nodeAsString.equals(((Variable) leaf).getName())) {
            occurrences.add(property);
        } else if (leaf instanceof ConstantExpression && leafParent instanceof PropertyExpression) {
            PropertyExpression propertyUnderCursor = (PropertyExpression) leafParent;

            if (nodeAsString.equals(propertyUnderCursor.getPropertyAsString())) {
                occurrences.add(property);
            }
        }
    }
}
 
Example 2
Source File: FindVariableUsages.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void visitPropertyExpression(PropertyExpression expression) {
    final Expression objectExpression = expression.getObjectExpression();
    if (objectExpression == null) {
        return;
    }

    final String varName = expression.getPropertyAsString();
    if (objectExpression instanceof VariableExpression) {
        final VariableExpression varExpression = ((VariableExpression) objectExpression);

        final String varType;
        if ("this".equals(varExpression.getName())) { // NOI18N
            String fileName = getSourceUnit().getName();            // returns file name (e.g. Tester.groovy)
            varType = fileName.substring(0, fileName.indexOf(".")); // remove the .groovy suffix
        } else {
            varType = varExpression.getType().getName();
        }
        addIfEqual(expression.getProperty(), varType, varName);
    } else {
        // No need to check for "this" here
        addIfEqual(expression.getProperty(), objectExpression.getType().getName(), varName);
    }
    super.visitPropertyExpression(expression);
}
 
Example 3
Source File: AnnotationVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
private boolean validateEnumConstant(Expression exp) {
    if (exp instanceof PropertyExpression) {
        PropertyExpression pe = (PropertyExpression) exp;
        String name = pe.getPropertyAsString();
        if (pe.getObjectExpression() instanceof ClassExpression && name != null) {
            ClassExpression ce = (ClassExpression) pe.getObjectExpression();
            ClassNode type = ce.getType();
            if (type.isEnum()) {
                boolean ok = false;
                try {
                    FieldNode enumField = type.getDeclaredField(name);
                    ok = enumField != null && enumField.getType().equals(type);
                } catch(Exception ex) {
                    // ignore
                }
                if(!ok) {
                    addError("No enum const " + type.getName() + "." + name, pe);
                    return false;
                }
            }
        }
    }
    return true;
}
 
Example 4
Source File: ResolveVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void checkThisAndSuperAsPropertyAccess(final PropertyExpression expression) {
    if (expression.isImplicitThis()) return;
    String prop = expression.getPropertyAsString();
    if (prop == null) return;
    if (!prop.equals("this") && !prop.equals("super")) return;

    ClassNode type = expression.getObjectExpression().getType();
    if (expression.getObjectExpression() instanceof ClassExpression) {
        if (!(currentClass instanceof InnerClassNode) && !Traits.isTrait(type)) {
            addError("The usage of 'Class.this' and 'Class.super' is only allowed in nested/inner classes.", expression);
            return;
        }
        if (currentScope != null && !currentScope.isInStaticContext() && Traits.isTrait(type) && "super".equals(prop) && directlyImplementsTrait(type)) {
            return;
        }
        ClassNode iterType = currentClass;
        while (iterType != null) {
            if (iterType.equals(type)) break;
            iterType = iterType.getOuterClass();
        }
        if (iterType == null) {
            addError("The class '" + type.getName() + "' needs to be an outer class of '" +
                    currentClass.getName() + "' when using '.this' or '.super'.", expression);
        }
        if ((currentClass.getModifiers() & Opcodes.ACC_STATIC) == 0) return;
        if (currentScope != null && !currentScope.isInStaticContext()) return;
        addError("The usage of 'Class.this' and 'Class.super' within static nested class '" +
                currentClass.getName() + "' is not allowed in a static context.", expression);
    }
}
 
Example 5
Source File: VariableScopeVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * A property on "this", like this.x is transformed to a direct field access,
 * so we need to check the static context here.
 */
private void checkPropertyOnExplicitThis(final PropertyExpression expression) {
    if (!currentScope.isInStaticContext()) return;
    Expression object = expression.getObjectExpression();
    if (!(object instanceof VariableExpression)) return;
    VariableExpression ve = (VariableExpression) object;
    if (!ve.getName().equals("this")) return;
    String name = expression.getPropertyAsString();
    if (name == null || name.equals("class")) return;
    Variable member = findClassMember(currentClass, name);
    if (member == null) return;
    checkVariableContextAccess(member, expression);
}
 
Example 6
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void visitAttributeOrProperty(final PropertyExpression pexp, final MethodCallerMultiAdapter adapter) {
    ClassNode classNode = controller.getClassNode();
    String propertyName = pexp.getPropertyAsString();
    Expression objectExpression = pexp.getObjectExpression();

    if (objectExpression instanceof ClassExpression && "this".equals(propertyName)) {
        // we have something like A.B.this, and need to make it
        // into this.this$0.this$0, where this.this$0 returns
        // A.B and this.this$0.this$0 return A.
        ClassNode type = objectExpression.getType();
        if (controller.getCompileStack().isInSpecialConstructorCall() && type.equals(classNode.getOuterClass())) {
            // Outer.this in a special constructor call
            ConstructorNode ctor = controller.getConstructorNode();
            Expression receiver = !classNode.isStaticClass() ? new VariableExpression(ctor.getParameters()[0]) : new ClassExpression(type);
            receiver.setSourcePosition(pexp);
            receiver.visit(this);
            return;
        }

        MethodVisitor mv = controller.getMethodVisitor();
        mv.visitVarInsn(ALOAD, 0);
        ClassNode iterType = classNode;
        while (!iterType.equals(type)) {
            String ownerName = BytecodeHelper.getClassInternalName(iterType);
            if (iterType.getOuterClass() == null) break;
            FieldNode thisField = iterType.getField("this$0");
            iterType = iterType.getOuterClass();
            if (thisField == null) {
                // closure within inner class
                while (ClassHelper.isGeneratedFunction(iterType)) {
                    // GROOVY-8881: cater for closures within closures - getThisObject is already outer class of all closures
                    iterType = iterType.getOuterClass();
                }
                mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ClassHelper.CLOSURE_TYPE), "getThisObject", "()Ljava/lang/Object;", false);
                mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(iterType));
            } else {
                ClassNode thisFieldType = thisField.getType();
                if (ClassHelper.CLOSURE_TYPE.equals(thisFieldType)) {
                    mv.visitFieldInsn(GETFIELD, ownerName, "this$0", BytecodeHelper.getTypeDescription(ClassHelper.CLOSURE_TYPE));
                    mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ClassHelper.CLOSURE_TYPE), "getThisObject", "()Ljava/lang/Object;", false);
                    mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(iterType));
                } else {
                    String typeName = BytecodeHelper.getTypeDescription(iterType);
                    mv.visitFieldInsn(GETFIELD, ownerName, "this$0", typeName);
                }
            }
        }
        controller.getOperandStack().push(type);
        return;
    }

    if (propertyName != null) {
        // TODO: spread safe should be handled inside
        if (adapter == getProperty && !pexp.isSpreadSafe()) {
            controller.getCallSiteWriter().makeGetPropertySite(objectExpression, propertyName, pexp.isSafe(), pexp.isImplicitThis());
        } else if (adapter == getGroovyObjectProperty && !pexp.isSpreadSafe()) {
            controller.getCallSiteWriter().makeGroovyObjectGetPropertySite(objectExpression, propertyName, pexp.isSafe(), pexp.isImplicitThis());
        } else {
            controller.getCallSiteWriter().fallbackAttributeOrPropertySite(pexp, objectExpression, propertyName, adapter);
        }
    } else {
        controller.getCallSiteWriter().fallbackAttributeOrPropertySite(pexp, objectExpression, null, adapter);
    }
}
 
Example 7
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitPropertyExpression(final PropertyExpression expression) {
    Expression objectExpression = expression.getObjectExpression();
    OperandStack operandStack = controller.getOperandStack();
    int mark = operandStack.getStackLength() - 1;
    boolean visited = false;

    if (isThisOrSuper(objectExpression)) {
        String name = expression.getPropertyAsString();
        if (name != null) {
            FieldNode fieldNode = null;
            ClassNode classNode = controller.getClassNode();

            if (isThisExpression(objectExpression)) {
                if (controller.isInGeneratedFunction()) { // params are stored as fields
                    if (expression.isImplicitThis()) fieldNode = classNode.getDeclaredField(name);
                } else {
                    fieldNode = classNode.getDeclaredField(name);

                    if (fieldNode == null && !isValidFieldNodeForByteCodeAccess(classNode.getField(name), classNode)) {
                        // GROOVY-9501, GROOVY-9569
                        if (checkStaticOuterField(expression, name)) return;
                    }
                }
            } else {
                fieldNode = classNode.getSuperClass().getDeclaredField(name);
                // GROOVY-4497: do not visit super class field if it is private
                if (fieldNode != null && fieldNode.isPrivate()) fieldNode = null;

                if (fieldNode == null) {
                    visited = tryPropertyOfSuperClass(expression, name);
                }
            }

            if (fieldNode != null) {
                fieldX(fieldNode).visit(this);
                visited = true;
            }
        }
    }

    if (!visited) {
        boolean useMetaObjectProtocol = isGroovyObject(objectExpression)
                && (!isThisOrSuper(objectExpression) || !controller.isStaticContext() || controller.isInGeneratedFunction());

        MethodCallerMultiAdapter adapter;
        if (controller.getCompileStack().isLHS()) {
            adapter = useMetaObjectProtocol ? setGroovyObjectProperty : setProperty;
        } else {
            adapter = useMetaObjectProtocol ? getGroovyObjectProperty : getProperty;
        }
        visitAttributeOrProperty(expression, adapter);
    }

    if (controller.getCompileStack().isLHS()) {
        operandStack.remove(operandStack.getStackLength() - mark);
    } else {
        controller.getAssertionWriter().record(expression.getProperty());
    }
}