-
Notifications
You must be signed in to change notification settings - Fork 3
Create dedicated Messages API #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
5
commits into
master
Choose a base branch
from
copilot/create-dedicated-messages-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+511
−0
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
07aabd9
Initial plan
Copilot b17ea7c
Implement Messages API with builder pattern
Copilot 738c950
Fix log message formatting and clarify INFO mapping
Copilot 99115dd
Address review feedback: shorter method names, package-private constr…
Copilot deb1fe6
Add @Deprecated annotations and move formatLogMessage to Message.toSt…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
src/main/java/org/codehaus/plexus/build/messages/DefaultMessages.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| /* | ||
| This program is licensed to you under the Apache License Version 2.0, | ||
| and you may not use this file except in compliance with the Apache License Version 2.0. | ||
| You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the Apache License Version 2.0 is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
| */ | ||
| package org.codehaus.plexus.build.messages; | ||
|
|
||
| import javax.inject.Inject; | ||
| import javax.inject.Named; | ||
| import javax.inject.Singleton; | ||
|
|
||
| import java.nio.file.Path; | ||
|
|
||
| import org.codehaus.plexus.build.BuildContext; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Default implementation of the Messages interface. | ||
| * <p> | ||
| * This implementation delegates to the BuildContext for compatibility with existing | ||
| * message handling infrastructure. It logs messages and calls the legacy BuildContext | ||
| * message API. | ||
| * </p> | ||
| */ | ||
| @Named("default") | ||
| @Singleton | ||
| public class DefaultMessages implements Messages { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(DefaultMessages.class); | ||
|
|
||
| private final BuildContext buildContext; | ||
|
|
||
| /** | ||
| * Creates a new DefaultMessages instance. | ||
| * | ||
| * @param buildContext the BuildContext to which messages will be delegated | ||
| */ | ||
| @Inject | ||
| public DefaultMessages(BuildContext buildContext) { | ||
| this.buildContext = buildContext; | ||
| } | ||
|
|
||
| @Override | ||
| public void clearAll() { | ||
| // This is a no-op in the default implementation | ||
| // Custom implementations may provide actual clearing functionality | ||
| } | ||
|
|
||
| @Override | ||
| public void clear(Path path) { | ||
| if (path != null) { | ||
| buildContext.removeMessages(path.toFile()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public MessageBuilder error(Path path) { | ||
| return build(MessageType.ERROR, path); | ||
| } | ||
|
|
||
| @Override | ||
| public MessageBuilder warning(Path path) { | ||
| return build(MessageType.WARNING, path); | ||
| } | ||
|
|
||
| @Override | ||
| public MessageBuilder info(Path path) { | ||
| return build(MessageType.INFO, path); | ||
| } | ||
|
|
||
| @Override | ||
| public MessageBuilder build(MessageType type, Path path) { | ||
| return new MessageBuilder(type, path, this::handleMessage); | ||
| } | ||
|
|
||
| /** | ||
| * Handles a message by logging it and delegating to the BuildContext. | ||
| * | ||
| * @param message the message to handle | ||
| */ | ||
| private void handleMessage(Message message) { | ||
| // Log the message | ||
| String logMessage = formatLogMessage(message); | ||
|
|
||
| switch (message.getType()) { | ||
| case ERROR: | ||
| logger.error(logMessage, message.getCause()); | ||
| break; | ||
| case WARNING: | ||
| logger.warn(logMessage, message.getCause()); | ||
| break; | ||
| case INFO: | ||
| logger.info(logMessage, message.getCause()); | ||
| break; | ||
| } | ||
|
|
||
| // Delegate to BuildContext for compatibility | ||
| if (message.getPath() != null) { | ||
| int severity = mapTypeToSeverity(message.getType()); | ||
| buildContext.addMessage( | ||
| message.getPath().toFile(), | ||
| message.getLine(), | ||
| message.getColumn(), | ||
| message.getMessage(), | ||
| severity, | ||
| message.getCause()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Formats a message for logging. | ||
| * | ||
| * @param message the message to format | ||
| * @return the formatted log message | ||
| */ | ||
| private String formatLogMessage(Message message) { | ||
laeubi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| if (message.getPath() != null) { | ||
| sb.append(message.getPath().toAbsolutePath()); | ||
| } | ||
|
|
||
| if (message.getLine() > 0 && message.getColumn() > 0) { | ||
| sb.append(" ["); | ||
| sb.append(message.getLine()); | ||
| sb.append(':').append(message.getColumn()); | ||
| sb.append("]"); | ||
| } else if (message.getLine() > 0) { | ||
| sb.append(" ["); | ||
| sb.append(message.getLine()); | ||
| sb.append("]"); | ||
| } else if (message.getColumn() > 0) { | ||
| sb.append(" [:"); | ||
| sb.append(message.getColumn()); | ||
| sb.append("]"); | ||
| } | ||
|
|
||
| if (message.getMessage() != null) { | ||
| if (sb.length() > 0) { | ||
| sb.append(": "); | ||
| } | ||
| sb.append(message.getMessage()); | ||
| } | ||
|
|
||
| return sb.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Maps a MessageType to a BuildContext severity level. | ||
| * | ||
| * @param type the message type | ||
| * @return the corresponding BuildContext severity | ||
| */ | ||
| private int mapTypeToSeverity(MessageType type) { | ||
| switch (type) { | ||
| case ERROR: | ||
| return BuildContext.SEVERITY_ERROR; | ||
| case WARNING: | ||
| return BuildContext.SEVERITY_WARNING; | ||
| case INFO: | ||
| default: | ||
| // BuildContext supports 0 as an info severity level (undocumented) | ||
| return 0; | ||
| } | ||
| } | ||
| } | ||
87 changes: 87 additions & 0 deletions
87
src/main/java/org/codehaus/plexus/build/messages/Message.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| This program is licensed to you under the Apache License Version 2.0, | ||
| and you may not use this file except in compliance with the Apache License Version 2.0. | ||
| You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the Apache License Version 2.0 is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. | ||
| */ | ||
| package org.codehaus.plexus.build.messages; | ||
|
|
||
| import java.nio.file.Path; | ||
|
|
||
| /** | ||
| * Represents a message with all its parameters. | ||
| * This class holds the collected parameters for a message that can be created through the MessageBuilder. | ||
| */ | ||
| public class Message { | ||
| private final MessageType type; | ||
| private final Path path; | ||
| private final int line; | ||
| private final int column; | ||
| private final String message; | ||
| private final Throwable cause; | ||
|
|
||
| /** | ||
| * Creates a new message with the specified parameters. | ||
| * | ||
| * @param type the message type | ||
| * @param path the file path associated with this message | ||
| * @param line the line number (1-based, 0 for unknown) | ||
| * @param column the column number (1-based, 0 for unknown) | ||
| * @param message the message text | ||
| * @param cause the exception cause, can be null | ||
| */ | ||
| Message(MessageType type, Path path, int line, int column, String message, Throwable cause) { | ||
| this.type = type; | ||
| this.path = path; | ||
| this.line = line; | ||
| this.column = column; | ||
| this.message = message; | ||
| this.cause = cause; | ||
| } | ||
|
|
||
| /** | ||
| * @return the message type | ||
| */ | ||
| public MessageType getType() { | ||
| return type; | ||
| } | ||
|
|
||
| /** | ||
| * @return the file path | ||
| */ | ||
| public Path getPath() { | ||
| return path; | ||
| } | ||
|
|
||
| /** | ||
| * @return the line number (1-based, 0 for unknown) | ||
| */ | ||
| public int getLine() { | ||
| return line; | ||
| } | ||
|
|
||
| /** | ||
| * @return the column number (1-based, 0 for unknown) | ||
| */ | ||
| public int getColumn() { | ||
| return column; | ||
| } | ||
|
|
||
| /** | ||
| * @return the message text | ||
| */ | ||
| public String getMessage() { | ||
| return message; | ||
| } | ||
|
|
||
| /** | ||
| * @return the exception cause, or null if none | ||
| */ | ||
| public Throwable getCause() { | ||
| return cause; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.