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  
16  
17  import java.io.BufferedReader;
18  import java.io.FileReader;
19  import java.io.IOException;
20  import java.util.Scanner;
21  import java.util.function.Function;
22  
23  public class InputJavadocMethodIgnoreThrows {
24  
25      /**
26       * Ignore try block, but keep catch and finally blocks.
27       *
28       * @param s String to parse
29       * @return A positive integer
30       */
31      private static int parsePositiveInt(String s) {
32          try {
33              int value = Integer.parseInt(s);
34              if (value <= 0) {
35                  throw new NumberFormatException(value + " is negative/zero"); // ok
36              }
37              return value;
38          } catch (NumberFormatException ex) {
39              // violation below '.* @throws .* 'IllegalArgumentException'.'
40              throw new IllegalArgumentException("Invalid number", ex);
41          } finally {
42              // violation below '.* @throws .* 'IllegalStateException'.'
43              throw new IllegalStateException("Should never reach here");
44          }
45      }
46  
47      /**
48       * For exceptions that are thrown, caught, and rethrown, violation is in the catch block.
49       * Prior to issue 7473, violation was logged in the try block instead.
50       *
51       * @param o An input
52       */
53      private static void catchAndRethrow(Object o) {
54          try {
55              if (o == null) {
56                  throw new IllegalArgumentException("null"); // ok, try
57              }
58          } catch (IllegalArgumentException ex) {
59              // violation below '.* @throws .* 'IllegalArgumentException'.'
60              throw new IllegalArgumentException(ex.toString());
61          }
62      }
63  
64      /**
65       * However, rethrowing the same exception (without creating a new one)
66       * will not be treated as a violation.
67       *
68       * @param o An input
69       */
70      private static void catchAndRethrowSame(Object o) {
71          try {
72              if (o == null) {
73                  throw new IllegalArgumentException("null"); // ok, try
74              }
75          } catch (IllegalArgumentException ex) {
76              throw ex; // no violation
77          }
78      }
79  
80      /**
81       * It is not possible to tell the exact type of the exception
82       * unless 'throw new' is used.
83       *
84       * @param o An input
85       * @param i Another input
86       */
87      private static void catchAndRethrowDifferent(Object o, int i) {
88          try {
89              float x = 1 / i; // ArithmeticException when i = 0
90          } catch (RuntimeException ex) {
91              if (o == null) {
92                  ex = new NullPointerException("");
93              }
94              throw ex; // ok
95          }
96      }
97  
98      /**
99       * Ignore everything inside lambda.
100      *
101      * @param maxLength Max length
102      * @return A function to truncate string
103      */
104     private static Function<String, String> getTruncateFunction(int maxLength) {
105         return s -> {
106             if (s == null) {
107                 throw new IllegalArgumentException("Cannot truncate null"); // ok, inside lambda
108             }
109             return s.length() > maxLength ? s.substring(0, maxLength) : s;
110         };
111     }
112 
113     /**
114      * Try-with-resources should also be ignored if there is a catch block.
115      *
116      * @param input file to read
117      */
118     private static void ignoreTryWithResources(String input) {
119         try (BufferedReader in = new BufferedReader(new FileReader(input))) {
120             String s = in.readLine();
121             System.out.println(s);
122             if (s.length() == 0) {
123                 // false negative, unable to tell what was caught
124                 throw new IllegalArgumentException("empty input"); // no violation
125             }
126             else {
127                 throw new IOException(); // ok, exception was caught
128             }
129         } catch (IOException e) {
130             System.out.println("Error reading file");
131         }
132     }
133 
134     /**
135      * However, do not ignore try block without catch.
136      */
137     private static void keepTryWithoutCatch() {
138         try (Scanner sc = new Scanner(System.in)) {
139             if (sc.nextInt() <= 0) {
140                 // violation below '.* @throws .* 'IllegalArgumentException'.'
141                 throw new IllegalArgumentException("");
142             }
143         }
144     }
145 
146     /**
147      * Local inner classes should be ignored.
148      *
149      * @param str input
150      */
151     void dfs(String str) {
152         class DFS {
153             void neverCalled() {
154                 throw new IllegalStateException(""); // ok, inside local class
155             }
156 
157             void dfs(String str) {
158                 if (str != null) {
159                     dfs(str.toLowerCase());
160                     System.out.println(str);
161                     dfs(str.toUpperCase());
162                 }
163             }
164         }
165         new DFS().dfs(str);
166     }
167 
168     /**
169      * Anonymous classes should be ignored as well.
170      *
171      * @return some Runnable
172      */
173     Runnable ignoreAnonClasses() {
174         return new Runnable() {
175             @Override
176             public void run() {
177                 throw new UnsupportedOperationException(""); // ok, inside anon class
178             }
179         };
180     }
181 
182     /**
183      * Catch block is not ignored, but lambda is still ignored.
184      *
185      * @param s a string
186      * @return a function
187      */
188     Function<String, Integer> lambdaInCatchBlock(String s) {
189         try {
190             int value = Integer.parseInt(s);
191             if (value <= 0) {
192                 throw new NumberFormatException(value + " is negative/zero"); // ok, try
193             }
194             return x -> value;
195         } catch (NumberFormatException ex) {
196             if (s.length() == 1) {
197                 // violation below '.* @throws .* 'IllegalArgumentException'.'
198                 throw new IllegalArgumentException("Invalid number", ex);
199             }
200             return x -> {
201                 throw new UnsupportedOperationException(""); // ok, inside lambda
202             };
203         }
204     }
205 
206 }