Skip to content

Commit 1c61d98

Browse files
Copilotslachiewicz
andcommitted
Convert SEVERITY constants to type-safe enum
Co-authored-by: slachiewicz <[email protected]>
1 parent 7eb8560 commit 1c61d98

File tree

3 files changed

+102
-8
lines changed

3 files changed

+102
-8
lines changed

src/main/java/org/codehaus/plexus/build/BuildContext.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@
2424
* <p>BuildContext interface.</p>
2525
*/
2626
public interface BuildContext {
27-
/** Constant <code>SEVERITY_WARNING=1</code> */
27+
/**
28+
* Constant <code>SEVERITY_WARNING=1</code>
29+
* @deprecated Use {@link Severity#WARNING} instead
30+
*/
31+
@Deprecated
2832
int SEVERITY_WARNING = 1;
2933

30-
/** Constant <code>SEVERITY_ERROR=2</code> */
34+
/**
35+
* Constant <code>SEVERITY_ERROR=2</code>
36+
* @deprecated Use {@link Severity#ERROR} instead
37+
*/
38+
@Deprecated
3139
int SEVERITY_ERROR = 2;
3240

3341
// TODO should we add File getBasedir()?
@@ -201,6 +209,20 @@ public interface BuildContext {
201209
*/
202210
void addError(File file, int line, int column, String message, Throwable cause);
203211

212+
/**
213+
* Adds a message to the build context. The message is associated with a file and a location inside that file.
214+
*
215+
* @param file The file or folder with which the message is associated. Should not be null and it is recommended to be
216+
* an absolute path.
217+
* @param line The line number inside the file. Use 1 (not 0) for the first line. Use 0 for unknown/unspecified.
218+
* @param column The column number inside the file. Use 1 (not 0) for the first column. Use 0 for unknown/unspecified.
219+
* @param severity The severity of the message.
220+
* @param cause A Throwable object associated with the message. Can be null.
221+
* @since 1.2.1
222+
* @param message a {@link java.lang.String} object.
223+
*/
224+
void addMessage(File file, int line, int column, String message, Severity severity, Throwable cause);
225+
204226
/**
205227
* Adds a message to the build context. The message is associated with a file and a location inside that file.
206228
*
@@ -210,9 +232,11 @@ public interface BuildContext {
210232
* @param column The column number inside the file. Use 1 (not 0) for the first column. Use 0 for unknown/unspecified.
211233
* @param severity The severity of the message: SEVERITY_WARNING or SEVERITY_ERROR.
212234
* @param cause A Throwable object associated with the message. Can be null.
235+
* @deprecated Use {@link #addMessage(File, int, int, String, Severity, Throwable)} instead
213236
* @since 0.0.7
214237
* @param message a {@link java.lang.String} object.
215238
*/
239+
@Deprecated
216240
void addMessage(File file, int line, int column, String message, int severity, Throwable cause);
217241

218242
/**

src/main/java/org/codehaus/plexus/build/DefaultBuildContext.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,34 +163,42 @@ public void setValue(String key, Object value) {
163163

164164
/** {@inheritDoc} */
165165
public void addError(File file, int line, int column, String message, Throwable cause) {
166-
addMessage(file, line, column, message, SEVERITY_ERROR, cause);
166+
addMessage(file, line, column, message, Severity.ERROR, cause);
167167
}
168168

169169
/** {@inheritDoc} */
170170
public void addWarning(File file, int line, int column, String message, Throwable cause) {
171-
addMessage(file, line, column, message, SEVERITY_WARNING, cause);
171+
addMessage(file, line, column, message, Severity.WARNING, cause);
172172
}
173173

174174
private String getMessage(File file, int line, int column, String message) {
175175
return file.getAbsolutePath() + " [" + line + ':' + column + "]: " + message;
176176
}
177177

178178
/** {@inheritDoc} */
179-
public void addMessage(File file, int line, int column, String message, int severity, Throwable cause) {
179+
@Override
180+
public void addMessage(File file, int line, int column, String message, Severity severity, Throwable cause) {
180181
if (isDefaultImplementation()) {
181182
switch (severity) {
182-
case BuildContext.SEVERITY_ERROR:
183+
case ERROR:
183184
logger.error(getMessage(file, line, column, message), cause);
184185
return;
185-
case BuildContext.SEVERITY_WARNING:
186+
case WARNING:
186187
logger.warn(getMessage(file, line, column, message), cause);
187188
return;
188189
default:
189190
logger.debug(getMessage(file, line, column, message), cause);
190191
return;
191192
}
192193
}
193-
legacy.addMessage(file, line, column, message, severity, cause);
194+
legacy.addMessage(file, line, column, message, severity.getValue(), cause);
195+
}
196+
197+
/** {@inheritDoc} */
198+
@Override
199+
@Deprecated
200+
public void addMessage(File file, int line, int column, String message, int severity, Throwable cause) {
201+
addMessage(file, line, column, message, Severity.fromValue(severity), cause);
194202
}
195203

196204
/** {@inheritDoc} */
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright (c) 2008 Sonatype, Inc. All rights reserved.
3+
4+
This program is licensed to you under the Apache License Version 2.0,
5+
and you may not use this file except in compliance with the Apache License Version 2.0.
6+
You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
8+
Unless required by applicable law or agreed to in writing,
9+
software distributed under the Apache License Version 2.0 is distributed on an
10+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package org.codehaus.plexus.build;
14+
15+
/**
16+
* Severity levels for build messages.
17+
*
18+
* @since 1.2.1
19+
*/
20+
public enum Severity {
21+
/**
22+
* Warning severity level.
23+
*/
24+
WARNING(1),
25+
26+
/**
27+
* Error severity level.
28+
*/
29+
ERROR(2);
30+
31+
private final int value;
32+
33+
Severity(int value) {
34+
this.value = value;
35+
}
36+
37+
/**
38+
* Returns the legacy integer value for this severity level.
39+
* This is provided for backward compatibility.
40+
*
41+
* @return the integer value
42+
*/
43+
public int getValue() {
44+
return value;
45+
}
46+
47+
/**
48+
* Converts a legacy integer severity value to a Severity enum.
49+
*
50+
* @param value the integer severity value
51+
* @return the corresponding Severity enum value
52+
* @throws IllegalArgumentException if the value doesn't correspond to a known severity
53+
*/
54+
public static Severity fromValue(int value) {
55+
for (Severity severity : values()) {
56+
if (severity.value == value) {
57+
return severity;
58+
}
59+
}
60+
throw new IllegalArgumentException("Unknown severity value: " + value);
61+
}
62+
}

0 commit comments

Comments
 (0)