org.codehaus.groovy.runtime.InvokerInvocationException Java Examples

The following examples show how to use org.codehaus.groovy.runtime.InvokerInvocationException. 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: SecurityTest.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void testChecksCreateClassLoaderPermissionForClassLoaderProtectedMethodAccess() throws Exception {
    cachedMethodUnderTest = createCachedMethod(ClassLoader.class, "defineClass", new Class[]{String.class, ByteBuffer.class, ProtectionDomain.class});
    forbidden = new Permissions();
    forbidden.add(new RuntimePermission("createClassLoader"));
    System.setSecurityManager(restrictiveSecurityManager);

    ClassLoader classLoader = getClass().getClassLoader();

    try {
        cachedMethodUnderTest.invoke(classLoader, new Object[]{null, null, null});
        fail();
    }
    catch (InvokerInvocationException e) {
        assertEquals(CacheAccessControlException.class, e.getCause().getClass());
    }
}
 
Example #2
Source File: AbstractTask.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void execute(Task task) {
    closure.setDelegate(task);
    closure.setResolveStrategy(Closure.DELEGATE_FIRST);
    ClassLoader original = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(closure.getClass().getClassLoader());
    try {
        if (closure.getMaximumNumberOfParameters() == 0) {
            closure.call();
        } else {
            closure.call(task);
        }
    } catch (InvokerInvocationException e) {
        Throwable cause = e.getCause();
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        throw e;
    } finally {
        Thread.currentThread().setContextClassLoader(original);
    }
}
 
Example #3
Source File: BeanDynamicObject.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Object getProperty(String name) throws MissingPropertyException {
    if (!includeProperties) {
        throw propertyMissingException(name);
    }

    MetaProperty property = getMetaClass().hasProperty(bean, name);
    if (property == null) {
        return getMetaClass().invokeMissingProperty(bean, name, null, true);
    }
    if (property instanceof MetaBeanProperty && ((MetaBeanProperty) property).getGetter() == null) {
        throw new GroovyRuntimeException(String.format(
                "Cannot get the value of write-only property '%s' on %s.", name, getDisplayName()));
    }

    try {
        return property.getProperty(bean);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #4
Source File: JUnit4GroovyMockery.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
void will(final Closure cl) {
    will(new Action() {
        public void describeTo(Description description) {
            description.appendText("execute closure");
        }

        public Object invoke(Invocation invocation) throws Throwable {
            List<Object> params = Arrays.asList(invocation.getParametersAsArray());
            Object result;
            try {
                List<Object> subParams = params.subList(0, Math.min(invocation.getParametersAsArray().length,
                        cl.getMaximumNumberOfParameters()));
                result = cl.call(subParams.toArray(new Object[subParams.size()]));
            } catch (InvokerInvocationException e) {
                throw e.getCause();
            }
            if (invocation.getInvokedMethod().getReturnType().isInstance(result)) {
                return result;
            }
            return null;
        }
    });
}
 
Example #5
Source File: AbstractTask.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void execute(Task task) {
    closure.setDelegate(task);
    closure.setResolveStrategy(Closure.DELEGATE_FIRST);
    ClassLoader original = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(closure.getClass().getClassLoader());
    try {
        if (closure.getMaximumNumberOfParameters() == 0) {
            closure.call();
        } else {
            closure.call(task);
        }
    } catch (InvokerInvocationException e) {
        Throwable cause = e.getCause();
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        throw e;
    } finally {
        Thread.currentThread().setContextClassLoader(original);
    }
}
 
Example #6
Source File: BeanDynamicObject.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Object getProperty(String name) throws MissingPropertyException {
    if (!includeProperties) {
        throw propertyMissingException(name);
    }

    MetaProperty property = getMetaClass().hasProperty(bean, name);
    if (property == null) {
        return getMetaClass().invokeMissingProperty(bean, name, null, true);
    }
    if (property instanceof MetaBeanProperty && ((MetaBeanProperty) property).getGetter() == null) {
        throw new GroovyRuntimeException(String.format(
                "Cannot get the value of write-only property '%s' on %s.", name, getDisplayName()));
    }

    try {
        return property.getProperty(bean);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #7
Source File: JUnit4GroovyMockery.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
void will(final Closure cl) {
    will(new Action() {
        public void describeTo(Description description) {
            description.appendText("execute closure");
        }

        public Object invoke(Invocation invocation) throws Throwable {
            List<Object> params = Arrays.asList(invocation.getParametersAsArray());
            Object result;
            try {
                List<Object> subParams = params.subList(0, Math.min(invocation.getParametersAsArray().length,
                        cl.getMaximumNumberOfParameters()));
                result = cl.call(subParams.toArray(new Object[subParams.size()]));
            } catch (InvokerInvocationException e) {
                throw e.getCause();
            }
            if (invocation.getInvokedMethod().getReturnType().isInstance(result)) {
                return result;
            }
            return null;
        }
    });
}
 
Example #8
Source File: JUnit4GroovyMockery.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
void will(final Closure cl) {
    will(new Action() {
        public void describeTo(Description description) {
            description.appendText("execute closure");
        }

        public Object invoke(Invocation invocation) throws Throwable {
            List<Object> params = Arrays.asList(invocation.getParametersAsArray());
            Object result;
            try {
                List<Object> subParams = params.subList(0, Math.min(invocation.getParametersAsArray().length,
                        cl.getMaximumNumberOfParameters()));
                result = cl.call(subParams.toArray(new Object[subParams.size()]));
            } catch (InvokerInvocationException e) {
                throw e.getCause();
            }
            if (invocation.getInvokedMethod().getReturnType().isInstance(result)) {
                return result;
            }
            return null;
        }
    });
}
 
Example #9
Source File: SecurityTest.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void testChecksReflectPermissionForInvokeOnPackagePrivateMethodsInRestrictedJavaPackages() throws Exception {
    // FIX_JDK9 remove this exemption for JDK9
    if (isAtLeastJdk("9.0")) {
        return;
    }
    cachedMethodUnderTest = createCachedMethod(ClassLoader.class, "getBootstrapClassPath", new Class[0]);
    System.setSecurityManager(restrictiveSecurityManager);

    try {
        cachedMethodUnderTest.invoke(null, new Object[]{});
        fail();
    }
    catch (InvokerInvocationException e) {
        assertEquals(CacheAccessControlException.class, e.getCause().getClass());
    }
}
 
Example #10
Source File: AbstractTask.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void execute(Task task) {
    closure.setDelegate(task);
    closure.setResolveStrategy(Closure.DELEGATE_FIRST);
    ClassLoader original = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(closure.getClass().getClassLoader());
    try {
        if (closure.getMaximumNumberOfParameters() == 0) {
            closure.call();
        } else {
            closure.call(task);
        }
    } catch (InvokerInvocationException e) {
        Throwable cause = e.getCause();
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        throw e;
    } finally {
        Thread.currentThread().setContextClassLoader(original);
    }
}
 
Example #11
Source File: BeanDynamicObject.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Object getProperty(String name) throws MissingPropertyException {
    if (!includeProperties) {
        throw propertyMissingException(name);
    }

    MetaProperty property = getMetaClass().hasProperty(bean, name);
    if (property == null) {
        return getMetaClass().invokeMissingProperty(bean, name, null, true);
    }
    if (property instanceof MetaBeanProperty && ((MetaBeanProperty) property).getGetter() == null) {
        throw new GroovyRuntimeException(String.format(
                "Cannot get the value of write-only property '%s' on %s.", name, getDisplayName()));
    }

    try {
        return property.getProperty(bean);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #12
Source File: BeanDynamicObject.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Object getProperty(String name) throws MissingPropertyException {
    if (!includeProperties) {
        throw propertyMissingException(name);
    }

    MetaProperty property = getMetaClass().hasProperty(bean, name);
    if (property == null) {
        return getMetaClass().invokeMissingProperty(bean, name, null, true);
    }
    if (property instanceof MetaBeanProperty && ((MetaBeanProperty) property).getGetter() == null) {
        throw new GroovyRuntimeException(String.format(
                "Cannot get the value of write-only property '%s' on %s.", name, getDisplayName()));
    }

    try {
        return property.getProperty(bean);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #13
Source File: AbstractTask.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void execute(Task task) {
    closure.setDelegate(task);
    closure.setResolveStrategy(Closure.DELEGATE_FIRST);
    ClassLoader original = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(closure.getClass().getClassLoader());
    try {
        if (closure.getMaximumNumberOfParameters() == 0) {
            closure.call();
        } else {
            closure.call(task);
        }
    } catch (InvokerInvocationException e) {
        Throwable cause = e.getCause();
        if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        }
        throw e;
    } finally {
        Thread.currentThread().setContextClassLoader(original);
    }
}
 
Example #14
Source File: JUnit4GroovyMockery.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
void will(final Closure cl) {
    will(new Action() {
        public void describeTo(Description description) {
            description.appendText("execute closure");
        }

        public Object invoke(Invocation invocation) throws Throwable {
            List<Object> params = Arrays.asList(invocation.getParametersAsArray());
            Object result;
            try {
                List<Object> subParams = params.subList(0, Math.min(invocation.getParametersAsArray().length,
                        cl.getMaximumNumberOfParameters()));
                result = cl.call(subParams.toArray(new Object[subParams.size()]));
            } catch (InvokerInvocationException e) {
                throw e.getCause();
            }
            if (invocation.getInvokedMethod().getReturnType().isInstance(result)) {
                return result;
            }
            return null;
        }
    });
}
 
Example #15
Source File: TupleListTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void assertIterate(String methodName, Expression listExpression) throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
    classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));

    Statement loopStatement = createPrintlnStatement(new VariableExpression("i"));

    BlockStatement block = new BlockStatement();
    block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("list"), Token.newSymbol("=", 0, 0), listExpression)));
    block.addStatement(new ForStatement(new Parameter(ClassHelper.DYNAMIC_TYPE, "i"), new VariableExpression("list"), loopStatement));
    classNode.addMethod(new MethodNode(methodName, ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, block));

    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);

    Object bean = fooClass.getDeclaredConstructor().newInstance();
    assertTrue("Managed to create bean", bean != null);

    System.out.println("################ Now about to invoke method");

    try {
        InvokerHelper.invokeMethod(bean, methodName, null);
    }
    catch (InvokerInvocationException e) {
        System.out.println("Caught: " + e.getCause());
        e.getCause().printStackTrace();
        fail("Should not have thrown an exception");
    }
    System.out.println("################ Done");
}
 
Example #16
Source File: BeanDynamicObject.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Object invokeMethod(String name, Object... arguments) throws MissingMethodException {
    try {
        return groovyObject.invokeMethod(name, arguments);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #17
Source File: BeanDynamicObject.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Object invokeMethod(final String name, final Object... arguments) throws MissingMethodException {
    try {
        return getMetaClass().invokeMethod(bean, name, arguments);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #18
Source File: BeanDynamicObject.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setProperty(final String name, Object value) throws MissingPropertyException {
    if (!includeProperties) {
        throw propertyMissingException(name);
    }

    MetaClass metaClass = getMetaClass();
    MetaProperty property = metaClass.hasProperty(bean, name);
    if (property == null) {
        getMetaClass().invokeMissingProperty(bean, name, null, false);
    }

    if (property instanceof MetaBeanProperty && ((MetaBeanProperty) property).getSetter() == null) {
        throw new ReadOnlyPropertyException(name, bean.getClass()) {
            @Override
            public String getMessage() {
                return String.format("Cannot set the value of read-only property '%s' on %s.", name,
                        getDisplayName());
            }
        };
    }
    try {

        // Attempt type coercion before trying to set the property
        value = argsTransformer.transform(bean, MetaProperty.getSetterName(name), value)[0];

        metaClass.setProperty(bean, name, value);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #19
Source File: BeanDynamicObject.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setProperty(final String name, Object value) throws MissingPropertyException {
    if (!includeProperties) {
        throw propertyMissingException(name);
    }

    MetaClass metaClass = getMetaClass();
    MetaProperty property = metaClass.hasProperty(bean, name);
    if (property == null) {
        getMetaClass().invokeMissingProperty(bean, name, null, false);
    }

    if (property instanceof MetaBeanProperty && ((MetaBeanProperty) property).getSetter() == null) {
        throw new ReadOnlyPropertyException(name, bean.getClass()) {
            @Override
            public String getMessage() {
                return String.format("Cannot set the value of read-only property '%s' on %s.", name,
                        getDisplayName());
            }
        };
    }
    try {

        // Attempt type coercion before trying to set the property
        value = argsTransformer.transform(bean, MetaProperty.getSetterName(name), value)[0];

        metaClass.setProperty(bean, name, value);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #20
Source File: BeanDynamicObject.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Object invokeMethod(final String name, final Object... arguments) throws MissingMethodException {
    try {
        return getMetaClass().invokeMethod(bean, name, arguments);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #21
Source File: BeanDynamicObject.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Object invokeMethod(String name, Object... arguments) throws MissingMethodException {
    try {
        return groovyObject.invokeMethod(name, arguments);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #22
Source File: BeanDynamicObject.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Object invokeMethod(String name, Object... arguments) throws MissingMethodException {
    try {
        return groovyObject.invokeMethod(name, arguments);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #23
Source File: BeanDynamicObject.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Object invokeMethod(final String name, final Object... arguments) throws MissingMethodException {
    try {
        return getMetaClass().invokeMethod(bean, name, arguments);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #24
Source File: BeanDynamicObject.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setProperty(final String name, Object value) throws MissingPropertyException {
    if (!includeProperties) {
        throw propertyMissingException(name);
    }

    MetaClass metaClass = getMetaClass();
    MetaProperty property = metaClass.hasProperty(bean, name);
    if (property == null) {
        getMetaClass().invokeMissingProperty(bean, name, null, false);
    }

    if (property instanceof MetaBeanProperty && ((MetaBeanProperty) property).getSetter() == null) {
        throw new ReadOnlyPropertyException(name, bean.getClass()) {
            @Override
            public String getMessage() {
                return String.format("Cannot set the value of read-only property '%s' on %s.", name,
                        getDisplayName());
            }
        };
    }
    try {

        // Attempt type coercion before trying to set the property
        value = argsTransformer.transform(bean, MetaProperty.getSetterName(name), value)[0];

        metaClass.setProperty(bean, name, value);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #25
Source File: BeanDynamicObject.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Object invokeMethod(String name, Object... arguments) throws MissingMethodException {
    try {
        return groovyObject.invokeMethod(name, arguments);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #26
Source File: ForTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void testManyParam() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));

    Parameter[] parameters = {new Parameter(ClassHelper.OBJECT_TYPE, "coll1"), new Parameter(ClassHelper.OBJECT_TYPE, "coll2"), new Parameter(ClassHelper.OBJECT_TYPE, "coll3")};

    BlockStatement statement = new BlockStatement();
    statement.addStatement(createPrintlnStatement(new VariableExpression("coll1")));
    statement.addStatement(createPrintlnStatement(new VariableExpression("coll2")));
    statement.addStatement(createPrintlnStatement(new VariableExpression("coll3")));

    classNode.addMethod(new MethodNode("manyParamDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement));

    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);

    Object bean = fooClass.getDeclaredConstructor().newInstance();
    assertTrue("Managed to create bean", bean != null);

    System.out.println("################ Now about to invoke a method with many parameters");
    Object[] array = {Integer.valueOf(1000 * 1000), "foo-", "bar~"};

    try {
        InvokerHelper.invokeMethod(bean, "manyParamDemo", array);
    } catch (InvokerInvocationException e) {
        System.out.println("Caught: " + e.getCause());
        e.getCause().printStackTrace();
        fail("Should not have thrown an exception");
    }
    System.out.println("################ Done");
}
 
Example #27
Source File: ForTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void testLoop() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));

    Parameter[] parameters = {new Parameter(ClassHelper.OBJECT_TYPE.makeArray(), "coll")};

    Statement loopStatement = createPrintlnStatement(new VariableExpression("i"));

    ForStatement statement = new ForStatement(new Parameter(ClassHelper.OBJECT_TYPE, "i"), new VariableExpression("coll"), loopStatement);
    classNode.addMethod(new MethodNode("iterateDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement));

    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);

    Object bean = fooClass.getDeclaredConstructor().newInstance();
    assertTrue("Managed to create bean", bean != null);

    System.out.println("################ Now about to invoke a method with looping");
    Object[] array = {Integer.valueOf(1234), "abc", "def"};

    try {
        InvokerHelper.invokeMethod(bean, "iterateDemo", new Object[]{array});
    } catch (InvokerInvocationException e) {
        System.out.println("Caught: " + e.getCause());
        e.getCause().printStackTrace();
        fail("Should not have thrown an exception");
    }
    System.out.println("################ Done");
}
 
Example #28
Source File: ForTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void testNonLoop() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));

    Parameter[] parameters = {new Parameter(ClassHelper.OBJECT_TYPE, "coll")};

    Statement statement = createPrintlnStatement(new VariableExpression("coll"));
    classNode.addMethod(new MethodNode("oneParamDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters, ClassNode.EMPTY_ARRAY, statement));

    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);

    Object bean = fooClass.getDeclaredConstructor().newInstance();
    assertTrue("Managed to create bean", bean != null);

    System.out.println("################ Now about to invoke a method without looping");
    Object value = Integer.valueOf(10000);

    try {
        InvokerHelper.invokeMethod(bean, "oneParamDemo", new Object[]{value});
    } catch (InvokerInvocationException e) {
        System.out.println("Caught: " + e.getCause());
        e.getCause().printStackTrace();
        fail("Should not have thrown an exception");
    }
    System.out.println("################ Done");
}
 
Example #29
Source File: BeanDynamicObject.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void setProperty(final String name, Object value) throws MissingPropertyException {
    if (!includeProperties) {
        throw propertyMissingException(name);
    }

    MetaClass metaClass = getMetaClass();
    MetaProperty property = metaClass.hasProperty(bean, name);
    if (property == null) {
        getMetaClass().invokeMissingProperty(bean, name, null, false);
    }

    if (property instanceof MetaBeanProperty && ((MetaBeanProperty) property).getSetter() == null) {
        throw new ReadOnlyPropertyException(name, bean.getClass()) {
            @Override
            public String getMessage() {
                return String.format("Cannot set the value of read-only property '%s' on %s.", name,
                        getDisplayName());
            }
        };
    }
    try {

        // Attempt type coercion before trying to set the property
        value = argsTransformer.transform(bean, MetaProperty.getSetterName(name), value)[0];

        metaClass.setProperty(bean, name, value);
    } catch (InvokerInvocationException e) {
        if (e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw e;
    }
}
 
Example #30
Source File: SecurityTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void testChecksReflectPermissionForInvokeOnPrivateMethods() throws Exception {
    cachedMethodUnderTest = createCachedMethod("privateMethod");
    System.setSecurityManager(restrictiveSecurityManager);
    try {
        invokesCachedMethod();
        fail();
    }
    catch (InvokerInvocationException e) {
        assertEquals(CacheAccessControlException.class, e.getCause().getClass());
    }
}