public void visit(ASTNode[] nodes, SourceUnit source) { init(nodes, source); AnnotatedNode parent = (AnnotatedNode) nodes[1]; AnnotationNode anno = (AnnotationNode) nodes[0]; if (!checkNotInterface(cNode, MY_TYPE_NAME)) return; boolean includeSuper = memberHasValue(anno, "includeSuper", true); boolean includeSuperProperties = memberHasValue(anno, "includeSuperProperties", true); boolean includeSuperFields = memberHasValue(anno, "includeSuperFields", true); boolean cacheToString = memberHasValue(anno, "cache", true); List<String> excludes = getMemberStringList(anno, "excludes"); List<String> includes = getMemberStringList(anno, "includes"); if (includes != null && includes.contains("super")) { includeSuper = true; addError("Error during " + MY_TYPE_NAME + " processing: includeSuper=true but '" + cNode.getName() + "' has no super class.", anno); boolean includeNames = memberHasValue(anno, "includeNames", true); boolean includeFields = memberHasValue(anno, "includeFields", true); boolean ignoreNulls = memberHasValue(anno, "ignoreNulls", true); boolean includePackage = !memberHasValue(anno, "includePackage", false); boolean allProperties = !memberHasValue(anno, "allProperties", false); boolean allNames = memberHasValue(anno, "allNames", true); if (!checkIncludeExcludeUndefinedAware(anno, excludes, includes, MY_TYPE_NAME)) return; if (!checkPropertyList(cNode, includes != null ? DefaultGroovyMethods.minus(includes, "super") : null, "includes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties)) return; if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields, includeSuperProperties, allProperties)) return; createToString(cNode, includeSuper, includeFields, excludes, includes, includeNames, ignoreNulls, includePackage, cacheToString, includeSuperProperties, allProperties, allNames, includeSuperFields);
private static void appendPrefix(BlockStatement body, Expression result, String name, boolean includeNames) { if (includeNames) body.addStatement(toStringPropertyName(result, name)); }
public void visit(ASTNode[] nodes, SourceUnit source) { init(nodes, source); AnnotatedNode parent = (AnnotatedNode) nodes[1]; AnnotationNode anno = (AnnotationNode) nodes[0]; if (!MY_TYPE.equals(anno.getClassNode())) return; if (parent instanceof ClassNode) { ClassNode cNode = (ClassNode) parent; checkNotInterface(cNode, MY_TYPE_NAME); boolean includeSuper = memberHasValue(anno, "includeSuper", true); if (includeSuper && cNode.getSuperClass().getName().equals("java.lang.Object")) { addError("Error during " + MY_TYPE_NAME + " processing: includeSuper=true but '" + cNode.getName() + "' has no super class.", anno); } boolean includeNames = memberHasValue(anno, "includeNames", true); boolean includeFields = memberHasValue(anno, "includeFields", true); List<String> excludes = tokenize((String) getMemberValue(anno, "excludes")); List<String> includes = tokenize((String) getMemberValue(anno, "includes")); if (hasAnnotation(cNode, CanonicalASTTransformation.MY_TYPE)) { AnnotationNode canonical = cNode.getAnnotations(CanonicalASTTransformation.MY_TYPE).get(0); if (excludes == null || excludes.isEmpty()) excludes = tokenize((String) getMemberValue(canonical, "excludes")); if (includes == null || includes.isEmpty()) includes = tokenize((String) getMemberValue(canonical, "includes")); } if (includes != null && !includes.isEmpty() && excludes != null && !excludes.isEmpty()) { addError("Error during " + MY_TYPE_NAME + " processing: Only one of 'includes' and 'excludes' should be supplied not both.", anno); } createToString(cNode, includeSuper, includeFields, excludes, includes, includeNames); } }
public static void createToString(ClassNode cNode, boolean includeSuper, boolean includeFields, List<String> excludes, List<String> includes, boolean includeNames) { createToString(cNode, includeSuper, includeFields, excludes, includes, includeNames, false); }
private static void appendValue(BlockStatement body, Expression result, VariableExpression first, Expression value, String name, boolean includeNames, boolean ignoreNulls, boolean canBeSelf) { final BlockStatement thenBlock = new BlockStatement(); final Statement appendValue = ignoreNulls ? ifS(notNullX(value), thenBlock) : thenBlock; appendCommaIfNotFirst(thenBlock, result, first); appendPrefix(thenBlock, result, name, includeNames); if (canBeSelf) { thenBlock.addStatement(ifElseS( sameX(value, new VariableExpression("this")), appendS(result, constX("(this)")), appendS(result, callX(INVOKER_TYPE, "toString", value)))); } else { thenBlock.addStatement(appendS(result, callX(INVOKER_TYPE, "toString", value))); } body.addStatement(appendValue); }
body.addStatement(appendS(result, constX(className + "("))); if (shouldSkipUndefinedAware(name, excludes, includes, allNames)) continue; FieldNode fNode = pNode.getField(); if (!cNode.hasProperty(name) && fNode.getDeclaringClass() != null) { elements.add(new ToStringElement(varX(fNode), name, canBeSelf(cNode, fNode.getType()))); } else { Expression getter = getterThisX(cNode, pNode); elements.add(new ToStringElement(getter, name, canBeSelf(cNode, pNode.getType()))); appendValue(body, result, first, el.value, el.name, includeNames, ignoreNulls, el.canBeSelf); body.addStatement(appendS(result, constX(")"))); MethodCallExpression toString = callX(result, "toString"); toString.setImplicitThis(false);
body.addStatement(new ExpressionStatement(new DeclarationExpression(result, ASSIGN, init))); body.addStatement(append(result, new ConstantExpression(cNode.getName()))); body.addStatement(append(result, new ConstantExpression("("))); boolean first = true; List<PropertyNode> pList = getInstanceProperties(cNode); for (PropertyNode pNode : pList) { if (shouldSkip(pNode.getName(), excludes, includes)) continue; first = appendPrefix(body, result, first, pNode.getName(), includeNames); String getterName = "get" + Verifier.capitalize(pNode.getName()); Expression getter = new MethodCallExpression(VariableExpression.THIS_EXPRESSION, getterName, MethodCallExpression.NO_ARGUMENTS); body.addStatement(append(result, new StaticMethodCallExpression(INVOKER_TYPE, "toString", getter))); if (shouldSkip(fNode.getName(), excludes, includes)) continue; first = appendPrefix(body, result, first, fNode.getName(), includeNames); body.addStatement(append(result, new StaticMethodCallExpression(INVOKER_TYPE, "toString", new VariableExpression(fNode)))); appendPrefix(body, result, first, "super", includeNames); body.addStatement(append(result, new MethodCallExpression(VariableExpression.SUPER_EXPRESSION, "toString", MethodCallExpression.NO_ARGUMENTS))); body.addStatement(append(result, new ConstantExpression(")"))); body.addStatement(new ReturnStatement(new MethodCallExpression(result, "toString", MethodCallExpression.NO_ARGUMENTS))); cNode.addMethod(new MethodNode(hasExistingToString ? "_toString" : "toString", hasExistingToString ? ACC_PRIVATE : ACC_PUBLIC,
private static boolean appendPrefix(BlockStatement body, Expression result, boolean first, String name, boolean includeNames) { if (first) { first = false; } else { body.addStatement(append(result, new ConstantExpression(", "))); } if (includeNames) body.addStatement(toStringPropertyName(result, name)); return first; }
private static Statement toStringPropertyName(Expression result, String fName) { final BlockStatement body = new BlockStatement(); body.addStatement(appendS(result, constX(fName + ":"))); return body; }
public static void createToString(ClassNode cNode, boolean includeSuper, boolean includeFields, List<String> excludes, List<String> includes, boolean includeNames, boolean ignoreNulls, boolean includePackage, boolean cache, boolean includeSuperProperties, boolean allProperties) { createToString(cNode, includeSuper, includeFields, excludes, includes, includeNames, ignoreNulls, includePackage, cache, includeSuperProperties, allProperties, false, false); }
private static void appendCommaIfNotFirst(BlockStatement body, Expression result, VariableExpression first) { // if ($toStringFirst) $toStringFirst = false else result.append(", ") body.addStatement(ifElseS( first, assignS(first, ConstantExpression.FALSE), appendS(result, constX(", ")))); }
public static void createToString(ClassNode cNode, boolean includeSuper, boolean includeFields, List<String> excludes, List<String> includes, boolean includeNames, boolean ignoreNulls, boolean includePackage, boolean cache) { createToString(cNode, includeSuper, includeFields, excludes, includes, includeNames, ignoreNulls, includePackage, cache, false); }
public static void createToString(ClassNode cNode, boolean includeSuper, boolean includeFields, List<String> excludes, List<String> includes, boolean includeNames, boolean ignoreNulls) { createToString(cNode, includeSuper, includeFields, excludes, includes, includeNames, ignoreNulls, true); }
public static void createToString(ClassNode cNode, boolean includeSuper, boolean includeFields, List<String> excludes, List<String> includes, boolean includeNames, boolean ignoreNulls, boolean includePackage) { createToString(cNode, includeSuper, includeFields, excludes, includes, includeNames, ignoreNulls, includePackage, false); }
public static void createToString(ClassNode cNode, boolean includeSuper, boolean includeFields, List<String> excludes, List<String> includes, boolean includeNames, boolean ignoreNulls, boolean includePackage, boolean cache, boolean includeSuperProperties) { createToString(cNode, includeSuper, includeFields, excludes, includes, includeNames, ignoreNulls, includePackage, cache, includeSuperProperties, true); }
createEquals(cNode, false, false, false, null, null); if (!hasAnnotation(cNode, ToStringASTTransformation.MY_TYPE)) { createToString(cNode, false, false, null, null, false);
public void visit(ASTNode[] nodes, SourceUnit source) { init(nodes, source); AnnotatedNode parent = (AnnotatedNode) nodes[1]; AnnotationNode anno = (AnnotationNode) nodes[0]; if (!MY_TYPE.equals(anno.getClassNode())) return; if (parent instanceof ClassNode) { ClassNode cNode = (ClassNode) parent; // TODO remove - let other validation steps pick this up if (hasAnnotation(cNode, ImmutableASTTransformation.MY_TYPE)) { addError(MY_TYPE_NAME + " class '" + cNode.getName() + "' can't also be " + ImmutableASTTransformation.MY_TYPE_NAME, parent); } checkNotInterface(cNode, MY_TYPE_NAME); List<String> excludes = tokenize((String) getMemberValue(anno, "excludes")); List<String> includes = tokenize((String) getMemberValue(anno, "includes")); if (includes != null && !includes.isEmpty() && excludes != null && !excludes.isEmpty()) { addError("Error during " + MY_TYPE_NAME + " processing: Only one of 'includes' and 'excludes' should be supplied not both.", anno); } if (!hasAnnotation(cNode, TupleConstructorASTTransformation.MY_TYPE)) { createConstructor(cNode, false, true, false, false, false, false, excludes, includes); } if (!hasAnnotation(cNode, EqualsAndHashCodeASTTransformation.MY_TYPE)) { createHashCode(cNode, false, false, false, excludes, includes); createEquals(cNode, false, false, true, excludes, includes); } if (!hasAnnotation(cNode, ToStringASTTransformation.MY_TYPE)) { createToString(cNode, false, false, excludes, includes, false); } } }