From d906899546edf29e23ed2ae4ac8b5a781337bdb4 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 27 Jul 2009 09:21:22 +0200 Subject: Added support for @ToString annotation. The code for generating toStrings has now moved from HandleData to the new HandleToString. --- src/lombok/javac/handlers/HandleData.java | 42 ++----------------------------- 1 file changed, 2 insertions(+), 40 deletions(-) (limited to 'src/lombok/javac/handlers/HandleData.java') 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 { 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 { 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 { return true; } - private JCMethodDecl createToString(Node typeNode, List fields) { - TreeMaker maker = typeNode.getTreeMaker(); - - JCAnnotation overrideAnnotation = maker.Annotation(chainDots(maker, typeNode, "java", "lang", "Override"), List.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.nil(), hcMethod, List.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.nil(), List.nil(), List.nil(), body, null); - } - private JCMethodDecl createHashCode(Node typeNode, List fields) { TreeMaker maker = typeNode.getTreeMaker(); -- cgit