1 33 34 35 package bsh; 36 37 import java.lang.reflect.InvocationTargetException ; 38 39 import java.io.PrintStream ; 40 41 52 public class TargetError extends EvalError 53 { 54 Throwable target; 55 boolean inNativeCode; 56 57 public TargetError( 58 String msg, Throwable t, SimpleNode node, CallStack callstack, 59 boolean inNativeCode ) 60 { 61 super( msg, node, callstack ); 62 target = t; 63 this.inNativeCode = inNativeCode; 64 } 65 66 public TargetError( Throwable t, SimpleNode node, CallStack callstack ) 67 { 68 this("TargetError", t, node, callstack, false); 69 } 70 71 public Throwable getTarget() 72 { 73 if(target instanceof InvocationTargetException ) 75 return((InvocationTargetException )target).getTargetException(); 76 else 77 return target; 78 } 79 80 public String toString() 81 { 82 return super.toString() 83 + "\nTarget exception: " + 84 printTargetError( target ); 85 } 86 87 public void printStackTrace() { 88 printStackTrace( false, System.err ); 89 } 90 91 public void printStackTrace( PrintStream out ) { 92 printStackTrace( false, out ); 93 } 94 95 public void printStackTrace( boolean debug, PrintStream out ) { 96 if ( debug ) { 97 super.printStackTrace( out ); 98 out.println("--- Target Stack Trace ---"); 99 } 100 target.printStackTrace( out ); 101 } 102 103 108 public String printTargetError( Throwable t ) 109 { 110 String s = target.toString(); 111 112 if ( Capabilities.canGenerateInterfaces() ) 113 s += "\n" + xPrintTargetError( t ); 114 115 return s; 116 } 117 118 127 public String xPrintTargetError( Throwable t ) 128 { 129 String getTarget = 130 "import java.lang.reflect.UndeclaredThrowableException;"+ 131 "String result=\"\";"+ 132 "while ( target instanceof UndeclaredThrowableException ) {"+ 133 " target=target.getUndeclaredThrowable(); " + 134 " result+=\"Nested: \"+target.toString();" + 135 "}"+ 136 "return result;"; 137 Interpreter i = new Interpreter(); 138 try { 139 i.set("target", t); 140 return (String )i.eval( getTarget ); 141 } catch ( EvalError e ) { 142 throw new InterpreterError("xprintarget: "+e.toString() ); 143 } 144 } 145 146 155 public boolean inNativeCode() { 156 return inNativeCode; 157 } 158 } 159 160 | Popular Tags |