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  public class InputUnnecessaryParenthesesIfStatement {
17  
18      void method(String sectionName) {
19          if ("Content".equals(sectionName) || "Overview".equals(sectionName)
20                  || (!"A".equals(sectionName) // violation 'Unnecessary paren.* around expression'
21                  && !"AbstractClassName".equals(sectionName)
22          )) {
23              System.out.println("sd");
24          }
25      }
26  
27      private void method() {
28          int x, y, z;
29          x = 0;
30          y = 0;
31  
32          z = (x < y) ? x : y;
33  
34          if ((x < y)           // violation 'Unnecessary parentheses around expression'
35                  && (x > z)) { // violation 'Unnecessary parentheses around expression'
36              return;
37          }
38  
39          if (((x < y)           // 2 violations
40                  && (x > z))) { // violation 'Unnecessary parentheses around expression'
41              return;
42          }
43  
44          if (!(x <= y)
45                  || (x >= z)) { // violation 'Unnecessary parentheses around expression'
46              return;
47          }
48  
49          if ((x == y)           // violation 'Unnecessary parentheses around expression'
50                  || (x != z)) { // violation 'Unnecessary parentheses around expression'
51              return;
52          }
53  
54          if ((                       // violation 'Unnecessary parentheses around expression'
55                  (x == y)            // violation 'Unnecessary parentheses around expression'
56                          || (x != z) // violation 'Unnecessary parentheses around expression'
57          )) {
58              return;
59          }
60  
61          if ((Integer.valueOf(x) instanceof Integer) // violation 'parentheses around expression'
62                  || Integer.valueOf(y) instanceof Integer) {
63              return;
64          }
65          if (x == ((y<z) ? y : z) &&
66              ((x>y && y>z)                  // violation 'Unnecessary parentheses around expression'
67                      || (!(x<z) && y>z))) { // violation 'Unnecessary parentheses around expression'
68              return;
69          }
70          if ((x >= 0 && y <= 9)            // violation 'Unnecessary parentheses around expression'
71                   || (z >= 5 && y <= 5)    // violation 'Unnecessary parentheses around expression'
72                   || (z >= 3 && x <= 7)) { // violation 'Unnecessary parentheses around expression'
73              return;
74          }
75          if(x>= 0 && (x<=8 || y<=11) && y>=8) {
76              return;
77          }
78          if((y>=11 && x<=5)            // violation 'Unnecessary parentheses around expression'
79                  || (x<=12 && y>=8)) { // violation 'Unnecessary parentheses around expression'
80              return;
81          }
82      }
83      private void check() {
84          String sectionName = "Some String";
85          if ("Some content".equals(sectionName) || "Some overview".equals(sectionName)
86                  || (!"A".equals(sectionName) // violation 'Unnecessary paren.* around expression'
87                  && !"AbstractClassName".equals(sectionName)
88          )) {
89              return;
90          }
91  
92          if (sectionName instanceof String && "Other Overview".equals(sectionName)
93                  && (!"AbbreviationAsWordInName".equals(sectionName)
94                  || !"AbstractClassName".equals(sectionName)
95          )) {
96              return;
97          }
98      }
99      private void UnaryAndPostfix() {
100         boolean x = true;
101         boolean y = true;
102         int a = 25;
103         if ((++a) >= 54 && x) { // violation 'Unnecessary parentheses around expression'
104             return;
105         }
106         if ((~a) > -27            // violation 'Unnecessary parentheses around expression'
107                  && (a-- < 30)) { // violation 'Unnecessary parentheses around expression'
108             return;
109         }
110         if ((-a) != -27 // violation 'Unnecessary parentheses around expression'
111                  && x) {
112             return;
113         }
114     }
115 }