org.mozilla.javascript.RhinoException Java Examples
The following examples show how to use
org.mozilla.javascript.RhinoException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ManageScriptsActivity.java From MCPELauncher with Apache License 2.0 | 6 votes |
private void reportError(final Throwable t) { this.runOnUiThread(new Runnable() { public void run() { final StringWriter strWriter = new StringWriter(); PrintWriter pWriter = new PrintWriter(strWriter); if (t instanceof RhinoException) { String lineSource = ((RhinoException) t).lineSource(); if (lineSource != null) pWriter.println(lineSource); } t.printStackTrace(pWriter); new AlertDialog.Builder(ManageScriptsActivity.this).setTitle(R.string.manage_patches_import_error).setMessage(strWriter.toString()). setPositiveButton(android.R.string.ok, null). setNeutralButton(android.R.string.copy, new DialogInterface.OnClickListener() { public void onClick(DialogInterface aDialog, int button) { ClipboardManager mgr = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); mgr.setText(strWriter.toString()); } }). show(); } }); }
Example #2
Source File: Main.java From astor with GNU General Public License v2.0 | 6 votes |
public static void processFileNoThrow(Context cx, Scriptable scope, String filename) { try { processFile(cx, scope, filename); } catch (IOException ioex) { Context.reportError(ToolErrorReporter.getMessage( "msg.couldnt.read.source", filename, ioex.getMessage())); exitCode = EXITCODE_FILE_NOT_FOUND; } catch (RhinoException rex) { ToolErrorReporter.reportException( cx.getErrorReporter(), rex); exitCode = EXITCODE_RUNTIME_ERROR; } catch (VirtualMachineError ex) { // Treat StackOverflow and OutOfMemory as runtime errors ex.printStackTrace(); String msg = ToolErrorReporter.getMessage( "msg.uncaughtJSException", ex.toString()); Context.reportError(msg); exitCode = EXITCODE_RUNTIME_ERROR; } }
Example #3
Source File: Main.java From astor with GNU General Public License v2.0 | 6 votes |
static void evalInlineScript(Context cx, String scriptText) { try { Script script = cx.compileString(scriptText, "<command>", 1, null); if (script != null) { script.exec(cx, getShellScope()); } } catch (RhinoException rex) { ToolErrorReporter.reportException( cx.getErrorReporter(), rex); exitCode = EXITCODE_RUNTIME_ERROR; } catch (VirtualMachineError ex) { // Treat StackOverflow and OutOfMemory as runtime errors ex.printStackTrace(); String msg = ToolErrorReporter.getMessage( "msg.uncaughtJSException", ex.toString()); Context.reportError(msg); exitCode = EXITCODE_RUNTIME_ERROR; } }
Example #4
Source File: ContextFactoryTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testCustomContextFactory() { ContextFactory factory = new MyFactory(); Context cx = factory.enterContext(); try { Scriptable globalScope = cx.initStandardObjects(); // Test that FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME is enabled /* TODO(stevey): fix this functionality in parser Object result = cx.evaluateString(globalScope, "var obj = {};" + "function obj.foo() { return 'bar'; }" + "obj.foo();", "test source", 1, null); assertEquals("bar", result); */ } catch (RhinoException e) { fail(e.toString()); } finally { Context.exit(); } }
Example #5
Source File: CrosstabScriptHandler.java From birt with Eclipse Public License 1.0 | 6 votes |
/** * Initialize the JavaScript context using given parent scope. * * @param scPrototype * Parent scope object. If it's null, use default scope. */ public void init( Scriptable scPrototype ) throws CrosstabException { final Context cx = Context.enter( ); try { if ( scPrototype == null ) { scope = new ImporterTopLevel( cx ); } else { scope = cx.newObject( scPrototype ); scope.setPrototype( scPrototype ); } } catch ( RhinoException jsx ) { throw convertException( jsx ); } finally { Context.exit( ); } }
Example #6
Source File: CrosstabScriptHandler.java From birt with Eclipse Public License 1.0 | 6 votes |
/** * Call JavaScript functions with an argument array. * * @param f * The function to be executed * @param oaArgs * The Java object arguments passed to the function being * executed */ private Object callJavaScriptFunction( Function f, Object[] oaArgs ) throws CrosstabException { final Context cx = Context.enter( ); Object oReturnValue = null; try { oReturnValue = f.call( cx, scope, scope, oaArgs ); } catch ( RhinoException ex ) { throw convertException( ex ); } finally { Context.exit( ); } return oReturnValue; }
Example #7
Source File: CrosstabScriptHandler.java From birt with Eclipse Public License 1.0 | 6 votes |
/** * Converts general exception to more readable format. * * @param ex * @return */ protected CrosstabException convertException( Exception ex ) { if ( ex instanceof RhinoException ) { RhinoException e = (RhinoException) ex; String lineSource = e.lineSource( ); String details = e.details( ); String lineNumber = String.valueOf( e.lineNumber( ) ); if ( lineSource == null ) lineSource = "";//$NON-NLS-1$ return new CrosstabException( Messages.getString( "CrosstabScriptHandler.error.javascript", //$NON-NLS-1$ new Object[]{ details, lineNumber, lineSource } ) ); } else { return new CrosstabException( ex ); } }
Example #8
Source File: EcmaScriptDataModel.java From JVoiceXML with GNU Lesser General Public License v2.1 | 6 votes |
/** * Creates a new error message from the message and the detailed message * with max 256 chars. * * @param message * the error message * @param e * the exception that caused the error * @return concatenated detailed message */ private String getConcatenadedErrorMessage(final String message, final RhinoException e) { final StringBuilder str = new StringBuilder(); str.append(message); str.append(" ("); str.append(e.getMessage()); str.append(" at line "); str.append(e.lineNumber()); if (e.lineSource() != null) { str.append(": "); str.append(e.lineSource()); } str.append(")"); if (str.length() > 256) { str.setLength(256); str.setCharAt(255, '.'); str.setCharAt(254, '.'); str.setCharAt(253, '.'); } return str.toString(); }
Example #9
Source File: AbstractScriptHandler.java From birt with Eclipse Public License 1.0 | 6 votes |
/** * Evaluates the given expression and returns the value. * * @param sScriptContent */ public final Object evaluate( String sScriptContent ) throws ChartException { final Context cx = Context.enter( ); try { return cx.evaluateString( scope, sScriptContent, "<cmd>", 1, null ); //$NON-NLS-1$ } catch ( RhinoException jsx ) { throw convertException( jsx ); } finally { Context.exit( ); } }
Example #10
Source File: Main.java From JsDroidCmd with Mozilla Public License 2.0 | 6 votes |
public static void processFileNoThrow(Context cx, Scriptable scope, String filename) { try { processFile(cx, scope, filename); } catch (IOException ioex) { Context.reportError(ToolErrorReporter.getMessage( "msg.couldnt.read.source", filename, ioex.getMessage())); exitCode = EXITCODE_FILE_NOT_FOUND; } catch (RhinoException rex) { ToolErrorReporter.reportException( cx.getErrorReporter(), rex); exitCode = EXITCODE_RUNTIME_ERROR; } catch (VirtualMachineError ex) { // Treat StackOverflow and OutOfMemory as runtime errors ex.printStackTrace(); String msg = ToolErrorReporter.getMessage( "msg.uncaughtJSException", ex.toString()); Context.reportError(msg); exitCode = EXITCODE_RUNTIME_ERROR; } }
Example #11
Source File: Main.java From JsDroidCmd with Mozilla Public License 2.0 | 6 votes |
static void evalInlineScript(Context cx, String scriptText) { try { Script script = cx.compileString(scriptText, "<command>", 1, null); if (script != null) { script.exec(cx, getShellScope()); } } catch (RhinoException rex) { ToolErrorReporter.reportException( cx.getErrorReporter(), rex); exitCode = EXITCODE_RUNTIME_ERROR; } catch (VirtualMachineError ex) { // Treat StackOverflow and OutOfMemory as runtime errors ex.printStackTrace(); String msg = ToolErrorReporter.getMessage( "msg.uncaughtJSException", ex.toString()); Context.reportError(msg); exitCode = EXITCODE_RUNTIME_ERROR; } }
Example #12
Source File: JavascriptTestUtilities.java From cxf with Apache License 2.0 | 6 votes |
/** * Call a Javascript function, identified by name, on a set of arguments. Optionally, expect it to throw * an exception. * * @param expectingException * @param functionName * @param args * @return */ public Object rhinoCallExpectingException(final Object expectingException, final String functionName, final Object... args) { Object fObj = rhinoScope.get(functionName, rhinoScope); if (!(fObj instanceof Function)) { throw new RuntimeException("Missing test function " + functionName); } Function function = (Function)fObj; try { return function.call(rhinoContext, rhinoScope, rhinoScope, args); } catch (RhinoException angryRhino) { if (expectingException != null && angryRhino instanceof JavaScriptException) { JavaScriptException jse = (JavaScriptException)angryRhino; Assert.assertEquals(jse.getValue(), expectingException); return null; } String trace = angryRhino.getScriptStackTrace(); Assert.fail("JavaScript error: " + angryRhino.toString() + " " + trace); } catch (JavaScriptAssertionFailed assertion) { Assert.fail(assertion.getMessage()); } return null; }
Example #13
Source File: ChartReportItemImpl.java From birt with Eclipse Public License 1.0 | 6 votes |
protected void checkScriptSyntax( String string ) throws RhinoException { if ( string == null ) return; if ( !isJavaClassName( string ) ) { try { Context cx = Context.enter( ); cx.compileString( string, "chartScript", 1, null ); //$NON-NLS-1$ } finally { Context.exit( ); } } }
Example #14
Source File: JavascriptEvalUtil.java From birt with Eclipse Public License 1.0 | 6 votes |
/** * Converts Rhino exception (a runtime exception) to BirtException * @param e Rhino exception * @param scriptText Javascript code which resulted in the exception (for error reporting purpose) * @param source description of the source script. If null, get this info from Rhino exception * @param lineNo lineNo of error location * @throws */ public static BirtException wrapRhinoException( RhinoException e, String scriptText, String source, int lineNo ) { if ( source == null ) { // Note that sourceName from RhinoException sometimes get truncated (need to find out why) // Better some than nothing source = e.sourceName(); lineNo = e.lineNumber(); } if ( logger.isLoggable( Level.FINE ) ) logger.log( Level.FINE, "Unexpected RhinoException. Source=" + source + ", line=" + lineNo+ ", Script=\n" + scriptText + "\n", e ); return new CoreException( ResourceConstants.JAVASCRIPT_ERROR, new Object[]{ e.getLocalizedMessage() }, e); }
Example #15
Source File: BytecodeExpression.java From birt with Eclipse Public License 1.0 | 6 votes |
/** * Evaluates the compiled byte code */ public Object evaluate( ScriptContext context, Scriptable scope ) throws DataException { try { Object result = JavascriptEvalUtil.convertJavascriptValue( m_script.exec( Context.getCurrentContext( ), scope ) ); return result; } catch ( RhinoException e ) { throw DataException.wrap( JavascriptEvalUtil.wrapRhinoException(e, "<compiled script>", null, 0) ); } }
Example #16
Source File: ErrorPropertiesTest.java From astor with GNU General Public License v2.0 | 5 votes |
private void testScriptStackTrace(final String script, final String expectedStackTrace, final int optimizationLevel) { try { Utils.executeScript(script, optimizationLevel); } catch (final RhinoException e) { Assert.assertEquals(expectedStackTrace, e.getScriptStackTrace()); } }
Example #17
Source File: ClassShutterExceptionTest.java From astor with GNU General Public License v2.0 | 5 votes |
public void testClassShutterException() { try { helper("java.lang.System.out.println('hi');"); fail(); } catch (RhinoException e) { // OpaqueShutter should prevent access to java.lang... return; } }
Example #18
Source File: MainActivity.java From MCPELauncher with Apache License 2.0 | 5 votes |
public void scriptErrorCallback(final String scriptName, final Throwable t) { this.runOnUiThread(new Runnable() { public void run() { final StringWriter strWriter = new StringWriter(); PrintWriter pWriter = new PrintWriter(strWriter); pWriter.println("Error occurred in script: " + scriptName); if (t instanceof RhinoException) { String lineSource = ((RhinoException) t).lineSource(); if (lineSource != null) pWriter.println(lineSource); } t.printStackTrace(pWriter); new AlertDialog.Builder(MainActivity.this) .setTitle(R.string.script_execution_error) .setMessage(strWriter.toString()) .setPositiveButton(android.R.string.ok, null) .setNeutralButton(android.R.string.copy, new DialogInterface.OnClickListener() { public void onClick(DialogInterface aDialog, int button) { ClipboardManager mgr = (ClipboardManager) MainActivity.this .getSystemService(CLIPBOARD_SERVICE); mgr.setText(strWriter.toString()); } }).show(); } }); }
Example #19
Source File: Main.java From astor with GNU General Public License v2.0 | 5 votes |
static void processFiles(Context cx, String[] args) { // define "arguments" array in the top-level object: // need to allocate new array since newArray requires instances // of exactly Object[], not ObjectSubclass[] Object[] array = new Object[args.length]; System.arraycopy(args, 0, array, 0, args.length); Scriptable argsObj = cx.newArray(global, array); global.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM); for (String file: fileList) { try { processSource(cx, file); } catch (IOException ioex) { Context.reportError(ToolErrorReporter.getMessage( "msg.couldnt.read.source", file, ioex.getMessage())); exitCode = EXITCODE_FILE_NOT_FOUND; } catch (RhinoException rex) { ToolErrorReporter.reportException( cx.getErrorReporter(), rex); exitCode = EXITCODE_RUNTIME_ERROR; } catch (VirtualMachineError ex) { // Treat StackOverflow and OutOfMemory as runtime errors ex.printStackTrace(); String msg = ToolErrorReporter.getMessage( "msg.uncaughtJSException", ex.toString()); Context.reportError(msg); exitCode = EXITCODE_RUNTIME_ERROR; } } }
Example #20
Source File: ErrorPropertiesTest.java From astor with GNU General Public License v2.0 | 5 votes |
@Test public void defaultStack() { RhinoException.useMozillaStackStyle(false); testScriptStackTrace("null.method()", "\tat myScript.js:1" + LS); final String script = "function f() \n{\n null.method();\n}\nf();\n"; testScriptStackTrace(script, "\tat myScript.js:3 (f)" + LS + "\tat myScript.js:5" + LS); testIt("try { null.method() } catch (e) { e.stack }", "\tat myScript.js:1" + LS); final String expectedStack = "\tat myScript.js:2 (f)" + LS + "\tat myScript.js:4" + LS; testIt("function f() {\n null.method(); \n}\n try { f() } catch (e) { e.stack }", expectedStack); }
Example #21
Source File: ErrorPropertiesTest.java From astor with GNU General Public License v2.0 | 5 votes |
@Test public void mozillaStack() { RhinoException.useMozillaStackStyle(true); testScriptStackTrace("null.method()", "@myScript.js:1" + LS); final String script = "function f() \n{\n null.method();\n}\nf();\n"; testScriptStackTrace(script, "f()@myScript.js:3" + LS + "@myScript.js:5" + LS); testIt("try { null.method() } catch (e) { e.stack }", "@myScript.js:1" + LS); final String expectedStack = "f()@myScript.js:2" + LS + "@myScript.js:4" + LS; testIt("function f() {\n null.method(); \n}\n try { f() } catch (e) { e.stack }", expectedStack); }
Example #22
Source File: StackTraceTest.java From astor with GNU General Public License v2.0 | 5 votes |
/** * As of CVS head on May, 11. 2009, stacktrace information is lost when a call to some * native function has been made. */ public void testFailureStackTrace() { RhinoException.useMozillaStackStyle(false); final String source1 = "function f2() { throw 'hello'; }; f2();"; final String source2 = "function f2() { 'H'.toLowerCase(); throw 'hello'; }; f2();"; final String source3 = "function f2() { new java.lang.String('H').toLowerCase(); throw 'hello'; }; f2();"; final String result = "\tat test.js (f2)" + LS + "\tat test.js" + LS; runWithExpectedStackTrace(source1, result); runWithExpectedStackTrace(source2, result); runWithExpectedStackTrace(source3, result); }
Example #23
Source File: JavascriptEvalUtil.java From birt with Eclipse Public License 1.0 | 5 votes |
/** * This method will not convert the data of return value, so it might the * Java data type or that of Java Script. * * @param cx * @param scope * @param scriptText * @param source * @param lineNo * @return the evaluated value * @throws BirtException */ public static Object evaluateRawScript(Context cx, Scriptable scope, String scriptText, String source, int lineNo) throws BirtException { Object result = null; // Use provided context, or get the thread context if none provided boolean enterContext = cx == null; if ( enterContext ) cx = Context.enter(); try { Script compiledScript = getCompiledScript( cx, scope, scriptText, source, lineNo ); result = compiledScript.exec( cx, scope ); } catch ( RhinoException e) { // Note: use the real source and lineNo here. The source and lineNo reported // by e can be wrong, since we may be executing an identical compiled script // from a different source/line throw wrapRhinoException( e, scriptText, source, lineNo ); } finally { if ( enterContext) Context.exit(); } return result; }
Example #24
Source File: AbstractScriptHandler.java From birt with Eclipse Public License 1.0 | 5 votes |
/** * Initialize the JavaScript context using given parent scope. * * @param scPrototype * Parent scope object. If it's null, use default scope. */ public final void init( Scriptable scPrototype ) throws ChartException { final Context cx = Context.enter( ); try { if ( scPrototype == null ) // NO PROTOTYPE { // scope = cx.initStandardObjects(); scope = new ImporterTopLevel( cx ); } else { scope = cx.newObject( scPrototype ); scope.setPrototype( scPrototype ); // !don't reset the parent scope here. // scope.setParentScope( null ); } // final Scriptable scopePrevious = scope; // !deprecated, remove this later. use script context instead. // registerExistingScriptableObject( this, "chart" ); //$NON-NLS-1$ // scope = scopePrevious; // RESTORE // !deprecated, remove this later, use logger from script context // instead. // ADD LOGGING CAPABILITIES TO JAVASCRIPT ACCESS final Object oConsole = Context.javaToJS( getLogger( ), scope ); scope.put( "logger", scope, oConsole ); //$NON-NLS-1$ } catch ( RhinoException jsx ) { throw convertException( jsx ); } finally { Context.exit( ); } }
Example #25
Source File: StackTraceTest.java From rhino-android with Apache License 2.0 | 5 votes |
/** * As of CVS head on May, 11. 2009, stacktrace information is lost when a call to some * native function has been made. */ public void testFailureStackTrace() { RhinoException.useMozillaStackStyle(false); final String source1 = "function f2() { throw 'hello'; }; f2();"; final String source2 = "function f2() { 'H'.toLowerCase(); throw 'hello'; }; f2();"; final String source3 = "function f2() { new java.lang.String('H').toLowerCase(); throw 'hello'; }; f2();"; final String result = "\tat test.js (f2)" + LS + "\tat test.js" + LS; runWithExpectedStackTrace(source1, result); runWithExpectedStackTrace(source2, result); runWithExpectedStackTrace(source3, result); }
Example #26
Source File: AbstractScriptHandler.java From birt with Eclipse Public License 1.0 | 5 votes |
/** * Call JavaScript functions with an argument array. * * @param f * The function to be executed * @param oaArgs * The Java object arguments passed to the function being * executed */ private final Object callJavaScriptFunction( Function f, Object[] oaArgs ) throws ChartException { final Context cx = Context.enter( ); Object oReturnValue = null; // #229402 ClassLoader oldLoader = cx.getApplicationClassLoader( ); ClassLoader appLader = SecurityUtil.getClassLoader( AbstractScriptHandler.this.getClass( ) ); cx.setApplicationClassLoader( appLader ); // Initialize BIRT functions, register them into current script context. new CoreJavaScriptInitializer( ).initialize( cx, scope ); try { oReturnValue = f.call( cx, scope, scope, oaArgs ); } catch ( RhinoException ex ) { throw convertException( ex ); } finally { cx.setApplicationClassLoader( oldLoader ); Context.exit( ); } return oReturnValue; }
Example #27
Source File: Main.java From JsDroidCmd with Mozilla Public License 2.0 | 5 votes |
static void processFiles(Context cx, String[] args) { // define "arguments" array in the top-level object: // need to allocate new array since newArray requires instances // of exactly Object[], not ObjectSubclass[] Object[] array = new Object[args.length]; System.arraycopy(args, 0, array, 0, args.length); Scriptable argsObj = cx.newArray(global, array); global.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM); for (String file: fileList) { try { processSource(cx, file); } catch (IOException ioex) { Context.reportError(ToolErrorReporter.getMessage( "msg.couldnt.read.source", file, ioex.getMessage())); exitCode = EXITCODE_FILE_NOT_FOUND; } catch (RhinoException rex) { ToolErrorReporter.reportException( cx.getErrorReporter(), rex); exitCode = EXITCODE_RUNTIME_ERROR; } catch (VirtualMachineError ex) { // Treat StackOverflow and OutOfMemory as runtime errors ex.printStackTrace(); String msg = ToolErrorReporter.getMessage( "msg.uncaughtJSException", ex.toString()); Context.reportError(msg); exitCode = EXITCODE_RUNTIME_ERROR; } } }
Example #28
Source File: ErrorPropertiesTest.java From rhino-android with Apache License 2.0 | 5 votes |
@Test public void mozillaStack() { RhinoException.useMozillaStackStyle(true); testScriptStackTrace("null.method()", "@myScript.js:1" + LS); final String script = "function f() \n{\n null.method();\n}\nf();\n"; testScriptStackTrace(script, "f()@myScript.js:3" + LS + "@myScript.js:5" + LS); testIt("try { null.method() } catch (e) { e.stack }", "@myScript.js:1" + LS); final String expectedStack = "f()@myScript.js:2" + LS + "@myScript.js:4" + LS; testIt("function f() {\n null.method(); \n}\n try { f() } catch (e) { e.stack }", expectedStack); }
Example #29
Source File: ErrorPropertiesTest.java From rhino-android with Apache License 2.0 | 5 votes |
@Test public void defaultStack() { RhinoException.useMozillaStackStyle(false); testScriptStackTrace("null.method()", "\tat myScript.js:1" + LS); final String script = "function f() \n{\n null.method();\n}\nf();\n"; testScriptStackTrace(script, "\tat myScript.js:3 (f)" + LS + "\tat myScript.js:5" + LS); testIt("try { null.method() } catch (e) { e.stack }", "\tat myScript.js:1" + LS); final String expectedStack = "\tat myScript.js:2 (f)" + LS + "\tat myScript.js:4" + LS; testIt("function f() {\n null.method(); \n}\n try { f() } catch (e) { e.stack }", expectedStack); }
Example #30
Source File: ErrorPropertiesTest.java From rhino-android with Apache License 2.0 | 5 votes |
private void testScriptStackTrace(final String script, final String expectedStackTrace, final int optimizationLevel) { try { Utils.executeScript(script, optimizationLevel); } catch (final RhinoException e) { Assert.assertEquals(expectedStackTrace, e.getScriptStackTrace()); } }