Skip to content

Commit 19a5828

Browse files
author
Vincent Potucek
committed
Pull #2292: fix: fix os usage
1 parent 0d2d33f commit 19a5828

File tree

5 files changed

+35
-229
lines changed

5 files changed

+35
-229
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/services/xml/XmlFactory.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.apache.maven.api.services.xml;
2020

21-
import java.io.IOException;
2221
import java.io.InputStream;
2322
import java.io.OutputStream;
2423
import java.io.Reader;
@@ -41,37 +40,37 @@
4140
public interface XmlFactory<T> extends Service {
4241

4342
@Nonnull
44-
default T read(@Nonnull Path path) throws XmlReaderException, IOException {
43+
default T read(@Nonnull Path path) throws XmlReaderException {
4544
return read(path, true);
4645
}
4746

4847
@Nonnull
49-
default T read(@Nonnull Path path, boolean strict) throws XmlReaderException, IOException {
48+
default T read(@Nonnull Path path, boolean strict) throws XmlReaderException {
5049
return read(XmlReaderRequest.builder().path(path).strict(strict).build());
5150
}
5251

5352
@Nonnull
54-
default T read(@Nonnull InputStream input) throws XmlReaderException, IOException {
53+
default T read(@Nonnull InputStream input) throws XmlReaderException {
5554
return read(input, true);
5655
}
5756

5857
@Nonnull
59-
default T read(@Nonnull InputStream input, boolean strict) throws XmlReaderException, IOException {
58+
default T read(@Nonnull InputStream input, boolean strict) throws XmlReaderException {
6059
return read(XmlReaderRequest.builder().inputStream(input).strict(strict).build());
6160
}
6261

6362
@Nonnull
64-
default T read(@Nonnull Reader reader) throws XmlReaderException, IOException {
63+
default T read(@Nonnull Reader reader) throws XmlReaderException {
6564
return read(reader, true);
6665
}
6766

6867
@Nonnull
69-
default T read(@Nonnull Reader reader, boolean strict) throws XmlReaderException, IOException {
68+
default T read(@Nonnull Reader reader, boolean strict) throws XmlReaderException {
7069
return read(XmlReaderRequest.builder().reader(reader).strict(strict).build());
7170
}
7271

7372
@Nonnull
74-
T read(@Nonnull XmlReaderRequest request) throws XmlReaderException, IOException;
73+
T read(@Nonnull XmlReaderRequest request) throws XmlReaderException;
7574

7675
default void write(@Nonnull T content, @Nonnull Path path) throws XmlWriterException {
7776
write(XmlWriterRequest.<T>builder().content(content).path(path).build());
@@ -99,7 +98,7 @@ default void write(@Nonnull T content, @Nonnull Writer writer) throws XmlWriterE
9998
* @see #toXmlString(Object)
10099
*/
101100
@Nonnull
102-
default T fromXmlString(@Nonnull String xml) throws XmlReaderException, IOException {
101+
default T fromXmlString(@Nonnull String xml) throws XmlReaderException {
103102
return read(new StringReader(xml));
104103
}
105104

impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultModelXmlFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.apache.maven.impl;
2020

21-
import java.io.IOException;
2221
import java.io.InputStream;
2322
import java.io.OutputStream;
2423
import java.io.Reader;
@@ -151,7 +150,7 @@ public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
151150
* @throws XmlReaderException if an error occurs during the parsing
152151
* @see #toXmlString(Object)
153152
*/
154-
public static Model fromXml(@Nonnull String xml) throws XmlReaderException, IOException {
153+
public static Model fromXml(@Nonnull String xml) throws XmlReaderException {
155154
return new DefaultModelXmlFactory().fromXmlString(xml);
156155
}
157156

impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.apache.maven.impl;
2020

21-
import java.io.IOException;
2221
import java.io.InputStream;
2322
import java.io.OutputStream;
2423
import java.io.Reader;
@@ -110,7 +109,7 @@ public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterEx
110109
* @throws XmlReaderException if an error occurs during the parsing
111110
* @see #toXmlString(Object)
112111
*/
113-
public static PluginDescriptor fromXml(@Nonnull String xml) throws XmlReaderException, IOException {
112+
public static PluginDescriptor fromXml(@Nonnull String xml) throws XmlReaderException {
114113
return new DefaultPluginXmlFactory().fromXmlString(xml);
115114
}
116115

impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,27 @@
3939
import org.apache.maven.api.spi.ModelParser;
4040
import org.apache.maven.api.spi.ModelParserException;
4141

42-
import static org.apache.maven.api.spi.ModelParser.STRICT;
43-
4442
/**
4543
*
4644
* Note: uses @Typed to limit the types it is available for injection to just ModelProcessor.
47-
* <p>
45+
*
4846
* This is because the ModelProcessor interface extends ModelLocator and ModelReader. If we
4947
* made this component available under all its interfaces then it could end up being injected
5048
* into itself leading to a stack overflow.
51-
* <p>
49+
*
5250
* A side effect of using @Typed is that it translates to explicit bindings in the container.
5351
* So instead of binding the component under a 'wildcard' key it is now bound with an explicit
5452
* key. Since this is a default component; this will be a plain binding of ModelProcessor to
5553
* this implementation type; that is, no hint/name.
56-
* <p>
54+
*
5755
* This leads to a second side effect in that any @Inject request for just ModelProcessor in
5856
* the same injector is immediately matched to this explicit binding, which means extensions
5957
* cannot override this binding. This is because the lookup is always short-circuited in this
6058
* specific situation (plain @Inject request, and plain explicit binding for the same type.)
61-
* <p>
59+
*
6260
* The simplest solution is to use a custom @Named here so it isn't bound under the plain key.
6361
* This is only necessary for default components using @Typed that want to support overriding.
64-
* <p>
62+
*
6563
* As a non-default component this now gets a negative priority relative to other implementations
6664
* of the same interface. Since we want to allow overriding this doesn't matter in this case.
6765
* (if it did we could add @Priority of 0 to match the priority given to default components.)
@@ -79,19 +77,17 @@ public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, @Nullable List<Mod
7977
this.modelParsers = modelParsers;
8078
}
8179

82-
/**
83-
* @implNote
84-
* The ModelProcessor#locatePom never returns null while the ModelParser#locatePom needs to return an existing path!
85-
*/
8680
@Override
8781
public Path locateExistingPom(Path projectDirectory) {
82+
// Note that the ModelProcessor#locatePom never returns null
83+
// while the ModelParser#locatePom needs to return an existing path!
8884
Path pom = modelParsers.stream()
8985
.map(m -> m.locate(projectDirectory)
9086
.map(org.apache.maven.api.services.Source::getPath)
9187
.orElse(null))
9288
.filter(Objects::nonNull)
9389
.findFirst()
94-
.orElseGet(() -> locateExistingPomWithUserDirDefault(projectDirectory));
90+
.orElseGet(() -> doLocateExistingPom(projectDirectory));
9591
if (pom != null && !pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
9692
throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
9793
}
@@ -101,50 +97,47 @@ public Path locateExistingPom(Path projectDirectory) {
10197
@Override
10298
public Model read(XmlReaderRequest request) throws IOException {
10399
Objects.requireNonNull(request, "source cannot be null");
104-
return readOnSelfOrParent(request, request.getPath());
105-
}
106-
107-
private Model readOnSelfOrParent(XmlReaderRequest request, Path pomFile) throws IOException {
100+
Path pomFile = request.getPath();
108101
if (pomFile != null) {
102+
Path projectDirectory = pomFile.getParent();
109103
List<ModelParserException> exceptions = new ArrayList<>();
110104
for (ModelParser parser : modelParsers) {
111105
try {
112-
Optional<Model> parent = readParent(request, pomFile, parser);
113-
if (parent.isPresent()) {
114-
return parent.get().withPomFile(pomFile);
106+
Optional<Model> model =
107+
parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
108+
if (model.isPresent()) {
109+
return model.get().withPomFile(pomFile);
115110
}
116111
} catch (ModelParserException e) {
117112
exceptions.add(e);
118113
}
119114
}
120115
try {
121-
return readOnSelf(request);
116+
return doRead(request);
122117
} catch (IOException e) {
123118
exceptions.forEach(e::addSuppressed);
124119
throw e;
125120
}
126121
} else {
127-
return readOnSelf(request);
122+
return doRead(request);
128123
}
129124
}
130125

131-
private static Optional<Model> readParent(XmlReaderRequest request, Path pomFile, ModelParser parser) {
132-
return parser.locateAndParse(pomFile.getParent(), Map.of(STRICT, request.isStrict()));
133-
}
134-
135-
private Path locateExistingPomWithUserDirDefault(Path project) {
136-
return locateExistingPomInDirOrFile(project != null ? project : Paths.get(System.getProperty("user.dir")));
137-
}
138-
139-
private static Path locateExistingPomInDirOrFile(Path project) {
126+
private Path doLocateExistingPom(Path project) {
127+
if (project == null) {
128+
project = Paths.get(System.getProperty("user.dir"));
129+
}
140130
if (Files.isDirectory(project)) {
141131
Path pom = project.resolve("pom.xml");
142132
return Files.isRegularFile(pom) ? pom : null;
133+
} else if (Files.isRegularFile(project)) {
134+
return project;
135+
} else {
136+
return null;
143137
}
144-
return project;
145138
}
146139

147-
private Model readOnSelf(XmlReaderRequest request) throws IOException {
140+
private Model doRead(XmlReaderRequest request) throws IOException {
148141
return modelXmlFactory.read(request);
149142
}
150143
}

0 commit comments

Comments
 (0)