Skip to content

Commit ecc85d4

Browse files
committed
Refactor AbstractCompilerMojo by moving part of the compile method body in a separated class.
The intend is to reduce the complexity by splitting one big method into smaller methods. It may also help external applications (e.g. IDE) to use `ToolExecutor` in their environment.
1 parent e42e360 commit ecc85d4

File tree

7 files changed

+1007
-596
lines changed

7 files changed

+1007
-596
lines changed

src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java

Lines changed: 165 additions & 372 deletions
Large diffs are not rendered by default.

src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public class CompilerMojo extends AbstractCompilerMojo {
9898

9999
/**
100100
* The directory for compiled classes.
101+
*
102+
* @see #getOutputDirectory()
101103
*/
102104
@Parameter(defaultValue = "${project.build.outputDirectory}", required = true, readonly = true)
103105
protected Path outputDirectory;
@@ -137,14 +139,16 @@ public class CompilerMojo extends AbstractCompilerMojo {
137139
protected String debugFileName;
138140

139141
/**
140-
* Creates a new compiler MOJO.
142+
* Creates a new compiler <abbr>MOJO</abbr> for the main code.
141143
*/
142144
public CompilerMojo() {
143145
super(PathScope.MAIN_COMPILE);
144146
}
145147

146148
/**
147149
* Runs the Java compiler on the main source code.
150+
* If {@link #skipMain} is {@code true}, then this method logs a message and does nothing else.
151+
* Otherwise, this method executes the steps described in the method of the parent class.
148152
*
149153
* @throws MojoException if the compiler cannot be run.
150154
*/
@@ -163,18 +167,18 @@ public void execute() throws MojoException {
163167
}
164168

165169
/**
166-
* Parses the parameters declared in the MOJO.
170+
* Parses the parameters declared in the <abbr>MOJO</abbr>.
167171
*
168172
* @param compiler the tools to use for verifying the validity of options
169173
* @return the options after validation
170174
*/
171175
@Override
172176
@SuppressWarnings("deprecation")
173-
protected Options acceptParameters(final OptionChecker compiler) {
174-
Options compilerConfiguration = super.acceptParameters(compiler);
175-
compilerConfiguration.addUnchecked(compilerArgs);
176-
compilerConfiguration.addUnchecked(compilerArgument);
177-
return compilerConfiguration;
177+
public Options parseParameters(final OptionChecker compiler) {
178+
Options configuration = super.parseParameters(compiler);
179+
configuration.addUnchecked(compilerArgs);
180+
configuration.addUnchecked(compilerArgument);
181+
return configuration;
178182
}
179183

180184
/**
@@ -245,7 +249,7 @@ protected String getDebugFileName() {
245249
*/
246250
@Override
247251
@Deprecated(since = "4.0.0")
248-
void addImplicitDependencies(
252+
final void addImplicitDependencies(
249253
List<SourceDirectory> sourceDirectories, Map<PathType, List<Path>> addTo, boolean hasModuleDeclaration)
250254
throws IOException {
251255
if (SUPPORT_LEGACY && multiReleaseOutput) {

src/main/java/org/apache/maven/plugin/compiler/PathFilter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,17 @@ final class PathFilter extends SimpleFileVisitor<Path> implements Predicate<Path
136136
/**
137137
* Creates a new filter.
138138
*
139-
* @param includes inclusion filters for the compiler, or empty for all source files
140-
* @param excludes exclusion filters for the compiler
141-
* @param incrementalExcludes exclusion filters for incremental build calculation
139+
* @param mojo the <abbr>MOJO</abbr> from which to take the includes/excludes configuration
142140
*/
143-
PathFilter(Collection<String> includes, Collection<String> excludes, Collection<String> incrementalExcludes) {
144-
useDefaultInclude = includes.isEmpty();
141+
PathFilter(AbstractCompilerMojo mojo) {
142+
Collection<String> specified = mojo.getIncludes();
143+
useDefaultInclude = specified.isEmpty();
145144
if (useDefaultInclude) {
146-
includes = List.of("**"); // Place-holder replaced by "**/*.java" in `test(…)`.
145+
specified = List.of("**"); // Place-holder replaced by "**/*.java" in `test(…)`.
147146
}
148-
this.includes = includes.toArray(String[]::new);
149-
this.excludes = excludes.toArray(String[]::new);
150-
this.incrementalExcludes = incrementalExcludes.toArray(String[]::new);
147+
includes = specified.toArray(String[]::new);
148+
excludes = mojo.getExcludes().toArray(String[]::new);
149+
incrementalExcludes = mojo.getIncrementalExcludes().toArray(String[]::new);
151150
}
152151

153152
/**

src/main/java/org/apache/maven/plugin/compiler/SourcesForRelease.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
*/
4242
final class SourcesForRelease implements Closeable {
4343
/**
44-
* The release for this set of sources. For this class, the
45-
* {@link SourceVersion#RELEASE_0} value means "no version".
44+
* The release for this set of sources, or {@code null} if the user did not specified a release.
4645
*
4746
* @see SourceDirectory#release
4847
*/
@@ -80,12 +79,12 @@ final class SourcesForRelease implements Closeable {
8079
/**
8180
* Creates an initially empty instance for the given Java release.
8281
*
83-
* @param release the release for this set of sources, or {@link SourceVersion#RELEASE_0} for no version.
82+
* @param release the release for this set of sources, or {@code null} if the user did not specified a release
8483
*/
8584
private SourcesForRelease(SourceVersion release) {
8685
this.release = release;
87-
roots = new LinkedHashMap<>();
8886
files = new ArrayList<>(256);
87+
roots = new LinkedHashMap<>();
8988
moduleInfos = new LinkedHashMap<>();
9089
}
9190

@@ -103,12 +102,25 @@ private void add(SourceFile source) {
103102
if (moduleName == null) {
104103
moduleName = "";
105104
}
106-
roots.computeIfAbsent(moduleName, (key) -> new LinkedHashSet<>()).add(directory.root);
105+
add(roots, moduleName, directory.root);
107106
directory.getModuleInfo().ifPresent((path) -> moduleInfos.put(directory, null));
108107
}
109108
files.add(source.file);
110109
}
111110

111+
/**
112+
* Adds the given directory in the given map.
113+
*
114+
* @param target the map where to add the specified directory
115+
* @param moduleName name of the module, or an empty string if none
116+
* @param directory the directory to add, or {@code null} if none
117+
*/
118+
private static void add(Map<String, Set<Path>> target, String moduleName, Path directory) {
119+
if (directory != null) {
120+
target.computeIfAbsent(moduleName, (key) -> new LinkedHashSet<>()).add(directory);
121+
}
122+
}
123+
112124
/**
113125
* Groups all sources files first by Java release versions, then by module names.
114126
* The elements in the returned collection are sorted in the order of {@link SourceVersion}
@@ -117,14 +129,14 @@ private void add(SourceFile source) {
117129
* @param sources the sources to group.
118130
* @return the given sources grouped by Java release versions and module names.
119131
*/
120-
public static Collection<SourcesForRelease> groupByReleaseAndModule(List<SourceFile> sources) {
132+
static Collection<SourcesForRelease> groupByReleaseAndModule(List<SourceFile> sources) {
121133
var result = new EnumMap<SourceVersion, SourcesForRelease>(SourceVersion.class);
122134
for (SourceFile source : sources) {
123-
SourceVersion release = source.directory.release;
124-
if (release == null) {
125-
release = SourceVersion.RELEASE_0; // No release sub-directory for the compiled classes.
126-
}
127-
result.computeIfAbsent(release, SourcesForRelease::new).add(source);
135+
final SourceVersion release = source.directory.release;
136+
result.computeIfAbsent(
137+
(release != null) ? release : SourceVersion.latest(),
138+
(key) -> new SourcesForRelease(release))
139+
.add(source);
128140
}
129141
return result.values();
130142
}
@@ -190,6 +202,7 @@ public void close() throws IOException {
190202
*/
191203
@Override
192204
public String toString() {
193-
return getClass().getSimpleName() + '[' + release + ": " + files.size() + " files]";
205+
return getClass().getSimpleName() + '[' + (release != null ? release : "default") + ": " + files.size()
206+
+ " files]";
194207
}
195208
}

0 commit comments

Comments
 (0)