use of java.lang.reflect.Method in project camel by apache.
the class MBeanInfoAssembler method doDoExtractAttributesAndOperations.
private void doDoExtractAttributesAndOperations(Class<?> managedClass, Map<String, ManagedAttributeInfo> attributes, Set<ManagedOperationInfo> operations) {
LOG.trace("Extracting attributes and operations from class: {}", managedClass);
// introspect the class, and leverage the cache to have better performance
IntrospectionSupport.ClassInfo cache = IntrospectionSupport.cacheClass(managedClass);
for (IntrospectionSupport.MethodInfo cacheInfo : cache.methods) {
// must be from declaring class
if (cacheInfo.method.getDeclaringClass() != managedClass) {
continue;
}
LOG.trace("Extracting attributes and operations from method: {}", cacheInfo.method);
ManagedAttribute ma = cacheInfo.method.getAnnotation(ManagedAttribute.class);
if (ma != null) {
String key;
String desc = ma.description();
Method getter = null;
Method setter = null;
boolean mask = ma.mask();
if (cacheInfo.isGetter) {
key = cacheInfo.getterOrSetterShorthandName;
getter = cacheInfo.method;
} else if (cacheInfo.isSetter) {
key = cacheInfo.getterOrSetterShorthandName;
setter = cacheInfo.method;
} else {
throw new IllegalArgumentException("@ManagedAttribute can only be used on Java bean methods, was: " + cacheInfo.method + " on bean: " + managedClass);
}
// they key must be capitalized
key = ObjectHelper.capitalize(key);
// lookup first
ManagedAttributeInfo info = attributes.get(key);
if (info == null) {
info = new ManagedAttributeInfo(key, desc);
}
if (getter != null) {
info.setGetter(getter);
}
if (setter != null) {
info.setSetter(setter);
}
info.setMask(mask);
attributes.put(key, info);
}
// operations
ManagedOperation mo = cacheInfo.method.getAnnotation(ManagedOperation.class);
if (mo != null) {
String desc = mo.description();
Method operation = cacheInfo.method;
boolean mask = mo.mask();
operations.add(new ManagedOperationInfo(desc, operation, mask));
}
}
}
use of java.lang.reflect.Method in project camel by apache.
the class IntrospectionSupport method doIntrospectClass.
private static ClassInfo doIntrospectClass(Class<?> clazz) {
ClassInfo answer = new ClassInfo();
answer.clazz = clazz;
// loop each method on the class and gather details about the method
// especially about getter/setters
List<MethodInfo> found = new ArrayList<MethodInfo>();
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (EXCLUDED_METHODS.contains(method)) {
continue;
}
MethodInfo cache = new MethodInfo();
cache.method = method;
if (isGetter(method)) {
cache.isGetter = true;
cache.isSetter = false;
cache.getterOrSetterShorthandName = getGetterShorthandName(method);
} else if (isSetter(method)) {
cache.isGetter = false;
cache.isSetter = true;
cache.getterOrSetterShorthandName = getSetterShorthandName(method);
} else {
cache.isGetter = false;
cache.isSetter = false;
cache.hasGetterAndSetter = false;
}
found.add(cache);
}
// so we have a read/write bean property.
for (MethodInfo info : found) {
info.hasGetterAndSetter = false;
if (info.isGetter) {
// loop and find the matching setter
for (MethodInfo info2 : found) {
if (info2.isSetter && info.getterOrSetterShorthandName.equals(info2.getterOrSetterShorthandName)) {
info.hasGetterAndSetter = true;
break;
}
}
} else if (info.isSetter) {
// loop and find the matching getter
for (MethodInfo info2 : found) {
if (info2.isGetter && info.getterOrSetterShorthandName.equals(info2.getterOrSetterShorthandName)) {
info.hasGetterAndSetter = true;
break;
}
}
}
}
answer.methods = found.toArray(new MethodInfo[found.size()]);
return answer;
}
use of java.lang.reflect.Method in project camel by apache.
the class IntrospectionSupport method getProperty.
public static Object getProperty(Object target, String property) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
ObjectHelper.notNull(target, "target");
ObjectHelper.notNull(property, "property");
property = property.substring(0, 1).toUpperCase(Locale.ENGLISH) + property.substring(1);
Class<?> clazz = target.getClass();
Method method = getPropertyGetter(clazz, property);
return method.invoke(target);
}
use of java.lang.reflect.Method in project camel by apache.
the class IntrospectionSupport method setProperty.
/**
* This method supports two modes to set a property:
*
* 1. Setting a property that has already been resolved, this is the case when {@code context} and {@code refName} are
* NULL and {@code value} is non-NULL.
*
* 2. Setting a property that has not yet been resolved, the property will be resolved based on the suitable methods
* found matching the property name on the {@code target} bean. For this mode to be triggered the parameters
* {@code context} and {@code refName} must NOT be NULL, and {@code value} MUST be NULL.
*
*/
public static boolean setProperty(CamelContext context, TypeConverter typeConverter, Object target, String name, Object value, String refName, boolean allowBuilderPattern) throws Exception {
Class<?> clazz = target.getClass();
Collection<Method> setters;
// we need to lookup the value from the registry
if (context != null && refName != null && value == null) {
setters = findSetterMethodsOrderedByParameterType(clazz, name, allowBuilderPattern);
} else {
// find candidates of setter methods as there can be overloaded setters
setters = findSetterMethods(clazz, name, value, allowBuilderPattern);
}
if (setters.isEmpty()) {
return false;
}
// loop and execute the best setter method
Exception typeConversionFailed = null;
for (Method setter : setters) {
Class<?> parameterType = setter.getParameterTypes()[0];
Object ref = value;
// try and lookup the reference based on the method
if (context != null && refName != null && ref == null) {
String s = StringHelper.replaceAll(refName, "#", "");
ref = CamelContextHelper.lookup(context, s);
if (ref == null) {
// try the next method if nothing was found
continue;
} else {
// setter method has not the correct type
// (must use ObjectHelper.isAssignableFrom which takes primitive types into account)
boolean assignable = isAssignableFrom(parameterType, ref.getClass());
if (!assignable) {
continue;
}
}
}
try {
try {
// If the type is null or it matches the needed type, just use the value directly
if (value == null || isAssignableFrom(parameterType, ref.getClass())) {
// we may want to set options on classes that has package view visibility, so override the accessible
setter.setAccessible(true);
setter.invoke(target, ref);
if (LOG.isDebugEnabled()) {
LOG.debug("Configured property: {} on bean: {} with value: {}", new Object[] { name, target, ref });
}
return true;
} else {
// We need to convert it
Object convertedValue = convert(typeConverter, parameterType, ref);
// we may want to set options on classes that has package view visibility, so override the accessible
setter.setAccessible(true);
setter.invoke(target, convertedValue);
if (LOG.isDebugEnabled()) {
LOG.debug("Configured property: {} on bean: {} with value: {}", new Object[] { name, target, ref });
}
return true;
}
} catch (InvocationTargetException e) {
// lets unwrap the exception
Throwable throwable = e.getCause();
if (throwable instanceof Exception) {
Exception exception = (Exception) throwable;
throw exception;
} else {
Error error = (Error) throwable;
throw error;
}
}
// ignore exceptions as there could be another setter method where we could type convert successfully
} catch (SecurityException e) {
typeConversionFailed = e;
} catch (NoTypeConversionAvailableException e) {
typeConversionFailed = e;
} catch (IllegalArgumentException e) {
typeConversionFailed = e;
}
if (LOG.isTraceEnabled()) {
LOG.trace("Setter \"{}\" with parameter type \"{}\" could not be used for type conversions of {}", new Object[] { setter, parameterType, ref });
}
}
if (typeConversionFailed != null && !isPropertyPlaceholder(context, value)) {
// this kind of exception as the caused by will hint this error
throw new IllegalArgumentException("Could not find a suitable setter for property: " + name + " as there isn't a setter method with same type: " + (value != null ? value.getClass().getCanonicalName() : "[null]") + " nor type conversion possible: " + typeConversionFailed.getMessage());
} else {
return false;
}
}
use of java.lang.reflect.Method in project camel by apache.
the class IntrospectionSupport method findSetterMethods.
public static Set<Method> findSetterMethods(Class<?> clazz, String name, boolean allowBuilderPattern) {
Set<Method> candidates = new LinkedHashSet<Method>();
// Build the method name.
name = "set" + ObjectHelper.capitalize(name);
while (clazz != Object.class) {
// Since Object.class.isInstance all the objects,
// here we just make sure it will be add to the bottom of the set.
Method objectSetMethod = null;
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (method.getName().equals(name) && isSetter(method, allowBuilderPattern)) {
Class<?>[] params = method.getParameterTypes();
if (params[0].equals(Object.class)) {
objectSetMethod = method;
} else {
candidates.add(method);
}
}
}
if (objectSetMethod != null) {
candidates.add(objectSetMethod);
}
clazz = clazz.getSuperclass();
}
return candidates;
}
Aggregations