Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwrap if statements #290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 21 additions & 20 deletions src/main/java/org/codehaus/gmavenplus/mojo/CompileTestsMojo.java
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
85 changes: 43 additions & 42 deletions src/main/java/org/codehaus/gmavenplus/mojo/ConsoleMojo.java
Expand Up @@ -85,55 +85,56 @@ public void execute() throws MojoExecutionException, MojoFailureException {
getLog().debug("Unable to log project test classpath");
}

if (groovyVersionSupportsAction()) {
final SecurityManager sm = System.getSecurityManager();
try {
if (!allowSystemExits) {
System.setSecurityManager(new NoExitSecurityManager());
}
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;
try {
consoleClass = classWrangler.getClass("groovy.console.ui.Console");
} catch (ClassNotFoundException e) {
consoleClass = classWrangler.getClass("groovy.ui.Console");
}
Class<?> bindingClass = classWrangler.getClass("groovy.lang.Binding");
final SecurityManager sm = System.getSecurityManager();
try {
if (!allowSystemExits) {
System.setSecurityManager(new NoExitSecurityManager());
}

// 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) {
System.setSecurityManager(sm);
}
// 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) {
System.setSecurityManager(sm);
}
} 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
53 changes: 27 additions & 26 deletions src/main/java/org/codehaus/gmavenplus/mojo/ExecuteMojo.java
Expand Up @@ -136,36 +136,37 @@ protected synchronized void doExecute() throws MojoExecutionException {
getLog().debug("Unable to log project test classpath");
}

if (groovyVersionSupportsAction()) {
final SecurityManager sm = System.getSecurityManager();
try {
if (!allowSystemExits) {
System.setSecurityManager(new NoExitSecurityManager());
}
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 sm = System.getSecurityManager();
try {
if (!allowSystemExits) {
System.setSecurityManager(new NoExitSecurityManager());
}

// 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) {
System.setSecurityManager(sm);
}
// 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) {
System.setSecurityManager(sm);
}
} 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
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