Search in sources :

Example 1 with ReflectionException

use of com.opensymphony.xwork2.util.reflection.ReflectionException in project struts by apache.

the class DefaultObjectTypeDeterminer method getClass.

/**
 * Returns the class for the given field via generic type check.
 *
 * @param parentClass the Class which contains as a property the Map or Collection we are finding the key for.
 * @param property    the property of the Map or Collection for the given parent class
 * @param element     <tt>true</tt> for indexed types and Maps.
 * @return Class of the specified field.
 */
private Class getClass(Class parentClass, String property, boolean element) {
    try {
        Field field = reflectionProvider.getField(parentClass, property);
        Type genericType = null;
        // Check fields first
        if (field != null) {
            genericType = field.getGenericType();
        }
        // Try to get ParameterType from setter method
        if (genericType == null || !(genericType instanceof ParameterizedType)) {
            try {
                Method setter = reflectionProvider.getSetMethod(parentClass, property);
                genericType = setter != null ? setter.getGenericParameterTypes()[0] : null;
            } catch (ReflectionException | IntrospectionException e) {
            // ignore
            }
        }
        // Try to get ReturnType from getter method
        if (genericType == null || !(genericType instanceof ParameterizedType)) {
            try {
                Method getter = reflectionProvider.getGetMethod(parentClass, property);
                genericType = getter.getGenericReturnType();
            } catch (ReflectionException | IntrospectionException e) {
            // ignore
            }
        }
        if (genericType instanceof ParameterizedType) {
            ParameterizedType type = (ParameterizedType) genericType;
            int index = (element && type.getRawType().toString().contains(Map.class.getName())) ? 1 : 0;
            Type resultType = type.getActualTypeArguments()[index];
            if (resultType instanceof ParameterizedType) {
                return (Class) ((ParameterizedType) resultType).getRawType();
            }
            return (Class) resultType;
        }
    } catch (Exception e) {
        LOG.debug("Error while retrieving generic property class for property: {}", property, e);
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) ReflectionException(com.opensymphony.xwork2.util.reflection.ReflectionException) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) IntrospectionException(java.beans.IntrospectionException) ReflectionException(com.opensymphony.xwork2.util.reflection.ReflectionException)

Example 2 with ReflectionException

use of com.opensymphony.xwork2.util.reflection.ReflectionException in project struts by apache.

the class ShowValidatorAction method execute.

public String execute() throws Exception {
    loadValidators();
    Validator validator = getSelectedValidator();
    properties = new TreeSet<PropertyInfo>();
    try {
        Map<String, Object> context = reflectionContextFactory.createDefaultContext(validator);
        BeanInfo beanInfoFrom;
        try {
            beanInfoFrom = Introspector.getBeanInfo(validator.getClass(), Object.class);
        } catch (IntrospectionException e) {
            LOG.error("An error occurred", e);
            addActionError("An error occurred while introspecting a validator of type " + validator.getClass().getName());
            return ERROR;
        }
        PropertyDescriptor[] pds = beanInfoFrom.getPropertyDescriptors();
        for (PropertyDescriptor pd : pds) {
            String name = pd.getName();
            Object value = null;
            if (pd.getReadMethod() == null) {
                value = "No read method for property";
            } else {
                try {
                    value = reflectionProvider.getValue(name, context, validator);
                } catch (ReflectionException e) {
                    addActionError("Caught exception while getting property value for '" + name + "' on validator of type " + validator.getClass().getName());
                }
            }
            properties.add(new PropertyInfo(name, pd.getPropertyType(), value));
        }
    } catch (Exception e) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("Unable to retrieve properties.", e);
        }
        addActionError("Unable to retrieve properties: " + e.toString());
    }
    if (hasErrors()) {
        return ERROR;
    } else {
        return SUCCESS;
    }
}
Also used : ReflectionException(com.opensymphony.xwork2.util.reflection.ReflectionException) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Validator(com.opensymphony.xwork2.validator.Validator) IntrospectionException(java.beans.IntrospectionException) ReflectionException(com.opensymphony.xwork2.util.reflection.ReflectionException)

Example 3 with ReflectionException

use of com.opensymphony.xwork2.util.reflection.ReflectionException in project struts by apache.

the class DefaultResultFactory method buildResult.

public Result buildResult(ResultConfig resultConfig, Map<String, Object> extraContext) throws Exception {
    String resultClassName = resultConfig.getClassName();
    Result result = null;
    if (resultClassName != null) {
        result = (Result) objectFactory.buildBean(resultClassName, extraContext);
        Map<String, String> params = resultConfig.getParams();
        if (params != null) {
            for (Map.Entry<String, String> paramEntry : params.entrySet()) {
                try {
                    reflectionProvider.setProperty(paramEntry.getKey(), paramEntry.getValue(), result, extraContext, true);
                } catch (ReflectionException ex) {
                    if (result instanceof ReflectionExceptionHandler) {
                        ((ReflectionExceptionHandler) result).handle(ex);
                    }
                }
            }
        }
    }
    return result;
}
Also used : ReflectionException(com.opensymphony.xwork2.util.reflection.ReflectionException) ReflectionExceptionHandler(com.opensymphony.xwork2.util.reflection.ReflectionExceptionHandler) Map(java.util.Map) Result(com.opensymphony.xwork2.Result)

Aggregations

ReflectionException (com.opensymphony.xwork2.util.reflection.ReflectionException)3 IntrospectionException (java.beans.IntrospectionException)2 Result (com.opensymphony.xwork2.Result)1 ReflectionExceptionHandler (com.opensymphony.xwork2.util.reflection.ReflectionExceptionHandler)1 Validator (com.opensymphony.xwork2.validator.Validator)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Map (java.util.Map)1