View Javadoc
1   /*
2   JavadocMethod
3   allowedAnnotations = (default)Override
4   validateThrows = true
5   accessModifiers = (default)public, protected, package, private
6   allowMissingParamTags = (default)false
7   allowMissingReturnTag = (default)false
8   tokens = (default)METHOD_DEF, CTOR_DEF, ANNOTATION_FIELD_DEF, COMPACT_CTOR_DEF
9   
10  
11  */
12  
13  package com.puppycrawl.tools.checkstyle.checks.javadoc.javadocmethod;
14  
15  import java.io.File;
16  import java.io.FileNotFoundException;
17  import java.io.IOException;
18  
19  public class InputJavadocMethodExtraThrows {
20  
21      /**
22       * Extra throws in javadoc it is ok.
23       * @param properties some value
24       * @throws IllegalArgumentException when argument is wrong
25       * @throws NullPointerException indicates null was passed
26       */
27      public InputJavadocMethodExtraThrows(String properties) {
28          // here is NPE possible
29          if (properties.charAt(0) == 0) {
30              throw new IllegalArgumentException("cannot have char with code 0");
31          }
32      }
33  
34      /**
35       * Extra throws in javadoc it is ok
36       * @param properties some value
37       * @throws java.lang.IllegalArgumentException when argument is wrong
38       * @throws java.lang.NullPointerException indicates null was passed
39       */
40      public void doSomething(String properties) throws IllegalArgumentException {
41          // here is NPE possible
42          if (properties.charAt(0) == 0) {
43              throw new IllegalArgumentException("cannot have char with code 0");
44          }
45      }
46  
47      /**
48       * declared exception in method signature is missed in javadoc
49       * @param properties some value
50       * @throws java.lang.IllegalArgumentException when argument is wrong
51       * @throws java.lang.NullPointerException indicates null was passed
52       */
53      // violation below 'Expected @throws tag for 'IllegalStateException'.'
54      public void doSomething2(String properties) throws IllegalStateException {
55          // here is NPE possible
56          if (properties.charAt(0) == 0) {
57              throw new IllegalArgumentException("cannot have char with code 0");
58          }
59      }
60  
61      /**
62       * exception is explitly thrown in code missed in javadoc
63       * @param properties some value
64       * @throws java.lang.IllegalStateException when argument is wrong
65       */
66      public void doSomething3(String properties) throws IllegalStateException {
67          // here is NPE possible
68          if (properties.charAt(0) == 0) {
69              // violation below 'Expected @throws tag for 'IllegalArgumentException'.'
70              throw new IllegalArgumentException("cannot have char with code 0");
71          }
72      }
73  
74      /**
75       * exception is explitly thrown in code missed in javadoc
76       * @param properties some value
77       * @throws java.lang.IllegalStateException when argument is wrong
78       */
79      public void doSomething4(String properties) {
80          // here is NPE possible
81          if (properties.charAt(0) == 0) {
82              // violation below 'Expected @throws tag for 'IllegalArgumentException'.'
83              throw new IllegalArgumentException("cannot have char with code 0");
84          }
85      }
86  
87      /**
88       * exception is explitly thrown in code missed in javadoc
89       * @param properties some value
90       * @throws java.lang.IllegalStateException when argument is wrong
91       */
92      public void doSomething5(String properties) {
93          // here is NPE possible
94          if (properties.charAt(0) == 0) {
95              // violation below 'Expected @throws tag for 'java.lang.IllegalArgumentException'.'
96              throw new java.lang.IllegalArgumentException(
97                      "cannot have char with code 0");
98          }
99      }
100 
101     /**
102      * expicitly throwed is declared in javadoc
103      * @param properties some value
104      * @throws IllegalArgumentException when argument is wrong
105      */
106     public void doSomething6(String properties) {
107         // here is NPE possible
108         if (properties.charAt(0) == 0) {
109             throw new java.lang.IllegalArgumentException("cannot have char with code 0");
110         }
111     }
112 
113     /**
114      * expicitly throwed  but throwed as variable, we do not catch this for now
115      * @param properties some value
116      */
117     public void doSomething7(String properties) {
118         // here is NPE possible
119         if (properties.charAt(0) == 0) {
120             IllegalArgumentException exception =
121                     new IllegalArgumentException("cannot have char with code 0");
122             throw exception;
123         }
124     }
125 
126     /**
127      * Actual exception thrown is child class of class that is declared in throws.
128      * It is limitation of checkstyle (as checkstyle does not know type hierarchy).
129      * Javadoc is valid not declaring {@link FileNotFoundException}
130      * BUT checkstyle can not distinguish relationship between exceptions.
131      * @param file some file
132      * @throws IOException if some problem
133      */
134     public void doSomething8(File file) throws IOException {
135         if (file == null) {
136             // violation below 'Expected @throws tag for 'FileNotFoundException'.'
137             throw new FileNotFoundException();
138         }
139     }
140 
141    /**
142     * Exact throw type referencing in javadoc even first is parent of second type.
143     * It is a limitation of checkstyle (as checkstyle does not know type hierarchy).
144     * This javadoc is valid for checkstyle and for javadoc tool.
145     * @param file some file
146     * @throws IOException if some problem
147     * @throws FileNotFoundException if file is not found
148     */
149     public void doSomething9(File file) throws IOException {
150         if (file == null) {
151             throw new FileNotFoundException();
152         }
153     }
154 }