diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-06-25 06:18:44 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-06-25 06:18:44 +0200 |
commit | 76135a9fb713afc4cdc8e20081b09dced51797af (patch) | |
tree | 4795c02e7e1c3c4fa8ae5862c54b239240c8bc59 /src/lombok/javac | |
parent | 29c610af1855c847a32e726e8556b80fdf98bfcb (diff) | |
download | lombok-76135a9fb713afc4cdc8e20081b09dced51797af.tar.gz lombok-76135a9fb713afc4cdc8e20081b09dced51797af.tar.bz2 lombok-76135a9fb713afc4cdc8e20081b09dced51797af.zip |
Added support for generating toString to javac's HandleData. Now javac's HandleData is COMPLETE! w00t!
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 */ { |