diff --git a/javaparser-core/src/main/java/com/github/javaparser/javadoc/Javadoc.java b/javaparser-core/src/main/java/com/github/javaparser/javadoc/Javadoc.java index 3239997cd8..ecaa070c71 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/javadoc/Javadoc.java +++ b/javaparser-core/src/main/java/com/github/javaparser/javadoc/Javadoc.java @@ -106,8 +106,16 @@ public JavadocDescription getDescription() { return description; } - public List getBlockTags() { - return blockTags; + public JavadocBlockTag getBlockTag(int i) { + return this.blockTags.get(i); + } + + public int getBlockTagCount() { + return this.blockTags.size(); + } + + public JavadocBlockTag removeBlockTag(int i) { + return this.blockTags.get(i); } @Override diff --git a/javaparser-core/src/main/java/com/github/javaparser/javadoc/description/JavadocDescription.java b/javaparser-core/src/main/java/com/github/javaparser/javadoc/description/JavadocDescription.java index 2718551b69..7180a6e34d 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/javadoc/description/JavadocDescription.java +++ b/javaparser-core/src/main/java/com/github/javaparser/javadoc/description/JavadocDescription.java @@ -69,8 +69,26 @@ public JavadocDescription() { elements = new LinkedList<>(); } - public void addElement(JavadocDescriptionElement element) { - this.elements.add(element); + public JavadocDescription(List elements) { + this(); + + this.elements.addAll(elements); + } + + public boolean addElement(JavadocDescriptionElement element) { + return this.elements.add(element); + } + + public JavadocDescriptionElement getElement(int i) { + return this.elements.get(i); + } + + public JavadocDescriptionElement removeElement(int i) { + return this.elements.remove(i); + } + + public int getElementCount() { + return this.elements.size(); } public String toText() { diff --git a/javaparser-testing/src/test/java/com/github/javaparser/JavadocParserTest.java b/javaparser-testing/src/test/java/com/github/javaparser/JavadocParserTest.java index 9458ec37e4..34dfe0840c 100644 --- a/javaparser-testing/src/test/java/com/github/javaparser/JavadocParserTest.java +++ b/javaparser-testing/src/test/java/com/github/javaparser/JavadocParserTest.java @@ -103,7 +103,8 @@ public void parseBlockTagsAndProvideTagName() { assertEquals(underTest, JavadocParser.parse(expectedText)); - assertEquals(underTest.getBlockTags().get(0).getTagName(), "unofficial"); + assertEquals(1, underTest.getBlockTagCount()); + assertEquals("unofficial", underTest.getBlockTag(0).getTagName()); } @Test @@ -137,10 +138,10 @@ public void parseMultilineParamBlockTags() { " "; Javadoc res = JavadocParser.parse(text); assertEquals(new Javadoc(JavadocDescription.parseText("Add a field to this and automatically add the import of the type if needed")) - .addBlockTag(JavadocBlockTag.createParamBlockTag("typeClass", "the type of the field continued in a second line")) - .addBlockTag(JavadocBlockTag.createParamBlockTag("name", "the name of the field")) - .addBlockTag(JavadocBlockTag.createParamBlockTag("modifiers", "the modifiers like {@link Modifier#PUBLIC}")) - .addBlockTag(new JavadocBlockTag(JavadocBlockTag.Type.RETURN, "the {@link FieldDeclaration} created")), res); + .addBlockTag(JavadocBlockTag.createParamBlockTag("typeClass", "the type of the field continued in a second line")) + .addBlockTag(JavadocBlockTag.createParamBlockTag("name", "the name of the field")) + .addBlockTag(JavadocBlockTag.createParamBlockTag("modifiers", "the modifiers like {@link Modifier#PUBLIC}")) + .addBlockTag(new JavadocBlockTag(JavadocBlockTag.Type.RETURN, "the {@link FieldDeclaration} created")), res); } diff --git a/javaparser-testing/src/test/java/com/github/javaparser/javadoc/JavadocTest.java b/javaparser-testing/src/test/java/com/github/javaparser/javadoc/JavadocTest.java index 554321a6e5..dadfcfc37b 100644 --- a/javaparser-testing/src/test/java/com/github/javaparser/javadoc/JavadocTest.java +++ b/javaparser-testing/src/test/java/com/github/javaparser/javadoc/JavadocTest.java @@ -24,48 +24,48 @@ import com.github.javaparser.JavaParser; import com.github.javaparser.ast.comments.JavadocComment; import com.github.javaparser.javadoc.description.JavadocDescription; +import com.github.javaparser.javadoc.description.JavadocDescriptionElement; +import com.github.javaparser.javadoc.description.JavadocInlineTag; import org.junit.Test; -import java.io.FileNotFoundException; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; public class JavadocTest { @Test - public void toTextForEmptyJavadoc() throws FileNotFoundException { + public void toTextForEmptyJavadoc() { Javadoc javadoc = new Javadoc(new JavadocDescription()); assertEquals("", javadoc.toText()); } @Test - public void toTextForJavadocWithTwoLinesOfJustDescription() throws FileNotFoundException { + public void toTextForJavadocWithTwoLinesOfJustDescription() { Javadoc javadoc = new Javadoc(JavadocDescription.parseText("first line\nsecond line")); assertEquals("first line\nsecond line\n", javadoc.toText()); } @Test - public void toTextForJavadocWithTwoLinesOfJustDescriptionAndOneBlockTag() throws FileNotFoundException { + public void toTextForJavadocWithTwoLinesOfJustDescriptionAndOneBlockTag() { Javadoc javadoc = new Javadoc(JavadocDescription.parseText("first line\nsecond line")); javadoc.addBlockTag("foo", "something useful"); assertEquals("first line\nsecond line\n\n@foo something useful\n", javadoc.toText()); } @Test - public void toCommentForEmptyJavadoc() throws FileNotFoundException { + public void toCommentForEmptyJavadoc() { Javadoc javadoc = new Javadoc(new JavadocDescription()); assertEquals(new JavadocComment("\n\t\t "), javadoc.toComment("\t\t")); } @Test - public void toCommentorJavadocWithTwoLinesOfJustDescription() throws FileNotFoundException { + public void toCommentorJavadocWithTwoLinesOfJustDescription() { Javadoc javadoc = new Javadoc(JavadocDescription.parseText("first line\nsecond line")); assertEquals(new JavadocComment("\n\t\t * first line\n\t\t * second line\n\t\t "), javadoc.toComment("\t\t")); } @Test - public void toCommentForJavadocWithTwoLinesOfJustDescriptionAndOneBlockTag() throws FileNotFoundException { + public void toCommentForJavadocWithTwoLinesOfJustDescriptionAndOneBlockTag() { Javadoc javadoc = new Javadoc(JavadocDescription.parseText("first line\nsecond line")); javadoc.addBlockTag("foo", "something useful"); assertEquals(new JavadocComment("\n\t\t * first line\n\t\t * second line\n\t\t * \n\t\t * @foo something useful\n\t\t "), javadoc.toComment("\t\t")); @@ -75,7 +75,7 @@ public void toCommentForJavadocWithTwoLinesOfJustDescriptionAndOneBlockTag() thr public void descriptionAndBlockTagsAreRetrievable() { Javadoc javadoc = JavaParser.parseJavadoc("first line\nsecond line\n\n@param node a node\n@return result the result"); assertEquals(javadoc.getDescription().toText(), "first line\nsecond line"); - assertEquals(javadoc.getBlockTags().size(), 2); + assertEquals(2, javadoc.getBlockTagCount()); } @Test @@ -101,7 +101,38 @@ public void emptyLinesBetweenBlockTagsGetsFiltered() { " * \n" + " * @param \n"; Javadoc javadoc = JavaParser.parseJavadoc(comment); - assertEquals(javadoc.getBlockTags().size(), 2); + assertEquals(2, javadoc.getBlockTagCount()); + } + + @Test + public void blockTagModificationWorks() { + Javadoc javadoc = new Javadoc(new JavadocDescription()); + + assertEquals(0, javadoc.getBlockTagCount()); + JavadocBlockTag blockTag = new JavadocBlockTag(JavadocBlockTag.Type.RETURN, "a value"); + javadoc.addBlockTag(blockTag); + + assertEquals(1, javadoc.getBlockTagCount()); + assertEquals(blockTag, javadoc.getBlockTag(0)); + + assertEquals(blockTag, javadoc.removeBlockTag(0)); + assertEquals(1, javadoc.getBlockTagCount()); + } + + @Test + public void descriptionModificationWorks() { + JavadocDescription description = new JavadocDescription(); + + assertEquals(0, description.getElementCount()); + + JavadocDescriptionElement inlineTag = new JavadocInlineTag("inheritDoc", JavadocInlineTag.Type.INHERIT_DOC, ""); + assertTrue(description.addElement(inlineTag)); + + assertEquals(1, description.getElementCount()); + assertEquals(inlineTag, description.getElement(0)); + + assertEquals(inlineTag, description.removeElement(0)); + assertEquals(0, description.getElementCount()); } }