use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.
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-core by groovy.
the class SortableASTTransformation method createSortable.
private void createSortable(AnnotationNode annotation, ClassNode classNode) {
List<String> includes = getMemberList(annotation, "includes");
List<String> excludes = getMemberList(annotation, "excludes");
if (!checkIncludeExclude(annotation, excludes, includes, MY_TYPE_NAME))
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-core by groovy.
the class ToStringASTTransformation method calculateToStringStatements.
private static Expression calculateToStringStatements(ClassNode cNode, boolean includeSuper, boolean includeFields, List<String> excludes, List<String> includes, boolean includeNames, boolean ignoreNulls, boolean includePackage, boolean includeSuperProperties, BlockStatement body) {
// def _result = new StringBuilder()
final Expression result = varX("_result");
body.addStatement(declS(result, ctorX(STRINGBUILDER_TYPE)));
// 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;
if (includeSuperProperties) {
pList = getAllProperties(cNode);
Iterator<PropertyNode> pIterator = pList.iterator();
while (pIterator.hasNext()) {
if (pIterator.next().isStatic()) {
pIterator.remove();
}
}
} else {
pList = getInstanceProperties(cNode);
}
for (PropertyNode pNode : pList) {
if (shouldSkip(pNode.getName(), excludes, includes))
continue;
Expression getter = getterX(cNode, pNode);
appendValue(body, result, first, getter, pNode.getName(), includeNames, ignoreNulls);
}
// 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))
continue;
appendValue(body, result, first, varX(fNode), fNode.getName(), includeNames, ignoreNulls);
}
}
// append super if needed
if (includeSuper) {
appendCommaIfNotFirst(body, result, first);
appendPrefix(body, result, "super", includeNames);
// not through MOP to avoid infinite recursion
body.addStatement(appendS(result, callSuperX("toString")));
}
// wrap up
body.addStatement(appendS(result, constX(")")));
MethodCallExpression toString = callX(result, "toString");
toString.setImplicitThis(false);
return toString;
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.
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.size() > 0) {
createNoArgConstructor(cNode);
}
}
use of org.codehaus.groovy.ast.PropertyNode in project groovy-core by groovy.
the class BindableASTTransformation method addListenerToClass.
private void addListenerToClass(SourceUnit source, ClassNode classNode) {
if (needsPropertyChangeSupport(classNode, source)) {
addPropertyChangeSupport(classNode);
}
for (PropertyNode propertyNode : classNode.getProperties()) {
FieldNode field = propertyNode.getField();
// look to see if per-field handlers will catch this one...
if (hasBindableAnnotation(field) || ((field.getModifiers() & Opcodes.ACC_FINAL) != 0) || field.isStatic() || VetoableASTTransformation.hasVetoableAnnotation(field)) {
// VetoableASTTransformation will handle both @Bindable and @Vetoable
continue;
}
createListenerSetter(classNode, propertyNode);
}
}
Aggregations