diff options
Diffstat (limited to 'src/lombok/javac')
-rw-r--r-- | src/lombok/javac/handlers/HandleData.java | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/src/lombok/javac/handlers/HandleData.java b/src/lombok/javac/handlers/HandleData.java index 2faf96fd..54d85251 100644 --- a/src/lombok/javac/handlers/HandleData.java +++ b/src/lombok/javac/handlers/HandleData.java @@ -88,10 +88,49 @@ public class HandleData implements JavacAnnotationHandler<Data> { injectMethod(typeNode, method); } - //TODO toString. + if ( methodExists("toString", typeNode) == MethodExistsResult.NOT_EXISTS ) { + JCMethodDecl method = createToString(typeNode, nodesForEquality); + injectMethod(typeNode, method); + } + return true; } + private JCMethodDecl createToString(Node typeNode, List<Node> fields) { + TreeMaker maker = typeNode.getTreeMaker(); + + JCAnnotation overrideAnnotation = maker.Annotation(chainDots(maker, typeNode, "java", "lang", "Override"), List.<JCExpression>nil()); + JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation)); + JCExpression returnType = chainDots(maker, typeNode, "java", "lang", "String"); + + JCExpression current = maker.Literal(((JCClassDecl) typeNode.get()).name.toString() + "("); + boolean first = true; + + for ( Node fieldNode : fields ) { + JCVariableDecl field = (JCVariableDecl) fieldNode.get(); + if ( !first ) current = maker.Binary(JCTree.PLUS, current, maker.Literal(", ")); + first = false; + if ( field.vartype instanceof JCArrayTypeTree ) { + boolean multiDim = ((JCArrayTypeTree)field.vartype).elemtype instanceof JCArrayTypeTree; + boolean primitiveArray = ((JCArrayTypeTree)field.vartype).elemtype instanceof JCPrimitiveTypeTree; + boolean useDeepTS = multiDim || !primitiveArray; + + JCExpression hcMethod = chainDots(maker, typeNode, "java", "util", "Arrays", useDeepTS ? "deepToString" : "toString"); + current = maker.Binary(JCTree.PLUS, current, maker.Apply( + List.<JCExpression>nil(), hcMethod, List.<JCExpression>of(maker.Ident(field.name)))); + } else current = maker.Binary(JCTree.PLUS, current, maker.Ident(field.name)); + } + + current = maker.Binary(JCTree.PLUS, current, maker.Literal(")")); + + JCStatement returnStatement = maker.Return(current); + + JCBlock body = maker.Block(0, List.of(returnStatement)); + + return maker.MethodDef(mods, typeNode.toName("toString"), returnType, + List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null); + } + private JCMethodDecl createHashCode(Node typeNode, List<Node> fields) { TreeMaker maker = typeNode.getTreeMaker(); @@ -161,9 +200,9 @@ public class HandleData implements JavacAnnotationHandler<Data> { /* java.util.Arrays.deepHashCode(this.fieldName) //use just hashCode() for primitive arrays. */ boolean multiDim = ((JCArrayTypeTree)fType).elemtype instanceof JCArrayTypeTree; boolean primitiveArray = ((JCArrayTypeTree)fType).elemtype instanceof JCPrimitiveTypeTree; - boolean useDeepEquals = multiDim || !primitiveArray; + boolean useDeepHC = multiDim || !primitiveArray; - JCExpression hcMethod = chainDots(maker, typeNode, "java", "util", "Arrays", useDeepEquals ? "deepHashCode" : "hashCode"); + JCExpression hcMethod = chainDots(maker, typeNode, "java", "util", "Arrays", useDeepHC ? "deepHashCode" : "hashCode"); intoResult = intoResult.append( maker.Apply(List.<JCExpression>nil(), hcMethod, List.of(thisDotField))); } else /* objects */ { |