List of usage examples for java.lang.reflect InvocationTargetException getTargetException
public Throwable getTargetException()
From source file:org.enerj.apache.commons.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 * * @exception ArrayIndexOutOfBoundsException if the specified index * is outside the valid range for the underlying array * @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 va2s . c om * @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) { throw new IllegalArgumentException("No name specified"); } // Handle DynaBean instances specially if (bean instanceof DynaBean) { DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(name); if (descriptor == null) { throw new NoSuchMethodException("Unknown property '" + name + "'"); } 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 + "'"); } // Call the indexed getter method if there is one if (descriptor instanceof IndexedPropertyDescriptor) { Method readMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedReadMethod(); 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 ArrayIndexOutOfBoundsException) { throw (ArrayIndexOutOfBoundsException) e.getTargetException(); } else { throw e; } } } } // Otherwise, the underlying property must be an array Method readMethod = getReadMethod(descriptor); if (readMethod == null) { throw new NoSuchMethodException("Property '" + name + "' has no getter method"); } // Call the property getter and return the value Object value = invokeMethod(readMethod, bean, new Object[0]); if (!value.getClass().isArray()) { if (!(value instanceof java.util.List)) { throw new IllegalArgumentException("Property '" + name + "' is not indexed"); } else { //get the List's value return ((java.util.List) value).get(index); } } else { //get the array's value return (Array.get(value, index)); } }
From source file:org.enerj.apache.commons.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 ArrayIndexOutOfBoundsException if the specified index * is outside the valid range for the underlying array * @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/*from ww w. ja v a2 s. c om*/ * @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) { throw new IllegalArgumentException("No name specified"); } // Handle DynaBean instances specially if (bean instanceof DynaBean) { DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(name); if (descriptor == null) { throw new NoSuchMethodException("Unknown property '" + name + "'"); } ((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 + "'"); } // Call the indexed setter method if there is one if (descriptor instanceof IndexedPropertyDescriptor) { Method writeMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod(); 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 ArrayIndexOutOfBoundsException) { throw (ArrayIndexOutOfBoundsException) e.getTargetException(); } else { throw e; } } return; } } // Otherwise, the underlying property must be an array or a list Method readMethod = descriptor.getReadMethod(); if (readMethod == null) { throw new NoSuchMethodException("Property '" + name + "' has no getter method"); } // Call the property getter to get the array or list Object array = invokeMethod(readMethod, bean, new Object[0]); 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"); } } else { // Modify the specified value in the array Array.set(array, index, value); } }
From source file:org.enhydra.shark.asap.util.BeanSerializerShark.java
/** * Serialize a bean. Done simply by serializing each bean property. * @param name is the element name/*from ww w . j a v a 2 s . c o m*/ * @param attributes are the attributes...serialize is free to add more. * @param value is the value * @param context is the SerializationContext */ public void serialize(QName name, Attributes attributes, Object value, SerializationContext context) throws IOException { // Check for meta-data in the bean that will tell us if any of the // properties are actually attributes, add those to the element // attribute list Attributes beanAttrs = getObjectAttributes(value, attributes, context); // Get the encoding style String encodingStyle = context.getEncodingStyle(); boolean isEncoded = Constants.isSOAP_ENC(encodingStyle); // check whether we have and xsd:any namespace="##any" type boolean suppressElement = !context.isEncoded() && name.getNamespaceURI().equals("") && name.getLocalPart().equals("any"); boolean grpElement = name.toString().endsWith("Group"); suppressElement |= grpElement; if (!suppressElement) context.startElement(name, beanAttrs); try { // Serialize each property for (int i = 0; i < propertyDescriptor.length; i++) { String propName = propertyDescriptor[i].getName(); if (propName.equals("class")) continue; QName qname = null; QName xmlType = null; boolean isOmittable = false; // If we have type metadata, check to see what we're doing // with this field. If it's an attribute, skip it. If it's // an element, use whatever qname is in there. If we can't // find any of this info, use the default. if (typeDesc != null) { FieldDesc field = typeDesc.getFieldByName(propName); if (field != null) { if (!field.isElement()) continue; // If we're SOAP encoded, just use the local part, // not the namespace. Otherwise use the whole // QName. if (isEncoded) { qname = new QName(field.getXmlName().getLocalPart()); } else { qname = field.getXmlName(); } isOmittable = field.isMinOccursZero(); xmlType = field.getXmlType(); } } if (qname == null) { qname = new QName(isEncoded ? "" : name.getNamespaceURI(), propName); } if (xmlType == null) { // look up the type QName using the class xmlType = context.getQNameForClass(propertyDescriptor[i].getType()); } // Read the value from the property if (propertyDescriptor[i].isReadable()) { if (!propertyDescriptor[i].isIndexed()) { // Normal case: serialize the value Object propValue = propertyDescriptor[i].get(value); // if meta data says minOccurs=0, then we can skip // it if its value is null and we aren't doing SOAP // encoding. if (propValue == null && isOmittable && !isEncoded) continue; if (null == propValue && qname.toString().endsWith("Group")) { System.err.println("\telemQName:" + qname + " contains 'Group' not appending nil"); continue; } context.serialize(qname, null, propValue, xmlType, true, null); } else { // Collection of properties: serialize each one int j = 0; while (j >= 0) { Object propValue = null; try { propValue = propertyDescriptor[i].get(value, j); j++; } catch (Exception e) { j = -1; } if (j >= 0) { context.serialize(qname, null, propValue, xmlType, true, null); } } } } } BeanPropertyDescriptor anyDesc = typeDesc == null ? null : typeDesc.getAnyDesc(); if (anyDesc != null) { // If we have "extra" content here, it'll be an array // of MessageElements. Serialize each one. Object anyVal = anyDesc.get(value); if (anyVal != null && anyVal instanceof MessageElement[]) { MessageElement[] anyContent = (MessageElement[]) anyVal; for (int i = 0; i < anyContent.length; i++) { MessageElement element = anyContent[i]; element.output(context); } } } } catch (InvocationTargetException ite) { Throwable target = ite.getTargetException(); log.error(Messages.getMessage("exception00"), target); throw new IOException(target.toString()); } catch (Exception e) { log.error(Messages.getMessage("exception00"), e); throw new IOException(e.toString()); } if (!suppressElement) context.endElement(); }
From source file:org.enhydra.shark.wfxml.util.BeanSerializerShark.java
/** * Serialize a bean. Done simply by serializing each bean property. * @param name is the element name// ww w . j a v a 2 s .c om * @param attributes are the attributes...serialize is free to add more. * @param value is the value * @param context is the SerializationContext */ public void serialize(QName name, Attributes attributes, Object value, SerializationContext context) throws IOException { // Check for meta-data in the bean that will tell us if any of the // properties are actually attributes, add those to the element // attribute list Attributes beanAttrs = getObjectAttributes(value, attributes, context); // Get the encoding style String encodingStyle = context.getEncodingStyle(); boolean isEncoded = Constants.isSOAP_ENC(encodingStyle); // check whether we have and xsd:any namespace="##any" type boolean suppressElement = !context.isEncoded() && name.getNamespaceURI().equals("") && name.getLocalPart().equals("any"); boolean grpElement = name.toString().endsWith("Group"); suppressElement |= grpElement; if (isEmptyObjectType(name)) { context.setWriteXMLType(null); } if (!suppressElement) context.startElement(name, beanAttrs); if (!isEmptyObjectType(name)) { try { // Serialize each property for (int i = 0; i < propertyDescriptor.length; i++) { String propName = propertyDescriptor[i].getName(); if (propName.equals("class")) continue; QName qname = null; QName xmlType = null; boolean isOmittable = false; // If we have type metadata, check to see what we're doing // with this field. If it's an attribute, skip it. If it's // an element, use whatever qname is in there. If we can't // find any of this info, use the default. if (typeDesc != null) { FieldDesc field = typeDesc.getFieldByName(propName); if (field != null) { if (!field.isElement()) continue; // If we're SOAP encoded, just use the local part, // not the namespace. Otherwise use the whole // QName. if (isEncoded) { qname = new QName(field.getXmlName().getLocalPart()); } else { qname = field.getXmlName(); } isOmittable = field.isMinOccursZero(); xmlType = field.getXmlType(); } } if (qname == null) { qname = new QName(isEncoded ? "" : name.getNamespaceURI(), propName); } if (xmlType == null) { // look up the type QName using the class xmlType = context.getQNameForClass(propertyDescriptor[i].getType()); } // Read the value from the property if (propertyDescriptor[i].isReadable()) { if (!propertyDescriptor[i].isIndexed()) { // Normal case: serialize the value Object propValue = propertyDescriptor[i].get(value); // if meta data says minOccurs=0, then we can skip // it if its value is null and we aren't doing SOAP // encoding. if (propValue == null && isOmittable && !isEncoded) continue; if (null == propValue && qname.toString().endsWith("Group")) { System.err.println("\telemQName:" + qname + " contains 'Group' not appending nil"); continue; } context.serialize(qname, null, propValue, xmlType, true, null); } else { // Collection of properties: serialize each one int j = 0; while (j >= 0) { Object propValue = null; try { propValue = propertyDescriptor[i].get(value, j); j++; } catch (Exception e) { j = -1; } if (j >= 0) { context.serialize(qname, null, propValue, xmlType, true, null); } } } } } BeanPropertyDescriptor anyDesc = typeDesc == null ? null : typeDesc.getAnyDesc(); if (anyDesc != null) { // If we have "extra" content here, it'll be an array // of MessageElements. Serialize each one. Object anyVal = anyDesc.get(value); if (anyVal != null && anyVal instanceof MessageElement[]) { MessageElement[] anyContent = (MessageElement[]) anyVal; for (int i = 0; i < anyContent.length; i++) { MessageElement element = anyContent[i]; element.output(context); } } } } catch (InvocationTargetException ite) { Throwable target = ite.getTargetException(); log.error(Messages.getMessage("exception00"), target); throw new IOException(target.toString()); } catch (Exception e) { log.error(Messages.getMessage("exception00"), e); throw new IOException(e.toString()); } } if (!suppressElement) context.endElement(); }
From source file:org.evergreen.web.utils.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//from w w w.j a v a 2 s . c om * @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); //? //((List) array).add(value); List list = (List) array; Type returnType = readMethod.getGenericReturnType(); Type acturalType = ((ParameterizedType) returnType).getActualTypeArguments()[0]; list.add(ConvertUtils.convert(value, (Class) acturalType)); } else if (array instanceof Set) { //?set?? //((Set) array).add(value); Set set = (Set) array; Type returnType = readMethod.getGenericReturnType(); Type acturalType = ((ParameterizedType) returnType).getActualTypeArguments()[0]; set.add(ConvertUtils.convert(value, (Class) acturalType)); } 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:org.fcrepo.server.security.xacml.pdp.decorator.PolicyIndexInvocationHandler.java
/** * Invoke the underlying method, catching any InvocationTargetException and rethrowing the target exception *//*from w w w .j a v a 2s .c o m*/ private Object invokeTarget(Object target, Method method, Object[] args) throws Throwable { Object returnValue; try { returnValue = method.invoke(target, args); } catch (InvocationTargetException ite) { throw ite.getTargetException(); } return returnValue; }
From source file:org.fornax.cartridges.sculptor.framework.event.DynamicMethodDispatcher.java
/** * Runtime dispatch to method with correct event parameter type *///from w w w. j a va 2 s . co m public static void dispatch(Object target, Event event, String methodName) { try { MethodUtils.invokeMethod(target, methodName, event); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof RuntimeException) { throw (RuntimeException) e.getTargetException(); } else { throw new UnsupportedOperationException(e.getTargetException()); } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new UnsupportedOperationException(e); } }
From source file:org.getobjects.foundation.kvc.PropertyAccessor.java
/** * * @throws MissingAccessorException//from w w w. j a v a2 s .c o m * if the class does not define an accessor method for the property. * */ public Object get(final Object _target, String key) { if (logger.isDebugEnabled()) logger.debug("Getting property " + this.getName() + " from " + _target); if (this.getter == null) { final String propertyName = this.getName(); throw new MissingAccessorException("Missing access for property '" + propertyName + "' on object of class " + _target.getClass() + ", accessor: " + this, _target, propertyName); } final Object result; try { result = this.getter.invoke(_target, (Object[]) null); } catch (RuntimeException e) { /* just reraise runtime exceptions */ throw e; } catch (java.lang.reflect.InvocationTargetException exx) { Throwable e = exx.getTargetException(); if (e instanceof RuntimeException) /* just reraise runtime exceptions */ throw ((RuntimeException) e); throw new DynamicInvocationException(this.getter, _target, e); } catch (java.lang.IllegalAccessException iae) { /** * TBD: This happens if we do KVC on a Collections.SingletonSet * (as returned by Collections.singleton(). The SingletonSet * is marked 'private' inside Collections, which is probably * why the call fails. * Though technically it shouldn't, I guess its a Java bug? * Maybe we need to lookup the method on the interface object, not on * the class. */ if (logger.isWarnEnabled()) { logger.warn("illegal access:\n" + " property: " + this.getName() + "\n" + " method: " + this.getter + "\n" + " target: " + _target.getClass(), iae); } throw new DynamicInvocationException(this.getter, _target, iae); } catch (Exception ex) { throw new DynamicInvocationException(this.getter, _target, ex); } return result; }
From source file:org.grails.orm.hibernate.proxy.GroovyAwareJavassistLazyInitializer.java
public Object invoke(final Object proxy, final Method thisMethod, final Method proceed, final Object[] args) throws Throwable { // while constructor is running if (thisMethod.getName().equals("getHibernateLazyInitializer")) { return this; }//from www.ja v a 2 s . c om Object result = groovyObjectMethodHandler.handleInvocation(proxy, thisMethod, args); if (groovyObjectMethodHandler.wasHandled(result)) { return result; } if (constructed) { try { result = invoke(thisMethod, args, proxy); } catch (Throwable t) { throw new Exception(t.getCause()); } if (result == INVOKE_IMPLEMENTATION) { Object target = getImplementation(); final Object returnValue; try { if (ReflectHelper.isPublic(persistentClass, thisMethod)) { if (!thisMethod.getDeclaringClass().isInstance(target)) { throw new ClassCastException(target.getClass().getName()); } returnValue = thisMethod.invoke(target, args); } else { if (!thisMethod.isAccessible()) { thisMethod.setAccessible(true); } returnValue = thisMethod.invoke(target, args); } return returnValue == target ? proxy : returnValue; } catch (InvocationTargetException ite) { throw ite.getTargetException(); } } return result; } return proceed.invoke(proxy, args); }
From source file:org.jgentleframework.integration.remoting.rmi.support.RmiBinderInterceptor.java
@Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { Remote stub = (Remote) this.binder.getStub(); MethodInvocation invocation = new BasicMethodInvocation(obj, method, args); Object result = null;//from ww w. j a v a 2s .c o m if (Modifier.isAbstract(method.getModifiers())) { try { result = this.binder.invoke(invocation, stub); } catch (InvocationTargetException ex) { Throwable targetEx = ex.getTargetException(); if (targetEx instanceof RemoteException) { RemoteException rex = (RemoteException) targetEx; throw ExceptionUtils.convertRmiAccessException(invocation.getMethod(), rex, this.binder.isConnectFailure(rex), this.binder.getServiceName()); } else { Throwable exToThrow = ex.getTargetException(); ExceptionUtils.fillInClientStackTraceIfPossible(exToThrow); throw exToThrow; } } catch (NoSuchMethodException ex) { throw new RemoteInvocationException("No matching RMI stub method found for: " + method, ex); } catch (RemoteException e) { if (this.binder.isConnectFailure(e) && this.binder.isRefreshStubOnConnectFailure()) { this.binder.refreshStubAndRetryInvocation(invocation); } else { throw new RemoteInvocationException("Invocation of method [" + invocation.getMethod() + "] failed in RMI service [" + this.binder.getServiceName() + "]", e); } } catch (Throwable ex) { if (log.isErrorEnabled()) { log.error("Invocation of method [" + invocation.getMethod() + "] failed in RMI service [" + this.binder.getServiceName() + "]", ex); } throw new RemoteInvocationException("Invocation of method [" + invocation.getMethod() + "] failed in RMI service [" + this.binder.getServiceName() + "]", ex); } } return result; }