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  public class InputJavadocMethodThrowsDetection {
16  
17      void noJavadoc() {
18          // no javadoc, no violations
19          throw new UnsupportedOperationException(""); // ok
20      }
21  
22      /** Simple, trivial case. */
23      void topLevel() {
24          // violation below 'Expected @throws tag for 'UnsupportedOperationException'.'
25          throw new UnsupportedOperationException("");
26      }
27  
28      /**
29       * Example raised in issue 8117.
30       *
31       * @param x some input for control flow statements
32       * @return 0 if there were no problems
33       */
34      int nestedWithinIf(int x) {
35          if (x > 0) {
36              // violation below 'Expected @throws tag for 'UnsupportedOperationException'.'
37              throw new UnsupportedOperationException("");
38          }
39          if (x < 0) {
40              // violation below 'Expected @throws tag for 'IllegalArgumentException'.'
41              throw new IllegalArgumentException("");
42          }
43          return 0;
44      }
45  
46      /**
47       * Some regression cases have comments in front.
48       *
49       * @param x some input for control flow statements
50       */
51      void nestedWithinIfLeadingComment(int x) {
52          if (x > 0) {
53              // some comment in front
54              // violation below 'Expected @throws tag for 'UnsupportedOperationException'.'
55              throw new UnsupportedOperationException("");
56          }
57          if (x < 0) {
58              // some comment in front
59              // violation below 'Expected @throws tag for 'IllegalArgumentException'.'
60              throw new IllegalArgumentException("");
61          }
62      }
63  
64      /**
65       * Check if-else block.
66       *
67       * @param x some input for control flow statements
68       */
69      void nestedWithinIfElse(int x) {
70          if (x > 0) {
71              // violation below 'Expected @throws tag for 'UnsupportedOperationException'.'
72              throw new UnsupportedOperationException("");
73          }
74          else {
75              // violation below 'Expected @throws tag for 'IllegalArgumentException'.'
76              throw new IllegalArgumentException("");
77          }
78      }
79  
80      /**
81       * Check if-else-if block.
82       *
83       * @param x some input for control flow statements
84       */
85      void nestedWithinIfElseIf(int x) {
86          if (x > 0) {
87              // violation below 'Expected @throws tag for 'UnsupportedOperationException'.'
88              throw new UnsupportedOperationException("");
89          }
90          else if (x < 0) {
91              // violation below 'Expected @throws tag for 'IllegalArgumentException'.'
92              throw new IllegalArgumentException("");
93          }
94      }
95  
96      /**
97       * Nesting inside 2 layers of if statements.
98       *
99       * @param x some input for control flow statements
100      */
101     void doubleNestedIf(int x) {
102         if (x >= 0) {
103             if (x <= 0) {
104                 // violation below 'Expected @throws tag for 'IllegalArgumentException'.'
105                 throw new IllegalArgumentException("0");
106             }
107         }
108     }
109 
110     /**
111      * Nesting inside 3 layers of if statements.
112      *
113      * @param x some input for control flow statements
114      */
115     void tripleNestedIf(int x) {
116         if (x >= 0)
117             if (x <= 0)
118                 if (x == 0)
119                     // violation below 'Expected @throws tag for 'IllegalArgumentException'.'
120                     throw new IllegalArgumentException("0");
121     }
122 
123     /**
124      * Throw outside but after if statement.
125      *
126      * @param x some input for control flow statements
127      */
128     void throwAfterIf(int x) {
129         if (x > 0) {
130             x = 0;
131         }
132         // violation below 'Expected @throws tag for 'UnsupportedOperationException'.'
133         throw new UnsupportedOperationException("");
134     }
135 
136     /**
137      * Throw in catch but after if statement.
138      *
139      * @param x some input for control flow statements
140      */
141     void tryCatchAfterIf(int x) {
142         if (x == 0) {
143             return;
144         }
145         try {
146             System.out.println("foo");
147         } catch (Exception e) {
148             // violation below 'Expected @throws tag for 'RuntimeException'.'
149             throw new RuntimeException(e);
150         }
151     }
152 
153     /**
154      * Throw in try/catch inside if statement.
155      * @param x some input for control flow statements
156      */
157     void tryCatchWithinIf(int x) {
158         if (x == 0) {
159             try {
160                 System.out.println("foo");
161             } catch (Exception e) {
162                 // violation below 'Expected @throws tag for 'RuntimeException'.'
163                 throw new RuntimeException(e);
164             }
165         }
166     }
167 
168 }