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