Java Code Examples for java.lang.reflect.InvocationTargetException#getTargetException()

The following examples show how to use java.lang.reflect.InvocationTargetException#getTargetException() . 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: StaxPrettyPrintHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Object handleException(Method method, Throwable throwable) throws Throwable {
	
	if (throwable instanceof InvocationTargetException) {
		InvocationTargetException itex = (InvocationTargetException)throwable;
		throwable = itex.getTargetException();
	}
	if (throwable instanceof RuntimeException) {
		throw throwable;
	}
	if (throwable instanceof Error) {
		throw throwable; 
	}
	for (Class<?> exType : method.getExceptionTypes())
	{
		if (exType.isAssignableFrom(throwable.getClass()))
		{
			throw throwable;
		}
	}
	throw new RuntimeException(throwable);
}
 
Example 2
Source File: IndirectlyLoadABundle.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public boolean testGetAnonymousLogger() throws Throwable {
    // Test getAnonymousLogger()
    URLClassLoader loadItUpCL = new URLClassLoader(getURLs(), null);
    Class<?> loadItUpClazz = Class.forName("LoadItUp1", true, loadItUpCL);
    ClassLoader actual = loadItUpClazz.getClassLoader();
    if (actual != loadItUpCL) {
        throw new Exception("LoadItUp1 was loaded by an unexpected CL: "
                             + actual);
    }
    Object loadItUpAnon = loadItUpClazz.newInstance();
    Method testAnonMethod = loadItUpClazz.getMethod("getAnonymousLogger",
                                                    String.class);
    try {
        return (Logger)testAnonMethod.invoke(loadItUpAnon, rbName) != null;
    } catch (InvocationTargetException ex) {
        throw ex.getTargetException();
    }
}
 
Example 3
Source File: HiveShimV100.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void alterPartition(IMetaStoreClient client, String databaseName, String tableName, Partition partition)
		throws InvalidOperationException, MetaException, TException {
	String errorMsg = "Failed to alter partition for table %s in database %s";
	try {
		Method method = client.getClass().getMethod("alter_partition", String.class, String.class, Partition.class);
		method.invoke(client, databaseName, tableName, partition);
	} catch (InvocationTargetException ite) {
		Throwable targetEx = ite.getTargetException();
		if (targetEx instanceof TException) {
			throw (TException) targetEx;
		} else {
			throw new CatalogException(String.format(errorMsg, tableName, databaseName), targetEx);
		}
	} catch (NoSuchMethodException | IllegalAccessException e) {
		throw new CatalogException(String.format(errorMsg, tableName, databaseName), e);
	}
}
 
Example 4
Source File: HiveShimV210.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void alterPartition(IMetaStoreClient client, String databaseName, String tableName, Partition partition)
		throws InvalidOperationException, MetaException, TException {
	String errorMsg = "Failed to alter partition for table %s in database %s";
	try {
		Method method = client.getClass().getMethod("alter_partition", String.class, String.class,
			Partition.class, EnvironmentContext.class);
		method.invoke(client, databaseName, tableName, partition, null);
	} catch (InvocationTargetException ite) {
		Throwable targetEx = ite.getTargetException();
		if (targetEx instanceof TException) {
			throw (TException) targetEx;
		} else {
			throw new CatalogException(String.format(errorMsg, tableName, databaseName), targetEx);
		}
	} catch (NoSuchMethodException | IllegalAccessException e) {
		throw new CatalogException(String.format(errorMsg, tableName, databaseName), e);
	}
}
 
Example 5
Source File: InjectionMetadata.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Either this or {@link #getResourceToInject} needs to be overridden.
 */
protected void inject(Object target, String requestingBeanName, PropertyValues pvs) throws Throwable {
	if (this.isField) {
		Field field = (Field) this.member;
		ReflectionUtils.makeAccessible(field);
		field.set(target, getResourceToInject(target, requestingBeanName));
	}
	else {
		if (checkPropertySkipping(pvs)) {
			return;
		}
		try {
			Method method = (Method) this.member;
			ReflectionUtils.makeAccessible(method);
			method.invoke(target, getResourceToInject(target, requestingBeanName));
		}
		catch (InvocationTargetException ex) {
			throw ex.getTargetException();
		}
	}
}
 
Example 6
Source File: SuperEngineFactory.java    From ramus with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable {
    try {

        Object res = invoker.invoke(method.getName(), args);

        return res;
    } catch (InvocationTargetException e) {
        throw e.getTargetException();
    } finally {
        if ("close".equals(method.getName())) {
            invoker = null;
        }
    }
}
 
Example 7
Source File: EeClassLoader.java    From kumuluzee with MIT License 6 votes vote down vote up
/**
 * Invokes main() method on class with provided parameters.
 */
public void invokeMain(String className, String[] args) throws Throwable {

    Class<?> clazz = loadClass(className);

    debug(String.format("Launch: %s.main(); Loader: %s", className, clazz.getClassLoader()));

    Method method = clazz.getMethod("main", String[].class);

    if (method == null) {
        throw new NoSuchMethodException("The main() method in class \"" + className + "\" not found.");
    }

    try {
        method.invoke(null, (Object)args);
    } catch (InvocationTargetException e) {
        throw e.getTargetException();
    }
}
 
Example 8
Source File: LocatableAnnotation.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
        if(method.getDeclaringClass()==Locatable.class)
            return method.invoke(this,args);
        if(Modifier.isStatic(method.getModifiers()))
            // malicious code can pass in a static Method object.
            // doing method.invoke() would end up executing it,
            // so we need to protect against it.
            throw new IllegalArgumentException();

        return method.invoke(core,args);
    } catch (InvocationTargetException e) {
        if(e.getTargetException()!=null)
            throw e.getTargetException();
        throw e;
    }
}
 
Example 9
Source File: IndirectlyLoadABundle.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public boolean testGetAnonymousLogger() throws Throwable {
    // Test getAnonymousLogger()
    URLClassLoader loadItUpCL = new URLClassLoader(getURLs(), null);
    Class<?> loadItUpClazz = Class.forName("LoadItUp1", true, loadItUpCL);
    ClassLoader actual = loadItUpClazz.getClassLoader();
    if (actual != loadItUpCL) {
        throw new Exception("LoadItUp1 was loaded by an unexpected CL: "
                             + actual);
    }
    Object loadItUpAnon = loadItUpClazz.newInstance();
    Method testAnonMethod = loadItUpClazz.getMethod("getAnonymousLogger",
                                                    String.class);
    try {
        return (Logger)testAnonMethod.invoke(loadItUpAnon, rbName) != null;
    } catch (InvocationTargetException ex) {
        throw ex.getTargetException();
    }
}
 
Example 10
Source File: JavaBehaviour.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
    // Handle Object level methods
    if (method.getName().equals("toString"))
    {
        return toString();
    }
    else if (method.getName().equals("hashCode"))
    {
        return hashCode();
    }
    else if (method.getName().equals("equals"))
    {
        if (Proxy.isProxyClass(args[0].getClass()))
        {
            return equals(Proxy.getInvocationHandler(args[0]));
        }
        return false;
    }
    
    // Delegate to designated method pointer
    if (behaviour.isEnabled())
    {
        try
        {
            behaviour.disable();
            return delegateMethod.invoke(behaviour.instance, args);
        }
        catch (InvocationTargetException e)
        {
            throw e.getTargetException();
        }
        finally
        {
            behaviour.enable();
        }
    }
    return null;
}
 
Example 11
Source File: SharedEntityManagerCreator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
	// Invocation on Query interface coming in...

	if (method.getName().equals("equals")) {
		// Only consider equal when proxies are identical.
		return (proxy == args[0]);
	}
	else if (method.getName().equals("hashCode")) {
		// Use hashCode of EntityManager proxy.
		return hashCode();
	}
	else if (method.getName().equals("unwrap")) {
		// Handle JPA 2.0 unwrap method - could be a proxy match.
		Class<?> targetClass = (Class<?>) args[0];
		if (targetClass == null) {
			return this.target;
		}
		else if (targetClass.isInstance(proxy)) {
			return proxy;
		}
	}

	// Invoke method on actual Query object.
	try {
		Object retVal = method.invoke(this.target, args);
		return (retVal == this.target ? proxy : retVal);
	}
	catch (InvocationTargetException ex) {
		throw ex.getTargetException();
	}
	finally {
		if (queryTerminationMethods.contains(method.getName())) {
			// Actual execution of the query: close the EntityManager right
			// afterwards, since that was the only reason we kept it open.
			EntityManagerFactoryUtils.closeEntityManager(this.em);
			this.em = null;
		}
	}
}
 
Example 12
Source File: DirContextPoolableObjectFactory.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable {

    String methodName = method.getName();
    if (methodName.equals("getTargetContext")) {
        return target;
    } else if (methodName.equals("hasFailed")) {
        return hasFailed;
    }

    try {
        return method.invoke(target, args);
    }
    catch (InvocationTargetException e) {
        Throwable targetException = e.getTargetException();
        Class<? extends Throwable> targetExceptionClass = targetException.getClass();

        boolean nonTransientEncountered = false;
        for (Class<? extends Throwable> clazz : nonTransientExceptions) {
            if(clazz.isAssignableFrom(targetExceptionClass)) {
                logger.info(
                        String.format("An %s - explicitly configured to be a non-transient exception - encountered; eagerly invalidating the target context.",
                                targetExceptionClass));
                nonTransientEncountered = true;
                break;
            }
        }

        if(nonTransientEncountered) {
            hasFailed = true;
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("An %s - not explicitly configured to be a non-transient exception - encountered; ignoring.",
                        targetExceptionClass));
            }
        }

        throw targetException;
    }
}
 
Example 13
Source File: IndirectlyLoadABundle.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean loadAndTest() throws Throwable {
    // Make sure we can find it via the URLClassLoader
    URLClassLoader yetAnotherResourceCL = new URLClassLoader(getURLs(), null);
    if (!testForValidResourceSetup(yetAnotherResourceCL)) {
        throw new Exception("Couldn't directly load bundle " + rbName
                + " as expected. Test config problem");
    }
    // But it shouldn't be available via the system classloader
    ClassLoader myCL = this.getClass().getClassLoader();
    if (testForValidResourceSetup(myCL)) {
        throw new Exception("Was able to directly load bundle " + rbName
                + " from " + myCL + " but shouldn't have been"
                + " able to. Test config problem");
    }

    Class<?> loadItUpClazz = Class.forName("LoadItUp1", true,
                                           yetAnotherResourceCL);
    ClassLoader actual = loadItUpClazz.getClassLoader();
    if (actual != yetAnotherResourceCL) {
        throw new Exception("LoadItUp1 was loaded by an unexpected CL: " + actual);
    }
    Object loadItUp = loadItUpClazz.newInstance();
    Method testMethod = loadItUpClazz.getMethod("getLogger", String.class, String.class);
    try {
        return (Logger)testMethod.invoke(loadItUp, "NestedLogger1", rbName) != null;
    } catch (InvocationTargetException ex) {
        throw ex.getTargetException();
    }
}
 
Example 14
Source File: Utils.java    From ArtHook with Apache License 2.0 5 votes vote down vote up
public static void callMain(String className, String... args) throws Throwable {
    try {
        Class.forName(className).getDeclaredMethod("main", String[].class).invoke(null,
                new Object[]{args});
    } catch (InvocationTargetException e) {
        throw e.getTargetException();
    }
}
 
Example 15
Source File: Util.java    From moshi with Apache License 2.0 5 votes vote down vote up
/** Throws the cause of {@code e}, wrapping it if it is checked. */
public static RuntimeException rethrowCause(InvocationTargetException e) {
  Throwable cause = e.getTargetException();
  if (cause instanceof RuntimeException) throw (RuntimeException) cause;
  if (cause instanceof Error) throw (Error) cause;
  throw new RuntimeException(cause);
}
 
Example 16
Source File: ThrowsAdviceInterceptor.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void invokeHandlerMethod(MethodInvocation mi, Throwable ex, Method method) throws Throwable {
	Object[] handlerArgs;
	if (method.getParameterCount() == 1) {
		handlerArgs = new Object[] {ex};
	}
	else {
		handlerArgs = new Object[] {mi.getMethod(), mi.getArguments(), mi.getThis(), ex};
	}
	try {
		method.invoke(this.throwsAdvice, handlerArgs);
	}
	catch (InvocationTargetException targetEx) {
		throw targetEx.getTargetException();
	}
}
 
Example 17
Source File: IndirectlyLoadABundle.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public boolean loadAndTest() throws Throwable {
    // Make sure we can find it via the URLClassLoader
    URLClassLoader yetAnotherResourceCL = new URLClassLoader(getURLs(), null);
    if (!testForValidResourceSetup(yetAnotherResourceCL)) {
        throw new Exception("Couldn't directly load bundle " + rbName
                + " as expected. Test config problem");
    }
    // But it shouldn't be available via the system classloader
    ClassLoader myCL = this.getClass().getClassLoader();
    if (testForValidResourceSetup(myCL)) {
        throw new Exception("Was able to directly load bundle " + rbName
                + " from " + myCL + " but shouldn't have been"
                + " able to. Test config problem");
    }

    Class<?> loadItUpClazz = Class.forName("LoadItUp1", true,
                                           yetAnotherResourceCL);
    ClassLoader actual = loadItUpClazz.getClassLoader();
    if (actual != yetAnotherResourceCL) {
        throw new Exception("LoadItUp1 was loaded by an unexpected CL: " + actual);
    }
    Object loadItUp = loadItUpClazz.newInstance();
    Method testMethod = loadItUpClazz.getMethod("getLogger", String.class, String.class);
    try {
        return (Logger)testMethod.invoke(loadItUp, "NestedLogger1", rbName) != null;
    } catch (InvocationTargetException ex) {
        throw ex.getTargetException();
    }
}
 
Example 18
Source File: Instantiate.java    From radon with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void handle(VM vm, Object[] operands) throws Throwable {
    String ownerName = (String) operands[0];
    String[] paramsAsStrings = ((String) operands[1]).split("\u0001\u0001");
    Class[] params;
    if (paramsAsStrings[0].equals("\u0000\u0000\u0000"))
        params = new Class[0];
    else
        params = stringsToParams(paramsAsStrings);
    Object[] args = new Object[params.length];

    Class clazz = VM.getClazz(ownerName);
    Constructor constructor = VM.getConstructor(clazz, params);

    if (constructor == null)
        throw new VMException();

    for (int i = params.length - 1; i >= 0; i--) {
        Class param = params[i];
        JWrapper arg = vm.pop();

        if (arg instanceof JTop)
            arg = vm.pop();

        if (param == boolean.class)
            args[i] = arg.asBool();
        else if (param == char.class)
            args[i] = arg.asChar();
        else if (param == short.class)
            args[i] = arg.asShort();
        else if (param == byte.class)
            args[i] = arg.asByte();
        else
            args[i] = arg.asObj();
    }

    JWrapper ref = vm.pop();
    try {
        ref.init(constructor.newInstance(args));
    } catch (InvocationTargetException e) {
        throw e.getTargetException();
    }
}
 
Example 19
Source File: JdbcTemplate.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
	// Invocation on ConnectionProxy interface coming in...

	if (method.getName().equals("equals")) {
		// Only consider equal when proxies are identical.
		return (proxy == args[0]);
	}
	else if (method.getName().equals("hashCode")) {
		// Use hashCode of PersistenceManager proxy.
		return System.identityHashCode(proxy);
	}
	else if (method.getName().equals("unwrap")) {
		if (((Class<?>) args[0]).isInstance(proxy)) {
			return proxy;
		}
	}
	else if (method.getName().equals("isWrapperFor")) {
		if (((Class<?>) args[0]).isInstance(proxy)) {
			return true;
		}
	}
	else if (method.getName().equals("close")) {
		// Handle close method: suppress, not valid.
		return null;
	}
	else if (method.getName().equals("isClosed")) {
		return false;
	}
	else if (method.getName().equals("getTargetConnection")) {
		// Handle getTargetConnection method: return underlying Connection.
		return this.target;
	}

	// Invoke method on target Connection.
	try {
		Object retVal = method.invoke(this.target, args);

		// If return value is a JDBC Statement, apply statement settings
		// (fetch size, max rows, transaction timeout).
		if (retVal instanceof Statement) {
			applyStatementSettings(((Statement) retVal));
		}

		return retVal;
	}
	catch (InvocationTargetException ex) {
		throw ex.getTargetException();
	}
}
 
Example 20
Source File: MethodProxy.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Invoke the original (super) method on the specified object.
 * @param obj the enhanced object, must be the object passed as the first
 * argument to the MethodInterceptor
 * @param args the arguments passed to the intercepted method; you may substitute a different
 * argument array as long as the types are compatible
 * @see MethodInterceptor#intercept
 * @throws Throwable the bare exceptions thrown by the called method are passed through
 * without wrapping in an <code>InvocationTargetException</code>
 */
public Object invokeSuper(Object obj, Object[] args) throws Throwable {
    try {
        init();
        FastClassInfo fci = fastClassInfo;
        return fci.f2.invoke(fci.i2, obj, args);
    } catch (InvocationTargetException e) {
        throw e.getTargetException();
    }
}