Example usage for java.lang.reflect InvocationTargetException getTargetException

List of usage examples for java.lang.reflect InvocationTargetException getTargetException

Introduction

In this page you can find the example usage for java.lang.reflect InvocationTargetException getTargetException.

Prototype

public Throwable getTargetException() 

Source Link

Document

Get the thrown target exception.

Usage

From source file:com.openddal.test.BaseTestCase.java

/**
 * Verify the next method call on the object will throw an exception.
 *
 * @param <T> the class of the object
 * @param verifier the result verifier to call
 * @param obj the object to wrap/*from   w w w  . ja  v  a 2  s. c  om*/
 * @return a proxy for the object
 */
@SuppressWarnings("unchecked")
protected <T> T assertThrows(final ResultVerifier verifier, final T obj) {
    Class<?> c = obj.getClass();
    InvocationHandler ih = new InvocationHandler() {
        private Exception called = new Exception("No method called");

        @Override
        protected void finalize() {
            if (called != null) {
                called.printStackTrace(System.err);
            }
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
            try {
                called = null;
                Object ret = method.invoke(obj, args);
                verifier.verify(ret, null, method, args);
                return ret;
            } catch (InvocationTargetException e) {
                verifier.verify(null, e.getTargetException(), method, args);
                Class<?> retClass = method.getReturnType();
                if (!retClass.isPrimitive()) {
                    return null;
                }
                if (retClass == boolean.class) {
                    return false;
                } else if (retClass == byte.class) {
                    return (byte) 0;
                } else if (retClass == char.class) {
                    return (char) 0;
                } else if (retClass == short.class) {
                    return (short) 0;
                } else if (retClass == int.class) {
                    return 0;
                } else if (retClass == long.class) {
                    return 0L;
                } else if (retClass == float.class) {
                    return 0F;
                } else if (retClass == double.class) {
                    return 0D;
                }
                return null;
            }
        }
    };
    if (!ProxyCodeGenerator.isGenerated(c)) {
        Class<?>[] interfaces = c.getInterfaces();
        if (Modifier.isFinal(c.getModifiers()) || (interfaces.length > 0 && getClass() != c)) {
            // interface class proxies
            if (interfaces.length == 0) {
                throw new RuntimeException("Can not create a proxy for the class " + c.getSimpleName()
                        + " because it doesn't implement any interfaces and is final");
            }
            return (T) Proxy.newProxyInstance(c.getClassLoader(), interfaces, ih);
        }
    }
    try {
        Class<?> pc = ProxyCodeGenerator.getClassProxy(c);
        Constructor<?> cons = pc.getConstructor(InvocationHandler.class);
        return (T) cons.newInstance(ih);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.espertech.esper.event.bean.EventBeanManufacturerBean.java

public Object makeUnderlying(Object[] propertyValues) {
    Object outObject = beanInstantiator.instantiate();

    if (writeMethodsFastClass != null) {
        if (!hasPrimitiveTypes) {
            Object[] parameters = new Object[1];
            for (int i = 0; i < writeMethodsFastClass.length; i++) {
                parameters[0] = propertyValues[i];
                try {
                    writeMethodsFastClass[i].invoke(outObject, parameters);
                } catch (InvocationTargetException e) {
                    handle(e, writeMethodsFastClass[i].getName());
                }/*from   w  ww  .ja va2 s.com*/
            }
        } else {
            Object[] parameters = new Object[1];
            for (int i = 0; i < writeMethodsFastClass.length; i++) {
                if (primitiveType[i]) {
                    if (propertyValues[i] == null) {
                        continue;
                    }
                }
                parameters[0] = propertyValues[i];
                try {
                    writeMethodsFastClass[i].invoke(outObject, parameters);
                } catch (InvocationTargetException e) {
                    handle(e, writeMethodsFastClass[i].getName());
                }
            }
        }
    } else {

        if (!hasPrimitiveTypes) {
            Object[] parameters = new Object[1];
            for (int i = 0; i < writeMethodsReflection.length; i++) {
                parameters[0] = propertyValues[i];
                try {
                    writeMethodsReflection[i].invoke(outObject, parameters);
                } catch (InvocationTargetException e) {
                    String message = "Unexpected exception encountered invoking setter-method '"
                            + writeMethodsReflection[i] + "' on class '"
                            + beanEventType.getUnderlyingType().getName() + "' : "
                            + e.getTargetException().getMessage();
                    log.error(message, e);
                } catch (IllegalAccessException e) {
                    handle(e, writeMethodsReflection[i].getName());
                }
            }
        } else {
            Object[] parameters = new Object[1];
            for (int i = 0; i < writeMethodsReflection.length; i++) {
                if (primitiveType[i]) {
                    if (propertyValues[i] == null) {
                        continue;
                    }
                }
                parameters[0] = propertyValues[i];
                try {
                    writeMethodsReflection[i].invoke(outObject, parameters);
                } catch (InvocationTargetException e) {
                    handle(e, writeMethodsReflection[i].getName());
                } catch (IllegalAccessException e) {
                    handle(e, writeMethodsReflection[i].getName());
                }
            }
        }
    }

    return outObject;
}

From source file:javadz.beanutils.PropertyUtilsBean.java

/**
 * Return the value of the specified indexed property of the specified
 * bean, with no type conversions.  In addition to supporting the JavaBeans
 * specification, this method has been extended to support
 * <code>List</code> objects as well.
 *
 * @param bean Bean whose property is to be extracted
 * @param name Simple property name of the property value to be extracted
 * @param index Index of the property value to be extracted
 * @return the indexed property value/* w  ww  .  j a  v a2  s  .co  m*/
 *
 * @exception IndexOutOfBoundsException if the specified index
 *  is outside the valid range for the underlying property
 * @exception IllegalAccessException if the caller does not have
 *  access to the property accessor method
 * @exception IllegalArgumentException if <code>bean</code> or
 *  <code>name</code> is null
 * @exception InvocationTargetException if the property accessor method
 *  throws an exception
 * @exception NoSuchMethodException if an accessor method for this
 *  propety cannot be found
 */
public Object getIndexedProperty(Object bean, String name, int index)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    if (bean == null) {
        throw new IllegalArgumentException("No bean specified");
    }
    if (name == null || name.length() == 0) {
        if (bean.getClass().isArray()) {
            return Array.get(bean, index);
        } else if (bean instanceof List) {
            return ((List) bean).get(index);
        }
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified for bean class '" + bean.getClass() + "'");
    }

    // Handle DynaBean instances specially
    if (bean instanceof DynaBean) {
        DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(name);
        if (descriptor == null) {
            throw new NoSuchMethodException(
                    "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'");
        }
        return (((DynaBean) bean).get(name, index));
    }

    // Retrieve the property descriptor for the specified property
    PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
    if (descriptor == null) {
        throw new NoSuchMethodException(
                "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'");
    }

    // Call the indexed getter method if there is one
    if (descriptor instanceof IndexedPropertyDescriptor) {
        Method readMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedReadMethod();
        readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod);
        if (readMethod != null) {
            Object[] subscript = new Object[1];
            subscript[0] = new Integer(index);
            try {
                return (invokeMethod(readMethod, bean, subscript));
            } catch (InvocationTargetException e) {
                if (e.getTargetException() instanceof IndexOutOfBoundsException) {
                    throw (IndexOutOfBoundsException) e.getTargetException();
                } else {
                    throw e;
                }
            }
        }
    }

    // Otherwise, the underlying property must be an array
    Method readMethod = getReadMethod(bean.getClass(), descriptor);
    if (readMethod == null) {
        throw new NoSuchMethodException(
                "Property '" + name + "' has no " + "getter method on bean class '" + bean.getClass() + "'");
    }

    // Call the property getter and return the value
    Object value = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY);
    if (!value.getClass().isArray()) {
        if (!(value instanceof java.util.List)) {
            throw new IllegalArgumentException(
                    "Property '" + name + "' is not indexed on bean class '" + bean.getClass() + "'");
        } else {
            //get the List's value
            return ((java.util.List) value).get(index);
        }
    } else {
        //get the array's value
        try {
            return (Array.get(value, index));
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new ArrayIndexOutOfBoundsException(
                    "Index: " + index + ", Size: " + Array.getLength(value) + " for property '" + name + "'");
        }
    }

}

From source file:javadz.beanutils.PropertyUtilsBean.java

/**
 * Set the value of the specified indexed property of the specified
 * bean, with no type conversions.  In addition to supporting the JavaBeans
 * specification, this method has been extended to support
 * <code>List</code> objects as well.
 *
 * @param bean Bean whose property is to be set
 * @param name Simple property name of the property value to be set
 * @param index Index of the property value to be set
 * @param value Value to which the indexed property element is to be set
 *
 * @exception IndexOutOfBoundsException if the specified index
 *  is outside the valid range for the underlying property
 * @exception IllegalAccessException if the caller does not have
 *  access to the property accessor method
 * @exception IllegalArgumentException if <code>bean</code> or
 *  <code>name</code> is null
 * @exception InvocationTargetException if the property accessor method
 *  throws an exception// w  w w. j  a v  a 2 s .com
 * @exception NoSuchMethodException if an accessor method for this
 *  propety cannot be found
 */
public void setIndexedProperty(Object bean, String name, int index, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    if (bean == null) {
        throw new IllegalArgumentException("No bean specified");
    }
    if (name == null || name.length() == 0) {
        if (bean.getClass().isArray()) {
            Array.set(bean, index, value);
            return;
        } else if (bean instanceof List) {
            ((List) bean).set(index, value);
            return;
        }
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified for bean class '" + bean.getClass() + "'");
    }

    // Handle DynaBean instances specially
    if (bean instanceof DynaBean) {
        DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(name);
        if (descriptor == null) {
            throw new NoSuchMethodException(
                    "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'");
        }
        ((DynaBean) bean).set(name, index, value);
        return;
    }

    // Retrieve the property descriptor for the specified property
    PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
    if (descriptor == null) {
        throw new NoSuchMethodException(
                "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'");
    }

    // Call the indexed setter method if there is one
    if (descriptor instanceof IndexedPropertyDescriptor) {
        Method writeMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod();
        writeMethod = MethodUtils.getAccessibleMethod(bean.getClass(), writeMethod);
        if (writeMethod != null) {
            Object[] subscript = new Object[2];
            subscript[0] = new Integer(index);
            subscript[1] = value;
            try {
                if (log.isTraceEnabled()) {
                    String valueClassName = value == null ? "<null>" : value.getClass().getName();
                    log.trace("setSimpleProperty: Invoking method " + writeMethod + " with index=" + index
                            + ", value=" + value + " (class " + valueClassName + ")");
                }
                invokeMethod(writeMethod, bean, subscript);
            } catch (InvocationTargetException e) {
                if (e.getTargetException() instanceof IndexOutOfBoundsException) {
                    throw (IndexOutOfBoundsException) e.getTargetException();
                } else {
                    throw e;
                }
            }
            return;
        }
    }

    // Otherwise, the underlying property must be an array or a list
    Method readMethod = getReadMethod(bean.getClass(), descriptor);
    if (readMethod == null) {
        throw new NoSuchMethodException(
                "Property '" + name + "' has no getter method on bean class '" + bean.getClass() + "'");
    }

    // Call the property getter to get the array or list
    Object array = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY);
    if (!array.getClass().isArray()) {
        if (array instanceof List) {
            // Modify the specified value in the List
            ((List) array).set(index, value);
        } else {
            throw new IllegalArgumentException(
                    "Property '" + name + "' is not indexed on bean class '" + bean.getClass() + "'");
        }
    } else {
        // Modify the specified value in the array
        Array.set(array, index, value);
    }

}

From source file:hudson.model.AbstractProject.java

private IOException handleInvocationTargetException(InvocationTargetException e) {
    Throwable t = e.getTargetException();
    if (t instanceof Error)
        throw (Error) t;
    if (t instanceof RuntimeException)
        throw (RuntimeException) t;
    if (t instanceof IOException)
        return (IOException) t;
    throw new Error(t);
}

From source file:com.netspective.commons.xdm.XmlDataModelSchema.java

/**
 * Sets the named attribute./* w  w  w  . ja  v a  2s  .c om*/
 */
public void setAttribute(XdmParseContext pc, Object element, String attributeName, String value,
        boolean withinCustom) throws DataModelException, UnsupportedAttributeException {
    boolean hasCustom = element instanceof CustomElementAttributeSetter;
    AttributeSetter as = (AttributeSetter) attributeSetters.get(attributeName);
    if (as == null && (withinCustom || !hasCustom)) {
        // see if we're trying to set a named flag
        for (Iterator i = flagsAttributeAccessors.entrySet().iterator(); i.hasNext();) {
            Map.Entry entry = (Map.Entry) i.next();
            AttributeAccessor accessor = (AttributeAccessor) entry.getValue();
            Object returnVal = null;
            try {
                returnVal = accessor.get(pc, element);
            } catch (Exception e) {
                pc.addError("Unable to set attribute '" + attributeName + "' to '" + value + "' at "
                        + pc.getLocator().getSystemId() + " line " + pc.getLocator().getLineNumber() + ": "
                        + e.getMessage());
                log.error(e);
                if (pc.isThrowErrorException())
                    throw new DataModelException(pc, e);
            }

            if (returnVal instanceof XdmBitmaskedFlagsAttribute) {
                XdmBitmaskedFlagsAttribute bfa = (XdmBitmaskedFlagsAttribute) returnVal;
                if (bfa.updateFlag(attributeName, TextUtils.getInstance().toBoolean(value)))
                    return;
            }
        }

        UnsupportedAttributeException e = new UnsupportedAttributeException(pc, element, attributeName);
        pc.addError(e);
        if (pc.isThrowErrorException())
            throw e;
        else
            return;
    }
    try {
        if (as == null && (!withinCustom && hasCustom))
            ((CustomElementAttributeSetter) element).setCustomDataModelElementAttribute(pc, this, element,
                    attributeName, value);
        else
            as.set(pc, element, value);
    } catch (InvocationTargetException ite) {
        pc.addError("Unable to set attribute '" + attributeName + "' to '" + value + "' using "
                + as.getDeclaringClass() + " at " + pc.getLocator().getSystemId() + " line "
                + pc.getLocator().getLineNumber() + ": " + ite.getTargetException());
        log.error(ite.getTargetException());
        if (pc.isThrowErrorException()) {
            Throwable t = ite.getTargetException();
            if (t instanceof DataModelException) {
                throw (DataModelException) t;
            }
            throw new DataModelException(pc, t);
        }
    } catch (Exception e) {
        pc.addError("Unable to set attribute '" + attributeName + "' to '" + value + "' at "
                + pc.getLocator().getSystemId() + " line " + pc.getLocator().getLineNumber() + ": "
                + e.getMessage());
        log.error(e);
        if (pc.isThrowErrorException())
            throw new DataModelException(pc, e);
    }
}

From source file:com.netspective.commons.xdm.XmlDataModelSchema.java

/**
 * Adds PCDATA areas.//ww w  .ja v  a  2  s  .  c om
 */
public void addText(XdmParseContext pc, Object element, String text)
        throws DataModelException, UnsupportedTextException {
    if (options.ignorePcData)
        return;

    if (addText == null) {
        UnsupportedTextException e = new UnsupportedTextException(pc, element, text);
        pc.addError(e);
        if (pc.isThrowErrorException())
            throw e;
        else
            return;
    }
    try {
        addText.invoke(element, new String[] { text });
    } catch (InvocationTargetException ite) {
        pc.addError("Unable to add text '" + text + "' at " + pc.getLocator().getSystemId() + " line "
                + pc.getLocator().getLineNumber() + ": " + ite.getMessage());
        log.error(ite);
        if (pc.isThrowErrorException()) {
            Throwable t = ite.getTargetException();
            if (t instanceof DataModelException) {
                throw (DataModelException) t;
            }
            throw new DataModelException(pc, t);
        }
    } catch (Exception e) {
        pc.addError("Unable to add text '" + text + "' at " + pc.getLocator().getSystemId() + " line "
                + pc.getLocator().getLineNumber() + ": " + e.getMessage());
        log.error(e);
        if (pc.isThrowErrorException())
            throw new DataModelException(pc, e);
    }
}

From source file:eu.qualityontime.commons.QPropertyUtilsBean.java

public Object _getIndexedProperty(final Object bean, final String name, final int index) throws Exception {

    if (bean == null) {
        throw new IllegalArgumentException("No bean specified");
    }//from w  w w.j  a v a  2s  . c  o  m
    if (name == null || name.length() == 0) {
        if (bean.getClass().isArray()) {
            return Array.get(bean, index);
        } else if (bean instanceof List) {
            return ((List<?>) bean).get(index);
        }
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified for bean class '" + bean.getClass() + "'");
    }

    if (name.startsWith("@")) {
        Object f = FieldUtils.readField(bean, trimAnnotations(name));
        if (null == f) {
            if (name.endsWith("?"))
                return null;
            else
                throw new NestedNullException();
        }
        if (f.getClass().isArray())
            return Array.get(f, index);
        else if (f instanceof List)
            return ((List<?>) f).get(index);
    }
    // Retrieve the property descriptor for the specified property
    final PropertyDescriptor descriptor = getPropertyDescriptor(bean, trimAnnotations(name));
    if (descriptor == null) {
        throw new NoSuchMethodException(
                "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'");
    }

    // Call the indexed getter method if there is one
    if (descriptor instanceof IndexedPropertyDescriptor) {
        Method readMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedReadMethod();
        readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod);
        if (readMethod != null) {
            final Object[] subscript = new Object[1];
            subscript[0] = new Integer(index);
            try {
                return invokeMethod(readMethod, bean, subscript);
            } catch (final InvocationTargetException e) {
                if (e.getTargetException() instanceof IndexOutOfBoundsException) {
                    throw (IndexOutOfBoundsException) e.getTargetException();
                } else {
                    throw e;
                }
            }
        }
    }

    // Otherwise, the underlying property must be an array
    final Method readMethod = getReadMethod(bean.getClass(), descriptor);
    if (readMethod == null) {
        throw new NoSuchMethodException(
                "Property '" + name + "' has no " + "getter method on bean class '" + bean.getClass() + "'");
    }

    // Call the property getter and return the value
    final Object value = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY);
    if (null == value && name.endsWith("?"))
        return null;
    if (!value.getClass().isArray()) {
        if (!(value instanceof java.util.List)) {
            throw new IllegalArgumentException(
                    "Property '" + name + "' is not indexed on bean class '" + bean.getClass() + "'");
        } else {
            // get the List's value
            return ((java.util.List<?>) value).get(index);
        }
    } else {
        // get the array's value
        try {
            return Array.get(value, index);
        } catch (final ArrayIndexOutOfBoundsException e) {
            throw new ArrayIndexOutOfBoundsException(
                    "Index: " + index + ", Size: " + Array.getLength(value) + " for property '" + name + "'");
        }
    }

}

From source file:com.netspective.commons.xdm.XmlDataModelSchema.java

/**
 * Creates a named nested element.//ww  w.ja v  a 2 s. c  o  m
 */
public Object createElement(XdmParseContext pc, String alternateClassName, Object element, String elementName,
        boolean withinCustom) throws DataModelException, UnsupportedElementException {
    //System.out.println("Creating: " + element.getClass().getName() + " " + elementName + " " + alternateClassName + " " + withinCustom);

    try {
        if (element instanceof CustomElementCreator && !withinCustom)
            return ((CustomElementCreator) element).createCustomDataModelElement(pc, this, element, elementName,
                    alternateClassName);
        else
            return createElement(pc, alternateClassName, element, elementName);
    } catch (InvocationTargetException ite) {
        pc.addError(
                "Could not create class for element '" + elementName + "' at " + pc.getLocator().getSystemId()
                        + " line " + pc.getLocator().getLineNumber() + ": " + ite.getMessage());
        log.error(ite);
        if (pc.isThrowErrorException()) {
            Throwable t = ite.getTargetException();
            if (t instanceof DataModelException) {
                throw (DataModelException) t;
            }
            throw new DataModelException(pc, t);
        } else
            return null;
    } catch (Exception e) {
        pc.addError(
                "Could not create class for element '" + elementName + "' at " + pc.getLocator().getSystemId()
                        + " line " + pc.getLocator().getLineNumber() + ": " + e.getMessage());
        log.error(e);
        if (pc.isThrowErrorException())
            throw new DataModelException(pc, e);
        return null;
    }
}

From source file:com.netspective.commons.xdm.XmlDataModelSchema.java

public void storeElement(XdmParseContext pc, Object element, Object child, String elementName,
        boolean withinCustom) throws DataModelException {
    if (elementName == null)
        return;/*from www.j  a v a 2  s. c o m*/

    NestedStorer ns = (NestedStorer) nestedStorers.get(elementName);
    try {
        if (ns == null && (!withinCustom && element instanceof CustomElementStorer))
            ((CustomElementStorer) element).storeCustomDataModelElement(pc, this, element, child, elementName);
        else if (ns != null)
            ns.store(element, child);
    } catch (InvocationTargetException ite) {
        pc.addError(
                "Could not store data for for element '" + elementName + "' at " + pc.getLocator().getSystemId()
                        + " line " + pc.getLocator().getLineNumber() + ": " + ite.getMessage());
        log.error(ite);
        if (pc.isThrowErrorException()) {
            Throwable t = ite.getTargetException();
            if (t instanceof DataModelException) {
                throw (DataModelException) t;
            }
            throw new DataModelException(pc, t);
        }
    } catch (Exception e) {
        pc.addError(
                "Could not store data for for element '" + elementName + "' at " + pc.getLocator().getSystemId()
                        + " line " + pc.getLocator().getLineNumber() + ": " + e.getMessage());
        log.error(e);
        if (pc.isThrowErrorException())
            throw new DataModelException(pc, e);
    }
}