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;
}
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;
}
}
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;
}
Aggregations