use of java.lang.reflect.Method in project camel by apache.
the class AvroDataFormat method loadSchema.
protected Schema loadSchema(String className) throws CamelException, ClassNotFoundException {
// must use same class loading procedure to ensure working in OSGi
Class<?> instanceClass = camelContext.getClassResolver().resolveMandatoryClass(className);
Class<?> genericContainer = camelContext.getClassResolver().resolveMandatoryClass(GENERIC_CONTAINER_CLASSNAME);
if (genericContainer.isAssignableFrom(instanceClass)) {
try {
Method method = instanceClass.getMethod("getSchema");
return (Schema) method.invoke(camelContext.getInjector().newInstance(instanceClass));
} catch (Exception ex) {
throw new CamelException("Error calling getSchema on " + instanceClass, ex);
}
} else {
throw new CamelException("Class " + instanceClass + " must be instanceof " + GENERIC_CONTAINER_CLASSNAME);
}
}
use of java.lang.reflect.Method in project camel by apache.
the class BeanInfo method introspect.
/**
* Introspects the given class
*
* @param clazz the class
*/
private void introspect(Class<?> clazz) {
// get the target clazz as it could potentially have been enhanced by CGLIB etc.
clazz = getTargetClass(clazz);
ObjectHelper.notNull(clazz, "clazz", this);
LOG.trace("Introspecting class: {}", clazz);
// does the class have any public constructors?
publicConstructors = clazz.getConstructors().length > 0;
// favor declared methods, and then filter out duplicate interface methods
List<Method> methods;
if (Modifier.isPublic(clazz.getModifiers())) {
LOG.trace("Preferring class methods as class: {} is public accessible", clazz);
methods = new ArrayList<Method>(Arrays.asList(clazz.getDeclaredMethods()));
} else {
LOG.trace("Preferring interface methods as class: {} is not public accessible", clazz);
methods = getInterfaceMethods(clazz);
// and then we must add its declared methods as well
List<Method> extraMethods = Arrays.asList(clazz.getDeclaredMethods());
methods.addAll(extraMethods);
}
Set<Method> overrides = new HashSet<Method>();
// do not remove duplicates form class from the Java itself as they have some "duplicates" we need
boolean javaClass = clazz.getName().startsWith("java.") || clazz.getName().startsWith("javax.");
if (!javaClass) {
// it may have duplicate methods already, even from declared or from interfaces + declared
for (Method source : methods) {
// skip bridge methods in duplicate checks (as the bridge method is inserted by the compiler due to type erasure)
if (source.isBridge()) {
continue;
}
for (Method target : methods) {
// skip ourselves
if (ObjectHelper.isOverridingMethod(source, target, true)) {
continue;
}
// skip duplicates which may be assign compatible (favor keep first added method when duplicate)
if (ObjectHelper.isOverridingMethod(source, target, false)) {
overrides.add(target);
}
}
}
methods.removeAll(overrides);
overrides.clear();
}
// if we are a public class, then add non duplicate interface classes also
if (Modifier.isPublic(clazz.getModifiers())) {
// add additional interface methods
List<Method> extraMethods = getInterfaceMethods(clazz);
for (Method source : extraMethods) {
for (Method target : methods) {
if (ObjectHelper.isOverridingMethod(source, target, false)) {
overrides.add(source);
}
}
for (Method target : methodMap.keySet()) {
if (ObjectHelper.isOverridingMethod(source, target, false)) {
overrides.add(source);
}
}
}
// remove all the overrides methods
extraMethods.removeAll(overrides);
methods.addAll(extraMethods);
}
// now introspect the methods and filter non valid methods
for (Method method : methods) {
boolean valid = isValidMethod(clazz, method);
LOG.trace("Method: {} is valid: {}", method, valid);
if (valid) {
introspect(clazz, method);
}
}
Class<?> superclass = clazz.getSuperclass();
if (superclass != null && !superclass.equals(Object.class)) {
introspect(superclass);
}
}
use of java.lang.reflect.Method in project camel by apache.
the class BeanInfo method findMostSpecificOverride.
/**
* Gets the most specific override of a given method, if any. Indeed,
* overrides may have already been found while inspecting sub classes. Or
* the given method could override an interface extra method.
*
* @param proposedMethodInfo the method for which a more specific override is
* searched
* @return The already registered most specific override if any, otherwise
* <code>null</code>
*/
private MethodInfo findMostSpecificOverride(MethodInfo proposedMethodInfo) {
for (MethodInfo alreadyRegisteredMethodInfo : methodMap.values()) {
Method alreadyRegisteredMethod = alreadyRegisteredMethodInfo.getMethod();
Method proposedMethod = proposedMethodInfo.getMethod();
if (ObjectHelper.isOverridingMethod(proposedMethod, alreadyRegisteredMethod, false)) {
return alreadyRegisteredMethodInfo;
} else if (ObjectHelper.isOverridingMethod(alreadyRegisteredMethod, proposedMethod, false)) {
return proposedMethodInfo;
}
}
return null;
}
use of java.lang.reflect.Method in project camel by apache.
the class DefaultAnnotationExpressionFactory method getAnnotationObjectValue.
/**
* @param annotation The annotation to get the value of
* @param methodName The annotation name
* @return The value of the annotation
*/
protected Object getAnnotationObjectValue(Annotation annotation, String methodName) {
try {
Method method = annotation.getClass().getMethod(methodName);
Object value = ObjectHelper.invokeMethod(method, annotation);
return value;
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("Cannot determine the Object value of the annotation: " + annotation + " as it does not have the method: " + methodName + "() method", e);
}
}
use of java.lang.reflect.Method in project camel by apache.
the class AggregationStrategyBeanAdapter method doStart.
@Override
protected void doStart() throws Exception {
Method found = null;
if (methodName != null) {
for (Method method : type.getMethods()) {
if (isValidMethod(method) && method.getName().equals(methodName)) {
if (found == null) {
found = method;
} else {
throw new IllegalArgumentException("The bean " + type + " has 2 or more methods with the name " + methodName);
}
}
}
} else {
for (Method method : type.getMethods()) {
if (isValidMethod(method)) {
if (found == null) {
found = method;
} else {
throw new IllegalArgumentException("The bean " + type + " has 2 or more methods and no explicit method name was configured.");
}
}
}
}
if (found == null) {
throw new UnsupportedOperationException("Cannot find a valid method with name: " + methodName + " on bean type: " + type);
}
// if its not a static method then we must have an instance of the pojo
if (!isStaticMethod(found) && pojo == null) {
pojo = camelContext.getInjector().newInstance(type);
}
// create the method info which has adapted to the pojo
AggregationStrategyBeanInfo bi = new AggregationStrategyBeanInfo(type, found);
mi = bi.createMethodInfo();
// in case the POJO is CamelContextAware
if (pojo != null && pojo instanceof CamelContextAware) {
((CamelContextAware) pojo).setCamelContext(getCamelContext());
}
// in case the pojo is a service
ServiceHelper.startService(pojo);
}
Aggregations