Repository navigation
Expand file tree
/
Copy pathInnerClassInInterfaceCheck.java
More file actions
97 lines (84 loc) · 3.61 KB
/
Copy pathInnerClassInInterfaceCheck.java
File metadata and controls
97 lines (84 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* SonarQube Java
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.checks;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.sonar.check.Rule;
import org.sonar.java.model.ModifiersUtils;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
import org.sonar.plugins.java.api.tree.ClassTree;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.Modifier;
import org.sonar.plugins.java.api.tree.Tree;
@Rule(key = "S9398")
public class InnerClassInInterfaceCheck extends IssuableSubscriptionVisitor {
private static final String BUILDER_SUFFIX = "Builder";
@Override
public List<Tree.Kind> nodesToVisit() {
return Collections.singletonList(Tree.Kind.INTERFACE);
}
@Override
public void visitNode(Tree tree) {
ClassTree interfaceTree = (ClassTree) tree;
Set<Symbol> signatureTypes = signatureTypes(interfaceTree);
interfaceTree.members().stream()
.filter(member -> member.is(Tree.Kind.CLASS))
.map(ClassTree.class::cast)
.filter(classTree -> !ModifiersUtils.hasModifier(classTree.modifiers(), Modifier.ABSTRACT))
.filter(classTree -> implementsInterface(classTree, interfaceTree) || !isHelperType(classTree, signatureTypes))
.forEach(classTree -> reportIssue(classTree.simpleName(), "Move this class out of this interface."));
}
private static boolean implementsInterface(ClassTree classTree, ClassTree interfaceTree) {
return classTree.symbol().type().isSubtypeOf(interfaceTree.symbol().type());
}
/**
* A nested class is considered a helper type of its enclosing interface when it is a builder,
* or when it appears in the signature (return type, parameter types or thrown exceptions)
* of a method declared by the interface.
*/
private static boolean isHelperType(ClassTree classTree, Set<Symbol> signatureTypes) {
return classTree.simpleName().name().endsWith(BUILDER_SUFFIX) || signatureTypes.contains(classTree.symbol());
}
private static Set<Symbol> signatureTypes(ClassTree interfaceTree) {
TypeSymbolsCollector collector = new TypeSymbolsCollector();
interfaceTree.members().stream()
.filter(MethodTree.class::isInstance)
.map(MethodTree.class::cast)
.forEach(collector::scanSignature);
return collector.symbols;
}
private static class TypeSymbolsCollector extends BaseTreeVisitor {
private final Set<Symbol> symbols = new HashSet<>();
void scanSignature(MethodTree methodTree) {
scan(methodTree.returnType());
methodTree.parameters().forEach(parameter -> scan(parameter.type()));
scan(methodTree.throwsClauses());
}
@Override
public void visitIdentifier(IdentifierTree tree) {
Symbol symbol = tree.symbol();
if (symbol.isTypeSymbol()) {
symbols.add(symbol.type().erasure().symbol());
}
}
}
}