コード例 #1
0
 private Method findMethod(Class<?> actualClass, String createMethod) {
   Method method = null;
   try {
     method = ReflectionUtils.getMethod(actualClass, createMethod, null);
   } catch (NoSuchMethodException e) {
     MappingUtils.throwMappingException(e);
   }
   return method;
 }
コード例 #2
0
    public Object create(BeanCreationDirective directive) {
      Class<?> actualClass = directive.getActualClass();
      String createMethod = directive.getCreateMethod();

      Method method;
      if (createMethod.contains(".")) {
        String methodName =
            createMethod.substring(createMethod.lastIndexOf(".") + 1, createMethod.length());
        String typeName = createMethod.substring(0, createMethod.lastIndexOf("."));
        DozerClassLoader loader = BeanContainer.getInstance().getClassLoader();
        Class type = loader.loadClass(typeName);
        method = findMethod(type, methodName);
      } else {
        method = findMethod(actualClass, createMethod);
      }
      return ReflectionUtils.invoke(method, null, null);
    }
コード例 #3
0
    public Object create(BeanCreationDirective directive) {
      Class<?> classToCreate = directive.getActualClass();
      String factoryName = directive.getFactoryName();
      String factoryBeanId = directive.getFactoryId();

      // By default, use dest object class name for factory bean id
      String beanId =
          !MappingUtils.isBlankOrNull(factoryBeanId) ? factoryBeanId : classToCreate.getName();

      BeanFactory factory = factoryCache.get(factoryName);
      if (factory == null) {
        Class<?> factoryClass = MappingUtils.loadClass(factoryName);
        if (!BeanFactory.class.isAssignableFrom(factoryClass)) {
          MappingUtils.throwMappingException(
              "Custom bean factory must implement "
                  + BeanFactory.class.getName()
                  + " interface : "
                  + factoryClass);
        }
        factory = (BeanFactory) ReflectionUtils.newInstance(factoryClass);
        // put the created factory in our factory map
        factoryCache.put(factoryName, factory);
      }

      Object result = factory.createBean(directive.getSrcObject(), directive.getSrcClass(), beanId);

      log.debug(
          "Bean instance created with custom factory -->\n  Bean Type: {}\n  Factory Name: {}",
          result.getClass().getName(),
          factoryName);

      if (!classToCreate.isAssignableFrom(result.getClass())) {
        MappingUtils.throwMappingException(
            "Custom bean factory ("
                + factory.getClass()
                + ") did not return correct type of destination data object. Expected : "
                + classToCreate
                + ", Actual : "
                + result.getClass());
      }
      return result;
    }