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       * Returns error name which is short, fixed, human-readable category of
20       * the error.
21       * @return Error name.
22       */
23      String name();
24  
25      /**
26       * Returns file name which caused this error.
27       * May return sentinel value if file information is not available.
28       * @return File name.
29       */
30      String fileName();
31  
32      /**
33       * Returns formatted line range which cause this error.
34       * May return sentinel value if line information is not available.
35       * @return Formatted line range.
36       */
37      String lines();
38  
39      /**
40       * Returns error description.
41       * @return Description.
42       */
43      String description();
44  
45      /**
46       * PmdError backed by a RuleViolation.
47       * @since 1.0
48       */
49      final class OfRuleViolation implements PmdError {
50          /**
51           * Internal RuleViolation.
52           */
53          private final RuleViolation violation;
54  
55          /**
56           * Creates a new PmdError, representing given RuleViolation.
57           * @param violation Internal RuleViolation.
58           */
59          public OfRuleViolation(final RuleViolation violation) {
60              this.violation = violation;
61          }
62  
63          @Override
64          public String name() {
65              return this.violation.getRule().getName();
66          }
67  
68          @Override
69          public String fileName() {
70              return this.violation.getFileId().getAbsolutePath();
71          }
72  
73          @Override
74          public String lines() {
75              return String.format(
76                  "%d-%d",
77                  this.violation.getBeginLine(), this.violation.getEndLine()
78              );
79          }
80  
81          @Override
82          public String description() {
83              return this.violation.getDescription();
84          }
85      }
86  
87      /**
88       * PmdError backed by a ProcessingError.
89       * @since 1.0
90       */
91      final class OfProcessingError implements PmdError {
92          /**
93           * Internal ProcessingError.
94           */
95          private final Report.ProcessingError error;
96  
97          /**
98           * Creates a new PmdError, representing given ProcessingError.
99           * @param error Internal ProcessingError.
100          */
101         public OfProcessingError(final Report.ProcessingError error) {
102             this.error = error;
103         }
104 
105         @Override
106         public String name() {
107             return "ProcessingError";
108         }
109 
110         @Override
111         public String fileName() {
112             return this.error.getFileId().getAbsolutePath();
113         }
114 
115         @Override
116         public String lines() {
117             return "unknown";
118         }
119 
120         @Override
121         public String description() {
122             return new UncheckedText(
123                 new FormattedText(
124                     "%s: %s",
125                     this.error.getMsg(),
126                     this.error.getDetail()
127                 )
128             ).asString();
129         }
130     }
131 
132     /**
133      * PmdError backed by a ConfigError.
134      * @since 1.0
135      */
136     final class OfConfigError implements PmdError {
137         /**
138          * Internal ConfigError.
139          */
140         private final Report.ConfigurationError error;
141 
142         /**
143          * Creates a new PmdError, representing given ProcessingError.
144          * @param error Internal ProcessingError.
145          */
146         public OfConfigError(final Report.ConfigurationError error) {
147             this.error = error;
148         }
149 
150         @Override
151         public String name() {
152             return "ProcessingError";
153         }
154 
155         @Override
156         public String fileName() {
157             return "unknown";
158         }
159 
160         @Override
161         public String lines() {
162             return "unknown";
163         }
164 
165         @Override
166         public String description() {
167             return this.error.issue();
168         }
169     }
170 }