Java Code Examples for org.mozilla.javascript.ScriptableObject#defineClass()

The following examples show how to use org.mozilla.javascript.ScriptableObject#defineClass() . 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: DefineClassMapInheritance.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws IllegalAccessException, InstantiationException,
        InvocationTargetException {
    Context cx = Context.enter();
    try {
        ScriptableObject scope = cx.initStandardObjects();

        // define two classes that share a parent prototype
        ScriptableObject.defineClass(scope, Fruit.class, false, true);
        ScriptableObject.defineClass(scope, Vegetable.class, false, true);

        assertEquals(Boolean.TRUE,
                evaluate(cx, scope, "(new Fruit instanceof Food)"));
        assertEquals(Boolean.TRUE,
                evaluate(cx, scope, "(new Vegetable instanceof Food)"));
    } finally {
        Context.exit();
    }
}
 
Example 2
Source File: CustomSetterAcceptNullScriptableTest.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
public void testSetNullForScriptableSetter() throws Exception {

		final String scriptCode = "foo.myProp = new Foo2();\n"
			+ "foo.myProp = null;";

		final Context cx = RhinoAndroidHelper.prepareContext();

		try {
	        final ScriptableObject topScope = cx.initStandardObjects();
	        final Foo foo = new Foo();

	        // define custom setter method
	        final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
	        foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);

	        topScope.put("foo", topScope, foo);

	        ScriptableObject.defineClass(topScope, Foo2.class);

	        cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
		}
		finally {
			Context.exit();
		}
	}
 
Example 3
Source File: CustomSetterAcceptNullScriptableTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSetNullForScriptableSetter() throws Exception {

		final String scriptCode = "foo.myProp = new Foo2();\n"
			+ "foo.myProp = null;";

		final ContextFactory factory = new ContextFactory();
		final Context cx = factory.enterContext();

		try {
	        final ScriptableObject topScope = cx.initStandardObjects();
	        final Foo foo = new Foo();

	        // define custom setter method
	        final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
	        foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);

	        topScope.put("foo", topScope, foo);

	        ScriptableObject.defineClass(topScope, Foo2.class);

	        cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
		}
		finally {
			Context.exit();
		}
	}
 
Example 4
Source File: ComplexExpressionCompilerTest.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
@Before
   public void complexExpressionCompilerSetUp() throws Exception
{
	context = new ScriptContext();
	context.compile("javascript", null, 0, "1==1");
	m_scope = Context.getCurrentContext( ).initStandardObjects();
	ScriptableObject.defineClass( m_scope, ComplexExpressionCompilerTest.Row.class );
	ScriptableObject.defineClass( m_scope, ComplexExpressionCompilerTest.AggrValue.class );
	Scriptable row = Context.getCurrentContext().newObject( m_scope, "Row" );
	m_scope.put( "row", m_scope, row );
	Scriptable aggr = Context.getCurrentContext().newObject( m_scope, "AggrValue" );
	m_scope.put( "_aggr_value", m_scope, aggr );
	m_compiler = new ExpressionCompiler();
	m_registry = 
		new AggregateRegistry()
		{
			private int m_index = 1;
		
			public int register( AggregateExpression aggregationExpr )
			{
				return m_index++;
			}
		};
}
 
Example 5
Source File: Environment.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
public static void defineClass(ScriptableObject scope) {
    try {
        ScriptableObject.defineClass(scope, Environment.class);
    } catch (Exception e) {
        throw new Error(e.getMessage());
    }
}
 
Example 6
Source File: DefineClassTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws Exception {
    Context cx = Context.enter();
    try {
        scope = cx.initStandardObjects();
        ScriptableObject.defineClass(scope, AnnotatedHostObject.class);
        ScriptableObject.defineClass(scope, TraditionalHostObject.class);
    } finally {
        Context.exit();
    }
}
 
Example 7
Source File: JsRuntimeReplFactoryBuilder.java    From stetho with MIT License 5 votes vote down vote up
private void importConsole(@NonNull ScriptableObject scope) throws StethoJsException {
  // Set the `console` object
  try {
    ScriptableObject.defineClass(scope, JsConsole.class);
    JsConsole console = new JsConsole(scope);
    scope.defineProperty("console", console, ScriptableObject.DONTENUM);
  } catch (Exception e) {
    throw new StethoJsException(e, "Failed to setup javascript console");
  }
}
 
Example 8
Source File: Environment.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public static void defineClass(ScriptableObject scope) {
    try {
        ScriptableObject.defineClass(scope, Environment.class);
    } catch (Exception e) {
        throw new Error(e.getMessage());
    }
}
 
Example 9
Source File: JsSimpleDomNode.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static void register(ScriptableObject scope) {
    try {
        ScriptableObject.defineClass(scope, JsSimpleDomNode.class);
    } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: JsSimpleDomParser.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static void register(ScriptableObject scope) {
    try {
        ScriptableObject.defineClass(scope, JsSimpleDomParser.class);
    } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}
 
Example 11
Source File: JsXMLHttpRequest.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static void register(ScriptableObject scope) {
    try {
        ScriptableObject.defineClass(scope, JsXMLHttpRequest.class);
    } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: JsNamedNodeMap.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static void register(ScriptableObject scope) {
    try {
        ScriptableObject.defineClass(scope, JsNamedNodeMap.class);
    } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}