diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-07-27 09:21:22 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-07-27 09:21:22 +0200 |
commit | d906899546edf29e23ed2ae4ac8b5a781337bdb4 (patch) | |
tree | 6f54ff1dd71fc4309614c4074be00a3764bf57e6 /src/lombok/javac/handlers/HandleData.java | |
parent | 3e03dcfac19901342a07f74b0dfd9d62dbb380f3 (diff) | |
download | lombok-d906899546edf29e23ed2ae4ac8b5a781337bdb4.tar.gz lombok-d906899546edf29e23ed2ae4ac8b5a781337bdb4.tar.bz2 lombok-d906899546edf29e23ed2ae4ac8b5a781337bdb4.zip |
Added support for @ToString annotation. The code for generating toStrings has now moved from HandleData to the new HandleToString.
Diffstat (limited to 'src/lombok/javac/handlers/HandleData.java')
-rw-r--r-- | src/lombok/javac/handlers/HandleData.java | 42 |
1 files changed, 2 insertions, 40 deletions
diff --git a/src/lombok/javac/handlers/HandleData.java b/src/lombok/javac/handlers/HandleData.java index e2754d59..dd0c4128 100644 --- a/src/lombok/javac/handlers/HandleData.java +++ b/src/lombok/javac/handlers/HandleData.java @@ -92,6 +92,8 @@ public class HandleData implements JavacAnnotationHandler<Data> { if ( !isFinal ) new HandleSetter().generateSetterForField(child, annotationNode.get()); } + new HandleToString().generateToStringForType(typeNode, annotationNode); + if ( methodExists("equals", typeNode) == MemberExistsResult.NOT_EXISTS ) { JCMethodDecl method = createEquals(typeNode, nodesForEquality); injectMethod(typeNode, method); @@ -102,11 +104,6 @@ public class HandleData implements JavacAnnotationHandler<Data> { injectMethod(typeNode, method); } - if ( methodExists("toString", typeNode) == MemberExistsResult.NOT_EXISTS ) { - JCMethodDecl method = createToString(typeNode, nodesForToString); - injectMethod(typeNode, method); - } - String staticConstructorName = annotation.getInstance().staticConstructor(); if ( constructorExists(typeNode) == MemberExistsResult.NOT_EXISTS ) { @@ -122,41 +119,6 @@ public class HandleData implements JavacAnnotationHandler<Data> { 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(); |