Package org.springframework.beans

Examples of org.springframework.beans.TypeConverter


    if (value != null) {
      if (value instanceof String) {
        String strVal = resolveEmbeddedValue((String) value);
        value = evaluateBeanDefinitionString(strVal, getMergedBeanDefinition(beanName));
      }
      TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
      return converter.convertIfNecessary(value, type);
    }

    if (type.isArray()) {
      Class componentType = type.getComponentType();
      Map<String, Object> matchingBeans = findAutowireCandidates(beanName, componentType, descriptor);
      if (matchingBeans.isEmpty()) {
        if (descriptor.isRequired()) {
          raiseNoSuchBeanDefinitionException(componentType, "array of " + componentType.getName(), descriptor);
        }
        return null;
      }
      if (autowiredBeanNames != null) {
        autowiredBeanNames.addAll(matchingBeans.keySet());
      }
      TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
      return converter.convertIfNecessary(matchingBeans.values(), type);
    }
    else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
      Class elementType = descriptor.getCollectionType();
      if (elementType == null) {
        if (descriptor.isRequired()) {
          throw new FatalBeanException("No element type declared for collection [" + type.getName() + "]");
        }
        return null;
      }
      Map<String, Object> matchingBeans = findAutowireCandidates(beanName, elementType, descriptor);
      if (matchingBeans.isEmpty()) {
        if (descriptor.isRequired()) {
          raiseNoSuchBeanDefinitionException(elementType, "collection of " + elementType.getName(), descriptor);
        }
        return null;
      }
      if (autowiredBeanNames != null) {
        autowiredBeanNames.addAll(matchingBeans.keySet());
      }
      TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
      return converter.convertIfNecessary(matchingBeans.values(), type);
    }
    else if (Map.class.isAssignableFrom(type) && type.isInterface()) {
      Class keyType = descriptor.getMapKeyType();
      if (keyType == null || !String.class.isAssignableFrom(keyType)) {
        if (descriptor.isRequired()) {
View Full Code Here


  protected TypeConverter getCustomTypeConverter() {
    return this.typeConverter;
  }

  public TypeConverter getTypeConverter() {
    TypeConverter customConverter = getCustomTypeConverter();
    if (customConverter != null) {
      return customConverter;
    }
    else {
      // Build default TypeConverter, registering custom editors.
View Full Code Here

   * @param propertyEditor editor to register
   * @see #setTypeConverter
   * @see org.springframework.beans.PropertyEditorRegistry#registerCustomEditor
   */
  public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor) {
    TypeConverter converter = getTypeConverter();
    if (!(converter instanceof PropertyEditorRegistry)) {
      throw new IllegalStateException(
          "TypeConverter does not implement PropertyEditorRegistry interface: " + converter);
    }
    ((PropertyEditorRegistry) converter).registerCustomEditor(requiredType, propertyEditor);
View Full Code Here

   * argument value is assignable to the corresponding parameter type.
   * @param arguments the argument values to match against method parameters
   * @return a matching method, or <code>null</code> if none
   */
  protected Method doFindMatchingMethod(Object[] arguments) {
    TypeConverter converter = getTypeConverter();
    if (converter != null) {
      String targetMethod = getTargetMethod();
      Method matchingMethod = null;
      int argCount = arguments.length;
      Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass());
      int minTypeDiffWeight = Integer.MAX_VALUE;
      Object[] argumentsToUse = null;
      for (Method candidate : candidates) {
        if (candidate.getName().equals(targetMethod)) {
          // Check if the inspected method has the correct number of parameters.
          Class[] paramTypes = candidate.getParameterTypes();
          if (paramTypes.length == argCount) {
            Object[] convertedArguments = new Object[argCount];
            boolean match = true;
            for (int j = 0; j < argCount && match; j++) {
              // Verify that the supplied argument is assignable to the method parameter.
              try {
                convertedArguments[j] = converter.convertIfNecessary(arguments[j], paramTypes[j]);
              }
              catch (TypeMismatchException ex) {
                // Ignore -> simply doesn't match.
                match = false;
              }
View Full Code Here

   * Resolve the specified cached method argument or field value.
   */
  private Object resolvedCachedArgument(String beanName, Object cachedArgument) {
    if (cachedArgument instanceof DependencyDescriptor) {
      DependencyDescriptor descriptor = (DependencyDescriptor) cachedArgument;
      TypeConverter typeConverter = beanFactory.getTypeConverter();
      return beanFactory.resolveDependency(descriptor, beanName, null, typeConverter);
    }
    else if (cachedArgument instanceof RuntimeBeanReference) {
      return beanFactory.getBean(((RuntimeBeanReference) cachedArgument).getBeanName());
    }
View Full Code Here

          value = resolvedCachedArgument(beanName, this.cachedFieldValue);
        }
        else {
          DependencyDescriptor descriptor = new DependencyDescriptor(field, this.required);
          Set<String> autowiredBeanNames = new LinkedHashSet<String>(1);
          TypeConverter typeConverter = beanFactory.getTypeConverter();
          value = beanFactory.resolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter);
          synchronized (this) {
            if (!this.cached) {
              if (value != null || this.required) {
                this.cachedFieldValue = descriptor;
View Full Code Here

        else {
          Class<?>[] paramTypes = method.getParameterTypes();
          arguments = new Object[paramTypes.length];
          DependencyDescriptor[] descriptors = new DependencyDescriptor[paramTypes.length];
          Set<String> autowiredBeanNames = new LinkedHashSet<String>(paramTypes.length);
          TypeConverter typeConverter = beanFactory.getTypeConverter();
          for (int i = 0; i < arguments.length; i++) {
            MethodParameter methodParam = new MethodParameter(method, i);
            GenericTypeResolver.resolveParameterType(methodParam, bean.getClass());
            descriptors[i] = new DependencyDescriptor(methodParam, this.required);
            arguments[i] = beanFactory.resolveDependency(
View Full Code Here

   * @param pvs the PropertyValues to register wired objects with
   */
  protected void autowireByType(
      String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {

    TypeConverter converter = getCustomTypeConverter();
    if (converter == null) {
      converter = bw;
    }

    Set<String> autowiredBeanNames = new LinkedHashSet<String>(4);
View Full Code Here

    }
    else {
      original = Arrays.asList(pvs.getPropertyValues());
    }

    TypeConverter converter = getCustomTypeConverter();
    if (converter == null) {
      converter = bw;
    }
    BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter);
View Full Code Here

      if (value instanceof String) {
        String strVal = resolveEmbeddedValue((String) value);
        BeanDefinition bd = (beanName != null && containsBean(beanName) ? getMergedBeanDefinition(beanName) : null);
        value = evaluateBeanDefinitionString(strVal, bd);
      }
      TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
      return converter.convertIfNecessary(value, type);
    }

    if (type.isArray()) {
      Class componentType = type.getComponentType();
      Map<String, Object> matchingBeans = findAutowireCandidates(beanName, componentType, descriptor);
      if (matchingBeans.isEmpty()) {
        if (descriptor.isRequired()) {
          raiseNoSuchBeanDefinitionException(componentType, "array of " + componentType.getName(), descriptor);
        }
        return null;
      }
      if (autowiredBeanNames != null) {
        autowiredBeanNames.addAll(matchingBeans.keySet());
      }
      TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
      return converter.convertIfNecessary(matchingBeans.values(), type);
    }
    else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
      Class elementType = descriptor.getCollectionType();
      if (elementType == null) {
        if (descriptor.isRequired()) {
          throw new FatalBeanException("No element type declared for collection [" + type.getName() + "]");
        }
        return null;
      }
      Map<String, Object> matchingBeans = findAutowireCandidates(beanName, elementType, descriptor);
      if (matchingBeans.isEmpty()) {
        if (descriptor.isRequired()) {
          raiseNoSuchBeanDefinitionException(elementType, "collection of " + elementType.getName(), descriptor);
        }
        return null;
      }
      if (autowiredBeanNames != null) {
        autowiredBeanNames.addAll(matchingBeans.keySet());
      }
      TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
      return converter.convertIfNecessary(matchingBeans.values(), type);
    }
    else if (Map.class.isAssignableFrom(type) && type.isInterface()) {
      Class keyType = descriptor.getMapKeyType();
      if (keyType == null || !String.class.isAssignableFrom(keyType)) {
        if (descriptor.isRequired()) {
View Full Code Here

TOP

Related Classes of org.springframework.beans.TypeConverter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.