|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| package net.sourceforge.pmd; |
|
5 |
| |
|
6 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceBodyDeclaration; |
|
7 |
| import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; |
|
8 |
| import net.sourceforge.pmd.ast.ASTFormalParameter; |
|
9 |
| import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration; |
|
10 |
| import net.sourceforge.pmd.ast.ASTMethodDeclaration; |
|
11 |
| import net.sourceforge.pmd.ast.ASTTypeDeclaration; |
|
12 |
| import net.sourceforge.pmd.ast.CanSuppressWarnings; |
|
13 |
| import net.sourceforge.pmd.ast.SimpleNode; |
|
14 |
| import net.sourceforge.pmd.symboltable.MethodScope; |
|
15 |
| |
|
16 |
| import java.util.Comparator; |
|
17 |
| import java.util.Iterator; |
|
18 |
| import java.util.List; |
|
19 |
| |
|
20 |
| public class RuleViolation implements IRuleViolation { |
|
21 |
| |
|
22 |
| public static class RuleViolationComparator implements Comparator { |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
13353
| public int compare(Object o1, Object o2) {
|
|
29 |
13353
| IRuleViolation r1 = (IRuleViolation) o1;
|
|
30 |
13353
| IRuleViolation r2 = (IRuleViolation) o2;
|
|
31 |
13353
| if (!r1.getFilename().equals(r2.getFilename())) {
|
|
32 |
5
| return r1.getFilename().compareTo(r2.getFilename());
|
|
33 |
| } |
|
34 |
| |
|
35 |
13348
| if (r1.getBeginLine() != r2.getBeginLine())
|
|
36 |
13329
| return r1.getBeginLine() - r2.getBeginLine();
|
|
37 |
| |
|
38 |
19
| if (r1.getDescription() != null && r2.getDescription() != null && !r1.getDescription().equals(r2.getDescription())) {
|
|
39 |
5
| return r1.getDescription().compareTo(r2.getDescription());
|
|
40 |
| } |
|
41 |
| |
|
42 |
14
| if (r1.getBeginLine() == r2.getBeginLine()) {
|
|
43 |
14
| return 1;
|
|
44 |
| } |
|
45 |
| |
|
46 |
| |
|
47 |
0
| return r1.getBeginLine() - r2.getBeginLine();
|
|
48 |
| } |
|
49 |
| } |
|
50 |
| |
|
51 |
| private Rule rule; |
|
52 |
| private String description; |
|
53 |
| private String filename; |
|
54 |
| |
|
55 |
| private String className; |
|
56 |
| private String methodName; |
|
57 |
| private String packageName; |
|
58 |
| private int beginLine; |
|
59 |
| private int endLine; |
|
60 |
| |
|
61 |
| private int beginColumn; |
|
62 |
| private int endColumn; |
|
63 |
| private boolean isSuppressed; |
|
64 |
| |
|
65 |
236
| public RuleViolation(Rule rule, RuleContext ctx, SimpleNode node) {
|
|
66 |
236
| this(rule, ctx, node, rule.getMessage());
|
|
67 |
| } |
|
68 |
| |
|
69 |
2895
| public RuleViolation(Rule rule, RuleContext ctx, SimpleNode node, String specificMsg) {
|
|
70 |
2895
| this.rule = rule;
|
|
71 |
2895
| this.filename = ctx.getSourceCodeFilename();
|
|
72 |
2895
| this.description = specificMsg;
|
|
73 |
| |
|
74 |
2895
| if (node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class) == null) {
|
|
75 |
| |
|
76 |
2333
| className = "";
|
|
77 |
| } else { |
|
78 |
| |
|
79 |
562
| className = node.getScope().getEnclosingClassScope().getClassName() == null ? "" : node.getScope().getEnclosingClassScope().getClassName();
|
|
80 |
| } |
|
81 |
| |
|
82 |
2895
| methodName = node.getFirstParentOfType(ASTMethodDeclaration.class) == null ? "" : ((MethodScope) node.getScope().getEnclosingMethodScope()).getName();
|
|
83 |
| |
|
84 |
2895
| packageName = node.getScope().getEnclosingSourceFileScope().getPackageName() == null ? "" : node.getScope().getEnclosingSourceFileScope().getPackageName();
|
|
85 |
| |
|
86 |
2895
| beginLine = node.getBeginLine();
|
|
87 |
2895
| endLine = node.getEndLine();
|
|
88 |
2895
| beginColumn = node.getBeginColumn();
|
|
89 |
2895
| endColumn = node.getEndColumn();
|
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
2895
| List parentTypes = node.getParentsOfType(ASTTypeDeclaration.class);
|
|
94 |
2895
| if (node instanceof ASTTypeDeclaration) {
|
|
95 |
1
| parentTypes.add(node);
|
|
96 |
| } |
|
97 |
2895
| parentTypes.addAll(node.getParentsOfType(ASTClassOrInterfaceBodyDeclaration.class));
|
|
98 |
2895
| if (node instanceof ASTClassOrInterfaceBodyDeclaration) {
|
|
99 |
4
| parentTypes.add(node);
|
|
100 |
| } |
|
101 |
2895
| parentTypes.addAll(node.getParentsOfType(ASTFormalParameter.class));
|
|
102 |
2895
| if (node instanceof ASTFormalParameter) {
|
|
103 |
11
| parentTypes.add(node);
|
|
104 |
| } |
|
105 |
2895
| parentTypes.addAll(node.getParentsOfType(ASTLocalVariableDeclaration.class));
|
|
106 |
2895
| if (node instanceof ASTLocalVariableDeclaration) {
|
|
107 |
4
| parentTypes.add(node);
|
|
108 |
| } |
|
109 |
2895
| for (Iterator i = parentTypes.iterator(); i.hasNext();) {
|
|
110 |
1302
| CanSuppressWarnings t = (CanSuppressWarnings) i.next();
|
|
111 |
1302
| if (t.hasSuppressWarningsAnnotationFor(getRule())) {
|
|
112 |
11
| isSuppressed = true;
|
|
113 |
| } |
|
114 |
| } |
|
115 |
| } |
|
116 |
| |
|
117 |
1368
| public Rule getRule() {
|
|
118 |
1368
| return rule;
|
|
119 |
| } |
|
120 |
| |
|
121 |
2889
| public boolean isSuppressed() {
|
|
122 |
2889
| return this.isSuppressed;
|
|
123 |
| } |
|
124 |
| |
|
125 |
0
| public int getBeginColumn() {
|
|
126 |
0
| return beginColumn;
|
|
127 |
| } |
|
128 |
| |
|
129 |
0
| public int getEndColumn() {
|
|
130 |
0
| return endColumn;
|
|
131 |
| } |
|
132 |
| |
|
133 |
128
| public String getDescription() {
|
|
134 |
128
| return description;
|
|
135 |
| } |
|
136 |
| |
|
137 |
132690
| public String getFilename() {
|
|
138 |
132690
| return filename;
|
|
139 |
| } |
|
140 |
| |
|
141 |
2887
| public String getClassName() {
|
|
142 |
2887
| return className;
|
|
143 |
| } |
|
144 |
| |
|
145 |
12
| public String getMethodName() {
|
|
146 |
12
| return methodName;
|
|
147 |
| } |
|
148 |
| |
|
149 |
2890
| public String getPackageName() {
|
|
150 |
2890
| return packageName;
|
|
151 |
| } |
|
152 |
| |
|
153 |
162212
| public int getBeginLine() {
|
|
154 |
162212
| return beginLine;
|
|
155 |
| } |
|
156 |
| |
|
157 |
6
| public int getEndLine() {
|
|
158 |
6
| return endLine;
|
|
159 |
| } |
|
160 |
| |
|
161 |
21
| public String getVariableName() {
|
|
162 |
21
| return "";
|
|
163 |
| } |
|
164 |
| |
|
165 |
0
| public String toString() {
|
|
166 |
0
| return getFilename() + ":" + getRule() + ":" + getDescription() + ":" + beginLine;
|
|
167 |
| } |
|
168 |
| |
|
169 |
| } |