-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathNoConstantsOnly.ql
More file actions
53 lines (47 loc) · 1.45 KB
/
NoConstantsOnly.ql
File metadata and controls
53 lines (47 loc) · 1.45 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
/**
* @name Constant interface anti-pattern
* @description Implementing an interface (or extending an abstract class)
* only to put a number of constant definitions into scope is considered bad practice.
* @kind problem
* @problem.severity recommendation
* @precision high
* @id java/constants-only-interface
* @tags quality
* maintainability
* readability
* modularity
*/
import semmle.code.java.Member
class ConstantField extends Field {
ConstantField() { this.isStatic() and this.isFinal() }
}
pragma[noinline]
predicate typeWithConstantField(RefType t) { exists(ConstantField f | f.getDeclaringType() = t) }
class ConstantRefType extends RefType {
ConstantRefType() {
this.fromSource() and
(
this instanceof Interface
or
this instanceof Class and this.isAbstract()
) and
typeWithConstantField(this) and
forall(Member m | m.getDeclaringType() = this |
m.(Constructor).isDefaultConstructor() or
m instanceof StaticInitializer or
m instanceof ConstantField
)
}
string getKind() {
result = "interface" and this instanceof Interface
or
result = "class" and this instanceof Class
}
}
from ConstantRefType t, RefType sub
where
sub.fromSource() and
sub.getASupertype() = t and
not sub instanceof ConstantRefType and
sub = sub.getSourceDeclaration()
select sub, "Type " + sub.getName() + " implements constant " + t.getKind() + " $@.", t, t.getName()