View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.pmd;
6   
7   import net.sourceforge.pmd.reporting.Report;
8   import net.sourceforge.pmd.reporting.RuleViolation;
9   import org.cactoos.text.FormattedText;
10  import org.cactoos.text.UncheckedText;
11  
12  /**
13   * Represents one PMD error (usually it will be violation).
14   *
15   * @since 1.0
16   */
17  public interface PmdError {
18  
19      /**
20       * Returns error name which is short, fixed, human-readable category of
21       * the error.
22       *
23       * @return Error name
24       */
25      String name();
26  
27      /**
28       * Returns file name which caused this error.
29       * May return sentinel value if file information is not available.
30       *
31       * @return File name
32       */
33      String fileName();
34  
35      /**
36       * Returns formatted line range which cause this error.
37       * May return sentinel value if line information is not available.
38       *
39       * @return Formatted line range
40       */
41      String lines();
42  
43      /**
44       * Returns error description.
45       *
46       * @return Description
47       */
48      String description();
49  
50      /**
51       * PmdError backed by a RuleViolation.
52       *
53       * @since 1.0
54       */
55      final class OfRuleViolation implements PmdError {
56  
57          /**
58           * Internal RuleViolation.
59           */
60          private final RuleViolation violation;
61  
62          /**
63           * Creates a new PmdError, representing given RuleViolation.
64           *
65           * @param violation Internal RuleViolation
66           */
67          public OfRuleViolation(final RuleViolation violation) {
68              this.violation = violation;
69          }
70  
71          @Override
72          public String name() {
73              return this.violation.getRule().getName();
74          }
75  
76          @Override
77          public String fileName() {
78              return this.violation.getFileId().getAbsolutePath();
79          }
80  
81          @Override
82          public String lines() {
83              return String.format(
84                  "%d-%d",
85                  this.violation.getBeginLine(), this.violation.getEndLine()
86              );
87          }
88  
89          @Override
90          public String description() {
91              return this.violation.getDescription();
92          }
93      }
94  
95      /**
96       * PmdError backed by a ProcessingError.
97       *
98       * @since 1.0
99       */
100     final class OfProcessingError implements PmdError {
101 
102         /**
103          * Internal ProcessingError.
104          */
105         private final Report.ProcessingError error;
106 
107         /**
108          * Creates a new PmdError, representing given ProcessingError.
109          *
110          * @param error Internal ProcessingError
111          */
112         public OfProcessingError(final Report.ProcessingError error) {
113             this.error = error;
114         }
115 
116         @Override
117         public String name() {
118             return "ProcessingError";
119         }
120 
121         @Override
122         public String fileName() {
123             return this.error.getFileId().getAbsolutePath();
124         }
125 
126         @Override
127         public String lines() {
128             return "unknown";
129         }
130 
131         @Override
132         public String description() {
133             return new UncheckedText(
134                 new FormattedText(
135                     "%s: %s",
136                     this.error.getMsg(),
137                     this.error.getDetail()
138                 )
139             ).asString();
140         }
141     }
142 
143     /**
144      * PmdError backed by a ConfigError.
145      *
146      * @since 1.0
147      */
148     final class OfConfigError implements PmdError {
149 
150         /**
151          * Internal ConfigError.
152          */
153         private final Report.ConfigurationError error;
154 
155         /**
156          * Creates a new PmdError, representing given ConfigurationError.
157          *
158          * @param error Internal ConfigurationError
159          */
160         public OfConfigError(final Report.ConfigurationError error) {
161             this.error = error;
162         }
163 
164         @Override
165         public String name() {
166             return "ConfigurationError";
167         }
168 
169         @Override
170         public String fileName() {
171             return "unknown";
172         }
173 
174         @Override
175         public String lines() {
176             return "unknown";
177         }
178 
179         @Override
180         public String description() {
181             return this.error.issue();
182         }
183     }
184 }