Skip to content

Commit

Permalink
Code cleanups - use newer JDK features
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
slawekjaranowski committed Nov 13, 2023
1 parent 5fb64ce commit 84c4b84
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 221 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ target
.classpath
dependency-reduced-pom.xml
build
.classpath
.project
.settings
.idea
Expand Down
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,6 @@
<version>${mavenVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-interpolation</artifactId>
<version>1.26</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/codehaus/mojo/exec/AbstractPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void setDependencies(Collection<String> deps) {
public void setDependency(String dependency) {
// Is the the correct thing to do? See MOJO-348
if (dependencies == null) {
setDependencies(new java.util.ArrayList<String>());
setDependencies(new java.util.ArrayList<>());
}
dependencies.add(dependency);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/codehaus/mojo/exec/EnvStreamConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class EnvStreamConsumer implements StreamConsumer {
public static final String START_PARSING_INDICATOR =
"================================This is the beginning of env parsing================================";

private Map<String, String> envs = new HashMap<String, String>();
private Map<String, String> envs = new HashMap<>();

private List<String> unparsed = new ArrayList<String>();
private List<String> unparsed = new ArrayList<>();

private boolean startParsing = false;

Expand Down
126 changes: 56 additions & 70 deletions src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.ProjectBuilder;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.graph.Dependency;
Expand All @@ -55,12 +54,6 @@ public class ExecJavaMojo extends AbstractExecMojo {
@Component
private RepositorySystem repositorySystem;

/**
* @since 1.0
*/
@Component
private ProjectBuilder projectBuilder;

/**
* The main class to execute.<br>
* With Java 9 and above you can prefix it with the modulename, e.g. <code>com.greetings/com.greetings.Main</code>
Expand Down Expand Up @@ -255,70 +248,63 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

IsolatedThreadGroup threadGroup = new IsolatedThreadGroup(mainClass /* name */);
// TODO:
// Adjust implementation for future JDKs after removal of SecurityManager.
// See https://openjdk.org/jeps/411 for basic information.
// See https://bugs.openjdk.org/browse/JDK-8199704 for details about how users might be able to
// block
// System::exit in post-removal JDKs (still undecided at the time of writing this comment).
Thread bootstrapThread = new Thread(
threadGroup,
new Runnable() {
// TODO:
// Adjust implementation for future JDKs after removal of SecurityManager.
// See https://openjdk.org/jeps/411 for basic information.
// See https://bugs.openjdk.org/browse/JDK-8199704 for details about how users might be able to
// block
// System::exit in post-removal JDKs (still undecided at the time of writing this comment).
@SuppressWarnings("removal")
public void run() {
int sepIndex = mainClass.indexOf('/');

final String bootClassName;
if (sepIndex >= 0) {
bootClassName = mainClass.substring(sepIndex + 1);
} else {
bootClassName = mainClass;
}
() -> {
int sepIndex = mainClass.indexOf('/');

final String bootClassName;
if (sepIndex >= 0) {
bootClassName = mainClass.substring(sepIndex + 1);
} else {
bootClassName = mainClass;
}

SecurityManager originalSecurityManager = System.getSecurityManager();

try {
Class<?> bootClass = Thread.currentThread()
.getContextClassLoader()
.loadClass(bootClassName);

MethodHandles.Lookup lookup = MethodHandles.lookup();

MethodHandle mainHandle = lookup.findStatic(
bootClass, "main", MethodType.methodType(void.class, String[].class));

if (blockSystemExit) {
System.setSecurityManager(new SystemExitManager(originalSecurityManager));
}
mainHandle.invoke(arguments);
} catch (IllegalAccessException
| NoSuchMethodException
| NoSuchMethodError e) { // just pass it on
Thread.currentThread()
.getThreadGroup()
.uncaughtException(
Thread.currentThread(),
new Exception(
"The specified mainClass doesn't contain a main method with appropriate signature.",
e));
} catch (
InvocationTargetException
e) { // use the cause if available to improve the plugin execution output
Throwable exceptionToReport = e.getCause() != null ? e.getCause() : e;
Thread.currentThread()
.getThreadGroup()
.uncaughtException(Thread.currentThread(), exceptionToReport);
} catch (SystemExitException systemExitException) {
getLog().info(systemExitException.getMessage());
if (systemExitException.getExitCode() != 0) {
throw systemExitException;
}
} catch (Throwable e) { // just pass it on
Thread.currentThread().getThreadGroup().uncaughtException(Thread.currentThread(), e);
} finally {
if (blockSystemExit) {
System.setSecurityManager(originalSecurityManager);
}
SecurityManager originalSecurityManager = System.getSecurityManager();

try {
Class<?> bootClass =
Thread.currentThread().getContextClassLoader().loadClass(bootClassName);

MethodHandles.Lookup lookup = MethodHandles.lookup();

MethodHandle mainHandle =
lookup.findStatic(bootClass, "main", MethodType.methodType(void.class, String[].class));

if (blockSystemExit) {
System.setSecurityManager(new SystemExitManager(originalSecurityManager));
}
mainHandle.invoke(arguments);
} catch (IllegalAccessException | NoSuchMethodException | NoSuchMethodError e) { // just pass it on
Thread.currentThread()
.getThreadGroup()
.uncaughtException(
Thread.currentThread(),
new Exception(
"The specified mainClass doesn't contain a main method with appropriate signature.",
e));
} catch (InvocationTargetException e) {
// use the cause if available to improve the plugin execution output
Throwable exceptionToReport = e.getCause() != null ? e.getCause() : e;
Thread.currentThread()
.getThreadGroup()
.uncaughtException(Thread.currentThread(), exceptionToReport);
} catch (SystemExitException systemExitException) {
getLog().info(systemExitException.getMessage());
if (systemExitException.getExitCode() != 0) {
throw systemExitException;
}
} catch (Throwable e) { // just pass it on
Thread.currentThread().getThreadGroup().uncaughtException(Thread.currentThread(), e);
} finally {
if (blockSystemExit) {
System.setSecurityManager(originalSecurityManager);
}
}
},
Expand Down Expand Up @@ -452,7 +438,7 @@ private void joinThread(Thread thread, long timeoutMsecs) {

private void terminateThreads(ThreadGroup threadGroup) {
long startTime = System.currentTimeMillis();
Set<Thread> uncooperativeThreads = new HashSet<Thread>(); // these were not responsive to interruption
Set<Thread> uncooperativeThreads = new HashSet<>(); // these were not responsive to interruption
for (Collection<Thread> threads = getActiveThreads(threadGroup);
!threads.isEmpty();
threads = getActiveThreads(threadGroup), threads.removeAll(uncooperativeThreads)) {
Expand Down Expand Up @@ -512,7 +498,7 @@ private void terminateThreads(ThreadGroup threadGroup) {
private Collection<Thread> getActiveThreads(ThreadGroup threadGroup) {
Thread[] threads = new Thread[threadGroup.activeCount()];
int numThreads = threadGroup.enumerate(threads);
Collection<Thread> result = new ArrayList<Thread>(numThreads);
Collection<Thread> result = new ArrayList<>(numThreads);
for (int i = 0; i < threads.length && threads[i] != null; i++) {
result.add(threads[i]);
}
Expand Down
Loading

0 comments on commit 84c4b84

Please sign in to comment.