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.BufferedReader;
16  import java.io.FileReader;
17  import java.io.IOException;
18  import java.util.Scanner;
19  import java.util.function.Function;
20  
21  public class InputJavadocMethodIgnoreThrowsTwo {
22  
23      /**
24       * Try-with-resources should also be ignored if there is a catch block.
25       *
26       * @param input file to read
27       */
28      private static void ignoreTryWithResources(String input) {
29          try (BufferedReader in = new BufferedReader(new FileReader(input))) {
30              String s = in.readLine();
31              System.out.println(s);
32              if (s.length() == 0) {
33                  // false negative, unable to tell what was caught
34                  throw new IllegalArgumentException("empty input"); // no violation
35              }
36              else {
37                  throw new IOException(); // ok, exception was caught
38              }
39          } catch (IOException e) {
40              System.out.println("Error reading file");
41          }
42      }
43  
44      /**
45       * However, do not ignore try block without catch.
46       */
47      private static void keepTryWithoutCatch() {
48          try (Scanner sc = new Scanner(System.in)) {
49              if (sc.nextInt() <= 0) {
50                  // violation below '.* @throws .* 'IllegalArgumentException'.'
51                  throw new IllegalArgumentException("");
52              }
53          }
54      }
55  
56      /**
57       * Local inner classes should be ignored.
58       *
59       * @param str input
60       */
61      void dfs(String str) {
62          class DFS {
63              void neverCalled() {
64                  throw new IllegalStateException(""); // ok, inside local class
65              }
66  
67              void dfs(String str) {
68                  if (str != null) {
69                      dfs(str.toLowerCase());
70                      System.out.println(str);
71                      dfs(str.toUpperCase());
72                  }
73              }
74          }
75          new DFS().dfs(str);
76      }
77  
78      /**
79       * Anonymous classes should be ignored as well.
80       *
81       * @return some Runnable
82       */
83      Runnable ignoreAnonClasses() {
84          return new Runnable() {
85              @Override
86              public void run() {
87                  throw new UnsupportedOperationException(""); // ok, inside anon class
88              }
89          };
90      }
91  
92      /**
93       * Catch block is not ignored, but lambda is still ignored.
94       *
95       * @param s a string
96       * @return a function
97       */
98      Function<String, Integer> lambdaInCatchBlock(String s) {
99          try {
100             int value = Integer.parseInt(s);
101             if (value <= 0) {
102                 throw new NumberFormatException(value + " is negative/zero"); // ok, try
103             }
104             return x -> value;
105         } catch (NumberFormatException ex) {
106             if (s.length() == 1) {
107                 // violation below '.* @throws .* 'IllegalArgumentException'.'
108                 throw new IllegalArgumentException("Invalid number", ex);
109             }
110             return x -> {
111                 throw new UnsupportedOperationException(""); // ok, inside lambda
112             };
113         }
114     }
115 }