@Test public void missingPropertyException() throws Exception { try { assertEvaluate(new ProxyWhitelist(), "should fail", "GOOP"); } catch (MissingPropertyException x) { assertEquals("GOOP", x.getProperty()); } }
/** * @param name the name of the variable to lookup * @return the variable value */ public Object getVariable(String name) { try { return getProxyBuilder().doGetVariable(name); } catch (MissingPropertyException mpe) { if (mpe.getProperty().equals(name) && propertyMissingDelegate != null) { return propertyMissingDelegate.call(new Object[] {name}); } throw mpe; } }
/** Overloaded to make variables appear as bean properties or via the subscript operator */ public Object getProperty(String property) { try { return getProxyBuilder().doGetProperty(property); } catch (MissingPropertyException mpe) { if ((getContext() != null) && (getContext().containsKey(property))) { return getContext().get(property); } else { try { return getMetaClass().getProperty(this, property); } catch (MissingPropertyException mpe2) { if (mpe2.getProperty().equals(property) && propertyMissingDelegate != null) { return propertyMissingDelegate.call(new Object[] {property}); } throw mpe2; } } } }
@Test public void specialScript() throws Exception { CompilerConfiguration cc = GroovySandbox.createSecureCompilerConfiguration(); cc.setScriptBaseClass(SpecialScript.class.getName()); GroovyShell shell = new GroovyShell(cc); Whitelist wl = new AbstractWhitelist() { @Override public boolean permitsMethod(Method method, Object receiver, Object[] args) { return method.getDeclaringClass() == GroovyObject.class && method.getName().equals("getProperty") && receiver instanceof SpecialScript && args[0].equals("magic"); } }; assertEquals(42, GroovySandbox.run(shell.parse("magic"), wl)); try { GroovySandbox.run(shell.parse("boring"), wl); } catch (MissingPropertyException x) { assertEquals("boring", x.getProperty()); } }