コード例 #1
0
 @Test
 public void missingPropertyException() throws Exception {
   try {
     assertEvaluate(new ProxyWhitelist(), "should fail", "GOOP");
   } catch (MissingPropertyException x) {
     assertEquals("GOOP", x.getProperty());
   }
 }
コード例 #2
0
 /**
  * @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;
   }
 }
コード例 #3
0
 /** 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;
       }
     }
   }
 }
コード例 #4
0
 @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());
   }
 }