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.util.function.Function;
16  
17  public class InputJavadocMethodIgnoreThrowsOne {
18  
19      /**
20       * Ignore try block, but keep catch and finally blocks.
21       *
22       * @param s String to parse
23       * @return A positive integer
24       */
25      private static int parsePositiveInt(String s) {
26          try {
27              int value = Integer.parseInt(s);
28              if (value <= 0) {
29                  throw new NumberFormatException(value + " is negative/zero");
30              }
31              return value;
32          } catch (NumberFormatException ex) {
33              // violation below '.* @throws .* 'IllegalArgumentException'.'
34              throw new IllegalArgumentException("Invalid number", ex);
35          } finally {
36              // violation below '.* @throws .* 'IllegalStateException'.'
37              throw new IllegalStateException("Should never reach here");
38          }
39      }
40  
41      /**
42       * For exceptions that are thrown, caught, and rethrown, violation is in the catch block.
43       * Prior to issue 7473, violation was logged in the try block instead.
44       *
45       * @param o An input
46       */
47      private static void catchAndRethrow(Object o) {
48          try {
49              if (o == null) {
50                  throw new IllegalArgumentException("null"); // ok, try
51              }
52          } catch (IllegalArgumentException ex) {
53              // violation below '.* @throws .* 'IllegalArgumentException'.'
54              throw new IllegalArgumentException(ex.toString());
55          }
56      }
57  
58      /**
59       * However, rethrowing the same exception (without creating a new one)
60       * will not be treated as a violation.
61       *
62       * @param o An input
63       */
64      private static void catchAndRethrowSame(Object o) {
65          try {
66              if (o == null) {
67                  throw new IllegalArgumentException("null"); // ok, try
68              }
69          } catch (IllegalArgumentException ex) {
70              throw ex; // no violation
71          }
72      }
73  
74      /**
75       * It is not possible to tell the exact type of the exception
76       * unless 'throw new' is used.
77       *
78       * @param o An input
79       * @param i Another input
80       */
81      private static void catchAndRethrowDifferent(Object o, int i) {
82          try {
83              float x = 1 / i; // ArithmeticException when i = 0
84          } catch (RuntimeException ex) {
85              if (o == null) {
86                  ex = new NullPointerException("");
87              }
88              throw ex;
89          }
90      }
91  
92      /**
93       * Ignore everything inside lambda.
94       *
95       * @param maxLength Max length
96       * @return A function to truncate string
97       */
98      private static Function<String, String> getTruncateFunction(int maxLength) {
99          return s -> {
100             if (s == null) {
101                 throw new IllegalArgumentException("Cannot truncate null"); // ok, inside lambda
102             }
103             return s.length() > maxLength ? s.substring(0, maxLength) : s;
104         };
105     }
106 
107 }