public void testExcludedMethod() throws Exception {
    BeanInfo info = new BeanInfo(context, MyDummyBean.class);

    Exchange exchange = new DefaultExchange(context);
    MyDummyBean pojo = new MyDummyBean();
    MethodInvocation mi = info.createInvocation(pojo, exchange);
    assertNull("Should not be possible to find a suitable method", mi);
  }
  public void testNotExcludedMethod() throws Exception {
    BeanInfo info = new BeanInfo(context, MyOtherDummyBean.class);

    Exchange exchange = new DefaultExchange(context);
    MyOtherDummyBean pojo = new MyOtherDummyBean();
    MethodInvocation mi = info.createInvocation(pojo, exchange);
    assertNotNull(mi);
    assertEquals("hello", mi.getMethod().getName());
  }
Exemple #3
0
 /**
  * Returns the {@link MethodInfo} for the given method if it exists or null if there is no
  * metadata available for the given method
  */
 public MethodInfo getMethodInfo(Method method) {
   MethodInfo answer = methodMap.get(method);
   if (answer == null) {
     // maybe the method is defined on a base class?
     if (type != Object.class) {
       Class<?> superclass = type.getSuperclass();
       if (superclass != null && superclass != Object.class) {
         BeanInfo superBeanInfo = new BeanInfo(camelContext, superclass, strategy);
         return superBeanInfo.getMethodInfo(method);
       }
     }
   }
   return answer;
 }
Exemple #4
0
  /**
   * Returns the {@link MethodInfo} for the given method if it exists or null if there is no
   * metadata available for the given method
   */
  public MethodInfo getMethodInfo(Method method) {
    MethodInfo answer = methodMap.get(method);
    if (answer == null) {
      // maybe the method overrides, and the method map keeps info of the source override we can use
      for (Method source : methodMap.keySet()) {
        if (ObjectHelper.isOverridingMethod(source, method, false)) {
          answer = methodMap.get(source);
          break;
        }
      }
    }

    if (answer == null) {
      // maybe the method is defined on a base class?
      if (type != Object.class) {
        Class<?> superclass = type.getSuperclass();
        if (superclass != null && superclass != Object.class) {
          BeanInfo superBeanInfo = new BeanInfo(camelContext, superclass, strategy);
          return superBeanInfo.getMethodInfo(method);
        }
      }
    }
    return answer;
  }
Exemple #5
0
  @Override
  public Processor createProcessor(RouteContext routeContext) throws Exception {
    BeanProcessor answer;
    Class<?> clazz = bean != null ? bean.getClass() : null;
    BeanHolder beanHolder;

    if (ObjectHelper.isNotEmpty(ref)) {
      if (cache != null && cache) {
        // cache the registry lookup which avoids repeat lookup in the registry
        beanHolder = new RegistryBean(routeContext.getCamelContext(), ref).createCacheHolder();
      } else {
        beanHolder = new RegistryBean(routeContext.getCamelContext(), ref);
      }
      // bean holder will check if the bean exists
      bean = beanHolder.getBean();
      answer = new BeanProcessor(beanHolder);
    } else {
      if (bean == null) {

        if (beanType == null && beanClass == null) {
          throw new IllegalArgumentException("bean, ref or beanType must be provided");
        }

        // the clazz is either from beanType or beanClass
        if (beanType != null) {
          try {
            clazz =
                routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(beanType);
          } catch (ClassNotFoundException e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
          }
        } else {
          clazz = beanClass;
        }

        // create a bean if there is a default public no-arg constructor
        if (ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
          bean = CamelContextHelper.newInstance(routeContext.getCamelContext(), clazz);
          ObjectHelper.notNull(bean, "bean", this);
        }
      }

      // validate the bean type is not from java so you by mistake think its a reference
      // to a bean name but the String is being invoke instead
      if (bean instanceof String) {
        throw new IllegalArgumentException(
            "The bean instance is a java.lang.String type: "
                + bean
                + ". We suppose you want to refer to a bean instance by its id instead. Please use beanRef.");
      }

      // the holder should either be bean or type based
      beanHolder =
          bean != null
              ? new ConstantBeanHolder(bean, routeContext.getCamelContext())
              : new ConstantTypeBeanHolder(clazz, routeContext.getCamelContext());
      answer = new BeanProcessor(beanHolder);
    }

    // check for method exists
    if (method != null) {
      answer.setMethod(method);

      // check there is a method with the given name, and leverage BeanInfo for that
      BeanInfo beanInfo = beanHolder.getBeanInfo();
      if (bean != null) {
        // there is a bean instance, so check for any methods
        if (!beanInfo.hasMethod(method)) {
          throw ObjectHelper.wrapRuntimeCamelException(
              new MethodNotFoundException(null, bean, method));
        }
      } else if (clazz != null) {
        // there is no bean instance, so check for static methods only
        if (!beanInfo.hasStaticMethod(method)) {
          throw ObjectHelper.wrapRuntimeCamelException(
              new MethodNotFoundException(null, clazz, method, true));
        }
      }
    }

    return answer;
  }