Search in sources :

Example 36 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.

the class ImmutableASTTransformation method createConstructorMap.

private void createConstructorMap(ClassNode cNode, List<PropertyNode> list, List<String> knownImmutableClasses, List<String> knownImmutables) {
    final BlockStatement body = new BlockStatement();
    body.addStatement(ifS(equalsNullX(varX("args")), assignS(varX("args"), new MapExpression())));
    for (PropertyNode pNode : list) {
        body.addStatement(createConstructorStatement(cNode, pNode, knownImmutableClasses, knownImmutables));
    }
    // check for missing properties
    body.addStatement(stmt(callX(SELF_TYPE, "checkPropNames", args("this", "args"))));
    createConstructorMapCommon(cNode, body);
    if (!list.isEmpty()) {
        createNoArgConstructor(cNode);
    }
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 37 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.

the class PropertyTest method testInheritedProperties.

public void testInheritedProperties() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC + ACC_SUPER, ClassHelper.make(DummyBean.class));
    classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);
    Object bean = fooClass.newInstance();
    assertTrue("Managed to create bean", bean != null);
    assertField(fooClass, "bar", 0, ClassHelper.STRING_TYPE);
    assertGetProperty(bean, "name", "James");
    assertSetProperty(bean, "name", "Bob");
    assertGetProperty(bean, "bar", null);
    assertSetProperty(bean, "bar", "newValue");
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) DummyBean(org.codehaus.groovy.runtime.DummyBean)

Example 38 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project grails-core by grails.

the class PluginAstReader method visitContents.

protected void visitContents(String className, final ClassNode classNode) {
    ClassCodeVisitorSupport visitor = new ClassCodeVisitorSupport() {

        @Override
        public void visitProperty(PropertyNode node) {
            String name = node.getName();
            final Expression expr = node.getField().getInitialExpression();
            if (expr != null) {
                Object value = null;
                if (expr instanceof ListExpression) {
                    final List<String> list = new ArrayList<String>();
                    for (Expression i : ((ListExpression) expr).getExpressions()) {
                        list.add(i.getText());
                    }
                    value = list;
                } else if (expr instanceof MapExpression) {
                    final Map<String, String> map = new LinkedHashMap<String, String>();
                    value = map;
                    for (MapEntryExpression mee : ((MapExpression) expr).getMapEntryExpressions()) {
                        map.put(mee.getKeyExpression().getText(), mee.getValueExpression().getText());
                    }
                } else {
                    if (expr instanceof ConstantExpression) {
                        value = expr.getText();
                    }
                }
                if (value != null) {
                    pluginInfo.setProperty(name, value);
                    super.visitProperty(node);
                }
            }
        }

        @Override
        protected SourceUnit getSourceUnit() {
            return classNode.getModule().getContext();
        }
    };
    classNode.visitContents(visitor);
    pluginInfo.setName(GrailsNameUtils.getPluginName(className + ".groovy"));
}
Also used : ClassCodeVisitorSupport(org.codehaus.groovy.ast.ClassCodeVisitorSupport) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 39 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.

the class StaticTypeCheckingVisitor method existsProperty.

/**
     * Checks whether a property exists on the receiver, or on any of the possible receiver classes (found in the
     * temporary type information table)
     *
     * @param pexp             a property expression
     * @param readMode         if true, look for property read, else for property set
     * @param visitor          if not null, when the property node is found, visit it with the provided visitor
     * @return true if the property is defined in any of the possible receiver classes
     */
protected boolean existsProperty(final PropertyExpression pexp, final boolean readMode, final ClassCodeVisitorSupport visitor) {
    super.visitPropertyExpression(pexp);
    String propertyName = pexp.getPropertyAsString();
    if (propertyName == null)
        return false;
    Expression objectExpression = pexp.getObjectExpression();
    final ClassNode objectExpressionType = getType(objectExpression);
    boolean staticOnlyAccess = isClassClassNodeWrappingConcreteType(objectExpressionType);
    if ("this".equals(propertyName) && staticOnlyAccess) {
        // Outer.this
        ClassNode outerNode = objectExpressionType.getGenericsTypes()[0].getType();
        ClassNode current = typeCheckingContext.getEnclosingClassNode();
        if (!current.isStaticClass() && current instanceof InnerClassNode) {
            InnerClassNode icn = (InnerClassNode) current;
            if (outerNode.equals(icn.getOuterClass())) {
                storeType(pexp, outerNode);
                return true;
            }
        }
    }
    if (objectExpressionType.isArray() && "length".equals(pexp.getPropertyAsString())) {
        storeType(pexp, int_TYPE);
        if (visitor != null) {
            PropertyNode node = new PropertyNode("length", Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, int_TYPE, objectExpressionType, null, null, null);
            visitor.visitProperty(node);
        }
        return true;
    }
    boolean foundGetterOrSetter = false;
    List<Receiver<String>> receivers = new LinkedList<Receiver<String>>();
    List<Receiver<String>> owners = makeOwnerList(objectExpression);
    addReceivers(receivers, owners, pexp.isImplicitThis());
    String capName = MetaClassHelper.capitalize(propertyName);
    boolean isAttributeExpression = pexp instanceof AttributeExpression;
    HashSet<ClassNode> handledNodes = new HashSet<ClassNode>();
    for (Receiver<String> receiver : receivers) {
        ClassNode testClass = receiver.getType();
        LinkedList<ClassNode> queue = new LinkedList<ClassNode>();
        queue.add(testClass);
        if (isPrimitiveType(testClass)) {
            queue.add(getWrapper(testClass));
        }
        while (!queue.isEmpty()) {
            ClassNode current = queue.removeFirst();
            if (handledNodes.contains(current))
                continue;
            handledNodes.add(current);
            Set<ClassNode> allInterfaces = current.getAllInterfaces();
            for (ClassNode intf : allInterfaces) {
                //TODO: apply right generics here!
                queue.add(GenericsUtils.parameterizeType(current, intf));
            }
            // in case of a lookup on Class we look for instance methods on Class
            // as well, since in case of a static property access we have the class
            // itself in the list of receivers already;
            boolean staticOnly;
            if (isClassClassNodeWrappingConcreteType(current)) {
                staticOnly = false;
            } else {
                staticOnly = staticOnlyAccess;
            }
            FieldNode field = current.getDeclaredField(propertyName);
            field = allowStaticAccessToMember(field, staticOnly);
            if (storeField(field, isAttributeExpression, pexp, current, visitor, receiver.getData()))
                return true;
            PropertyNode propertyNode = current.getProperty(propertyName);
            propertyNode = allowStaticAccessToMember(propertyNode, staticOnly);
            if (storeProperty(propertyNode, pexp, current, visitor, receiver.getData()))
                return true;
            boolean isThisExpression = objectExpression instanceof VariableExpression && ((VariableExpression) objectExpression).isThisExpression();
            if (storeField(field, isThisExpression, pexp, receiver.getType(), visitor, receiver.getData()))
                return true;
            MethodNode getter = current.getGetterMethod("get" + capName);
            getter = allowStaticAccessToMember(getter, staticOnly);
            if (getter == null)
                getter = current.getGetterMethod("is" + capName);
            getter = allowStaticAccessToMember(getter, staticOnly);
            final String setterName = "set" + capName;
            List<MethodNode> setters = findSetters(current, setterName, false);
            setters = allowStaticAccessToMember(setters, staticOnly);
            // need to visit even if we only look for a setters for compatibility
            if (visitor != null && getter != null)
                visitor.visitMethod(getter);
            if (readMode) {
                if (getter != null) {
                    ClassNode cn = inferReturnTypeGenerics(current, getter, ArgumentListExpression.EMPTY_ARGUMENTS);
                    storeInferredTypeForPropertyExpression(pexp, cn);
                    pexp.removeNodeMetaData(StaticTypesMarker.READONLY_PROPERTY);
                    String delegationData = receiver.getData();
                    if (delegationData != null)
                        pexp.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, delegationData);
                    return true;
                }
            } else {
                if (!setters.isEmpty()) {
                    if (visitor != null) {
                        if (field != null) {
                            visitor.visitField(field);
                        } else {
                            for (MethodNode setter : setters) {
                                ClassNode setterType = setter.getParameters()[0].getOriginType();
                                FieldNode virtual = new FieldNode(propertyName, 0, setterType, current, EmptyExpression.INSTANCE);
                                visitor.visitField(virtual);
                            }
                        }
                    }
                    //TODO: apply generics on parameter[0]?
                    //                                storeType(pexp, setter.getParameters()[0].getType());
                    SetterInfo info = new SetterInfo(current, setterName, setters);
                    BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression();
                    if (enclosingBinaryExpression != null) {
                        putSetterInfo(enclosingBinaryExpression.getLeftExpression(), info);
                    }
                    String delegationData = receiver.getData();
                    if (delegationData != null) {
                        pexp.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, delegationData);
                    }
                    return true;
                } else if (getter != null) {
                    pexp.putNodeMetaData(StaticTypesMarker.READONLY_PROPERTY, true);
                }
            }
            foundGetterOrSetter = foundGetterOrSetter || !setters.isEmpty() || getter != null;
            if (storeField(field, true, pexp, current, visitor, receiver.getData()))
                return true;
            // we stop now, otherwise we must check the parent class
            if (/*!isAttributeExpression && */
            current.getSuperClass() != null) {
                queue.add(current.getUnresolvedSuperClass());
            }
        }
        // GROOVY-5568, the property may be defined by DGM
        List<MethodNode> methods = findDGMMethodsByNameAndArguments(getTransformLoader(), testClass, "get" + capName, ClassNode.EMPTY_ARRAY);
        if (!methods.isEmpty()) {
            List<MethodNode> methodNodes = chooseBestMethod(testClass, methods, ClassNode.EMPTY_ARRAY);
            if (methodNodes.size() == 1) {
                MethodNode getter = methodNodes.get(0);
                if (visitor != null) {
                    visitor.visitMethod(getter);
                }
                ClassNode cn = inferReturnTypeGenerics(testClass, getter, ArgumentListExpression.EMPTY_ARGUMENTS);
                storeInferredTypeForPropertyExpression(pexp, cn);
                return true;
            }
        }
    }
    for (Receiver<String> receiver : receivers) {
        ClassNode testClass = receiver.getType();
        ClassNode propertyType = getTypeForMapPropertyExpression(testClass, objectExpressionType, pexp);
        if (propertyType == null)
            propertyType = getTypeForListPropertyExpression(testClass, objectExpressionType, pexp);
        if (propertyType == null)
            propertyType = getTypeForSpreadExpression(testClass, objectExpressionType, pexp);
        if (propertyType == null)
            continue;
        if (visitor != null) {
            // todo : type inference on maps and lists, if possible
            PropertyNode node = new PropertyNode(propertyName, Opcodes.ACC_PUBLIC, propertyType, receiver.getType(), null, null, null);
            node.setDeclaringClass(receiver.getType());
            visitor.visitProperty(node);
        }
        storeType(pexp, propertyType);
        String delegationData = receiver.getData();
        if (delegationData != null)
            pexp.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, delegationData);
        return true;
    }
    return foundGetterOrSetter;
}
Also used : LowestUpperBoundClassNode(org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) LinkedList(java.util.LinkedList) MethodNode(org.codehaus.groovy.ast.MethodNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 40 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.

the class StaticTypeCheckingVisitor method visitVariableExpression.

@Override
public void visitVariableExpression(VariableExpression vexp) {
    super.visitVariableExpression(vexp);
    if (storeTypeForThis(vexp))
        return;
    if (storeTypeForSuper(vexp))
        return;
    if (vexp.getAccessedVariable() instanceof PropertyNode) {
        // overloaded setters, the type of the property node is arbitrary!
        if (tryVariableExpressionAsProperty(vexp, vexp.getName())) {
            BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression();
            if (enclosingBinaryExpression != null) {
                Expression leftExpression = enclosingBinaryExpression.getLeftExpression();
                Expression rightExpression = enclosingBinaryExpression.getRightExpression();
                SetterInfo setterInfo = removeSetterInfo(leftExpression);
                if (setterInfo != null) {
                    if (!ensureValidSetter(vexp, leftExpression, rightExpression, setterInfo)) {
                        return;
                    }
                }
            }
        }
    }
    TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
    if (enclosingClosure != null) {
        String name = vexp.getName();
        if (name.equals("owner") || name.equals("thisObject")) {
            storeType(vexp, typeCheckingContext.getEnclosingClassNode());
            return;
        } else if ("delegate".equals(name)) {
            DelegationMetadata md = getDelegationMetadata(enclosingClosure.getClosureExpression());
            ClassNode type = typeCheckingContext.getEnclosingClassNode();
            if (md != null)
                type = md.getType();
            storeType(vexp, type);
            return;
        }
    }
    if (!(vexp.getAccessedVariable() instanceof DynamicVariable))
        return;
    // a dynamic variable is either an undeclared variable
    // or a member of a class used in a 'with'
    DynamicVariable dyn = (DynamicVariable) vexp.getAccessedVariable();
    // first, we must check the 'with' context
    String dynName = dyn.getName();
    if (tryVariableExpressionAsProperty(vexp, dynName))
        return;
    if (!extension.handleUnresolvedVariableExpression(vexp)) {
        addStaticTypeError("The variable [" + vexp.getName() + "] is undeclared.", vexp);
    }
}
Also used : LowestUpperBoundClassNode(org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) PropertyNode(org.codehaus.groovy.ast.PropertyNode)

Aggregations

PropertyNode (org.codehaus.groovy.ast.PropertyNode)71 ClassNode (org.codehaus.groovy.ast.ClassNode)36 FieldNode (org.codehaus.groovy.ast.FieldNode)30 ArrayList (java.util.ArrayList)25 MethodNode (org.codehaus.groovy.ast.MethodNode)19 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)16 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)12 Parameter (org.codehaus.groovy.ast.Parameter)11 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)10 Expression (org.codehaus.groovy.ast.expr.Expression)10 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)8 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)7 ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)6 DynamicVariable (org.codehaus.groovy.ast.DynamicVariable)6 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)6 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)6 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)6 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)6 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)6 HashSet (java.util.HashSet)5