Skip to content

Commit

Permalink
SONARJAVA-1757 Ignore serializable classes (#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wohops authored Jul 22, 2016
1 parent 0851762 commit 610858d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 44 deletions.
30 changes: 0 additions & 30 deletions its/ruling/src/test/resources/jdk6/squid-S1118.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,6 @@
'jdk6:java/io/ObjectStreamClass.java':[
65,
],
'jdk6:java/lang/CharacterData00.java':[
19,
],
'jdk6:java/lang/CharacterData01.java':[
18,
],
'jdk6:java/lang/CharacterData02.java':[
17,
],
'jdk6:java/lang/CharacterData0E.java':[
16,
],
'jdk6:java/lang/CharacterDataLatin1.java':[
17,
],
'jdk6:java/lang/CharacterDataPrivateUse.java':[
16,
],
'jdk6:java/lang/CharacterDataUndefined.java':[
16,
],
'jdk6:java/lang/ConditionalSpecialCasing.java':[
29,
],
'jdk6:java/lang/Shutdown.java':[
22,
],
'jdk6:java/lang/Terminator.java':[
23,
],
'jdk6:java/lang/reflect/Modifier.java':[
35,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonar.java.checks;

import com.google.common.collect.ImmutableList;

import org.sonar.check.Rule;
import org.sonar.java.model.ModifiersUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
Expand All @@ -43,26 +44,27 @@ public List<Tree.Kind> nodesToVisit() {
@Override
public void visitNode(Tree tree) {
ClassTree classTree = (ClassTree) tree;
if (!anonymousClass(classTree) && !extendsAnotherClass(classTree) && hasOnlyStaticMembers(classTree)) {
boolean hasImplicitPublicConstructor = true;
for (MethodTree explicitConstructor : getExplicitConstructors(classTree)) {
hasImplicitPublicConstructor = false;
if (isPublicConstructor(explicitConstructor)) {
reportIssue(explicitConstructor.simpleName(), "Hide this public constructor.");
}
}
if (hasImplicitPublicConstructor) {
reportIssue(classTree.simpleName(), "Add a private constructor to hide the implicit public one.");
if (!hasSemantic() || anonymousClass(classTree) || extendsAnotherClassOrImplementsSerializable(classTree) || !hasOnlyStaticMembers(classTree)) {
return;
}
boolean hasImplicitPublicConstructor = true;
for (MethodTree explicitConstructor : getExplicitConstructors(classTree)) {
hasImplicitPublicConstructor = false;
if (isPublicConstructor(explicitConstructor)) {
reportIssue(explicitConstructor.simpleName(), "Hide this public constructor.");
}
}
if (hasImplicitPublicConstructor) {
reportIssue(classTree.simpleName(), "Add a private constructor to hide the implicit public one.");
}
}

private static boolean anonymousClass(ClassTree classTree) {
return classTree.simpleName() == null;
}

private static boolean extendsAnotherClass(ClassTree classTree) {
return classTree.superClass() != null;
private static boolean extendsAnotherClassOrImplementsSerializable(ClassTree classTree) {
return classTree.superClass() != null || classTree.symbol().type().isSubtypeOf("java.io.Serializable");
}

private static boolean hasOnlyStaticMembers(ClassTree classTree) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.io.Serializable;
import java.lang.Object;

class Foo1 {
Expand Down Expand Up @@ -91,10 +92,10 @@ static class plop {
}

class Foo13 {

private Foo13() {
}

;
}

Expand All @@ -108,3 +109,7 @@ class Foo15 {
public static void foo() {}
};
}

class Foo16 implements Serializable { // Compliant
private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class UtilityClassWithPublicConstructorCheckTest {
@Test
public void test() {
JavaCheckVerifier.verify("src/test/files/checks/UtilityClassWithPublicConstructorCheck.java", new UtilityClassWithPublicConstructorCheck());
JavaCheckVerifier.verifyNoIssueWithoutSemantic("src/test/files/checks/UtilityClassWithPublicConstructorCheck.java", new UtilityClassWithPublicConstructorCheck());
}

}

0 comments on commit 610858d

Please sign in to comment.