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