From efe68c7e710aa8c54144715408b7faca36f52c27 Mon Sep 17 00:00:00 2001 From: Christopher Vogt Date: Sat, 2 Apr 2016 15:55:37 -0400 Subject: Fix exit code trapping in NailgunLauncher this showed exception when compile errors happened during Stage1. --- nailgun_launcher/NailgunLauncher.java | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'nailgun_launcher/NailgunLauncher.java') diff --git a/nailgun_launcher/NailgunLauncher.java b/nailgun_launcher/NailgunLauncher.java index 5337b0a..50a3c91 100644 --- a/nailgun_launcher/NailgunLauncher.java +++ b/nailgun_launcher/NailgunLauncher.java @@ -79,7 +79,6 @@ public class NailgunLauncher{ } if(stage1SourcesChanged || stage1classLoader == null){ - System.err.println("CBT stage1 changed. Re-compiling."); EarlyDependencies earlyDeps = new EarlyDependencies(); int exitCode = zinc(earlyDeps, stage1SourceFiles); if( exitCode == 0 ){ @@ -118,10 +117,22 @@ public class NailgunLauncher{ } } - public static void runMain(String cls, String[] args, ClassLoader cl) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{ - cl.loadClass(cls) - .getMethod("main", String[].class) - .invoke( null, (Object) args); + public static int runMain(String cls, String[] args, ClassLoader cl) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{ + try{ + System.setSecurityManager( new TrapSecurityManager() ); + cl.loadClass(cls) + .getMethod("main", String[].class) + .invoke( null, (Object) args); + return 0; + }catch( InvocationTargetException exception ){ + Throwable cause = exception.getCause(); + if(cause instanceof TrappedExitCode){ + return ((TrappedExitCode) cause).exitCode; + } + throw exception; + } finally { + System.setSecurityManager(NailgunLauncher.defaultSecurityManager); + } } static int zinc( EarlyDependencies earlyDeps, List sourceFiles ) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException{ @@ -144,17 +155,12 @@ public class NailgunLauncher{ zincArgs.add(f.toString()); } + PrintStream oldOut = System.out; try{ - System.setSecurityManager( new TrapSecurityManager() ); - PrintStream oldOut = System.out; System.setOut(System.err); - runMain( "com.typesafe.zinc.Main", zincArgs.toArray(new String[zincArgs.size()]), earlyDeps.zinc ); - System.setOut(oldOut); - return 0;//throw new RuntimeException("zinc should have thrown an exit code"); - }catch( TrappedExitCode trapped ){ - return trapped.exitCode; + return runMain( "com.typesafe.zinc.Main", zincArgs.toArray(new String[zincArgs.size()]), earlyDeps.zinc ); } finally { - System.setSecurityManager(NailgunLauncher.defaultSecurityManager); + System.setOut(oldOut); } } -- cgit v1.2.3