Skip to content

Commit 049ae6a

Browse files
committed
SONARJAVA-1504 use semantic to match methods from serializable
1 parent 5ecbf60 commit 049ae6a

3 files changed

Lines changed: 25 additions & 19 deletions

File tree

its/ruling/src/test/resources/guava/squid-S1172.json

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@
1111
'com.google.guava:guava:src/com/google/common/collect/Cut.java':[
1212
66,
1313
],
14-
'com.google.guava:guava:src/com/google/common/collect/ImmutableAsList.java':[
15-
79,
16-
],
17-
'com.google.guava:guava:src/com/google/common/collect/ImmutableList.java':[
18-
601,
19-
],
20-
'com.google.guava:guava:src/com/google/common/collect/ImmutableSortedSet.java':[
21-
779,
22-
],
2314
'com.google.guava:guava:src/com/google/common/collect/MapMakerInternalMap.java':[
2415
2364,
2516
],

java-checks/src/main/java/org/sonar/java/checks/unused/UnusedMethodParameterCheck.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.sonar.check.Priority;
2727
import org.sonar.check.Rule;
2828
import org.sonar.java.checks.SubscriptionBaseVisitor;
29+
import org.sonar.java.checks.methods.MethodInvocationMatcherCollection;
30+
import org.sonar.java.checks.methods.MethodMatcher;
2931
import org.sonar.java.model.ModifiersUtils;
3032
import org.sonar.java.model.declaration.MethodTreeImpl;
3133
import org.sonar.java.tag.Tag;
@@ -55,6 +57,9 @@
5557
public class UnusedMethodParameterCheck extends SubscriptionBaseVisitor {
5658

5759
private static final String AUTHORIZED_ANNOTATION = "javax.enterprise.event.Observes";
60+
private static final MethodInvocationMatcherCollection SERIALIZABLE_METHODS = MethodInvocationMatcherCollection.create(
61+
MethodMatcher.create().name("writeObject").addParameter("java.io.ObjectOutputStream"),
62+
MethodMatcher.create().name("readObject").addParameter("java.io.ObjectInputStream"));
5863

5964
@Override
6065
public List<Tree.Kind> nodesToVisit() {
@@ -96,13 +101,7 @@ private static boolean isEmptyOrThrowStatement(BlockTree block) {
96101
}
97102

98103
private static boolean isSerializableMethod(MethodTree methodTree) {
99-
boolean result = false;
100-
// FIXME detect methods based on type of arg and throws, not arity.
101-
if (ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.PRIVATE) && methodTree.parameters().size() == 1) {
102-
result |= "writeObject".equals(methodTree.simpleName().name()) && methodTree.throwsClauses().size() == 1;
103-
result |= "readObject".equals(methodTree.simpleName().name()) && methodTree.throwsClauses().size() == 2;
104-
}
105-
return result;
104+
return ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.PRIVATE) && SERIALIZABLE_METHODS.anyMatch(methodTree);
106105
}
107106

108107
private static boolean isOverriding(MethodTree tree) {

java-checks/src/test/files/checks/unused/UnusedMethodParameterCheck.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import javax.annotation.Nonnull;
22
import javax.enterprise.event.Observes;
3+
import java.io.IOException;
4+
import java.io.NotSerializableException;
5+
import java.io.ObjectInputStream;
6+
import java.io.ObjectOutputStream;
37

48
class A extends B{
59
void doSomething(int a, int b) { // Noncompliant {{Remove this unused method parameter "b".}} [[sc=31;ec=32]]
@@ -37,7 +41,7 @@ void foo(int a) {
3741
}
3842

3943
class D extends C {
40-
void foo(int b, int a) { // Noncompliant {{Remove this unused method parameter "b".}} [[sc=16;ec=17;secondary=40]]
44+
void foo(int b, int a) { // Noncompliant {{Remove this unused method parameter "b".}} [[sc=16;ec=17;secondary=44]]
4145
System.out.println("");
4246
}
4347
}
@@ -66,12 +70,12 @@ class G implements inter {
6670
void foo(int a) {
6771
System.out.println("plop");
6872
}
69-
private void writeObject(ObjectOutputStream out)
73+
private void writeObject(ObjectOutputStream out) // Compliant
7074
throws IOException {
7175
throw new NotSerializableException(getClass().getName());
7276
}
7377

74-
private void readObject(ObjectInputStream in)
78+
private void readObject(ObjectInputStream in) // Compliant
7579
throws IOException, ClassNotFoundException {
7680
throw new NotSerializableException(getClass().getName());
7781
}
@@ -102,6 +106,18 @@ public Supplier<String> parameterNotUsed(final Object o) {
102106
}
103107
}
104108

109+
class MethodFromSerialization {
110+
private void writeObject(ObjectOutputStream out) throws MyException { // Compliant
111+
throw new MyException();
112+
}
113+
114+
private void readObject(ObjectInputStream in) throws MyException { // Compliant
115+
throw new MyException();
116+
}
117+
118+
private static class MyException extends Exception {}
119+
}
120+
105121
class Annotations {
106122
public void foo(@Observes Object event, int arg2) { // Compliant
107123
System.out.println(arg2);

0 commit comments

Comments
 (0)