Search in sources :

Example 6 with PropertyNode

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

the class ImmutableASTTransformation method createConstructorOrdered.

private static void createConstructorOrdered(ClassNode cNode, List<PropertyNode> list) {
    final MapExpression argMap = new MapExpression();
    final Parameter[] orderedParams = new Parameter[list.size()];
    int index = 0;
    for (PropertyNode pNode : list) {
        Parameter param = new Parameter(pNode.getField().getType(), pNode.getField().getName());
        orderedParams[index++] = param;
        argMap.addMapEntryExpression(constX(pNode.getName()), varX(pNode.getName()));
    }
    final BlockStatement orderedBody = new BlockStatement();
    orderedBody.addStatement(stmt(ctorX(ClassNode.THIS, args(castX(HASHMAP_TYPE, argMap)))));
    doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, orderedParams, ClassNode.EMPTY_ARRAY, orderedBody));
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 7 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 8 with PropertyNode

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

the class PropertyTest method testProperties.

public void testProperties() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC + ACC_SUPER, ClassHelper.OBJECT_TYPE);
    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, "bar", null);
    assertSetProperty(bean, "bar", "newValue");
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode)

Example 9 with PropertyNode

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

the class TraitComposer method isExistingProperty.

private static boolean isExistingProperty(final String methodName, final ClassNode cNode, final Parameter[] params) {
    String propertyName = methodName;
    boolean getter = false;
    if (methodName.startsWith("get")) {
        propertyName = propertyName.substring(3);
        getter = true;
    } else if (methodName.startsWith("is")) {
        propertyName = propertyName.substring(2);
        getter = true;
    } else if (methodName.startsWith("set")) {
        propertyName = propertyName.substring(3);
    } else {
        return false;
    }
    if (getter && params.length > 0) {
        return false;
    }
    if (!getter && params.length != 1) {
        return false;
    }
    if (propertyName.length() == 0) {
        return false;
    }
    propertyName = MetaClassHelper.convertPropertyName(propertyName);
    PropertyNode pNode = cNode.getProperty(propertyName);
    return pNode != null;
}
Also used : PropertyNode(org.codehaus.groovy.ast.PropertyNode)

Example 10 with PropertyNode

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

the class VetoableASTTransformation method addListenerToProperty.

private void addListenerToProperty(SourceUnit source, AnnotationNode node, AnnotatedNode parent) {
    ClassNode declaringClass = parent.getDeclaringClass();
    FieldNode field = ((FieldNode) parent);
    String fieldName = field.getName();
    for (PropertyNode propertyNode : declaringClass.getProperties()) {
        boolean bindable = BindableASTTransformation.hasBindableAnnotation(parent) || BindableASTTransformation.hasBindableAnnotation(parent.getDeclaringClass());
        if (propertyNode.getName().equals(fieldName)) {
            if (field.isStatic()) {
                //noinspection ThrowableInstanceNeverThrown
                source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Vetoable cannot annotate a static property.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
            } else {
                createListenerSetter(source, bindable, declaringClass, propertyNode);
            }
            return;
        }
    }
    //noinspection ThrowableInstanceNeverThrown
    source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Vetoable must be on a property, not a field.  Try removing the private, protected, or public modifier.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) PropertyNode(org.codehaus.groovy.ast.PropertyNode) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

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