Repository navigation
Expand file tree
/
Copy pathFieldDeclarationsShouldBeAtStartOfClassRule.java
More file actions
110 lines (91 loc) · 4.49 KB
/
Copy pathFieldDeclarationsShouldBeAtStartOfClassRule.java
File metadata and controls
110 lines (91 loc) · 4.49 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
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.codestyle;
import static net.sourceforge.pmd.properties.PropertyFactory.booleanProperty;
import net.sourceforge.pmd.lang.java.ast.ASTBodyDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTEmptyDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTEnumConstant;
import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTInitializer;
import net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.reporting.RuleContext;
/**
* Detects fields that are declared after methods, constructors, etc. It was a
* XPath rule, but the Java version is much faster.
*/
public class FieldDeclarationsShouldBeAtStartOfClassRule extends AbstractJavaRulechainRule {
private static final PropertyDescriptor<Boolean> IGNORE_ANONYMOUS_CLASS_DECLARATIONS =
booleanProperty("ignoreAnonymousClassDeclarations")
.defaultValue(true)
.desc("Ignore field declarations, that are initialized with an anonymous class creation expression").build();
private static final PropertyDescriptor<Boolean> IGNORE_ENUM_DECLARATIONS =
booleanProperty("ignoreEnumDeclarations")
.defaultValue(true)
.desc("Ignore enum declarations that precede fields").build();
private static final PropertyDescriptor<Boolean> IGNORE_INTERFACE_DECLARATIONS =
booleanProperty("ignoreInterfaceDeclarations")
.defaultValue(false)
.desc("Ignore interface declarations that precede fields").build();
public FieldDeclarationsShouldBeAtStartOfClassRule() {
super(ASTTypeDeclaration.class);
definePropertyDescriptor(IGNORE_ANONYMOUS_CLASS_DECLARATIONS);
definePropertyDescriptor(IGNORE_INTERFACE_DECLARATIONS);
definePropertyDescriptor(IGNORE_ENUM_DECLARATIONS);
}
@Override
public Object visitJavaNode(JavaNode node, Object data) {
ASTTypeDeclaration typeDeclaration = (ASTTypeDeclaration) node;
RuleContext ctx = (RuleContext) data;
visitTypeDeclaration(typeDeclaration, ctx);
return null;
}
/**
* @deprecated since 7.25.0. This method should have never been public.
*/
@Deprecated
public Object visit(ASTTypeDeclaration node, Object data) {
RuleContext ctx = (RuleContext) data;
visitTypeDeclaration(node, ctx);
return null;
}
private void visitTypeDeclaration(ASTTypeDeclaration node, RuleContext ctx) {
boolean inStartOfClass = true;
for (ASTBodyDeclaration declaration : node.getDeclarations()) {
if (!isAllowedAtStartOfClass(declaration)) {
inStartOfClass = false;
}
if (!inStartOfClass && declaration instanceof ASTFieldDeclaration) {
ASTFieldDeclaration field = (ASTFieldDeclaration) declaration;
if (!isInitializerOk(field)) {
ctx.addViolation(declaration);
}
}
}
}
private boolean isAllowedAtStartOfClass(ASTBodyDeclaration declaration) {
return declaration instanceof ASTFieldDeclaration
|| declaration instanceof ASTInitializer
|| declaration instanceof ASTEnumConstant
|| declaration instanceof ASTEmptyDeclaration
|| declaration instanceof ASTEnumDeclaration && getProperty(IGNORE_ENUM_DECLARATIONS)
|| isInterface(declaration) && getProperty(IGNORE_INTERFACE_DECLARATIONS);
}
private boolean isInterface(ASTBodyDeclaration declaration) {
return declaration instanceof ASTTypeDeclaration
&& ((ASTTypeDeclaration) declaration).isRegularInterface();
}
private boolean isInitializerOk(ASTFieldDeclaration fieldDeclaration) {
if (getProperty(IGNORE_ANONYMOUS_CLASS_DECLARATIONS) && fieldDeclaration.getVarIds().count() == 1) {
ASTExpression initializer = fieldDeclaration.getVarIds().firstOrThrow().getInitializer();
return JavaAstUtils.isAnonymousClassCreation(initializer);
}
return false;
}
}