use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.
the class PackageScopeASTTransformation method visitFieldNode.
private static 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 by apache.
the class SortableASTTransformation method createCompareToMethodBody.
private static Statement createCompareToMethodBody(List<PropertyNode> properties) {
List<Statement> statements = new ArrayList<Statement>();
// if (this.is(other)) return 0;
statements.add(ifS(callThisX("is", args(OTHER)), returnS(constX(0))));
if (properties.isEmpty()) {
// perhaps overkill but let compareTo be based on hashes for commutativity
// return this.hashCode() <=> other.hashCode()
statements.add(declS(varX(THIS_HASH, ClassHelper.Integer_TYPE), callX(varX("this"), "hashCode")));
statements.add(declS(varX(OTHER_HASH, ClassHelper.Integer_TYPE), callX(varX(OTHER), "hashCode")));
statements.add(returnS(cmpX(varX(THIS_HASH), varX(OTHER_HASH))));
} else {
// int value = 0;
statements.add(declS(varX(VALUE, ClassHelper.int_TYPE), constX(0)));
for (PropertyNode property : properties) {
String propName = property.getName();
// value = this.prop <=> other.prop;
statements.add(assignS(varX(VALUE), cmpX(propX(varX("this"), propName), propX(varX(OTHER), propName))));
// if (value != 0) return value;
statements.add(ifS(neX(varX(VALUE), constX(0)), returnS(varX(VALUE))));
}
// objects are equal
statements.add(returnS(constX(0)));
}
final BlockStatement body = new BlockStatement();
body.addStatements(statements);
return body;
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.
the class SortableASTTransformation method findProperties.
private List<PropertyNode> findProperties(AnnotationNode annotation, ClassNode classNode, final List<String> includes, final List<String> excludes) {
List<PropertyNode> properties = new ArrayList<PropertyNode>();
for (PropertyNode property : classNode.getProperties()) {
String propertyName = property.getName();
if (property.isStatic() || (excludes != null && excludes.contains(propertyName)) || includes != null && !includes.contains(propertyName))
continue;
properties.add(property);
}
for (PropertyNode pNode : properties) {
checkComparable(pNode);
}
if (includes != null) {
Comparator<PropertyNode> includeComparator = new Comparator<PropertyNode>() {
public int compare(PropertyNode o1, PropertyNode o2) {
return new Integer(includes.indexOf(o1.getName())).compareTo(includes.indexOf(o2.getName()));
}
};
Collections.sort(properties, includeComparator);
}
return properties;
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.
the class SortableASTTransformation method createSortable.
private void createSortable(AnnotationNode annotation, ClassNode classNode) {
List<String> includes = getMemberStringList(annotation, "includes");
List<String> excludes = getMemberStringList(annotation, "excludes");
if (!checkIncludeExcludeUndefinedAware(annotation, excludes, includes, MY_TYPE_NAME))
return;
if (!checkPropertyList(classNode, includes, "includes", annotation, MY_TYPE_NAME, false))
return;
if (!checkPropertyList(classNode, excludes, "excludes", annotation, MY_TYPE_NAME, false))
return;
if (classNode.isInterface()) {
addError(MY_TYPE_NAME + " cannot be applied to interface " + classNode.getName(), annotation);
}
List<PropertyNode> properties = findProperties(annotation, classNode, includes, excludes);
implementComparable(classNode);
classNode.addMethod(new MethodNode("compareTo", ACC_PUBLIC, ClassHelper.int_TYPE, params(param(newClass(classNode), OTHER)), ClassNode.EMPTY_ARRAY, createCompareToMethodBody(properties)));
for (PropertyNode property : properties) {
createComparatorFor(classNode, property);
}
new VariableScopeVisitor(sourceUnit, true).visitClass(classNode);
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy by apache.
the class ToStringASTTransformation method calculateToStringStatements.
private static Expression calculateToStringStatements(ClassNode cNode, boolean includeSuper, boolean includeFields, List<String> excludes, final List<String> includes, boolean includeNames, boolean ignoreNulls, boolean includePackage, boolean includeSuperProperties, boolean allProperties, BlockStatement body, boolean allNames) {
// def _result = new StringBuilder()
final Expression result = varX("_result");
body.addStatement(declS(result, ctorX(STRINGBUILDER_TYPE)));
List<ToStringElement> elements = new ArrayList<ToStringElement>();
// def $toStringFirst = true
final VariableExpression first = varX("$toStringFirst");
body.addStatement(declS(first, constX(Boolean.TRUE)));
// <class_name>(
String className = (includePackage) ? cNode.getName() : cNode.getNameWithoutPackage();
body.addStatement(appendS(result, constX(className + "(")));
// append properties
List<PropertyNode> pList = BeanUtils.getAllProperties(cNode, includeSuperProperties, false, allProperties);
for (PropertyNode pNode : pList) {
if (shouldSkip(pNode.getName(), excludes, includes, allNames))
continue;
Expression getter = getterThisX(cNode, pNode);
elements.add(new ToStringElement(getter, pNode.getName(), canBeSelf(cNode, pNode.getOriginType())));
}
// append fields if needed
if (includeFields) {
List<FieldNode> fList = new ArrayList<FieldNode>();
fList.addAll(getInstanceNonPropertyFields(cNode));
for (FieldNode fNode : fList) {
if (shouldSkip(fNode.getName(), excludes, includes, allNames))
continue;
elements.add(new ToStringElement(varX(fNode), fNode.getName(), canBeSelf(cNode, fNode.getType())));
}
}
// append super if needed
if (includeSuper) {
// not through MOP to avoid infinite recursion
elements.add(new ToStringElement(callSuperX("toString"), "super", false));
}
if (includes != null) {
Comparator<ToStringElement> includeComparator = new Comparator<ToStringElement>() {
public int compare(ToStringElement tse1, ToStringElement tse2) {
return new Integer(includes.indexOf(tse1.name)).compareTo(includes.indexOf(tse2.name));
}
};
Collections.sort(elements, includeComparator);
}
for (ToStringElement el : elements) {
appendValue(body, result, first, el.value, el.name, includeNames, ignoreNulls, el.canBeSelf);
}
// wrap up
body.addStatement(appendS(result, constX(")")));
MethodCallExpression toString = callX(result, "toString");
toString.setImplicitThis(false);
return toString;
}
Aggregations