Java源码示例:org.mozilla.javascript.WrappedException
示例1
/**
* Since a continuation can only capture JavaScript frames and not Java
* frames, ensure that Rhino throws an exception when the JavaScript frames
* don't reach all the way to the code called by
* executeScriptWithContinuations or callFunctionWithContinuations.
*/
public void testErrorOnEvalCall() {
Context cx = Context.enter();
try {
cx.setOptimizationLevel(-1); // must use interpreter mode
Script script = cx.compileString("eval('myObject.f(3);');",
"test source", 1, null);
cx.executeScriptWithContinuations(script, globalScope);
fail("Should throw IllegalStateException");
} catch (WrappedException we) {
Throwable t = we.getWrappedException();
assertTrue(t instanceof IllegalStateException);
assertTrue(t.getMessage().startsWith("Cannot capture continuation"));
} finally {
Context.exit();
}
}
示例2
/**
* Since a continuation can only capture JavaScript frames and not Java
* frames, ensure that Rhino throws an exception when the JavaScript frames
* don't reach all the way to the code called by
* executeScriptWithContinuations or callFunctionWithContinuations.
*/
public void testErrorOnEvalCall() {
Context cx = Context.enter();
try {
cx.setOptimizationLevel(-1); // must use interpreter mode
Script script = cx.compileString("eval('myObject.f(3);');",
"test source", 1, null);
cx.executeScriptWithContinuations(script, globalScope);
fail("Should throw IllegalStateException");
} catch (WrappedException we) {
Throwable t = we.getWrappedException();
assertTrue(t instanceof IllegalStateException);
assertTrue(t.getMessage().startsWith("Cannot capture continuation"));
} finally {
Context.exit();
}
}
示例3
public Object call( Context cx, Scriptable scope, Scriptable thisObj,
java.lang.Object[] args )
{
Object[] convertedArgs = JavascriptEvalUtil
.convertToJavaObjects( args );
try
{
if ( convertedArgs.length == 2 )
report.setUserProperty( (String) convertedArgs[0],
(String) convertedArgs[1] );
else if ( convertedArgs.length == 3 )
report.setUserProperty( (String) convertedArgs[0],
convertedArgs[1], (String) convertedArgs[2] );
}
catch ( SemanticException e )
{
throw new WrappedException( e );
}
return null;
}
示例4
private static List<Runner> runnersFromJs(final Class<?> cls)
throws InitializationError, IOException {
final JavascriptResource js = cls.getAnnotation(JavascriptResource.class);
final List<Runner> runners = new LinkedList<>();
try (final LineNumberReader reader = new LineNumberReader(
new InputStreamReader(ClassLoader.getSystemResourceAsStream(js.name())))) {
while (true) {
final String expr = reader.readLine();
final int line = reader.getLineNumber();
if (expr == null) {
break;
}
if (!expr.trim().isEmpty() && !expr.trim().startsWith("//")) {
runners.add(new Runner() {
@Override
public Description getDescription() {
final String[] parts = expr.split(" *; *");
final String desc = parts[parts.length - 1];
return Description.createTestDescription(cls,
String.format("%s:%s => %s", js.name(), line, desc), js);
}
@Override
public void run(final RunNotifier notifier) {
notifier.fireTestStarted(getDescription());
// should really do something more generic here
// instead of hard-coded setup, explicit
// data frame construction...
System.setIn(new ByteArrayInputStream(
String.format("tmp = frames[0]; df = frames[1]; %s;", expr).getBytes()));
try {
final DataFrame<Object> df = DataFrame.readCsv(ClassLoader.getSystemResourceAsStream("grouping.csv"));
final Object result = Shell.repl(Arrays.asList(new DataFrame<>(), df));
if (result instanceof WrappedException) {
throw WrappedException.class.cast(result).getWrappedException();
} else if (result instanceof Throwable) {
throw Throwable.class.cast(result);
}
org.junit.Assert.assertFalse(result == null);
} catch (final IOException ioe) {
notifier.fireTestAssumptionFailed(new Failure(getDescription(), ioe));
} catch (final AssertionError err) {
notifier.fireTestFailure(new Failure(getDescription(), err));
} catch (final Throwable ex) {
notifier.fireTestFailure(new Failure(getDescription(), ex));
} finally {
notifier.fireTestFinished(getDescription());
}
}
});
}
}
}
return runners;
}
示例5
private void initFunctions( )
{
Method[] tmpMethods = this.getClass( ).getDeclaredMethods( );
HashMap<String, Method> methods = new LinkedHashMap<String, Method>( );
for ( int i = 0; i < tmpMethods.length; i++ )
{
Method tmpMethod = tmpMethods[i];
String methodName = tmpMethod.getName( );
// must handle special case with long parameter or polymiorphism
if ( "getReportElementByID".equals( methodName ) //$NON-NLS-1$
|| "setUserProperty".equals( methodName ) ) //$NON-NLS-1$
continue;
if ( ( tmpMethod.getModifiers( ) & Modifier.PUBLIC ) != 0 )
methods.put( methodName, tmpMethod );
}
Context.enter( );
try
{
for ( final Entry<String, Method> entry : methods.entrySet( ) )
{
this.defineProperty( entry.getKey( ), new BaseFunction( ) {
private static final long serialVersionUID = 1L;
public Object call( Context cx, Scriptable scope,
Scriptable thisObj, Object[] args )
{
Object[] convertedArgs = JavascriptEvalUtil
.convertToJavaObjects( args );
try
{
Method method = entry.getValue( );
return method.invoke( ReportDesign.this,
convertedArgs );
}
catch ( Exception e )
{
throw new WrappedException( e );
}
}
}, DONTENUM );
}
}
finally
{
Context.exit( );
}
this.defineProperty( "getReportElementByID", //$NON-NLS-1$
new Function_getReportElementByID( ), DONTENUM );
this.defineProperty( "setUserProperty", //$NON-NLS-1$
new Function_setUserProperty( ), DONTENUM );
}
示例6
private void handleError(Throwable t) throws BSFException {
if (t instanceof WrappedException)
t = ((WrappedException) t).getWrappedException();
String message = null;
Throwable target = t;
if (t instanceof JavaScriptException) {
message = t.getLocalizedMessage();
// Is it an exception wrapped in a JavaScriptException?
Object value = ((JavaScriptException) t).getValue();
if (value instanceof Throwable) {
// likely a wrapped exception from a LiveConnect call.
// Display its stack trace as a diagnostic
target = (Throwable) value;
}
}
else if (t instanceof EvaluatorException ||
t instanceof SecurityException) {
message = t.getLocalizedMessage();
}
else if (t instanceof RuntimeException) {
message = "Internal Error: " + t.toString();
}
else if (t instanceof StackOverflowError) {
message = "Stack Overflow";
}
if (message == null)
message = t.toString();
if (t instanceof Error && !(t instanceof StackOverflowError)) {
// Re-throw Errors because we're supposed to let the JVM see it
// Don't re-throw StackOverflows, because we know we've
// corrected the situation by aborting the loop and
// a long stacktrace would end up on the user's console
throw (Error) t;
}
else {
throw new BSFException(BSFException.REASON_OTHER_ERROR,
"JavaScript Error: " + message,
target);
}
}