IG: Ignore case in "Suspicious getter/setter" inspection (IDEA-161104)
authorBas Leijdekkers <basleijdekkers@gmail.com>
Sat, 17 Sep 2016 09:33:18 +0000 (11:33 +0200)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Tue, 20 Sep 2016 15:37:16 +0000 (17:37 +0200)
plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/javabeans/SuspiciousGetterSetterInspection.java
plugins/InspectionGadgets/test/com/siyeh/igtest/javabeans/suspicious_getter_setter/NoPrefix.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igtest/javabeans/suspicious_getter_setter/SuspiciousGetterSetter.java
plugins/InspectionGadgets/testsrc/com/siyeh/ig/javabeans/SuspiciousGetterSetterInspectionTest.java

index 114e5d184195a1e965c1914ce4377085e4d139ca..7f7cf361b10bf64ba5b3545e692b8006fd8c8568 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2015 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -104,11 +104,13 @@ public class SuspiciousGetterSetterInspection extends BaseInspection {
       else {
         return;
       }
+      if (fieldName == null) {
+        return;
+      }
       final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(method.getProject());
-      final String decapitalized = decapitalize(extractedFieldName);
-      final String computedFieldName = codeStyleManager.propertyNameToVariableName(decapitalized, VariableKind.FIELD);
-      final String computedStaticFieldName = codeStyleManager.propertyNameToVariableName(decapitalized, VariableKind.STATIC_FINAL_FIELD);
-      if (fieldName.equals(computedFieldName) || fieldName.equals(computedStaticFieldName)) {
+      final String computedFieldName = codeStyleManager.propertyNameToVariableName(extractedFieldName, VariableKind.FIELD);
+      final String computedStaticFieldName = codeStyleManager.propertyNameToVariableName(extractedFieldName, VariableKind.STATIC_FINAL_FIELD);
+      if (fieldName.equalsIgnoreCase(computedFieldName) || fieldName.equalsIgnoreCase(computedStaticFieldName)) {
         return;
       }
       if (onlyWarnWhenFieldPresent) {
@@ -128,19 +130,4 @@ public class SuspiciousGetterSetterInspection extends BaseInspection {
   private static boolean nameStartsWith(String name, String prefix) {
     return name.startsWith(prefix) && name.length() != prefix.length() && Character.isUpperCase(name.charAt(prefix.length()));
   }
-
-  private static String decapitalize(String name) {
-    final StringBuilder result = new StringBuilder();
-    for (int i = 0, length = name.length(); i < length; i++) {
-      final char c = name.charAt(i);
-      if (Character.isUpperCase(c)) {
-        result.append(Character.toLowerCase(c));
-      }
-      else {
-        result.append(name.substring(i));
-        return result.toString();
-      }
-    }
-    return result.toString();
-  }
 }
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/javabeans/suspicious_getter_setter/NoPrefix.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/javabeans/suspicious_getter_setter/NoPrefix.java
new file mode 100644 (file)
index 0000000..f524b60
--- /dev/null
@@ -0,0 +1,12 @@
+class NoPrefix {
+  private String sKey;
+  private String url;
+
+  public String getSKey() {
+    return sKey;
+  }
+
+  public String getURL() {
+    return url;
+  }
+}
\ No newline at end of file
index fa8af9982ab3448bbe8a1da5ffeaa5143483d303..bf46110547b4154a2adf141c79f36186d44a7c6b 100644 (file)
@@ -5,6 +5,8 @@ public class SuspiciousGetterSetter {
 
   private static final String MSG_KEY = "";
 
+  private String myUrl;
+
   public String <warning descr="Getter 'getTwo()' returns field 'myOne'">getTwo</warning>() {
     return myOne;
   }
@@ -16,4 +18,8 @@ public class SuspiciousGetterSetter {
   String getMsgKey() { // suspicious getter
     return MSG_KEY;
   }
+
+  String getURL() {
+    return myUrl;
+  }
 }
\ No newline at end of file
index 376de8cb8f77f1436531fda8e3ceb13794ac0eb6..10f92bbe89ce966655080cc65ac3456f8f5bb4e8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2014 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -37,11 +37,13 @@ public class SuspiciousGetterSetterInspectionTest extends LightInspectionTestCas
     }
   }
 
+  public void testNoPrefix() {
+    doTest();
+  }
+
   @Nullable
   @Override
   protected InspectionProfileEntry getInspection() {
-    final SuspiciousGetterSetterInspection inspection = new SuspiciousGetterSetterInspection();
-    inspection.onlyWarnWhenFieldPresent = true;
-    return inspection;
+    return new SuspiciousGetterSetterInspection();
   }
 }
\ No newline at end of file