Skip to content

[MNG-8490] Combine XmlNode and XmlNodeImpl. - #2029

Closed
RainYuY wants to merge 2 commits into
apache:masterfrom
RainYuY:MNG-8490
Closed

RainYuY wants to merge 2 commits into
apache:masterfrom
RainYuY:MNG-8490

Conversation

@RainYuY

@RainYuY RainYuY commented Jan 6, 2025 •

Copy link
Copy Markdown
Member

No description provided.

@RainYuY

RainYuY commented Jan 6, 2025

Copy link
Copy Markdown
Member Author

We need to synchronize the implementation in plexus-xml, so we will keep it as a draft for now.

@slachiewicz
slachiewicz requested a review from gnodet January 6, 2025 11:20

@gnodet gnodet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm -1 on this.
The role of an API is to decouple the contract from the implementation.
All interfaces in Maven have almost a single implementation, except a couple of interfaces.
The Single Interface Single Implementation is only an anti-pattern in a closed world, but the API is designed to be consumed by all plugins, extensions, etc...

@gnodet

gnodet commented Jan 6, 2025

Copy link
Copy Markdown
Contributor

And this implementation class changes quite often with bug fixes, so I would not put it in the API.

@RainYuY
RainYuY marked this pull request as ready for review January 6, 2025 13:46
@RainYuY

RainYuY commented Jan 6, 2025

Copy link
Copy Markdown
Member Author

I’ve almost completed most of the coding work and linked it to a plexus-xml PR. However, this PR seems to have a broader impact than expected. Could you please help evaluate it?

@RainYuY

RainYuY commented Jan 6, 2025

Copy link
Copy Markdown
Member Author

And this implementation class changes quite often with bug fixes, so I would not put it in the API.

I’ve placed most of the implementation in the Util class to avoid impacting the API layer. Could you please take another look?

@RainYuY

RainYuY commented Jan 6, 2025

Copy link
Copy Markdown
Member Author

There are still some comment-related tasks remaining, and I will complete them later.

* An immutable xml node.
*
* @since 4.0.0
* NOTE: remove all the util code in here when separated, this class should be pure data.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's do this now, maybe even in a preceding PR

@Immutable
public interface XmlNode {
public class XmlNode implements Serializable {
private static final long serialVersionUID = 2567894443061173996L;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

String DEFAULT_SELF_COMBINATION_MODE = SELF_COMBINATION_MERGE;
public static final String DEFAULT_SELF_COMBINATION_MODE = SELF_COMBINATION_MERGE;

protected final String prefix;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should all be private

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessary if using an interface + builder and non public class.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is. They'd just be in a different class.


@Nullable
String getAttribute(@Nonnull String name);
public Map<String, String> getAttributes() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs javadoc, particularly about immutability

public String toStringObject() {
StringBuilder sb = new StringBuilder();
sb.append("XmlNode[");
boolean w = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename variable

sb.append("XmlNode[");
boolean w = false;
w = addToStringField(sb, prefix, o -> !o.isEmpty(), "prefix", w);
w = addToStringField(sb, namespaceUri, o -> !o.isEmpty(), "namespaceUri", w);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this variable reassigned?

return sb.toString();
}

private static <T> boolean addToStringField(StringBuilder sb, T o, Function<T, Boolean> p, String n, boolean w) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename w variable and other variables; in general avoid single letter names

Comment thread apache-maven/pom.xml
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-xml</artifactId>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We tried hard to get rid of that dependency, don't add it back.

Comment thread api/maven-api-xml/pom.xml
<artifactId>maven-api-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.woodstox</groupId>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No external dependencies in the API.

@ThreadSafe
@Immutable
public interface XmlNode {
public class XmlNode implements Serializable {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an interface and a builder API, similar to requests / models in other parts of the API.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly what I don't what to do. That just makes the API more complex than it needs to be.

return children.size();
}

XmlNode merge(@Nullable XmlNode source, @Nullable Boolean childMergeOverride);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes one important piece from the whole API. It suddenly becomes completely unavailable. A service needs to be defined. However, services are currently only available from the Session, but it cannot be used here.
A service locator could be used, but that's really overkill. No better idea at this point though (and don't put the merge code in the API, this really needs to be decoupled).

@gnodet gnodet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few points need to be addressed:

  • no third party dependency in the API
  • no dependency on plexus-xml, this would re-create a cyclic dependency
  • use an interface + builder api, similar to other data classes in the API
  • the merge code needs to be abstracted and decoupled using a service interface so that it can be used from plugins (they are only provided with the API jars)

For the merge service, maybe using a factory using ServiceLoader if we want to make that generally available. However, if we only target plugins, a service accessible from the Session could actually be used.
But we do have plugins that use Xpp3Dom.mergeXpp3Dom so that feature cannot be removed from the API

@ThreadSafe
@Immutable
public interface XmlNode {
public class XmlNode implements Serializable {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly what I don't what to do. That just makes the API more complex than it needs to be.

@ThreadSafe
@Immutable
public interface XmlNode {
public class XmlNode implements Serializable {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove Serializable

String DEFAULT_SELF_COMBINATION_MODE = SELF_COMBINATION_MERGE;
public static final String DEFAULT_SELF_COMBINATION_MODE = SELF_COMBINATION_MERGE;

protected final String prefix;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is. They'd just be in a different class.


protected final Object location;

public XmlNode(String name) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs javadoc

// Name handling
// ----------------------------------------------------------------------

public String getPrefix() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Javadoc on all public methods, and thi9s should be more than simply "Returns prefix". It should say what a prefix is.

public Object getInputLocation() {
return location;
}
public class XmlNodeUtil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the Guava convention of using plurals for utility classes; e.g. instead of XmlNodeUtil consider XmlNodes

@RainYuY
RainYuY marked this pull request as draft January 8, 2025 11:37
@RainYuY RainYuY closed this by deleting the head repository Jan 9, 2025
@jira-importer

Copy link
Copy Markdown

Resolve #9946

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants