View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.pmd;
32  
33  import net.sourceforge.pmd.Report.ConfigurationError;
34  import net.sourceforge.pmd.Report.ProcessingError;
35  import net.sourceforge.pmd.RuleViolation;
36  
37  /**
38   * Represents one PMD error (usually it will be violation).
39   *
40   * @since 1.0
41   */
42  public interface PmdError {
43      /**
44       * Returns error name which is short, fixed, human-readable category of
45       * the error.
46       * @return Error name.
47       */
48      String name();
49  
50      /**
51       * Returns file name which caused this error.
52       * May return sentinel value if file information is not available.
53       * @return File name.
54       */
55      String fileName();
56  
57      /**
58       * Returns formatted line range which cause this error.
59       * May return sentinel value if line information is not available.
60       * @return Formatted line range.
61       */
62      String lines();
63  
64      /**
65       * Returns error description.
66       * @return Description.
67       */
68      String description();
69  
70      /**
71       * PmdError backed by a RuleViolation.
72       * @since 1.0
73       */
74      final class OfRuleViolation implements PmdError {
75          /**
76           * Internal RuleViolation.
77           */
78          private final RuleViolation violation;
79  
80          /**
81           * Creates a new PmdError, representing given RuleViolation.
82           * @param violation Internal RuleViolation.
83           */
84          public OfRuleViolation(final RuleViolation violation) {
85              this.violation = violation;
86          }
87  
88          @Override
89          public String name() {
90              return this.violation.getRule().getName();
91          }
92  
93          @Override
94          public String fileName() {
95              return this.violation.getFilename();
96          }
97  
98          @Override
99          public String lines() {
100             return String.format(
101                 "%d-%d",
102                 this.violation.getBeginLine(), this.violation.getEndLine()
103             );
104         }
105 
106         @Override
107         public String description() {
108             return this.violation.getDescription();
109         }
110     }
111 
112     /**
113      * PmdError backed by a ProcessingError.
114      * @since 1.0
115      */
116     final class OfProcessingError implements PmdError {
117         /**
118          * Internal ProcessingError.
119          */
120         private final ProcessingError error;
121 
122         /**
123          * Creates a new PmdError, representing given ProcessingError.
124          * @param error Internal ProcessingError.
125          */
126         public OfProcessingError(final ProcessingError error) {
127             this.error = error;
128         }
129 
130         @Override
131         public String name() {
132             return "ProcessingError";
133         }
134 
135         @Override
136         public String fileName() {
137             return this.error.getFile();
138         }
139 
140         @Override
141         public String lines() {
142             return "unknown";
143         }
144 
145         @Override
146         public String description() {
147             return new StringBuilder()
148                 .append(this.error.getMsg())
149                 .append(": ")
150                 .append(this.error.getDetail())
151                 .toString();
152         }
153     }
154 
155     /**
156      * PmdError backed by a ConfigError.
157      * @since 1.0
158      */
159     final class OfConfigError implements PmdError {
160         /**
161          * Internal ConfigError.
162          */
163         private final ConfigurationError error;
164 
165         /**
166          * Creates a new PmdError, representing given ProcessingError.
167          * @param error Internal ProcessingError.
168          */
169         public OfConfigError(final ConfigurationError error) {
170             this.error = error;
171         }
172 
173         @Override
174         public String name() {
175             return "ProcessingError";
176         }
177 
178         @Override
179         public String fileName() {
180             return "unknown";
181         }
182 
183         @Override
184         public String lines() {
185             return "unknown";
186         }
187 
188         @Override
189         public String description() {
190             return this.error.issue();
191         }
192     }
193 }