IG: generate serialVersionUID correctly in the presence of bridge methods
authorBas Leijdekkers <basleijdekkers@gmail.com>
Mon, 22 Jun 2015 15:26:56 +0000 (17:26 +0200)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Tue, 23 Jun 2015 13:56:53 +0000 (15:56 +0200)
plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/fixes/SerialVersionUIDBuilder.java
plugins/InspectionGadgets/test/com/siyeh/igfixes/serialization/serialVersionUID/BridgeMethod.after.java [new file with mode: 0644]
plugins/InspectionGadgets/test/com/siyeh/igfixes/serialization/serialVersionUID/BridgeMethod.java [new file with mode: 0644]
plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/serialization/AddSerialVersionUIDFixTest.java

index d2e50461b5860a3bcd32c5893159518647e078c2..9c7dec10b9ca2ca111233bb81283546d1cf2a0b2 100644 (file)
@@ -18,9 +18,12 @@ package com.siyeh.ig.fixes;
 import com.intellij.openapi.project.Project;
 import com.intellij.psi.*;
 import com.intellij.psi.search.GlobalSearchScope;
+import com.intellij.psi.search.searches.SuperMethodsSearch;
 import com.intellij.psi.tree.IElementType;
+import com.intellij.psi.util.MethodSignatureBackedByPsiMethod;
 import com.intellij.psi.util.PsiFormatUtil;
 import com.intellij.psi.util.PsiUtil;
+import com.intellij.util.Processor;
 import com.siyeh.ig.psiutils.ClassUtils;
 import com.siyeh.ig.psiutils.TypeUtils;
 import org.jetbrains.annotations.NonNls;
@@ -76,16 +79,22 @@ public class SerialVersionUIDBuilder extends JavaRecursiveElementVisitor {
     };
 
   private SerialVersionUIDBuilder(PsiClass clazz) {
-    super();
     this.clazz = clazz;
     nonPrivateMethods = new HashSet<MemberSignature>();
     final PsiMethod[] methods = clazz.getMethods();
     for (final PsiMethod method : methods) {
-      if (!method.isConstructor() &&
-          !method.hasModifierProperty(PsiModifier.PRIVATE)) {
-        final MemberSignature methodSignature =
-          new MemberSignature(method);
+      if (!method.isConstructor() && !method.hasModifierProperty(PsiModifier.PRIVATE)) {
+        final MemberSignature methodSignature = new MemberSignature(method);
         nonPrivateMethods.add(methodSignature);
+        SuperMethodsSearch.search(method, null, true, false).forEach(new Processor<MethodSignatureBackedByPsiMethod>() {
+          @Override
+          public boolean process(MethodSignatureBackedByPsiMethod method) {
+            final MemberSignature superSignature = new MemberSignature(methodSignature.getName(), methodSignature.getModifiers(),
+                                                                       MemberSignature.createMethodSignature(method.getMethod()));
+            nonPrivateMethods.add(superSignature);
+            return true;
+          }
+        });
       }
     }
     nonPrivateFields = new HashSet<MemberSignature>();
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/serialization/serialVersionUID/BridgeMethod.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/serialization/serialVersionUID/BridgeMethod.after.java
new file mode 100644 (file)
index 0000000..53eea62
--- /dev/null
@@ -0,0 +1,12 @@
+import java.io.Serializable;
+
+public class BridgeMethod implements Serializable, Comparable<BridgeMethod> {
+
+    private static final long serialVersionUID = 937806248141277629L;
+
+    @Override
+  public int compareTo(BridgeMethod serialVersionUIDTest2) {
+    return 0;
+  }
+
+}
\ No newline at end of file
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/serialization/serialVersionUID/BridgeMethod.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/serialization/serialVersionUID/BridgeMethod.java
new file mode 100644 (file)
index 0000000..8995d65
--- /dev/null
@@ -0,0 +1,10 @@
+import java.io.Serializable;
+
+public class <caret>BridgeMethod implements Serializable, Comparable<BridgeMethod> {
+
+  @Override
+  public int compareTo(BridgeMethod serialVersionUIDTest2) {
+    return 0;
+  }
+
+}
\ No newline at end of file
index c2d340cf56a5c96e3c86fd2bd7c21503195dda35..01e33ad1a237ceb8512406990367a6757c229387 100644 (file)
@@ -34,4 +34,5 @@ public class AddSerialVersionUIDFixTest extends IGQuickFixesTestCase {
 
   public void testClassAccess() { doTest(); }
   public void testGenericParameter() { doTest(); }
+  public void testBridgeMethod() { doTest(); }
 }