diff --git a/pom.xml b/pom.xml index 13484b004..89b5ebb49 100644 --- a/pom.xml +++ b/pom.xml @@ -1409,7 +1409,7 @@ true 100 100 - 92 + 91 98 @@ -1422,12 +1422,29 @@ 79 97 + + com.puppycrawl.tools.checkstyle.gui.ParseTreeTablePModel + 41 + 44 + com/puppycrawl/tools/checkstyle/grammars/javadoc/*.class - com/puppycrawl/tools/checkstyle/gui/*.class + + com/puppycrawl/tools/checkstyle/gui/BaseCellEditor*.class + com/puppycrawl/tools/checkstyle/gui/CodeSelector*.class + com/puppycrawl/tools/checkstyle/gui/JTreeTable*.class + com/puppycrawl/tools/checkstyle/gui/ListToTreeSelectionModelWrapper*.class + com/puppycrawl/tools/checkstyle/gui/Main*.class + com/puppycrawl/tools/checkstyle/gui/MainFrame*.class + com/puppycrawl/tools/checkstyle/gui/ParseTreeTableModel*.class + com/puppycrawl/tools/checkstyle/gui/TreeTableCellRenderer*.class + com/puppycrawl/tools/checkstyle/gui/TreeTableModelAdapter*.class + + com/puppycrawl/tools/checkstyle/gui/CodeSelectorPModel*.class + com/puppycrawl/tools/checkstyle/gui/MainFrameModel*.class com/puppycrawl/tools/checkstyle/checks/AbstractFormatCheck.class com/puppycrawl/tools/checkstyle/checks/AbstractDeclarationCollector*.class diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/gui/ParseTreeTablePModel.java b/src/main/java/com/puppycrawl/tools/checkstyle/gui/ParseTreeTablePModel.java index a49c62557..92f794bf1 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/gui/ParseTreeTablePModel.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/gui/ParseTreeTablePModel.java @@ -182,7 +182,16 @@ public class ParseTreeTablePModel { result = ((DetailNode) parent).getChildren().length; } else { - result = ((DetailAST) parent).getChildCount(); + if (parseMode == ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS + && ((AST) parent).getType() == TokenTypes.COMMENT_CONTENT + && JavadocUtils.isJavadocComment(((DetailAST) parent).getParent())) { + //getChildCount return 0 on COMMENT_CONTENT, + //but we need to attach javadoc tree, that is separate tree + result = 1; + } + else { + result = ((DetailAST) parent).getChildCount(); + } } return result; @@ -255,20 +264,22 @@ public class ParseTreeTablePModel { * and parseMode is JAVA_WITH_JAVADOC_AND_COMMENTS. */ private Object getChildAtDetailAst(DetailAST parent, int index) { - int currentIndex = 0; - DetailAST child = parent.getFirstChild(); - while (currentIndex < index) { - child = child.getNextSibling(); - currentIndex++; - } - - Object result = child; - + final Object result; if (parseMode == ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS - && child.getType() == TokenTypes.BLOCK_COMMENT_BEGIN - && JavadocUtils.isJavadocComment(child)) { - result = getJavadocTree(child); + && parent.getType() == TokenTypes.COMMENT_CONTENT + && JavadocUtils.isJavadocComment(parent.getParent())) { + result = getJavadocTree(parent.getParent()); } + else { + int currentIndex = 0; + DetailAST child = parent.getFirstChild(); + while (currentIndex < index) { + child = child.getNextSibling(); + currentIndex++; + } + result = child; + } + return result; } diff --git a/src/site/resources/images/gui_javadoc_screenshot.png b/src/site/resources/images/gui_javadoc_screenshot.png new file mode 100644 index 000000000..9ae7f5c78 Binary files /dev/null and b/src/site/resources/images/gui_javadoc_screenshot.png differ diff --git a/src/site/resources/images/gui_screenshot.png b/src/site/resources/images/gui_screenshot.png index 491f4de1d..d7e81c347 100644 Binary files a/src/site/resources/images/gui_screenshot.png and b/src/site/resources/images/gui_screenshot.png differ diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/gui/ParseTreeTablePModelTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/gui/ParseTreeTablePModelTest.java new file mode 100644 index 000000000..538184642 --- /dev/null +++ b/src/test/java/com/puppycrawl/tools/checkstyle/gui/ParseTreeTablePModelTest.java @@ -0,0 +1,173 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2016 the original author or authors. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.gui; + +import java.io.File; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import antlr.collections.AST; + +import com.puppycrawl.tools.checkstyle.TreeWalker; + +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.DetailNode; +import com.puppycrawl.tools.checkstyle.api.FileContents; +import com.puppycrawl.tools.checkstyle.api.FileText; +import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; + +import com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode; + +public class ParseTreeTablePModelTest { + + private DetailAST tree; + + public static String getPath(String filename) { + return "src/test/resources/com/puppycrawl/tools/checkstyle/gui/" + filename; + } + + public static DetailAST parseFile(File file) throws Exception { + final FileContents contents = new FileContents( + new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", "UTF-8"))); + return TreeWalker.parseWithComments(contents); + } + + @Before + public void loadTree() throws Exception { + tree = parseFile( + new File(getPath("InputJavadocAttributesAndMethods.java"))); + } + + @Test + public void testRoot() { + final Object root = new ParseTreeTablePModel(tree).getRoot(); + final int childCount = new ParseTreeTablePModel(null).getChildCount(root); + Assert.assertEquals(1, childCount); + } + + @Test + public void testChildCount() { + final int childCount = new ParseTreeTablePModel(null).getChildCount(tree); + Assert.assertEquals(5, childCount); + } + + @Test + public void testChildCountInJavaAndJavadocMode() { + final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null); + parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS); + final int childCount = parseTree.getChildCount(tree); + Assert.assertEquals(5, childCount); + } + + @Test + public void testChild() { + final Object child = new ParseTreeTablePModel(null).getChild(tree, 1); + Assert.assertTrue(child instanceof DetailAST); + Assert.assertEquals(TokenTypes.BLOCK_COMMENT_BEGIN, ((AST) child).getType()); + } + + @Test + public void testChildInJavaAndJavadocMode() { + final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null); + parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS); + final Object child = parseTree.getChild(tree, 1); + Assert.assertTrue(child instanceof DetailAST); + Assert.assertEquals(TokenTypes.BLOCK_COMMENT_BEGIN, ((AST) child).getType()); + } + + @Test + public void testCommentChildCount() { + final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild(); + final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null); + parseTree.setParseMode(ParseMode.JAVA_WITH_COMMENTS); + final int javadocCommentChildCount = parseTree.getChildCount(commentContentNode); + Assert.assertEquals(0, javadocCommentChildCount); + } + + @Test + public void testCommentChildCountInJavaAndJavadocMode() { + final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null); + parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS); + final DetailAST commentContentNode = tree.getLastChild().getLastChild() + .getPreviousSibling().getLastChild().getFirstChild().getFirstChild(); + final int commentChildCount = parseTree.getChildCount(commentContentNode); + Assert.assertEquals(0, commentChildCount); + } + + @Test + public void testCommentChildInJavaAndJavadocMode() { + final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null); + parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS); + final DetailAST commentContentNode = tree.getLastChild().getLastChild() + .getPreviousSibling().getLastChild().getFirstChild().getFirstChild(); + final Object commentChild = parseTree.getChild(commentContentNode, 0); + Assert.assertNull(commentChild); + } + + @Test + public void testJavadocCommentChildCount() { + final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild(); + final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null); + final int commentChildCount = parseTree.getChildCount(commentContentNode); + Assert.assertEquals(0, commentChildCount); + parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS); + final int javadocCommentChildCount = parseTree.getChildCount(commentContentNode); + Assert.assertEquals(1, javadocCommentChildCount); + } + + @Test + public void testJavadocCommentChild() { + final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild(); + final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null); + parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS); + final Object child = parseTree.getChild(commentContentNode, 0); + Assert.assertTrue(child instanceof DetailNode); + Assert.assertEquals(JavadocTokenTypes.JAVADOC, ((DetailNode) child).getType()); + } + + @Test + public void testJavadocChildCount() { + final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild(); + final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null); + parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS); + final Object javadoc = parseTree.getChild(commentContentNode, 0); + Assert.assertTrue(javadoc instanceof DetailNode); + Assert.assertEquals(JavadocTokenTypes.JAVADOC, ((DetailNode) javadoc).getType()); + final int javadocChildCount = parseTree.getChildCount(javadoc); + Assert.assertEquals(5, javadocChildCount); + } + + @Test + public void testJavadocChild() { + final DetailAST commentContentNode = tree.getFirstChild().getNextSibling().getFirstChild(); + final ParseTreeTablePModel parseTree = new ParseTreeTablePModel(null); + parseTree.setParseMode(ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS); + final Object javadoc = parseTree.getChild(commentContentNode, 0); + Assert.assertTrue(javadoc instanceof DetailNode); + Assert.assertEquals(JavadocTokenTypes.JAVADOC, ((DetailNode) javadoc).getType()); + final Object javadocChild = parseTree.getChild(javadoc, 2); + Assert.assertTrue(javadoc instanceof DetailNode); + Assert.assertEquals(JavadocTokenTypes.TEXT, ((DetailNode) javadocChild).getType()); + } + +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/gui/InputJavadocAttributesAndMethods.java b/src/test/resources/com/puppycrawl/tools/checkstyle/gui/InputJavadocAttributesAndMethods.java new file mode 100644 index 000000000..eacb8536e --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/gui/InputJavadocAttributesAndMethods.java @@ -0,0 +1,16 @@ +/** +* class javadoc +*/ +class InputJavadocAttributesAndMethods { + + /** attribute javadoc*/ + int attribute; + + /** + * method javadoc + */ + public void method() { + /* just comment */ + } + +} diff --git a/src/xdocs/writingchecks.xml b/src/xdocs/writingchecks.xml index 9f1b3a34a..62a6a33e8 100644 --- a/src/xdocs/writingchecks.xml +++ b/src/xdocs/writingchecks.xml @@ -146,6 +146,20 @@ java -cp checkstyle-${projectVersion}-all.jar com.puppycrawl.tools.checkstyle.gu (a node that represents a class definition) while you will see token types like IDENT (an identifier) near the leaves of the tree.

+

+ In the bottom of frame you can find buttons "Open File", "Reload File" + and dropdown list with parse modes to choose. First one opens file + choose window. After choosing file tree that corresponds to java source + file builds in frame. Notice that only files with ".java" extension + can be opened. Second one reloads chosen file from file system and + rebuilds source code tree. Dropdown list allow to choose one of three + parse modes: "PLAIN JAVA", "JAVA WITH COMMENTS", "JAVA WITH JAVADOC AND COMMENTS". + "PLAIN JAVA" mode uses to show java source code without comments. In + "JAVA WITH COMMENTS" you can see also comments blocks on the tree. When + "JAVA WITH JAVADOC AND COMMENTS" chosen javadoc tree builds and attaches + for every comment block, that contains javadoc. +

+

Notice: text of a tree node and its children is selected automaticaly after either pressing "Enter" or double-clicking on it, so there is no diff --git a/src/xdocs/writingjavadocchecks.xml.vm b/src/xdocs/writingjavadocchecks.xml.vm index 5c2d0a6a0..aa81ea5d0 100644 --- a/src/xdocs/writingjavadocchecks.xml.vm +++ b/src/xdocs/writingjavadocchecks.xml.vm @@ -528,7 +528,28 @@ JAVADOC ->

First

\r\n

Second

[0:0]
- Not implemented yet. See Github Issue #408. +

+ Checkstyle GUI allows to showing javadoc tree in java files. To run in use +

+ +java -cp checkstyle-${projectVersion}-all.jar com.puppycrawl.tools.checkstyle.gui.Main + +

+ and choose "JAVA WITH JAVADOC MODE" in dropdown list in bottom of frame. +

+

+ Now you can see parsed javadoc tree as child of comment block. +

+

+ screenshot +

+

+ Notice that only files with ".java" extension can be opened. +

+

+ For detail reference you can see + Checkstyle GUI documentation. +