public String getMessage() { Throwable cause = getCause(); return (cause==null)?"java.lang.NullPointerException":cause.toString(); } }
private static void setPropertySafe(Object object, MetaClass mc, String key, Object value) { try { mc.setProperty(object, key, value); } catch (MissingPropertyException mpe) { // Ignore } catch (InvokerInvocationException iie) { // GROOVY-5802 IAE for missing properties with classes that extend List Throwable cause = iie.getCause(); if (!(cause instanceof IllegalArgumentException)) throw iie; } }
/** * Note: This method may throw checked exceptions although it doesn't say so. */ @SuppressWarnings("unchecked") public static <T> T invokeClosure(Closure<T> closure, Object... args) { try { return closure.call(args); } catch (InvokerInvocationException e) { ExceptionUtil.sneakyThrow(e.getCause()); return null; // never reached } }
/** * Note: This method may throw checked exceptions although it doesn't say so. */ public static void setProperty(Object target, String property, Object value) { try { InvokerHelper.setProperty(target, property, value); } catch (InvokerInvocationException e) { ExceptionUtil.sneakyThrow(e.getCause()); } }
/** * Note: This method may throw checked exceptions although it doesn't say so. */ public static Object invokeMethod(Object target, String method, Object... args) { try { return InvokerHelper.invokeMethod(target, method, args); } catch (InvokerInvocationException e) { ExceptionUtil.sneakyThrow(e.getCause()); return null; // never reached } }
/** * Note: This method may throw checked exceptions although it doesn't say so. */ public static Iterator<Object> asIterator(Object object) { try { return InvokerHelper.asIterator(object); } catch (InvokerInvocationException e) { ExceptionUtil.sneakyThrow(e.getCause()); return null; // never reached } }
/** * Note: This method may throw checked exceptions although it doesn't say so. */ public static Object getProperty(Object target, String property) { try { return InvokerHelper.getProperty(target, property); } catch (InvokerInvocationException e) { ExceptionUtil.sneakyThrow(e.getCause()); return null; // never reached } }
/** * Note: This method may throw checked exceptions although it doesn't say so. */ public static Object invokeConstructor(Class<?> clazz, Object... args) { try { return InvokerHelper.invokeConstructorOf(clazz, args); } catch (InvokerInvocationException e) { ExceptionUtil.sneakyThrow(e.getCause()); return null; // never reached } }
public static Object invokeMethodNullSafe(Object target, String method, Object... args) { try { return InvokerHelper.invokeMethodSafe(target, method, args); } catch (InvokerInvocationException e) { ExceptionUtil.sneakyThrow(e.getCause()); return null; // never reached } }
/** * Note: This method may throw checked exceptions although it doesn't say so. */ public static Object getAttribute(Object target, String name) { try { return InvokerHelper.getAttribute(target, name); } catch (InvokerInvocationException e) { ExceptionUtil.sneakyThrow(e.getCause()); return null; // never reached } }
private void setBeanProperty(Object newValue) { try { propertyAccessor().write(bean, propertyName, newValue); } catch (InvokerInvocationException iie) { if (!(iie.getCause() instanceof PropertyVetoException)) { throw iie; } // ignore veto exceptions, just let the binding fail like a validation does } }
@SuppressWarnings("unchecked") public V call(Object... args) { try { return (V) getMetaClass().invokeMethod(this,"doCall",args); } catch (InvokerInvocationException e) { UncheckedThrow.rethrow(e.getCause()); return null; // unreachable statement } catch (Exception e) { return (V) throwRuntimeException(e); } }
protected Object invoke(Class type, Object args) throws Throwable { try { return InvokerHelper.invokeConstructorOf(type, args); } catch (InvokerInvocationException e) { throw e.getCause(); } } }
protected Object invoke(Object object, String method, Object args) throws Throwable { try { return InvokerHelper.invokeMethod(object, method, args); } catch (InvokerInvocationException e) { throw e.getCause(); } } }
/** * Run the script. */ private boolean run() { try { if (processSockets) { processSockets(); } else if (processFiles) { processFiles(); } else { processOnce(); } return true; } catch (CompilationFailedException e) { System.err.println(e); return false; } catch (Throwable e) { if (e instanceof InvokerInvocationException) { InvokerInvocationException iie = (InvokerInvocationException) e; e = iie.getCause(); } System.err.println("Caught: " + e); if (!debug) { StackTraceUtils.deepSanitize(e); } e.printStackTrace(); return false; } }
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.newInstance(); assertTrue("Managed to create bean", bean != null); System.out.println("################ Now about to invoke a method without looping"); Object value = new Integer(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"); }
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.newInstance(); assertTrue("Managed to create bean", bean != null); System.out.println("################ Now about to invoke a method with looping"); Object[] array = {new Integer(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"); }
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.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"); }