View Javadoc

1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd;
5   
6   import java.util.Comparator;
7   
8   public class RuleViolation {
9   
10      public static class RuleViolationComparator implements Comparator {
11          //
12          // Changed logic of Comparator so that rules in the same file
13          // get grouped together in the output report.
14          // DDP 7/11/2002
15          //
16          public int compare(Object o1, Object o2) {
17              RuleViolation r1 = (RuleViolation) o1;
18              RuleViolation r2 = (RuleViolation) o2;
19              if (!r1.getFilename().equals(r2.getFilename())) {
20                  return r1.getFilename().compareTo(r2.getFilename());
21              }
22  
23              if (r1.getLine() != r2.getLine())
24                  return r1.getLine() - r2.getLine();
25  
26              if (r1.getDescription() != null && r2.getDescription() != null && !r1.getDescription().equals(r2.getDescription())) {
27                  return r1.getDescription().compareTo(r2.getDescription());
28              }
29  
30              if (r1.getLine() == r2.getLine()) {
31                  return 1;
32              }
33              
34              // line number diff maps nicely to compare()
35              return r1.getLine() - r2.getLine();
36          }
37      }
38  
39      private int line;
40      private Rule rule;
41      private String description;
42      private String filename;
43      private int line2 = -1;
44      privateong> String packageName;
45      private String className;
46      private String methodName;
47      private String variableName;
48      private int beginColumn = -1;
49      private int endColumn = -1;
50  
51      /***
52       * gets the character in the line where the violation starts
53       * @return a greater than or zero if set and a negative value if not available
54       */
55      public final int getBeginColumn() {
56          return beginColumn;
57      }
58      /***
59       * gets the character in the line where the violation ends
60       * @return a greater than or zero if set and a negative value if not available
61       */
62      public final int getEndColumn() {
63          return endColumn;
64      }
65      /***
66       * sets both beginColumn and endColumn
67       * @param begin
68       * @param end
69       */
70      public void setColumnInfo(int begin, int end) {
71          this.beginColumn = begin;
72          this.endColumn = end;
73      }
74  
75      publicRuleViolation(Rule rule, int line, RuleContext ctx, String packageName, String className, String methodName) {/package-summary.html">ong> RuleViolation(Rule rule, int line, RuleContext ctx, String packageName, String className, String methodName) {
76          thisg>(rule, line, rule.getMessage(), ctx, packageName, className, methodName);
77      }
78  
79      publicRuleViolation(Rule rule, int line, String specificDescription, RuleContext ctx, String packageName, String className, String methodName) {/package-summary.html">ong> RuleViolation(Rule rule, int line, String specificDescription, RuleContext ctx, String packageName, String className, String methodName) {
80          this(rule, line, -1, "", specificDescription, ctx, packageName, className, methodName);
81      }
82  
83      publicRuleViolation(Rule rule, int line, int line2, String variableName, String specificDescription, RuleContext ctx, String packageName, String className, String methodName) {/package-summary.html">ong> RuleViolation(Rule rule, int line, int line2, String variableName, String specificDescription, RuleContext ctx, String packageName, String className, String methodName) {
84          this.line = line;
85          this.line2 = line2;
86          this.rule = rule;
87          this.description = specificDescription;
88          this.filename = ctx.getSourceCodeFilename();
89          this.packageName = packageName;
90          this.className = className;
91          this.methodName = methodName;
92          this.variableName = variableName;
93      }
94  
95      public RuleViolation(Rule rule, int line, String specificDescription, RuleContext ctx) {
96          this.line = line;
97          this.rule = rule;
98          this.description = specificDescription;
99          this.filename = ctx.getSourceCodeFilename();
100     }
101 
102     publicRuleViolation(AbstractRule rule, RuleContext ctx, String packageName, String className, String methodName) {/package-summary.html">ong> RuleViolation(AbstractRule rule, RuleContext ctx, String packageName, String className, String methodName) {
103         thisg>(rule, 0, ctx, packageName, className, methodName);
104     }
105     
106     public Rule getRule() {
107         return rule;
108     }
109 
110     public int getLine() {
111         return line;
112     }
113 
114     public String getDescription() {
115         return description;
116     }
117 
118     public String getFilename() {
119         return filename;
120     }
121 
122     public String getClassName() {
123         return className;
124     }
125 
126     public String getMethodName() {
127         return methodName;
128     }
129 
130     public int getLine2() {
131         return line2;
132     }
133 
134     public String getPackageName() {
135         return</strong> packageName;
136     }
137 
138     public String getVariableName() {
139         return variableName;
140     }
141 
142     public String toString() {
143         return getFilename() + ":" + getRule() + ":" + getDescription() + ":" + getLine();
144     }
145     public final void setLine(int line) {
146         this.line = line;
147     }
148 }