View Javadoc
1   /*
2   UnnecessaryParentheses
3   tokens = (default)EXPR, IDENT, NUM_DOUBLE, NUM_FLOAT, NUM_INT, NUM_LONG, \
4            STRING_LITERAL, LITERAL_NULL, LITERAL_FALSE, LITERAL_TRUE, ASSIGN, \
5            BAND_ASSIGN, BOR_ASSIGN, BSR_ASSIGN, BXOR_ASSIGN, DIV_ASSIGN, \
6            MINUS_ASSIGN, MOD_ASSIGN, PLUS_ASSIGN, SL_ASSIGN, SR_ASSIGN, STAR_ASSIGN, \
7            LAMBDA, TEXT_BLOCK_LITERAL_BEGIN, LAND, LITERAL_INSTANCEOF, GT, LT, GE, \
8            LE, EQUAL, NOT_EQUAL, UNARY_MINUS, UNARY_PLUS, INC, DEC, LNOT, BNOT, \
9            POST_INC, POST_DEC
10  
11  
12  */
13  
14  package com.puppycrawl.tools.checkstyle.checks.coding.unnecessaryparentheses;
15  
16  import static org.junit.Assert.assertEquals;
17  import static java.lang.Math.abs;
18  import java.util.Arrays;
19  import java.util.HashSet;
20  import java.util.function.ToIntFunction;
21  
22  public class InputUnnecessaryParenthesesCasts2 {
23      public void fooConditionals(T element) {
24          int x = 23;
25          int y = 44;
26          float k = 12f;
27          boolean finished = true;
28          boolean result = false;
29  
30          String filevalue = "FILEVALUE";
31          if (!finished || !((boolean) filevalue.contains("O"))) {
32              // no violation until https://github.com/checkstyle/checkstyle/issues/14872
33              filevalue += "F";
34          }
35          else if (finished || !(boolean) filevalue.contains("O")) {
36              filevalue += "G";
37          }
38  
39          if (result && finished || ((int) 23.1 + 21) == 32) {
40              y--;
41          }
42  
43          // no violation until https://github.com/checkstyle/checkstyle/issues/14872
44          if (!((boolean) filevalue.contains("G")) || finished) {
45              x++;
46          }
47          else if (!(boolean) filevalue.contains("P") || finished) {
48              filevalue += "p";
49          }
50  
51          String[] a = {"s", "a", "1", "2", "3"};
52          String[] abr = {"18", "z", "w", "30", "u", "vel"};
53          Arrays.stream(a)
54                  .filter(s -> !((boolean) s.isEmpty()))
55                  // no violation below until https://github.com/checkstyle/checkstyle/issues/14872
56                  .toArray(String[]::new);
57  
58          Arrays.stream(abr).filter(s -> !(boolean) s.isEmpty())
59                  .toArray(String[]::new);
60  
61          Arrays.stream(a)//no violation until https://github.com/checkstyle/checkstyle/issues/14872
62                  .filter(s -> ((boolean) s.isEmpty()))
63                  .toArray(String[]::new);
64          Arrays.stream(abr)
65                  .filter(s -> (boolean) s.isEmpty())
66                  .toArray(String[]::new);
67  
68          new HashSet<Integer>().stream()
69                  .filter(f -> f > ((int) 1.1 + 200));
70  
71          //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
72          if (((double) abs(10 - 2)) / 2 <= 0.01) {
73              y += 10;
74          }
75          else if ((double) abs(10 - 2) / 2 >= 0.02) {
76              x += 2;
77          }
78  
79          final ToIntFunction<T> comparison1 = ((Comparable<T>) element)::compareTo; // no violation
80  
81          Bean bean = new Bean();
82          assertEquals(1, ((int[]) bean.array)[0]); // no violation
83  
84          //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
85          float rest = ((float) (50 - System.currentTimeMillis())) / 1000;
86          float stop = (float) (50 - System.currentTimeMillis()) / 1000;
87  
88          // no violation below as parentheses detection in ternary(?) operators
89          // is handled by different logic and is dependent on QUESION token
90          float pin = false ? ((float) 21) : ((float) 31);
91  
92          Object obj = "hello"; // no violation below
93          String result1 = (obj instanceof String) ? ((String) obj) : "Default";
94  
95          //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
96          String op1 = ((String) obj) + ((Boolean) finished).toString();
97          String op2 = (String) obj + ((Boolean) finished).toString();
98  
99          filevalue = "'" + ((char) 32) + "'";
100         // violation above 'Unnecessary parentheses around expression.'
101         filevalue = "'" + (char) 31 + "'"; // no violation
102 
103         //no violation below until https://github.com/checkstyle/checkstyle/issues/14872
104         ck("name", (long) k, (long) ((byte) 0x22));
105         ck("check", (long) k, (long) (byte) 0x24);
106     }
107 
108     public void ck(String byt, long a, long c) {}
109     static class T {}
110     public class Bean {
111         public Object array;
112         public Bean() {
113             array = new int[]{1, 2, 3};
114         }
115     }
116 }