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 |
11600
| public int compare(Object o1, Object o2) {
|
29 |
11600
| IRuleViolation r1 = (IRuleViolation) o1;
|
30 |
11600
| IRuleViolation r2 = (IRuleViolation) o2;
|
31 |
11600
| if (!r1.getFilename().equals(r2.getFilename())) {
|
32 |
1
| return r1.getFilename().compareTo(r2.getFilename());
|
33 |
| } |
34 |
| |
35 |
11599
| if (r1.getBeginLine() != r2.getBeginLine())
|
36 |
11590
| return r1.getBeginLine() - r2.getBeginLine();
|
37 |
| |
38 |
9
| if (r1.getDescription() != null && r2.getDescription() != null && !r1.getDescription().equals(r2.getDescription())) {
|
39 |
4
| return r1.getDescription().compareTo(r2.getDescription());
|
40 |
| } |
41 |
| |
42 |
5
| if (r1.getBeginLine() == r2.getBeginLine()) {
|
43 |
5
| 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 |
136
| public RuleViolation(Rule rule, RuleContext ctx, SimpleNode node) {
|
66 |
136
| this(rule, ctx, node, rule.getMessage());
|
67 |
| } |
68 |
| |
69 |
2675
| public RuleViolation(Rule rule, RuleContext ctx, SimpleNode node, String specificMsg) {
|
70 |
2675
| this.rule = rule;
|
71 |
2675
| this.filename = ctx.getSourceCodeFilename();
|
72 |
2675
| this.description = specificMsg;
|
73 |
| |
74 |
2675
| if (node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class) == null) {
|
75 |
| |
76 |
2221
| className = "";
|
77 |
| } else { |
78 |
| |
79 |
454
| className = node.getScope().getEnclosingClassScope().getClassName() == null ? "" : node.getScope().getEnclosingClassScope().getClassName();
|
80 |
| } |
81 |
| |
82 |
2675
| methodName = node.getFirstParentOfType(ASTMethodDeclaration.class) == null ? "" : ((MethodScope) node.getScope().getEnclosingMethodScope()).getName();
|
83 |
| |
84 |
2675
| packageName = node.getScope().getEnclosingSourceFileScope().getPackageName() == null ? "" : node.getScope().getEnclosingSourceFileScope().getPackageName();
|
85 |
| |
86 |
2675
| beginLine = node.getBeginLine();
|
87 |
2675
| endLine = node.getEndLine();
|
88 |
2675
| beginColumn = node.getBeginColumn();
|
89 |
2675
| endColumn = node.getEndColumn();
|
90 |
| |
91 |
| |
92 |
| |
93 |
2675
| List parentTypes = node.getParentsOfType(ASTTypeDeclaration.class);
|
94 |
2675
| if (node instanceof ASTTypeDeclaration) {
|
95 |
0
| parentTypes.add(node);
|
96 |
| } |
97 |
2675
| parentTypes.addAll(node.getParentsOfType(ASTClassOrInterfaceBodyDeclaration.class));
|
98 |
2675
| if (node instanceof ASTClassOrInterfaceBodyDeclaration) {
|
99 |
1
| parentTypes.add(node);
|
100 |
| } |
101 |
2675
| parentTypes.addAll(node.getParentsOfType(ASTFormalParameter.class));
|
102 |
2675
| if (node instanceof ASTFormalParameter) {
|
103 |
10
| parentTypes.add(node);
|
104 |
| } |
105 |
2675
| parentTypes.addAll(node.getParentsOfType(ASTLocalVariableDeclaration.class));
|
106 |
2675
| if (node instanceof ASTLocalVariableDeclaration) {
|
107 |
4
| parentTypes.add(node);
|
108 |
| } |
109 |
2675
| for (Iterator i = parentTypes.iterator(); i.hasNext();) {
|
110 |
1055
| CanSuppressWarnings t = (CanSuppressWarnings) i.next();
|
111 |
1055
| if (t.hasSuppressWarningsAnnotationFor(getRule())) {
|
112 |
10
| isSuppressed = true;
|
113 |
| } |
114 |
| } |
115 |
| } |
116 |
| |
117 |
1079
| public Rule getRule() {
|
118 |
1079
| return rule;
|
119 |
| } |
120 |
| |
121 |
2670
| public boolean isSuppressed() {
|
122 |
2670
| 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 |
53
| public String getDescription() {
|
134 |
53
| return description;
|
135 |
| } |
136 |
| |
137 |
106894
| public String getFilename() {
|
138 |
106894
| return filename;
|
139 |
| } |
140 |
| |
141 |
2665
| public String getClassName() {
|
142 |
2665
| return className;
|
143 |
| } |
144 |
| |
145 |
5
| public String getMethodName() {
|
146 |
5
| return methodName;
|
147 |
| } |
148 |
| |
149 |
2665
| public String getPackageName() {
|
150 |
2665
| return packageName;
|
151 |
| } |
152 |
| |
153 |
132749
| public int getBeginLine() {
|
154 |
132749
| return beginLine;
|
155 |
| } |
156 |
| |
157 |
0
| public int getEndLine() {
|
158 |
0
| return endLine;
|
159 |
| } |
160 |
| |
161 |
18
| public String getVariableName() {
|
162 |
18
| return "";
|
163 |
| } |
164 |
| |
165 |
0
| public String toString() {
|
166 |
0
| return getFilename() + ":" + getRule() + ":" + getDescription() + ":" + beginLine;
|
167 |
| } |
168 |
| |
169 |
| } |