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      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) {
75          thisg>(rule, line, rule.getMessage(), ctx, packageName, className, methodName);
76      }
77  
78      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) {
79          this(rule, line, -1, "", specificDescription, ctx, packageName, className, methodName);
80      }
81  
82      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) {
83          this.line = line;
84          this.line2 = line2;
85          this.rule = rule;
86          this.description = specificDescription;
87          this.filename = ctx.getSourceCodeFilename();
88          this.packageName = packageName;
89          this.className = className;
90          this.methodName = methodName;
91          this.variableName = variableName;
92      }
93  
94      public RuleViolation(Rule rule, int line, String specificDescription, RuleContext ctx) {
95          this.line = line;
96          this.rule = rule;
97          this.description = specificDescription;
98          this.filename = ctx.getSourceCodeFilename();
99      }
100 
101     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) {
102         thisg>(rule, 0, ctx, packageName, className, methodName);
103     }
104     
105     public Rule getRule() {
106         return rule;
107     }
108 
109     public int getLine() {
110         return line;
111     }
112 
113     public String getDescription() {
114         return description;
115     }
116 
117     public String getFilename() {
118         return filename;
119     }
120 
121     public String getClassName() {
122         return className;
123     }
124 
125     public String getMethodName() {
126         return methodName;
127     }
128 
129     public int getLine2() {
130         return line2;
131     }
132 
133     public String getPackageName() {
134         return</strong> packageName;
135     }
136 
137     public String getVariableName() {
138         return variableName;
139     }
140 
141     public String toString() {
142         return getFilename() + ":" + getRule() + ":" + getDescription() + ":" + getLine();
143     }
144     public final void setLine(int line) {
145         this.line = line;
146     }
147 }