Skip to content

Commit

Permalink
Merge pull request #290 from groovy/reformat
Browse files Browse the repository at this point in the history
Unwrap if statements
  • Loading branch information
keeganwitt authored Oct 2, 2024
2 parents 96af141 + 1eb822c commit f27e10d
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 171 deletions.
41 changes: 21 additions & 20 deletions src/main/java/org/codehaus/gmavenplus/mojo/CompileTestsMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,30 @@ public class CompileTestsMojo extends AbstractCompileMojo {
*/
@Override
public void execute() throws MojoExecutionException {
if (!skipTests) {
if (skipTests) {
getLog().info("Compilation of tests is skipped.");
return;
}

try {
try {
try {
getLog().debug("Project test classpath:\n" + project.getTestClasspathElements());
} catch (DependencyResolutionRequiredException e) {
getLog().debug("Unable to log project test classpath");
}
doCompile(getTestFiles(testSources, false), project.getTestClasspathElements(), testOutputDirectory);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project?", e);
} catch (InvocationTargetException e) {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
getLog().debug("Project test classpath:\n" + project.getTestClasspathElements());
} catch (DependencyResolutionRequiredException e) {
throw new MojoExecutionException("Test dependencies weren't resolved.", e);
} catch (MalformedURLException e) {
throw new MojoExecutionException("Unable to add project test dependencies to classpath.", e);
getLog().debug("Unable to log project test classpath");
}
} else {
getLog().info("Compilation of tests is skipped.");
doCompile(getTestFiles(testSources, false), project.getTestClasspathElements(), testOutputDirectory);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project?", e);
} catch (InvocationTargetException e) {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} catch (DependencyResolutionRequiredException e) {
throw new MojoExecutionException("Test dependencies weren't resolved.", e);
} catch (MalformedURLException e) {
throw new MojoExecutionException("Unable to add project test dependencies to classpath.", e);
}
}

Expand Down
97 changes: 49 additions & 48 deletions src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,64 +85,65 @@ public void execute() throws MojoExecutionException, MojoFailureException {
getLog().debug("Unable to log project test classpath");
}

if (groovyVersionSupportsAction()) {
final SecurityManager defaultSecurityManager = System.getSecurityManager();
try {
if (!allowSystemExits) {
getLog().warn("JEP 411 deprecated Security Manager in Java 17 for removal. Therefore `allowSystemExits` is also deprecated for removal.");
try {
System.setSecurityManager(new NoExitSecurityManager());
} catch (UnsupportedOperationException e) {
getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=allow` to override this.");
}
}
if (!groovyVersionSupportsAction()) {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a console. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping console startup.");
return;
}

// get classes we need with reflection
Class<?> consoleClass;
final SecurityManager defaultSecurityManager = System.getSecurityManager();
try {
if (!allowSystemExits) {
getLog().warn("JEP 411 deprecated Security Manager in Java 17 for removal. Therefore `allowSystemExits` is also deprecated for removal.");
try {
consoleClass = classWrangler.getClass("groovy.console.ui.Console");
} catch (ClassNotFoundException e) {
consoleClass = classWrangler.getClass("groovy.ui.Console");
System.setSecurityManager(new NoExitSecurityManager());
} catch (UnsupportedOperationException e) {
getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=allow` to override this.");
}
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");
}

// get classes we need with reflection
Class<?> consoleClass;
try {
consoleClass = classWrangler.getClass("groovy.console.ui.Console");
} catch (ClassNotFoundException e) {
consoleClass = classWrangler.getClass("groovy.ui.Console");
}
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");

// create console to run
Object console = setupConsole(consoleClass, bindingClass);
// create console to run
Object console = setupConsole(consoleClass, bindingClass);

// run the console
invokeMethod(findMethod(consoleClass, "run"), console);
// run the console
invokeMethod(findMethod(consoleClass, "run"), console);

// TODO: for some reason instantiating AntBuilder before calling run() causes its stdout and stderr streams to not be captured by the Console
bindAntBuilder(consoleClass, bindingClass, console);
// TODO: for some reason instantiating AntBuilder before calling run() causes its stdout and stderr streams to not be captured by the Console
bindAntBuilder(consoleClass, bindingClass, console);

// open script file
loadScript(consoleClass, console);
// open script file
loadScript(consoleClass, console);

// wait for console to be closed
waitForConsoleClose();
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof NoClassDefFoundError && "org/apache/ivy/core/report/ResolveReport".equals(e.getCause().getMessage())) {
throw new MojoExecutionException("Groovy 1.7.6 and 1.7.7 have a dependency on Ivy to run the console. Either change your Groovy version or add Ivy as a project or plugin dependency.", e);
} else {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
}
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
try {
System.setSecurityManager(defaultSecurityManager);
} catch (UnsupportedOperationException e) {
getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=allow` to override this.");
}
// wait for console to be closed
waitForConsoleClose();
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof NoClassDefFoundError && "org/apache/ivy/core/report/ResolveReport".equals(e.getCause().getMessage())) {
throw new MojoExecutionException("Groovy 1.7.6 and 1.7.7 have a dependency on Ivy to run the console. Either change your Groovy version or add Ivy as a project or plugin dependency.", e);
} else {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
}
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
try {
System.setSecurityManager(defaultSecurityManager);
} catch (UnsupportedOperationException e) {
getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=allow` to override this.");
}
}
} else {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support running a console. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping console startup.");
}
}

Expand Down
67 changes: 34 additions & 33 deletions src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,45 +136,46 @@ protected synchronized void doExecute() throws MojoExecutionException {
getLog().debug("Unable to log project test classpath");
}

if (groovyVersionSupportsAction()) {
final SecurityManager defaultSecurityManager = System.getSecurityManager();
try {
if (!allowSystemExits) {
getLog().warn("JEP 411 deprecated Security Manager in Java 17 for removal. Therefore `allowSystemExits` is also deprecated for removal.");
try {
System.setSecurityManager(new NoExitSecurityManager());
} catch (UnsupportedOperationException e) {
getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=allow` to override this.");
}
if (!groovyVersionSupportsAction()) {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support script execution. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping script execution.");
return;
}

final SecurityManager defaultSecurityManager = System.getSecurityManager();
try {
if (!allowSystemExits) {
getLog().warn("JEP 411 deprecated Security Manager in Java 17 for removal. Therefore `allowSystemExits` is also deprecated for removal.");
try {
System.setSecurityManager(new NoExitSecurityManager());
} catch (UnsupportedOperationException e) {
getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=allow` to override this.");
}
}

// get classes we need with reflection
Class<?> groovyShellClass = classWrangler.getClass("groovy.lang.GroovyShell");
// get classes we need with reflection
Class<?> groovyShellClass = classWrangler.getClass("groovy.lang.GroovyShell");

// create a GroovyShell to run scripts in
Object shell = setupShell(groovyShellClass);
// create a GroovyShell to run scripts in
Object shell = setupShell(groovyShellClass);

// run the scripts
executeScripts(groovyShellClass, shell);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
try {
System.setSecurityManager(defaultSecurityManager);
} catch (UnsupportedOperationException e) {
getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=allow` to override this.");
}
// run the scripts
executeScripts(groovyShellClass, shell);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project or the plugin?", e);
} catch (InvocationTargetException e) {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} finally {
if (!allowSystemExits) {
try {
System.setSecurityManager(defaultSecurityManager);
} catch (UnsupportedOperationException e) {
getLog().warn("Attempted to use Security Manager in a JVM where it's disabled by default. You might try `-Djava.security.manager=allow` to override this.");
}
}
} else {
getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support script execution. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping script execution.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,37 @@ public class GenerateTestStubsMojo extends AbstractGenerateStubsMojo {
*/
@Override
public void execute() throws MojoExecutionException {
if (!skipTests) {
minGroovyVersion = GROOVY_1_8_2;
try {
try {
getLog().debug("Project test classpath:\n" + project.getTestClasspathElements());
} catch (DependencyResolutionRequiredException e) {
getLog().debug("Unable to log project test classpath");
}

doStubGeneration(getTestFiles(testSources, false), project.getTestClasspathElements(), testStubsOutputDirectory);
logGeneratedStubs(testStubsOutputDirectory);
resetStubModifiedDates(getStubs(testStubsOutputDirectory));
if (skipTests) {
getLog().info("Generation of test stubs is skipped.");
return;
}

// add stubs to project source so the Maven Compiler Plugin can find them
project.addTestCompileSourceRoot(testStubsOutputDirectory.getAbsolutePath());
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project?", e);
} catch (InvocationTargetException e) {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
minGroovyVersion = GROOVY_1_8_2;
try {
try {
getLog().debug("Project test classpath:\n" + project.getTestClasspathElements());
} catch (DependencyResolutionRequiredException e) {
throw new MojoExecutionException("Test dependencies weren't resolved.", e);
} catch (MalformedURLException e) {
throw new MojoExecutionException("Unable to add project test dependencies to classpath.", e);
getLog().debug("Unable to log project test classpath");
}
} else {
getLog().info("Generation of test stubs is skipped.");

doStubGeneration(getTestFiles(testSources, false), project.getTestClasspathElements(), testStubsOutputDirectory);
logGeneratedStubs(testStubsOutputDirectory);
resetStubModifiedDates(getStubs(testStubsOutputDirectory));

// add stubs to project source so the Maven Compiler Plugin can find them
project.addTestCompileSourceRoot(testStubsOutputDirectory.getAbsolutePath());
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Unable to get a Groovy class from classpath (" + e.getMessage() + "). Do you have Groovy as a compile dependency in your project?", e);
} catch (InvocationTargetException e) {
throw new MojoExecutionException("Error occurred while calling a method on a Groovy class from classpath.", e);
} catch (InstantiationException e) {
throw new MojoExecutionException("Error occurred while instantiating a Groovy class from classpath.", e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Unable to access a method on a Groovy class from classpath.", e);
} catch (DependencyResolutionRequiredException e) {
throw new MojoExecutionException("Test dependencies weren't resolved.", e);
} catch (MalformedURLException e) {
throw new MojoExecutionException("Unable to add project test dependencies to classpath.", e);
}
}

Expand Down
Loading

0 comments on commit f27e10d

Please sign in to comment.