throwAsUncheckedException is moved from ScriptRuntime to Context as a useful API to use in external applications.

This commit is contained in:
igor%mir2.org 2004-01-15 20:41:57 +00:00
parent f26418ea3f
commit a7050e5460
10 changed files with 54 additions and 53 deletions

View File

@ -1445,6 +1445,40 @@ public class Context
return NativeJavaObject.coerceType(desiredType, value, false);
}
/**
* Convinient method to rethrow the exception as unchecked exception.
* The exception will be wrapped as {@link WrappedException} unless
* it is an instance of {@link EvaluatorExceptions},
* {@link EcmaError} or java.lang.Error which are rethrown as-is.
* <p>
* Instances of java.lang.reflect.InvocationTargetExceptions are treated
* specially. They are unwrapped and throwAsUncheckedException is applied
* recursively to its target.
* <p>
* This method always throws an exception, its return value is provided
* only for convenience to allow a usage like:
* <pre>
* throw Context.throwAsUncheckedException(ex);
* </pre>
* to indicate that code after the method is unreachable.
*/
public static RuntimeException throwAsUncheckedException(Throwable e)
{
while ((e instanceof InvocationTargetException)) {
e = ((InvocationTargetException) e).getTargetException();
}
if (e instanceof Error) {
throw (Error)e;
}
if (e instanceof EvaluatorException) {
throw (EvaluatorException)e;
}
if (e instanceof EcmaError) {
throw (EcmaError)e;
}
throw new WrappedException(e);
}
/**
* Tell whether debug information is being generated.
* @since 1.3

View File

@ -87,7 +87,7 @@ public class Delegator implements Function {
try {
return (Delegator)this.getClass().newInstance();
} catch (Exception ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
}
}

View File

@ -471,7 +471,7 @@ public class FunctionObject extends BaseFunction
try {
result = (Scriptable) member.getDeclaringClass().newInstance();
} catch (Exception ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
}
result.setPrototype(getClassPrototype());

View File

@ -255,7 +255,7 @@ public final class JavaAdapter
newInstance(ctorArgs);
return getAdapterSelf(adapterClass, adapter);
} catch (Exception ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
}
}
@ -509,7 +509,7 @@ public final class JavaAdapter
try {
master = (IFGlue)glueClass.newInstance();
} catch (Exception ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
}
int[] argsToConvert = getArgsToConvert(argTypes);
master.ifglue_initMaster(argsToConvert);
@ -633,7 +633,7 @@ public final class JavaAdapter
try {
return f.call(cx, scope, thisObj, args);
} catch (JavaScriptException ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
}
}

View File

@ -102,7 +102,7 @@ class JavaMembers
type = field.getType();
}
} catch (Exception ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
}
// Need to wrap the object before we return it.
scope = ScriptableObject.getTopLevelScope(scope);
@ -137,7 +137,7 @@ class JavaMembers
try {
bp.setter.invoke(javaObject, args);
} catch (Exception ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
}
}
else {

View File

@ -63,7 +63,7 @@ public final class LazilyLoadedCtor {
ScriptableObject.DONTENUM);
}
catch (PropertyException e) {
throw ScriptRuntime.throwAsUncheckedException(e);
throw Context.throwAsUncheckedException(e);
}
}
@ -88,7 +88,7 @@ public final class LazilyLoadedCtor {
} catch (SecurityException ex) {
removeOnError = true;
} catch (Exception e) {
throw ScriptRuntime.throwAsUncheckedException(e);
throw Context.throwAsUncheckedException(e);
}
}
if (removeOnError) {

View File

@ -163,7 +163,7 @@ final class MemberBox implements Serializable
try {
return invoker.invoke(target, args);
} catch (Exception ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
} catch (LinkageError ex) {
invoker = null;
}
@ -179,16 +179,16 @@ final class MemberBox implements Serializable
method = accessible;
} else {
if (!tryToMakeAccessible(method)) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
}
}
// Retry after recovery
return method.invoke(target, args);
}
} catch (IllegalAccessException ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
} catch (InvocationTargetException ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
}
}
@ -200,16 +200,16 @@ final class MemberBox implements Serializable
return ctor.newInstance(args);
} catch (IllegalAccessException ex) {
if (!tryToMakeAccessible(ctor)) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
}
}
return ctor.newInstance(args);
} catch (IllegalAccessException ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
} catch (InvocationTargetException ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
} catch (InstantiationException ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
}
}

View File

@ -691,7 +691,7 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)}
try {
glue = adapter_makeIFGlue.invoke(null, args);
} catch (Exception ex) {
throw ScriptRuntime.throwAsUncheckedException(ex);
throw Context.throwAsUncheckedException(ex);
}
if (glue != null) {
// Store for later retrival

View File

@ -899,39 +899,6 @@ public class ScriptRuntime {
return errorObject;
}
/**
* Rethrow exception wrapped into {@link WrappedException} unless
* the exception is an instance of {@link EvaluatorExceptions},
* {@link EcmaError} or java.lang.Error which are rethrown as-is.
* <p>
* Instances of java.lang.reflect.InvocationTargetExceptions are treated
* specially. They are unwrapped and throwAsUncheckedException is applied
* recursively to its target.
* <p>
* This method always throws an exception, its return value is provided
* only for convenience to allow a usage like:
* <pre>
* throw ScriptRuntime.throwAsUncheckedException(ex);
* </pre>
* to indicate that code after the method is unreachable.
*/
public static RuntimeException throwAsUncheckedException(Throwable e)
{
while ((e instanceof InvocationTargetException)) {
e = ((InvocationTargetException) e).getTargetException();
}
if (e instanceof Error) {
throw (Error)e;
}
if (e instanceof EvaluatorException) {
throw (EvaluatorException)e;
}
if (e instanceof EcmaError) {
throw (EcmaError)e;
}
throw new WrappedException(e);
}
public static Object getProp(Object obj, String id, Scriptable scope) {
if (obj == null || obj == Undefined.instance) {
throw undefReadError(obj, id);

View File

@ -96,7 +96,7 @@ public class Codegen extends Interpreter {
onlySave = true;
}
} catch (IOException iox) {
throw ScriptRuntime.throwAsUncheckedException(iox);
throw Context.throwAsUncheckedException(iox);
}
if (!isPrimary) {
@ -124,7 +124,7 @@ public class Codegen extends Interpreter {
onlySave = true;
}
} catch (IOException iox) {
throw ScriptRuntime.throwAsUncheckedException(iox);
throw Context.throwAsUncheckedException(iox);
}
}
}