1 /* 2 DescendantToken 3 limitedTokens = STRING_LITERAL 4 minimumDepth = (default)0 5 maximumDepth = 1 6 minimumNumber = (default)0 7 maximumNumber = 0 8 sumTokenCounts = (default)false 9 minimumMessage = (default)null 10 maximumMessage = Literal Strings should be compared using equals(), not ''==''. 11 tokens = EQUAL, NOT_EQUAL 12 13 14 */ 15 16 package com.puppycrawl.tools.checkstyle.checks.descendanttoken; 17 18 public class InputDescendantTokenStringLiteralEquality 19 { 20 void foo(String name) 21 { 22 if (name == "Lars") // violation 'Literal Strings .* be compared using equals(), not '=='' 23 { 24 // flagged, should use equals 25 } 26 27 if ("Oleg" == name) // violation 'Literal Strings .* be compared using equals(), not '=='' 28 { 29 // flagged, should use equals 30 } 31 32 if ("Oliver" == "Oliver") // violation 'Lit.* Strings .* compared using equals(), not '=='' 33 { 34 // doesn't make much sense because this can be evaluated 35 // to true at compile-time, but is flagged anyway 36 } 37 38 String compare = "Rick"; 39 if (name == compare) 40 { 41 // currently not flagged. 42 // 43 // Implementing this is very complicated, we would need 44 // - type info on the == operands 45 // - prevent false alarms where the user explicitly wants 46 // to compare object identities 47 // 48 // My current feeling is that we should leave finding 49 // this one to manual code inspections. After all MCI is 50 // what some of us get paid for :-) 51 } 52 53 if ("Rick".toUpperCase(java.util.Locale.getDefault()) 54 == "Rick".toLowerCase(java.util.Locale.getDefault())) 55 { 56 // completely dynamic, don't flag 57 } 58 } 59 }