View Javadoc
1   /*
2   UnnecessaryParentheses
3   tokens = 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, QUESTION
10  
11  
12  */
13  
14  package com.puppycrawl.tools.checkstyle.checks.coding.unnecessaryparentheses;
15  
16  public class InputUnnecessaryParenthesesConditionalExpression {
17      void method() {
18          int w = 5;
19          int x = (w == 3) ? (3) : (4);
20          // 3 violations above
21          //  'Unnecessary parentheses around expression'
22          //  'Unnecessary parentheses around literal '3''.
23          //  'Unnecessary parentheses around literal '4''.
24          int y = !(w>x) ? 3 : 4;
25          int z1 = (!(y >= w)) ? 5 : 6; // violation 'Unnecessary parentheses around expression'
26  
27          int z2 = 5 > 3 ? 2 : 1;
28          int z3 = (z2 > 0) ? 5 : (z2 < -10) ? 7 : 3;
29          // 2 violations above
30          //  'Unnecessary parentheses around expression'
31          //  'Unnecessary parentheses around expression'
32  
33          Object o = "";
34          // violation below 'Unnecessary parentheses around expression'
35          boolean result = (o instanceof String) ?
36                  (o instanceof String) : (!(o instanceof String));
37          // 2 violations above
38          //  'Unnecessary parentheses around expression'
39          //  'Unnecessary parentheses around expression'
40  
41      }
42  }