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));
}
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);
}
}
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");
}
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;
}
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));
}
Aggregations