List of usage examples for java.lang.reflect InvocationTargetException getCause
public Throwable getCause()
From source file:nl.talsmasoftware.enumerables.support.json.jackson2.Compatibility.java
@SuppressWarnings("unchecked") private static <T> T call(Object target, String method) throws NoSuchMethodException { try {// w w w.j av a 2 s.c o m return (T) method(target.getClass(), method).invoke(target); } catch (IllegalAccessException iae) { NoSuchMethodException nsme = new NoSuchMethodException( String.format("Not allowed to call method \"%s\": %s", method, iae.getMessage())); nsme.initCause(iae); throw nsme; } catch (InvocationTargetException ite) { Throwable cause = ite.getCause(); if (cause == null) cause = ite; // shouldn't happen! throw cause instanceof RuntimeException ? (RuntimeException) cause : new RuntimeException(cause.getMessage(), cause); } }
From source file:ClassUtils.java
/** * Helper for invoking a static method that takes one parameter. * /* w w w.ja v a2s . com*/ * @param cl * The class that implements the static method * @param methodName * The method name * @param param * A parameter * @param paramClass * Class of the parameter * @throws Throwable */ public static Object invokeStaticMethod(Class cl, String methodName, Object param, Class paramClass) throws Throwable { Method method = cl.getMethod(methodName, new Class[] { paramClass }); try { return method.invoke(null, new Object[] { param }); } catch (InvocationTargetException e) { throw e.getCause(); } }
From source file:org.getobjects.appserver.core.WODirectAction.java
/** * Implements the "direct action" request handling / method lookup. This is * a static method because its reused by WOComponent. * <p>//from ww w.ja v a2 s . co m * This implementation checks for a method with a name which ends in "Action" * and which has no parameters, eg:<pre> * defaultAction * viewAction</pre> * Hence only methods ending in "Action" are exposed (automagically) to the * web. * * @param _o - the WODirectAction or WOComponent * @param _name - the name of the action to invoke * @param _ctx - the WOContext to run the action in * @return the result, eg a WOComponent or WOResponse */ public static Object performActionNamed(Object _o, String _name, WOContext _ctx) { if (_o == null || _name == null) return null; Method m = NSJavaRuntime.NSMethodFromString(_o.getClass(), _name + "Action", emptyClassArray); if (m == null) { /* Hm, warn can be turned off since handleMissingAction could perform * a sensible implementation. Or should handleMissingAction() do the * log? */ daLog.warn("did not find method for DA: " + _name + " (object=" + _o + ")"); return _ctx.application().handleMissingAction(_name, _ctx); } Object results; try { results = m.invoke(_o, emptyObjectArray); } catch (InvocationTargetException ite) { results = _ctx.application().handleException(ite.getCause(), _ctx); } catch (IllegalAccessException iae) { // TODO: improve logging daLog.error("invalid access for DA, must be public: " + iae); results = _ctx.application().handleMissingAction(_name, _ctx); } return results; }
From source file:ClassUtils.java
/** * Helper for invoking an instance method that takes a single parameter. * This method also handles parameters of primitive type. * /* w ww .j ava2 s . c om*/ * @param cl * The class that the instance belongs to * @param instance * The object on which we will invoke the method * @param methodName * The method name * @param param * The parameter * @throws Throwable */ public static Object invokeMethod(Class cl, Object instance, String methodName, Object param) throws Throwable { Class paramClass; if (param instanceof Integer) paramClass = Integer.TYPE; else if (param instanceof Long) paramClass = Long.TYPE; else if (param instanceof Short) paramClass = Short.TYPE; else if (param instanceof Boolean) paramClass = Boolean.TYPE; else if (param instanceof Double) paramClass = Double.TYPE; else if (param instanceof Float) paramClass = Float.TYPE; else if (param instanceof Character) paramClass = Character.TYPE; else if (param instanceof Byte) paramClass = Byte.TYPE; else paramClass = param.getClass(); Method method = cl.getMethod(methodName, new Class[] { paramClass }); try { return method.invoke(instance, new Object[] { param }); } catch (InvocationTargetException e) { throw e.getCause(); } }
From source file:com.cloudera.oryx.app.serving.als.model.ALSServingModelManager.java
private static RescorerProvider loadInstanceOf(String implClassName) { try {/* w w w. ja v a 2 s. c o m*/ // ClassUtils is not available here Class<? extends RescorerProvider> configClass = Class .forName(implClassName, true, RescorerProvider.class.getClassLoader()) .asSubclass(RescorerProvider.class); Constructor<? extends RescorerProvider> constructor = configClass.getConstructor(); return constructor.newInstance(); } catch (ClassNotFoundException e) { throw new IllegalArgumentException("Could not load " + implClassName + " due to exception", e); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) { throw new IllegalArgumentException("Could not instantiate " + implClassName + " due to exception", e); } catch (InvocationTargetException ite) { throw new IllegalStateException("Could not instantiate " + implClassName + " due to exception", ite.getCause()); } }
From source file:org.openadaptor.legacy.convertor.dataobjects.LegacyUtils.java
/** * Assign attributes for the legacy convertor compenent. * Consult legacy openadaptor documentation for details on * possible attributes./* ww w .j a v a 2s . c o m*/ * @param attributeMap */ public static void setAttributes(Object legacyOpenadaptorObject, Map attributeMap) { Method method = getMethod(legacyOpenadaptorObject.getClass(), LEGACY_SET_ATTRIBUTE_METHOD_NAME, LEGACY_SET_ATTRIBUTE_METHOD_ARGS); for (Iterator iter = attributeMap.entrySet().iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry) iter.next(); try { method.invoke(legacyOpenadaptorObject, new Object[] { (String) entry.getKey(), (String) entry.getValue() }); } //ToDo: Revisit this. catch (MissingResourceException se) { //Ignore the stub warning, it would break springcheck ant task for now log.warn(se); if (!ignoreStubExceptions()) { throw new RuntimeException(se.getMessage(), se); } } catch (InvocationTargetException ite) { Throwable cause = ite.getCause(); log.debug("InvocationTargetException cause: " + cause); if (cause instanceof MissingResourceException) { if (ignoreStubExceptions()) { log.warn("Ignoring StubException generated on setAttributes"); } else { log.error("Stub code invoked - " + cause.getMessage()); throw (MissingResourceException) cause; } } else { String msg = "Failed to setAttributes - " + cause; throw new RuntimeException(msg, cause); } } catch (Exception e) { String msg = "Failed to setAttributes. Exception was: " + e; log.warn(msg); throw new RuntimeException(msg, e); } } }
From source file:Main.java
/** * Execute the given runnable code dedicated to Swing using the Event Dispatcher Thread (EDT) * And waits for completion//from ww w . ja va 2 s. c om * @param runnable runnable code dedicated to Swing * @throws IllegalStateException if any exception occurs while the given runnable code executes using EDT */ public static void invokeAndWaitEDT(final Runnable runnable) throws IllegalStateException { if (isEDT()) { // current Thread is EDT, simply execute runnable: runnable.run(); } else { // If the current thread is interrupted, then use invoke later EDT (i.e. do not wait): if (Thread.currentThread().isInterrupted()) { invokeLaterEDT(runnable); } else { try { // Using invokeAndWait to be in sync with the calling thread: SwingUtilities.invokeAndWait(runnable); } catch (InterruptedException ie) { // propagate the exception because it should never happen: throw new IllegalStateException( "SwingUtils.invokeAndWaitEDT : interrupted while running " + runnable, ie); } catch (InvocationTargetException ite) { // propagate the internal exception : throw new IllegalStateException( "SwingUtils.invokeAndWaitEDT : an exception occured while running " + runnable, ite.getCause()); } } } }
From source file:org.openehealth.ipf.commons.lbs.utils.NiceClass.java
/** * Ensures that a constructors of a class is null safe. * @param obj/*www . j a v a2s . c o m*/ * object to test * @param args * dummy arguments of all different parameter types that the constructor has * @param optional * optional dummy arguments that can also be set to {@code null} without breaking * null-safety * @throws any thrown exception that were not expected. Never an {@link IllegalArgumentException}. */ public static void checkConstructorIsNullSafe(Constructor<?> constructor, List<?> args, List<?> optional) throws Exception { log.info("checked " + constructor); constructor.setAccessible(true); Class<?>[] parameterTypes = constructor.getParameterTypes(); Object[] parameterValues = getParameterValues(parameterTypes, args); for (int idx = 0; idx < parameterValues.length; ++idx) { if (!isOptional(constructor.getName(), idx, optional, parameterValues[idx])) { Object[] parameterValuesWithNull = Arrays.copyOf(parameterValues, parameterValues.length); parameterValuesWithNull[idx] = null; try { constructor.newInstance(parameterValuesWithNull); fail("Should throw an " + IllegalArgumentException.class.getSimpleName()); } catch (InvocationTargetException e) { assertEquals(IllegalArgumentException.class, e.getCause().getClass()); } } } }
From source file:org.openehealth.ipf.commons.lbs.utils.NiceClass.java
/** * Ensures that a methods of a class is null safe. * @param obj//w w w.j a v a 2s. c om * object to test * @param args * dummy arguments of all different parameter types that the methods has * @param optional * optional dummy arguments that can also be set to {@code null} without breaking * null-safety * @throws any thrown exception that were not expected. Never an {@link IllegalArgumentException}. */ public static void checkMethodIsNullSafe(Object obj, Method method, List<?> args, List<?> optional) throws Exception { log.info("checked " + method); method.setAccessible(true); Class<?>[] parameterTypes = method.getParameterTypes(); Object[] parameterValues = getParameterValues(parameterTypes, args); for (int idx = 0; idx < parameterValues.length; ++idx) { if (!isOptional(method.getName(), idx, optional, parameterValues[idx])) { Object[] parameterValuesWithNull = Arrays.copyOf(parameterValues, parameterValues.length); parameterValuesWithNull[idx] = null; try { method.invoke(obj, parameterValuesWithNull); fail(method + " not null safe"); } catch (InvocationTargetException e) { assertEquals(method + " not null safe", IllegalArgumentException.class, e.getCause().getClass()); } } } }
From source file:org.jiemamy.utils.reflect.MethodUtil.java
/** * {@link Method#invoke(Object, Object[])}??? * /*from w w w . java 2s .c om*/ * @param method * @param target * @param args * @return * @throws IllegalAccessException ??????? * @throws IllegalArgumentException ??????? * @throws InvocationTargetException ??? * @see Method#invoke(Object, Object[]) * @throws IllegalArgumentException {@code method}?{@code null}??? */ public static Object invoke(Method method, Object target, Object[] args) throws IllegalAccessException, InvocationTargetException { Validate.notNull(method); try { return method.invoke(target, args); } catch (InvocationTargetException ex) { Throwable t = ex.getCause(); if (t instanceof RuntimeException) { throw (RuntimeException) t; } if (t instanceof Error) { throw (Error) t; } throw ex; } }