-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ASTAnnotation.java
110 lines (92 loc) · 3.62 KB
/
ASTAnnotation.java
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.ast;
import java.util.Arrays;
import java.util.List;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.annotation.InternalApi;
/**
* Represents an annotation. This node has three possible children,
* that correspond to specific syntactic variants.
*
* <pre>
*
* Annotation ::= {@linkplain ASTNormalAnnotation NormalAnnotation}
* | {@linkplain ASTSingleMemberAnnotation SingleMemberAnnotation}
* | {@linkplain ASTMarkerAnnotation MarkerAnnotation}
*
* </pre>
*/
public class ASTAnnotation extends AbstractJavaTypeNode {
private static final List<String> UNUSED_RULES
= Arrays.asList("UnusedPrivateField", "UnusedLocalVariable", "UnusedPrivateMethod", "UnusedFormalParameter", "UnusedAssignment");
private static final List<String> SERIAL_RULES = Arrays.asList("BeanMembersShouldSerialize", "MissingSerialVersionUID");
@InternalApi
@Deprecated
public ASTAnnotation(int id) {
super(id);
}
@InternalApi
@Deprecated
public ASTAnnotation(JavaParser p, int id) {
super(p, id);
}
/**
* Returns the name of the annotation as it is used,
* eg {@code java.lang.Override} or {@code Override}.
*/
public String getAnnotationName() {
return getChild(0).getChild(0).getImage();
}
// @formatter:off
/**
* Returns true if this annotation suppresses the given rule.
* The suppression annotation is {@link SuppressWarnings}.
* This method returns true if this annotation is a SuppressWarnings,
* and if the set of suppressed warnings ({@link SuppressWarnings#value()})
* contains at least one of those:
* <ul>
* <li>"PMD" (suppresses all rules);
* <li>"PMD.rulename", where rulename is the name of the given rule;
* <li>"all" (conventional value to suppress all warnings).
* </ul>
*
* <p>Additionally, the following values suppress a specific set of rules:
* <ul>
* <li>{@code "unused"}: suppresses rules like UnusedLocalVariable or UnusedPrivateField;
* <li>{@code "serial"}: suppresses BeanMembersShouldSerialize and MissingSerialVersionUID;
* </ul>
*
* @param rule The rule for which to check for suppression
*
* @return True if this annotation suppresses the given rule
*/
// @formatter:on
public boolean suppresses(Rule rule) {
if (getChild(0) instanceof ASTMarkerAnnotation) {
return false;
}
// if (SuppressWarnings.class.equals(getType())) { // typeres is not always on
if (isSuppressWarnings()) {
for (ASTLiteral element : findDescendantsOfType(ASTLiteral.class)) {
if (element.hasImageEqualTo("\"PMD\"") || element.hasImageEqualTo("\"PMD." + rule.getName() + "\"")
// Check for standard annotations values
|| element.hasImageEqualTo("\"all\"")
|| element.hasImageEqualTo("\"serial\"") && SERIAL_RULES.contains(rule.getName())
|| element.hasImageEqualTo("\"unused\"") && UNUSED_RULES.contains(rule.getName())) {
return true;
}
}
}
return false;
}
private boolean isSuppressWarnings() {
return "SuppressWarnings".equals(getAnnotationName())
|| "java.lang.SuppressWarnings".equals(getAnnotationName());
}
@Override
public Object jjtAccept(JavaParserVisitor visitor, Object data) {
return visitor.visit(this, data);
}
}