From 7631fde0aeb3323b1c03c63aba08f4bb1906d597 Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Wed, 17 Jan 2018 16:32:47 +0100 Subject: [PATCH 1/2] Add a test to show bug #1826 --- src/test/java/spoon/test/comment/CommentTest.java | 11 +++++++++++ .../spoon/test/comment/testclasses/OtherJavaDoc.java | 7 +++++++ 2 files changed, 18 insertions(+) create mode 100644 src/test/java/spoon/test/comment/testclasses/OtherJavaDoc.java diff --git a/src/test/java/spoon/test/comment/CommentTest.java b/src/test/java/spoon/test/comment/CommentTest.java index 3344aefc01c..c1bc5186827 100644 --- a/src/test/java/spoon/test/comment/CommentTest.java +++ b/src/test/java/spoon/test/comment/CommentTest.java @@ -48,6 +48,7 @@ import spoon.test.comment.testclasses.InlineComment; import spoon.test.comment.testclasses.JavaDocComment; import spoon.test.comment.testclasses.JavaDocEmptyCommentAndTags; +import spoon.test.comment.testclasses.OtherJavaDoc; import spoon.test.comment.testclasses.WildComments; import spoon.test.comment.testclasses.WindowsEOL; @@ -130,6 +131,16 @@ public void testJavaDocCommentOnUnix() { this.testJavaDocComment(type, EOL); } + @Test + public void testJavadocShortAndLongComment() { + // contract: in case we cannot determine if it is a short comment, we take the whole content + Factory f = getSpoonFactory(); + CtClass type = (CtClass)f.Type().get(OtherJavaDoc.class); + CtJavaDoc classJavaDoc = (CtJavaDoc) type.getComments().get(0); + assertEquals("A short description without a proper end", classJavaDoc.getShortDescription()); + assertEquals("A short description without a proper end", classJavaDoc.getLongDescription()); + } + @Test public void testJavaDocCommentOnMac() { String EOL = "\n"; diff --git a/src/test/java/spoon/test/comment/testclasses/OtherJavaDoc.java b/src/test/java/spoon/test/comment/testclasses/OtherJavaDoc.java new file mode 100644 index 00000000000..d70420eb91b --- /dev/null +++ b/src/test/java/spoon/test/comment/testclasses/OtherJavaDoc.java @@ -0,0 +1,7 @@ +package spoon.test.comment.testclasses; + +/** + * A short description without a proper end + */ +public class OtherJavaDoc { +} From 6597f8d4340e0ef1d9fd032d34f63e215a740185 Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Wed, 17 Jan 2018 16:33:19 +0100 Subject: [PATCH 2/2] Fix the bug in CtJavadocImpl --- .../spoon/support/reflect/code/CtJavaDocImpl.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/spoon/support/reflect/code/CtJavaDocImpl.java b/src/main/java/spoon/support/reflect/code/CtJavaDocImpl.java index fbd4d316ca4..933843bca5d 100644 --- a/src/main/java/spoon/support/reflect/code/CtJavaDocImpl.java +++ b/src/main/java/spoon/support/reflect/code/CtJavaDocImpl.java @@ -92,20 +92,23 @@ public String getShortDescription() { if (indexEndSentence == -1) { indexEndSentence = this.getContent().indexOf("\n"); } - if (indexEndSentence == -1) { - indexEndSentence = this.getContent().length(); - } if (indexEndSentence != -1) { return this.getContent().substring(0, indexEndSentence + 1).trim(); + } else { + return this.getContent().trim(); } - return ""; } @Override public String getLongDescription() { int indexStartLongDescription = getShortDescription().length(); - return this.getContent().substring(indexStartLongDescription).trim(); + if (indexStartLongDescription < this.getContent().trim().length()) { + return this.getContent().substring(indexStartLongDescription).trim(); + } else { + return this.getContent().trim(); + } + } @Override