1
2
3
4
5 package com.qulice.pmd.rules;
6
7 import net.sourceforge.pmd.lang.java.ast.ASTClassType;
8 import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
9 import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
10
11
12
13
14
15
16
17
18
19
20
21 final class NamedSupertype {
22
23
24
25
26 private final String canonical;
27
28
29
30
31
32
33 NamedSupertype(final String name) {
34 this.canonical = name;
35 }
36
37
38
39
40
41
42
43
44 boolean matches(final ASTClassType clause) {
45 return clause != null
46 && this.simple().equals(clause.getSimpleName())
47 && (this.pack().equals(clause.getPackageQualifier())
48 || this.imported(clause.getRoot()));
49 }
50
51 private String simple() {
52 return this.canonical.substring(this.canonical.lastIndexOf('.') + 1);
53 }
54
55 private String pack() {
56 return this.canonical.substring(0, this.canonical.lastIndexOf('.'));
57 }
58
59 private boolean imported(final ASTCompilationUnit unit) {
60 return unit.children(ASTImportDeclaration.class).any(this::brings);
61 }
62
63 private boolean brings(final ASTImportDeclaration imported) {
64 return this.canonical.equals(imported.getImportedName())
65 || imported.isImportOnDemand()
66 && this.pack().equals(imported.getImportedName());
67 }
68 }