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