KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > RuleViolation


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 JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 public class RuleViolation implements IRuleViolation {
21
22     public static class RuleViolationComparator implements Comparator JavaDoc {
23         //
24
// Changed logic of Comparator so that rules in the same file
25
// get grouped together in the output report.
26
// DDP 7/11/2002
27
//
28
public int compare(Object JavaDoc o1, Object JavaDoc 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             // line number diff maps nicely to compare()
47
return r1.getBeginLine() - r2.getBeginLine();
48         }
49     }
50
51     private Rule rule;
52     private String JavaDoc description;
53     private String JavaDoc filename;
54
55     private String JavaDoc className;
56     private String JavaDoc methodName;
57     private String JavaDoc 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 JavaDoc specificMsg) {
70         this.rule = rule;
71         this.filename = ctx.getSourceCodeFilename();
72         this.description = specificMsg;
73
74         if (node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class) == null) {
75             // This takes care of nodes which are outside a class definition - i.e., import declarations
76
className = "";
77         } else {
78             // default to symbol table lookup
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         // TODO combine this duplicated code
92
// TODO same for duplicated code in ASTTypeDeclaration && ASTClassOrInterfaceBodyDeclaration
93
List JavaDoc 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 JavaDoc 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 JavaDoc getDescription() {
134         return description;
135     }
136
137     public String JavaDoc getFilename() {
138         return filename;
139     }
140
141     public String JavaDoc getClassName() {
142         return className;
143     }
144
145     public String JavaDoc getMethodName() {
146         return methodName;
147     }
148
149     public String JavaDoc getPackageName() {
150         return packageName;
151     }
152
153     public int getBeginLine() {
154         return beginLine;
155     }
156
157     public int getEndLine() {
158         return endLine;
159     }
160
161     public String JavaDoc getVariableName() {
162         return "";
163     }
164
165     public String JavaDoc toString() {
166         return getFilename() + ":" + getRule() + ":" + getDescription() + ":" + beginLine;
167     }
168
169 }
170
Popular Tags