Issue #410: writing javadoc checks page is extended
This commit is contained in:
parent
7e100714bb
commit
d08ee6db62
|
|
@ -23,34 +23,63 @@
|
|||
|
||||
<section name="What is Javadoc comment">
|
||||
<p>
|
||||
Javadoc comment is multiline comment that starts with <b>*</b> character and placed under class definition, interface definition, enum definition, method definition or field definition.
|
||||
The comment should be written in XHTML to be correctly processed by Checkstyle. This means that every HTML tag should have matching closed HTML tag or it is self-closed one (singlton tag).
|
||||
The only exceptions are <p>, <li>, <tr>, <td>, <th>, <body>, <colgroup>, <dd>, <dt>, <head>, <html>, <option>,
|
||||
<tbody>, <thead>, <tfoot> and Checkstyle won't show error about missing closing tag, however, it leads to broken XHTML structure and, therefore,
|
||||
incorrect Abstract Syntax Tree of the Javadoc comment anyway. See examples at "HTML Code In Javadoc Comments" chapter.
|
||||
Javadoc comment is multiline comment that starts with <b>*</b> character and placed above class definition, interface definition, enum definition, method definition or field definition.
|
||||
<p>For example, here is java file:</p>
|
||||
<source><![CDATA[
|
||||
/**
|
||||
* My <b>class</b>.
|
||||
* @see AbstractClass
|
||||
*/
|
||||
public class MyClass {
|
||||
|
||||
}
|
||||
]]></source>
|
||||
Javadoc content is:
|
||||
<source><![CDATA[
|
||||
* My <b>class</b>.
|
||||
* @see AbstractClass
|
||||
]]></source>
|
||||
</p>
|
||||
Attention that java comment is start with <source>/*</source>, following with Identificator of comment type. Javadoc Identificator is <source>*</source>. All symbols after Javadoc Identificator till <source>*/</source> are part of javadoc comment. In internet you can find different types of documentation
|
||||
generation tools similar to javadoc. Such tools reply on different Identificators: "!", "#", "$".
|
||||
Comments looks like "/*! some comment */" , "/*# some comment */" , "/*$ some comment */". Such multiline comments are not a javadoc.
|
||||
</section>
|
||||
|
||||
<section name="Overview">
|
||||
<section name="Limitations">
|
||||
<p>
|
||||
To start implementing your own Check create new class and extend <a href='http://checkstyle.sourceforge.net/apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheck.html'>AbstractJavadocCheck</a>. It has two abstract methods you should implement:
|
||||
Javadoc by specification could contain any HTML tags that to let user generate content hi needs.
|
||||
Checkstyle can not parse something that looks like an HTML, so limitation appear.
|
||||
The comment should be written in XHTML to be correctly processed by Checkstyle. This means that every HTML tag should have matching closed HTML tag or it is self-closed one (singlton tag).
|
||||
The only exceptions are <p>, <li>, <tr>, <td>, <th>, <body>, <colgroup>, <dd>, <dt>, <head>, <html>, <option>,
|
||||
<tbody>, <thead>, <tfoot> and Checkstyle won't show error about missing closing tag, however, it leads to broken XHTML structure and, therefore,
|
||||
incorrect Abstract Syntax Tree of the Javadoc comment anyway. See examples at "HTML Code In Javadoc Comments" chapter.
|
||||
</p>
|
||||
<ul>
|
||||
<li>getDefaultJavadocTokens() - return int array of token types your Check is going to process. The array should contain int constants from JavadocTokenTypes class.
|
||||
There is also TokenTypes class in Checkstyle. Make sure you use JavadocTokenTypes class in your Check, because the second one is used to describe standard Java DetailAST token type.</li>
|
||||
<li>visitJavadocToken(DetailNode) - it's the place you should put tree nodes proccessing. The argument is Javadoc tree node of type you described
|
||||
before in getDefaultJavadocTokens() method.</li>
|
||||
</ul>
|
||||
<p>
|
||||
Javadoc parser requires XHTML to be used in Javadoc comments, i.e. if there is some open tag(for example <div>) then there have to be its close tag </div>.
|
||||
This means that if Javadoc comment has incorrect XHTML structure then Javadoc Parser will fail processing the comment, therefore, your new Check can't get its parse tree and process anything from this Javadoc comment. For more details and examples go to "HTML code in Javadoc comments" section.
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<section name="Overview">
|
||||
<p>
|
||||
To start implementing new Check create new class and extend <a href='http://checkstyle.sourceforge.net/apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheck.html'>AbstractJavadocCheck</a>. It has two abstract methods you should implement:
|
||||
</p>
|
||||
<ul>
|
||||
<li>getDefaultJavadocTokens() - return int array of javadoc token types your Check is going to process. The array should contain int constants from JavadocTokenTypes class.
|
||||
There is also TokenTypes class in Checkstyle. Make sure you use JavadocTokenTypes class in your Check, because the TokenTypes is used to describe standard Java DetailAST token type.</li>
|
||||
<li>visitJavadocToken(DetailNode) - it's the place you should put tree nodes proccessing. The argument is Javadoc tree node of type you described
|
||||
before in getDefaultJavadocTokens() method.</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section name="Difference between Java Grammar and Javadoc comments Grammar">
|
||||
<p>
|
||||
Java grammar parses java file due to language specifications. So, there are singleline comments and multiline/block comments in it. Java compiler doesn't know about Javadoc because it is just a multiline comment.
|
||||
To parse multiline comment as a Javadoc comment, checkstyle has second grammar - Javadoc grammar. So, it's supposed to proccess block comments and parse them to Abstract Syntax Tree.
|
||||
Java grammar parses java file due to Java language specifications. So, there are singleline comments and multiline/block comments in it. Java compiler doesn't know about Javadoc because it is just a multiline comment.
|
||||
To parse multiline comment as a Javadoc comment, checkstyle has special Parser
|
||||
that is based on ANTLR Javadoc grammar. So, it's supposed to proccess block comments
|
||||
that start with Javadoc Identificator and parse them to Abstract Syntax Tree (AST).
|
||||
</p>
|
||||
<p>
|
||||
The problem is that Java grammar is old one and uses ANTLR v2, while Javadoc grammar uses ANTLR v4. Because of that, these two grammars and their trees are not compatible.
|
||||
Java AST consists of DetailAST objects, while Javadoc AST consists of DetailNode objects.
|
||||
</p>
|
||||
|
|
@ -58,9 +87,9 @@
|
|||
|
||||
<section name="Tools to see Javadoc tree structure">
|
||||
<p>
|
||||
Checkstyle can print Abstract Syntax Tree including Javadoc trees. You need to run checkstyle jar file with <b>-J</b> argument, providing java file.
|
||||
Checkstyle can print Abstract Syntax Tree for Java and Javadoc trees. You need to run checkstyle jar file with <b>-J</b> argument, providing java file.
|
||||
</p>
|
||||
<p>For example, here is java file:</p>
|
||||
<p>For example, here is MyClass.java file:</p>
|
||||
<source><![CDATA[
|
||||
/**
|
||||
* My <b>class</b>.
|
||||
|
|
@ -126,7 +155,7 @@ CLASS_DEF -> CLASS_DEF [5:0]
|
|||
But in most cases while developing Javadoc Check you need only parse tree of the exact Javadoc comment.
|
||||
To do that just copy Javadoc comment to separate file and remove <b>/**</b> at the begining and <b>*/</b> at the end. After that, run checkstyle with <b>-j</b> argument.
|
||||
</p>
|
||||
<p>File:</p>
|
||||
<p>MyJavadocComment.javadoc file:</p>
|
||||
<source><![CDATA[
|
||||
* My <b>class</b>.
|
||||
* @see AbstractClass
|
||||
|
|
@ -174,14 +203,17 @@ JAVADOC -> * My <b>class</b>.\r\n * @see AbstractClass<EOF> [0:0]
|
|||
</section>
|
||||
|
||||
<section name="Access Java AST from Javadoc Check">
|
||||
As you alreasy know Javadoc parse tree is result of parsing block comment. There is a method to get the original block comment.
|
||||
You may need this block comment to check its position or something else in main DetailAST tree. For example, to write a JavadocCheck that verifies @param tags in Javadoc comment of a method definition, you also need all method's parameter names. To get method definition AST you should access main DetailAST tree throuth block comment AST. For this purpose use <a href="http://checkstyle.sourceforge.net/apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheck.html#getBlockCommentAst--">getBlockCommentAst()</a> method that returns DetailAST node.
|
||||
As you alreasy know Javadoc parse tree is result of parsing block comment. There is a method to get the original block comment from Javadoc Check.
|
||||
You may need this block comment to check its position or something else in main DetailAST tree.
|
||||
<p>
|
||||
For example, to write a JavadocCheck that verifies @param tags in Javadoc comment of a method definition, you also need all method's parameter names. To get method definition AST you should access main DetailAST tree throuth block comment AST. For this purpose use <a href="http://checkstyle.sourceforge.net/apidocs/com/puppycrawl/tools/checkstyle/checks/javadoc/AbstractJavadocCheck.html#getBlockCommentAst--">getBlockCommentAst()</a> method that returns DetailAST node.
|
||||
</p>
|
||||
</section>
|
||||
|
||||
<section name="HTML Code In Javadoc Comments">
|
||||
<p>
|
||||
Examples:
|
||||
1) Unclosed paragraph HTML tag. As you see in the tree, content of the paragraph tag is not nested to this tag. That is because HTML tags are not closed by pair tag </p>, and Checkstyle requires XHTML to correctly parse Javadoc comments.
|
||||
1) Unclosed paragraph HTML tag. As you see in the tree, content of the paragraph tag is not nested to this tag. That is because HTML tags are not closed by pair tag </p>, and Checkstyle requires XHTML to predictably parse Javadoc comments.
|
||||
</p>
|
||||
<source><![CDATA[
|
||||
<p> First
|
||||
|
|
@ -218,7 +250,7 @@ JAVADOC -> <p> First\r\n<p> Second<EOF> [0:0]
|
|||
`--EOF -> <EOF> [1:10]
|
||||
]]></source>
|
||||
<p>
|
||||
2) Here is correct version with open and closed HTML tags.
|
||||
2) Here is correct version with open and closed HTML tags.
|
||||
</p>
|
||||
<source><![CDATA[
|
||||
<p> First </p>
|
||||
|
|
@ -278,5 +310,12 @@ JAVADOC -> <p> First </p>\r\n<p> Second </p><EOF> [0:0]
|
|||
Javadoc Checks as well as regular Checks extend <a href="http://checkstyle.sourceforge.net/apidocs/index.html">AbstractCheck</a> class. So integrating new Javadoc Check is similar to <a href="writingchecks.html#Integrate_your_Check">integrating other Checks</a>.
|
||||
</section>
|
||||
|
||||
<section name="Examples of Javadoc Checks">
|
||||
The best source knowledge about how to write Javadoc Checks
|
||||
could be taken from
|
||||
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fjava+repo%3Acheckstyle%2Fcheckstyle+%22extends+AbstractJavadocCheck%22">
|
||||
existing Checks</a>.
|
||||
</section>
|
||||
|
||||
</body>
|
||||
</document>
|
||||
|
|
|
|||
Loading…
Reference in New Issue