Skip to content

Commit 2afa124

Browse files
Code cleanups - use newer JDK features
- use diamond operators - use try-with-resources - catch multiple exceptions in one block - use Files.createTempFile instead of File.createTempFile - drop unused classes and fields
1 parent 5fb64ce commit 2afa124

File tree

11 files changed

+117
-221
lines changed

11 files changed

+117
-221
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ target
55
.classpath
66
dependency-reduced-pom.xml
77
build
8-
.classpath
98
.project
109
.settings
1110
.idea

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,6 @@
216216
<version>${mavenVersion}</version>
217217
<scope>test</scope>
218218
</dependency>
219-
<dependency>
220-
<groupId>org.codehaus.plexus</groupId>
221-
<artifactId>plexus-interpolation</artifactId>
222-
<version>1.26</version>
223-
<scope>test</scope>
224-
</dependency>
225219
<dependency>
226220
<groupId>org.mockito</groupId>
227221
<artifactId>mockito-core</artifactId>

src/main/java/org/codehaus/mojo/exec/AbstractPath.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void setDependencies(Collection<String> deps) {
3535
public void setDependency(String dependency) {
3636
// Is the the correct thing to do? See MOJO-348
3737
if (dependencies == null) {
38-
setDependencies(new java.util.ArrayList<String>());
38+
setDependencies(new java.util.ArrayList<>());
3939
}
4040
dependencies.add(dependency);
4141
}

src/main/java/org/codehaus/mojo/exec/EnvStreamConsumer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public class EnvStreamConsumer implements StreamConsumer {
1313
public static final String START_PARSING_INDICATOR =
1414
"================================This is the beginning of env parsing================================";
1515

16-
private Map<String, String> envs = new HashMap<String, String>();
16+
private Map<String, String> envs = new HashMap<>();
1717

18-
private List<String> unparsed = new ArrayList<String>();
18+
private List<String> unparsed = new ArrayList<>();
1919

2020
private boolean startParsing = false;
2121

src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java

Lines changed: 56 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.apache.maven.plugins.annotations.Mojo;
3030
import org.apache.maven.plugins.annotations.Parameter;
3131
import org.apache.maven.plugins.annotations.ResolutionScope;
32-
import org.apache.maven.project.ProjectBuilder;
3332
import org.eclipse.aether.RepositorySystem;
3433
import org.eclipse.aether.collection.CollectRequest;
3534
import org.eclipse.aether.graph.Dependency;
@@ -55,12 +54,6 @@ public class ExecJavaMojo extends AbstractExecMojo {
5554
@Component
5655
private RepositorySystem repositorySystem;
5756

58-
/**
59-
* @since 1.0
60-
*/
61-
@Component
62-
private ProjectBuilder projectBuilder;
63-
6457
/**
6558
* The main class to execute.<br>
6659
* With Java 9 and above you can prefix it with the modulename, e.g. <code>com.greetings/com.greetings.Main</code>
@@ -255,70 +248,63 @@ public void execute() throws MojoExecutionException, MojoFailureException {
255248
}
256249

257250
IsolatedThreadGroup threadGroup = new IsolatedThreadGroup(mainClass /* name */);
251+
// TODO:
252+
// Adjust implementation for future JDKs after removal of SecurityManager.
253+
// See https://openjdk.org/jeps/411 for basic information.
254+
// See https://bugs.openjdk.org/browse/JDK-8199704 for details about how users might be able to
255+
// block
256+
// System::exit in post-removal JDKs (still undecided at the time of writing this comment).
258257
Thread bootstrapThread = new Thread(
259258
threadGroup,
260-
new Runnable() {
261-
// TODO:
262-
// Adjust implementation for future JDKs after removal of SecurityManager.
263-
// See https://openjdk.org/jeps/411 for basic information.
264-
// See https://bugs.openjdk.org/browse/JDK-8199704 for details about how users might be able to
265-
// block
266-
// System::exit in post-removal JDKs (still undecided at the time of writing this comment).
267-
@SuppressWarnings("removal")
268-
public void run() {
269-
int sepIndex = mainClass.indexOf('/');
270-
271-
final String bootClassName;
272-
if (sepIndex >= 0) {
273-
bootClassName = mainClass.substring(sepIndex + 1);
274-
} else {
275-
bootClassName = mainClass;
276-
}
259+
() -> {
260+
int sepIndex = mainClass.indexOf('/');
261+
262+
final String bootClassName;
263+
if (sepIndex >= 0) {
264+
bootClassName = mainClass.substring(sepIndex + 1);
265+
} else {
266+
bootClassName = mainClass;
267+
}
277268

278-
SecurityManager originalSecurityManager = System.getSecurityManager();
279-
280-
try {
281-
Class<?> bootClass = Thread.currentThread()
282-
.getContextClassLoader()
283-
.loadClass(bootClassName);
284-
285-
MethodHandles.Lookup lookup = MethodHandles.lookup();
286-
287-
MethodHandle mainHandle = lookup.findStatic(
288-
bootClass, "main", MethodType.methodType(void.class, String[].class));
289-
290-
if (blockSystemExit) {
291-
System.setSecurityManager(new SystemExitManager(originalSecurityManager));
292-
}
293-
mainHandle.invoke(arguments);
294-
} catch (IllegalAccessException
295-
| NoSuchMethodException
296-
| NoSuchMethodError e) { // just pass it on
297-
Thread.currentThread()
298-
.getThreadGroup()
299-
.uncaughtException(
300-
Thread.currentThread(),
301-
new Exception(
302-
"The specified mainClass doesn't contain a main method with appropriate signature.",
303-
e));
304-
} catch (
305-
InvocationTargetException
306-
e) { // use the cause if available to improve the plugin execution output
307-
Throwable exceptionToReport = e.getCause() != null ? e.getCause() : e;
308-
Thread.currentThread()
309-
.getThreadGroup()
310-
.uncaughtException(Thread.currentThread(), exceptionToReport);
311-
} catch (SystemExitException systemExitException) {
312-
getLog().info(systemExitException.getMessage());
313-
if (systemExitException.getExitCode() != 0) {
314-
throw systemExitException;
315-
}
316-
} catch (Throwable e) { // just pass it on
317-
Thread.currentThread().getThreadGroup().uncaughtException(Thread.currentThread(), e);
318-
} finally {
319-
if (blockSystemExit) {
320-
System.setSecurityManager(originalSecurityManager);
321-
}
269+
SecurityManager originalSecurityManager = System.getSecurityManager();
270+
271+
try {
272+
Class<?> bootClass =
273+
Thread.currentThread().getContextClassLoader().loadClass(bootClassName);
274+
275+
MethodHandles.Lookup lookup = MethodHandles.lookup();
276+
277+
MethodHandle mainHandle =
278+
lookup.findStatic(bootClass, "main", MethodType.methodType(void.class, String[].class));
279+
280+
if (blockSystemExit) {
281+
System.setSecurityManager(new SystemExitManager(originalSecurityManager));
282+
}
283+
mainHandle.invoke(arguments);
284+
} catch (IllegalAccessException | NoSuchMethodException | NoSuchMethodError e) { // just pass it on
285+
Thread.currentThread()
286+
.getThreadGroup()
287+
.uncaughtException(
288+
Thread.currentThread(),
289+
new Exception(
290+
"The specified mainClass doesn't contain a main method with appropriate signature.",
291+
e));
292+
} catch (InvocationTargetException e) {
293+
// use the cause if available to improve the plugin execution output
294+
Throwable exceptionToReport = e.getCause() != null ? e.getCause() : e;
295+
Thread.currentThread()
296+
.getThreadGroup()
297+
.uncaughtException(Thread.currentThread(), exceptionToReport);
298+
} catch (SystemExitException systemExitException) {
299+
getLog().info(systemExitException.getMessage());
300+
if (systemExitException.getExitCode() != 0) {
301+
throw systemExitException;
302+
}
303+
} catch (Throwable e) { // just pass it on
304+
Thread.currentThread().getThreadGroup().uncaughtException(Thread.currentThread(), e);
305+
} finally {
306+
if (blockSystemExit) {
307+
System.setSecurityManager(originalSecurityManager);
322308
}
323309
}
324310
},
@@ -452,7 +438,7 @@ private void joinThread(Thread thread, long timeoutMsecs) {
452438

453439
private void terminateThreads(ThreadGroup threadGroup) {
454440
long startTime = System.currentTimeMillis();
455-
Set<Thread> uncooperativeThreads = new HashSet<Thread>(); // these were not responsive to interruption
441+
Set<Thread> uncooperativeThreads = new HashSet<>(); // these were not responsive to interruption
456442
for (Collection<Thread> threads = getActiveThreads(threadGroup);
457443
!threads.isEmpty();
458444
threads = getActiveThreads(threadGroup), threads.removeAll(uncooperativeThreads)) {
@@ -512,7 +498,7 @@ private void terminateThreads(ThreadGroup threadGroup) {
512498
private Collection<Thread> getActiveThreads(ThreadGroup threadGroup) {
513499
Thread[] threads = new Thread[threadGroup.activeCount()];
514500
int numThreads = threadGroup.enumerate(threads);
515-
Collection<Thread> result = new ArrayList<Thread>(numThreads);
501+
Collection<Thread> result = new ArrayList<>(numThreads);
516502
for (int i = 0; i < threads.length && threads[i] != null; i++) {
517503
result.add(threads[i]);
518504
}

0 commit comments

Comments
 (0)