use of org.codehaus.groovy.ast.FieldNode in project groovy-core by groovy.
the class ImmutableASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
// if (!MY_TYPE.equals(node.getClassNode())) return;
if (!node.getClassNode().getName().endsWith(".Immutable"))
return;
List<PropertyNode> newProperties = new ArrayList<PropertyNode>();
if (parent instanceof ClassNode) {
final List<String> knownImmutableClasses = getKnownImmutableClasses(node);
final List<String> knownImmutables = getKnownImmutables(node);
ClassNode cNode = (ClassNode) parent;
String cName = cNode.getName();
if (!checkNotInterface(cNode, MY_TYPE_NAME))
return;
makeClassFinal(cNode);
final List<PropertyNode> pList = getInstanceProperties(cNode);
for (PropertyNode pNode : pList) {
adjustPropertyForImmutability(pNode, newProperties);
}
for (PropertyNode pNode : newProperties) {
cNode.getProperties().remove(pNode);
addProperty(cNode, pNode);
}
final List<FieldNode> fList = cNode.getFields();
for (FieldNode fNode : fList) {
ensureNotPublic(cName, fNode);
}
boolean includeSuperProperties = false;
if (hasAnnotation(cNode, TupleConstructorASTTransformation.MY_TYPE)) {
AnnotationNode tupleCons = cNode.getAnnotations(TupleConstructorASTTransformation.MY_TYPE).get(0);
includeSuperProperties = memberHasValue(tupleCons, "includeSuperProperties", true);
if (unsupportedTupleAttribute(tupleCons, "excludes"))
return;
if (unsupportedTupleAttribute(tupleCons, "includes"))
return;
if (unsupportedTupleAttribute(tupleCons, "includeFields"))
return;
if (unsupportedTupleAttribute(tupleCons, "includeProperties"))
return;
if (unsupportedTupleAttribute(tupleCons, "includeSuperFields"))
return;
if (unsupportedTupleAttribute(tupleCons, "callSuper"))
return;
if (unsupportedTupleAttribute(tupleCons, "force"))
return;
}
createConstructors(cNode, knownImmutableClasses, knownImmutables, includeSuperProperties);
if (!hasAnnotation(cNode, EqualsAndHashCodeASTTransformation.MY_TYPE)) {
createHashCode(cNode, true, false, false, null, null);
createEquals(cNode, false, false, false, null, null);
}
if (!hasAnnotation(cNode, ToStringASTTransformation.MY_TYPE)) {
createToString(cNode, false, false, null, null, false, true);
}
if (memberHasValue(node, MEMBER_ADD_COPY_WITH, true) && pList.size() > 0 && !hasDeclaredMethod(cNode, COPY_WITH_METHOD, 1)) {
createCopyWith(cNode, pList);
}
}
}
use of org.codehaus.groovy.ast.FieldNode 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.FieldNode in project groovy-core by groovy.
the class PackageScopeASTTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
init(nodes, source);
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode node = (AnnotationNode) nodes[0];
boolean legacyMode = LEGACY_TYPE_NAME.equals(node.getClassNode().getName());
if (!MY_TYPE.equals(node.getClassNode()) && !legacyMode)
return;
Expression value = node.getMember("value");
if (parent instanceof ClassNode) {
List<groovy.transform.PackageScopeTarget> targets;
if (value == null)
targets = Collections.singletonList(legacyMode ? PackageScopeTarget.FIELDS : PackageScopeTarget.CLASS);
else
targets = determineTargets(value);
visitClassNode((ClassNode) parent, targets);
parent.getAnnotations();
} else {
if (value != null) {
addError("Error during " + MY_TYPE_NAME + " processing: " + TARGET_CLASS_NAME + " only allowed at class level.", parent);
return;
}
if (parent instanceof MethodNode) {
visitMethodNode((MethodNode) parent);
} else if (parent instanceof FieldNode) {
visitFieldNode((FieldNode) parent);
}
}
}
use of org.codehaus.groovy.ast.FieldNode in project groovy-core by groovy.
the class ReadWriteLockASTTransformation method determineLock.
private String determineLock(String value, ClassNode targetClass, boolean isStatic, String myTypeName) {
if (value != null && value.length() > 0 && !value.equalsIgnoreCase(DEFAULT_INSTANCE_LOCKNAME)) {
FieldNode existingLockField = targetClass.getDeclaredField(value);
if (existingLockField == null) {
addError("Error during " + myTypeName + " processing: lock field with name '" + value + "' not found in class " + targetClass.getName(), targetClass);
return null;
}
if (existingLockField.isStatic() != isStatic) {
addError("Error during " + myTypeName + " processing: lock field with name '" + value + "' should " + (isStatic ? "" : "not ") + "be static", existingLockField);
return null;
}
return value;
}
if (isStatic) {
FieldNode field = targetClass.getDeclaredField(DEFAULT_STATIC_LOCKNAME);
if (field == null) {
int visibility = ACC_PRIVATE | ACC_STATIC | ACC_FINAL;
targetClass.addField(DEFAULT_STATIC_LOCKNAME, visibility, LOCK_TYPE, createLockObject());
} else if (!field.isStatic()) {
addError("Error during " + myTypeName + " processing: " + DEFAULT_STATIC_LOCKNAME + " field must be static", field);
return null;
}
return DEFAULT_STATIC_LOCKNAME;
}
FieldNode field = targetClass.getDeclaredField(DEFAULT_INSTANCE_LOCKNAME);
if (field == null) {
int visibility = ACC_PRIVATE | ACC_FINAL;
targetClass.addField(DEFAULT_INSTANCE_LOCKNAME, visibility, LOCK_TYPE, createLockObject());
} else if (field.isStatic()) {
addError("Error during " + myTypeName + " processing: " + DEFAULT_INSTANCE_LOCKNAME + " field must not be static", field);
return null;
}
return DEFAULT_INSTANCE_LOCKNAME;
}
use of org.codehaus.groovy.ast.FieldNode in project groovy-core by groovy.
the class SortableASTTransformation method createComparatorFor.
private static void createComparatorFor(ClassNode classNode, PropertyNode property) {
String propName = property.getName();
String className = classNode.getName() + "$" + StringGroovyMethods.capitalize(propName) + "Comparator";
ClassNode superClass = makeClassSafeWithGenerics(AbstractComparator.class, classNode);
InnerClassNode cmpClass = new InnerClassNode(classNode, className, ACC_PRIVATE | ACC_STATIC, superClass);
classNode.getModule().addClass(cmpClass);
cmpClass.addMethod(new MethodNode("compare", ACC_PUBLIC, ClassHelper.int_TYPE, params(param(newClass(classNode), ARG0), param(newClass(classNode), ARG1)), ClassNode.EMPTY_ARRAY, createCompareMethodBody(property)));
String fieldName = "this$" + StringGroovyMethods.capitalize(propName) + "Comparator";
// private final Comparator this$<property>Comparator = new <type>$<property>Comparator();
FieldNode cmpField = classNode.addField(fieldName, ACC_STATIC | ACC_FINAL | ACC_PRIVATE | ACC_SYNTHETIC, COMPARATOR_TYPE, ctorX(cmpClass));
classNode.addMethod(new MethodNode("comparatorBy" + StringGroovyMethods.capitalize(propName), ACC_PUBLIC | ACC_STATIC, COMPARATOR_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, returnS(fieldX(cmpField))));
}
Aggregations