use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.
the class ImmutableASTTransformation method createCopyWith.
private void createCopyWith(final ClassNode cNode, final List<PropertyNode> pList) {
BlockStatement body = new BlockStatement();
body.addStatement(ifS(orX(equalsNullX(varX("map", ClassHelper.MAP_TYPE)), eqX(callX(varX("map", HASHMAP_TYPE), "size"), constX(0))), returnS(varX("this", cNode))));
body.addStatement(declS(varX("dirty", ClassHelper.boolean_TYPE), ConstantExpression.PRIM_FALSE));
body.addStatement(declS(varX("construct", HASHMAP_TYPE), ctorX(HASHMAP_TYPE)));
// Check for each property
for (final PropertyNode pNode : pList) {
body.addStatement(createCheckForProperty(pNode));
}
body.addStatement(returnS(ternaryX(isTrueX(varX("dirty", ClassHelper.boolean_TYPE)), ctorX(cNode, args(varX("construct", HASHMAP_TYPE))), varX("this", cNode))));
final ClassNode clonedNode = cNode.getPlainNodeReference();
cNode.addMethod(COPY_WITH_METHOD, ACC_PUBLIC | ACC_FINAL, clonedNode, params(new Parameter(new ClassNode(Map.class), "map")), null, body);
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.
the class ImmutableASTTransformation method createConstructors.
private void createConstructors(ClassNode cNode, List<String> knownImmutableClasses, List<String> knownImmutables, boolean includeSuperProperties) {
if (!validateConstructors(cNode))
return;
List<PropertyNode> list = getInstanceProperties(cNode);
if (includeSuperProperties) {
ClassNode next = cNode.getSuperClass();
while (next != null) {
List<PropertyNode> tail = list;
list = getInstanceProperties(next);
list.addAll(tail);
next = next.getSuperClass();
}
}
boolean specialHashMapCase = list.size() == 1 && list.get(0).getField().getType().equals(HASHMAP_TYPE);
if (specialHashMapCase) {
createConstructorMapSpecial(cNode, list);
} else {
createConstructorMap(cNode, list, knownImmutableClasses, knownImmutables);
createConstructorOrdered(cNode, list);
}
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.
the class PackageScopeASTTransformation method visitClassNode.
private void visitClassNode(ClassNode cNode, List<PackageScopeTarget> value) {
String cName = cNode.getName();
if (cNode.isInterface() && value.size() != 1 && value.get(0) != PackageScopeTarget.CLASS) {
addError("Error processing interface '" + cName + "'. " + MY_TYPE_NAME + " not allowed for interfaces except when targeting Class level.", cNode);
}
if (value.contains(groovy.transform.PackageScopeTarget.CLASS)) {
if (cNode.isSyntheticPublic())
revertVisibility(cNode);
else
addError("Can't use " + MY_TYPE_NAME + " for class '" + cNode.getName() + "' which has explicit visibility.", cNode);
}
if (value.contains(groovy.transform.PackageScopeTarget.METHODS)) {
final List<MethodNode> mList = cNode.getMethods();
for (MethodNode mNode : mList) {
if (mNode.isSyntheticPublic())
revertVisibility(mNode);
}
}
if (value.contains(groovy.transform.PackageScopeTarget.CONSTRUCTORS)) {
final List<ConstructorNode> cList = cNode.getDeclaredConstructors();
for (MethodNode mNode : cList) {
if (mNode.isSyntheticPublic())
revertVisibility(mNode);
}
}
if (value.contains(PackageScopeTarget.FIELDS)) {
final List<PropertyNode> pList = cNode.getProperties();
List<PropertyNode> foundProps = new ArrayList<PropertyNode>();
List<String> foundNames = new ArrayList<String>();
for (PropertyNode pNode : pList) {
foundProps.add(pNode);
foundNames.add(pNode.getName());
}
for (PropertyNode pNode : foundProps) {
pList.remove(pNode);
}
final List<FieldNode> fList = cNode.getFields();
for (FieldNode fNode : fList) {
if (foundNames.contains(fNode.getName())) {
revertVisibility(fNode);
}
}
}
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.
the class PackageScopeASTTransformation method visitFieldNode.
private void visitFieldNode(FieldNode fNode) {
final ClassNode cNode = fNode.getDeclaringClass();
final List<PropertyNode> pList = cNode.getProperties();
PropertyNode foundProp = null;
for (PropertyNode pNode : pList) {
if (pNode.getName().equals(fNode.getName())) {
foundProp = pNode;
break;
}
}
if (foundProp != null) {
revertVisibility(fNode);
pList.remove(foundProp);
}
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.
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");
}
Aggregations