Skip to content

Commit e375b6c

Browse files
andrewbwogiwoutersmeenk
authored andcommitted
fix: get correct descriptions from CtJavaDocImpl (INRIA#4061)
1 parent 1326196 commit e375b6c

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/main/java/spoon/support/reflect/code/CtJavaDocImpl.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,27 @@ public <E extends CtJavaDoc> E removeTag(CtJavaDocTag tag) {
8585

8686
@Override
8787
public String getShortDescription() {
88-
int indexEndSentence = this.getContent().indexOf('.');
89-
if (indexEndSentence == -1) {
90-
indexEndSentence = this.getContent().indexOf('\n');
88+
int indexOfFirstSentenceEnd = indexOfFirstSentenceEnd(this.getContent());
89+
if (indexOfFirstSentenceEnd == -1) {
90+
indexOfFirstSentenceEnd = this.getContent().indexOf('\n');
9191
}
92-
if (indexEndSentence != -1) {
93-
return this.getContent().substring(0, indexEndSentence + 1).trim();
92+
if (indexOfFirstSentenceEnd != -1) {
93+
return this.getContent().substring(0, indexOfFirstSentenceEnd + 1).trim();
9494
} else {
9595
return this.getContent().trim();
9696
}
9797
}
9898

99+
private int indexOfFirstSentenceEnd(String content) {
100+
int index = content.indexOf('.');
101+
while (index < content.length() - 1
102+
&& !Character.isWhitespace(content.charAt(index + 1))
103+
&& index != -1) {
104+
index = content.indexOf('.', index + 1);
105+
}
106+
return index;
107+
}
108+
99109
/**
100110
* Parses the content string to split in two: the description and the Javadoc tags
101111
*/

src/test/java/spoon/test/comment/CommentTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import spoon.test.comment.testclasses.TestClassWithComments;
8181
import spoon.test.comment.testclasses.WildComments;
8282
import spoon.test.comment.testclasses.WindowsEOL;
83+
import spoon.test.comment.testclasses.JavaDocWithLink;
8384

8485
import java.io.File;
8586
import java.io.FileInputStream;
@@ -169,6 +170,22 @@ public void testJavadocShortAndLongComment() {
169170
assertEquals("A short description without a proper end", classJavaDoc.getLongDescription());
170171
}
171172

173+
@Test
174+
public void testJavadocCommentWithLink() {
175+
// contract: the CtJavaDoc short and long descriptions are correct when the Javadoc comment contains a qualified name
176+
Factory f = getSpoonFactory();
177+
CtClass<?> type = (CtClass<?>) f.Type().get(JavaDocWithLink.class);
178+
CtJavaDoc classJavaDoc = (CtJavaDoc) type.getComments().get(0);
179+
assertEquals("{@link spoon.Launcher Launcher}.", classJavaDoc.getShortDescription());
180+
181+
classJavaDoc = (CtJavaDoc) type.getField("field1").getComments().get(0);
182+
assertEquals("{@link spoon.Launcher Launcher}", classJavaDoc.getShortDescription());
183+
184+
classJavaDoc = (CtJavaDoc) type.getField("field2").getComments().get(0);
185+
assertEquals("{@link spoon.Launcher Launcher}.", classJavaDoc.getShortDescription());
186+
assertEquals("Additional text.", classJavaDoc.getLongDescription());
187+
}
188+
172189
@Test
173190
public void testJavaDocCommentOnMac() {
174191
String EOL = "\n";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package spoon.test.comment.testclasses;
2+
3+
/**
4+
* {@link spoon.Launcher Launcher}.
5+
*/
6+
public class JavaDocWithLink {
7+
8+
/**
9+
* {@link spoon.Launcher Launcher}
10+
*/
11+
int field1;
12+
13+
/**
14+
* {@link spoon.Launcher Launcher}. Additional text.
15+
*/
16+
int field2;
17+
}

0 commit comments

Comments
 (0)