View Javadoc
1   /*
2   StringLiteralEquality
3   
4   
5   */
6   
7   package com.puppycrawl.tools.checkstyle.checks.coding.stringliteralequality;
8   import java.util.Locale;
9   /**
10   * Input file for the StringLiteralEqualityCheck
11   * @author Lars Kühne
12   */
13  public class InputStringLiteralEquality
14  {
15      void foo(String name)
16      {
17          if (name == "Lars") // violation
18          {
19              // flagged, should use equals
20          }
21  
22          if ("Oleg" == name) // violation
23          {
24              // flagged, should use equals
25          }
26  
27          if ("Oliver" == "Oliver") // violation
28          {
29              // doesn't make much sense because this can be evaluated
30              // to true at compile-time, but is flagged anyway
31          }
32  
33          String compare = "Rick";
34          if (name == compare)
35          {
36              // currently not flagged.
37              //
38              // Implementing this is very complicated, we would need
39              // - type info on the == operands
40              // - prevent false alarms where the user explicitly wants
41              //   to compare object identities
42              //
43              // My current feeling is that we should leave finding
44              // this one to manual code inspections. After all MCI is
45              // what some of us get paid for :-)
46          }
47  
48          if ("Rick".toUpperCase(Locale.getDefault()) == "Rick".toLowerCase(Locale.getDefault()))
49          {
50              // completely dynamic, don't flag
51          }
52      }
53  
54      public static boolean isMethod(Integer value) {
55          return value.intValue() != value.intValue() + 1;
56      }
57  }