Search in sources :

Example 1 with PropertyExpression

use of org.codehaus.groovy.ast.expr.PropertyExpression in project groovy by apache.

the class AnnotationVisitor method validateEnumConstant.

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;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 2 with PropertyExpression

use of org.codehaus.groovy.ast.expr.PropertyExpression in project groovy by apache.

the class GeneralUtils method copyAnnotatedNodeAnnotations.

/**
     * Copies all <tt>candidateAnnotations</tt> with retention policy {@link java.lang.annotation.RetentionPolicy#RUNTIME}
     * and {@link java.lang.annotation.RetentionPolicy#CLASS}.
     * <p>
     * Annotations with {@link org.codehaus.groovy.runtime.GeneratedClosure} members are not supported at present.
     */
public static void copyAnnotatedNodeAnnotations(final AnnotatedNode annotatedNode, final List<AnnotationNode> copied, List<AnnotationNode> notCopied) {
    List<AnnotationNode> annotationList = annotatedNode.getAnnotations();
    for (AnnotationNode annotation : annotationList) {
        List<AnnotationNode> annotations = annotation.getClassNode().getAnnotations(AbstractASTTransformation.RETENTION_CLASSNODE);
        if (annotations.isEmpty())
            continue;
        if (hasClosureMember(annotation)) {
            notCopied.add(annotation);
            continue;
        }
        AnnotationNode retentionPolicyAnnotation = annotations.get(0);
        Expression valueExpression = retentionPolicyAnnotation.getMember("value");
        if (!(valueExpression instanceof PropertyExpression))
            continue;
        PropertyExpression propertyExpression = (PropertyExpression) valueExpression;
        boolean processAnnotation = propertyExpression.getProperty() instanceof ConstantExpression && ("RUNTIME".equals(((ConstantExpression) (propertyExpression.getProperty())).getValue()) || "CLASS".equals(((ConstantExpression) (propertyExpression.getProperty())).getValue()));
        if (processAnnotation) {
            AnnotationNode newAnnotation = new AnnotationNode(annotation.getClassNode());
            for (Map.Entry<String, Expression> member : annotation.getMembers().entrySet()) {
                newAnnotation.addMember(member.getKey(), member.getValue());
            }
            newAnnotation.setSourcePosition(annotatedNode);
            copied.add(newAnnotation);
        }
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) Map(java.util.Map)

Example 3 with PropertyExpression

use of org.codehaus.groovy.ast.expr.PropertyExpression in project groovy by apache.

the class VariableExpressionTransformer method tryTransformDelegateToProperty.

private static Expression tryTransformDelegateToProperty(VariableExpression expr) {
    // we need to transform variable expressions that go to a delegate
    // to a property expression, as ACG would loose the information
    // in processClassVariable before it reaches any makeCall, that could
    // handle it
    Object val = expr.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
    if (val == null)
        return null;
    VariableExpression implicitThis = new VariableExpression("this");
    PropertyExpression pexp = new PropertyExpression(implicitThis, expr.getName());
    pexp.copyNodeMetaData(expr);
    pexp.setImplicitThis(true);
    ClassNode owner = expr.getNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER);
    if (owner != null) {
        implicitThis.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, owner);
        implicitThis.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, val);
    }
    return pexp;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 4 with PropertyExpression

use of org.codehaus.groovy.ast.expr.PropertyExpression in project groovy by apache.

the class StaticImportVisitor method transformMethodCallExpression.

protected Expression transformMethodCallExpression(MethodCallExpression mce) {
    Expression args = transform(mce.getArguments());
    Expression method = transform(mce.getMethod());
    Expression object = transform(mce.getObjectExpression());
    boolean isExplicitThisOrSuper = false;
    boolean isExplicitSuper = false;
    if (object instanceof VariableExpression) {
        VariableExpression ve = (VariableExpression) object;
        isExplicitThisOrSuper = !mce.isImplicitThis() && (ve.isThisExpression() || ve.isSuperExpression());
        isExplicitSuper = ve.isSuperExpression();
    }
    if (mce.isImplicitThis() || isExplicitThisOrSuper) {
        if (mce.isImplicitThis()) {
            Expression ret = findStaticMethodImportFromModule(method, args);
            if (ret != null) {
                setSourcePosition(ret, mce);
                return ret;
            }
            if (method instanceof ConstantExpression && !inLeftExpression) {
                // could be a closure field
                String methodName = (String) ((ConstantExpression) method).getValue();
                ret = findStaticFieldOrPropAccessorImportFromModule(methodName);
                if (ret != null) {
                    ret = new MethodCallExpression(ret, "call", args);
                    setSourcePosition(ret, mce);
                    return ret;
                }
            }
        } else if (currentMethod != null && currentMethod.isStatic() && isExplicitSuper) {
            MethodCallExpression ret = new MethodCallExpression(new ClassExpression(currentClass.getSuperClass()), method, args);
            setSourcePosition(ret, mce);
            return ret;
        }
        if (method instanceof ConstantExpression) {
            ConstantExpression ce = (ConstantExpression) method;
            Object value = ce.getValue();
            if (value instanceof String) {
                boolean foundInstanceMethod = false;
                String methodName = (String) value;
                boolean inInnerClass = isInnerClass(currentClass);
                if (currentMethod != null && !currentMethod.isStatic()) {
                    if (currentClass.hasPossibleMethod(methodName, args)) {
                        foundInstanceMethod = true;
                    }
                }
                boolean lookForPossibleStaticMethod = !methodName.equals("call");
                lookForPossibleStaticMethod &= !foundInstanceMethod;
                lookForPossibleStaticMethod |= inSpecialConstructorCall;
                lookForPossibleStaticMethod &= !inInnerClass;
                if (!inClosure && lookForPossibleStaticMethod && (hasPossibleStaticMethod(currentClass, methodName, args, true)) || hasPossibleStaticProperty(currentClass, methodName)) {
                    StaticMethodCallExpression smce = new StaticMethodCallExpression(currentClass, methodName, args);
                    setSourcePosition(smce, mce);
                    return smce;
                }
                if (!inClosure && inInnerClass && inSpecialConstructorCall && mce.isImplicitThis() && !foundInstanceMethod) {
                    if (currentClass.getOuterClass().hasPossibleMethod(methodName, args)) {
                        object = new PropertyExpression(new ClassExpression(currentClass.getOuterClass()), new ConstantExpression("this"));
                    } else if (hasPossibleStaticMethod(currentClass.getOuterClass(), methodName, args, true) || hasPossibleStaticProperty(currentClass.getOuterClass(), methodName)) {
                        StaticMethodCallExpression smce = new StaticMethodCallExpression(currentClass.getOuterClass(), methodName, args);
                        setSourcePosition(smce, mce);
                        return smce;
                    }
                }
            }
        }
    }
    MethodCallExpression result = new MethodCallExpression(object, method, args);
    result.setSafe(mce.isSafe());
    result.setImplicitThis(mce.isImplicitThis());
    result.setSpreadSafe(mce.isSpreadSafe());
    result.setMethodTarget(mce.getMethodTarget());
    // GROOVY-6757
    result.setGenericsTypes(mce.getGenericsTypes());
    setSourcePosition(result, mce);
    return result;
}
Also used : StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 5 with PropertyExpression

use of org.codehaus.groovy.ast.expr.PropertyExpression in project groovy by apache.

the class BinaryExpressionHelper method execMethodAndStoreForSubscriptOperator.

private void execMethodAndStoreForSubscriptOperator(int op, String method, Expression expression, VariableSlotLoader usesSubscript, Expression orig) {
    final OperandStack operandStack = controller.getOperandStack();
    writePostOrPrefixMethod(op, method, expression, orig);
    // we need special code for arrays to store the result (like for a[1]++)
    if (usesSubscript != null) {
        CompileStack compileStack = controller.getCompileStack();
        BinaryExpression be = (BinaryExpression) expression;
        ClassNode methodResultType = operandStack.getTopOperand();
        final int resultIdx = compileStack.defineTemporaryVariable("postfix_" + method, methodResultType, true);
        BytecodeExpression methodResultLoader = new VariableSlotLoader(methodResultType, resultIdx, operandStack);
        // execute the assignment, this will leave the right side 
        // (here the method call result) on the stack
        assignToArray(be, be.getLeftExpression(), usesSubscript, methodResultLoader);
        compileStack.removeVar(resultIdx);
    } else // here we handle a.b++ and a++
    if (expression instanceof VariableExpression || expression instanceof FieldExpression || expression instanceof PropertyExpression) {
        operandStack.dup();
        controller.getCompileStack().pushLHS(true);
        expression.visit(controller.getAcg());
        controller.getCompileStack().popLHS();
    }
// other cases don't need storing, so nothing to be done for them
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Aggregations

PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)51 Expression (org.codehaus.groovy.ast.expr.Expression)41 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)39 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)37 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)30 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)26 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)26 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)25 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)24 ClassNode (org.codehaus.groovy.ast.ClassNode)23 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)22 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)17 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)15 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)14 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)13 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)10 TernaryExpression (org.codehaus.groovy.ast.expr.TernaryExpression)10 FieldNode (org.codehaus.groovy.ast.FieldNode)9 FieldExpression (org.codehaus.groovy.ast.expr.FieldExpression)8 MapEntryExpression (org.codehaus.groovy.ast.expr.MapEntryExpression)8