Package org.springframework.beans

Examples of org.springframework.beans.TypeConverter


        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);
            DependencyDescriptor desc = new DependencyDescriptor(methodParam, this.required);
            desc.setContainingClass(bean.getClass());
            descriptors[i] = desc;
View Full Code Here


    Class<?> valueType = null;
    if (this.targetListClass != null) {
      valueType = GenericCollectionTypeResolver.getCollectionType(this.targetListClass);
    }
    if (valueType != null) {
      TypeConverter converter = getBeanTypeConverter();
      for (Object elem : this.sourceList) {
        result.add(converter.convertIfNecessary(elem, valueType));
      }
    }
    else {
      result.addAll(this.sourceList);
    }
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

   * <p>This method is also used for handling invocations of static factory methods.
   */
  private int resolveConstructorArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw,
      ConstructorArgumentValues cargs, ConstructorArgumentValues resolvedValues) {

    TypeConverter converter = (this.beanFactory.getCustomTypeConverter() != null ?
        this.beanFactory.getCustomTypeConverter() : bw);
    BeanDefinitionValueResolver valueResolver =
        new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);

    int minNrOfArgs = cargs.getArgumentCount();
View Full Code Here

      String beanName, RootBeanDefinition mbd, ConstructorArgumentValues resolvedValues,
      BeanWrapper bw, Class<?>[] paramTypes, String[] paramNames, Object methodOrCtor,
      boolean autowiring) throws UnsatisfiedDependencyException {

    String methodType = (methodOrCtor instanceof Constructor ? "constructor" : "factory method");
    TypeConverter converter = (this.beanFactory.getCustomTypeConverter() != null ?
        this.beanFactory.getCustomTypeConverter() : bw);

    ArgumentsHolder args = new ArgumentsHolder(paramTypes.length);
    Set<ConstructorArgumentValues.ValueHolder> usedValueHolders =
        new HashSet<ConstructorArgumentValues.ValueHolder>(paramTypes.length);
    Set<String> autowiredBeanNames = new LinkedHashSet<String>(4);

    for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) {
      Class<?> paramType = paramTypes[paramIndex];
      String paramName = (paramNames != null ? paramNames[paramIndex] : null);
      // Try to find matching constructor argument value, either indexed or generic.
      ConstructorArgumentValues.ValueHolder valueHolder =
          resolvedValues.getArgumentValue(paramIndex, paramType, paramName, usedValueHolders);
      // If we couldn't find a direct match and are not supposed to autowire,
      // let's try the next generic, untyped argument value as fallback:
      // it could match after type conversion (for example, String -> int).
      if (valueHolder == null && !autowiring) {
        valueHolder = resolvedValues.getGenericArgumentValue(null, null, usedValueHolders);
      }
      if (valueHolder != null) {
        // We found a potential match - let's give it a try.
        // Do not consider the same value definition multiple times!
        usedValueHolders.add(valueHolder);
        Object originalValue = valueHolder.getValue();
        Object convertedValue;
        if (valueHolder.isConverted()) {
          convertedValue = valueHolder.getConvertedValue();
          args.preparedArguments[paramIndex] = convertedValue;
        }
        else {
          ConstructorArgumentValues.ValueHolder sourceHolder =
              (ConstructorArgumentValues.ValueHolder) valueHolder.getSource();
          Object sourceValue = sourceHolder.getValue();
          try {
            convertedValue = converter.convertIfNecessary(originalValue, paramType,
                MethodParameter.forMethodOrConstructor(methodOrCtor, paramIndex));
            // TODO re-enable once race condition has been found (SPR-7423)
            /*
            if (originalValue == sourceValue || sourceValue instanceof TypedStringValue) {
              // Either a converted value or still the original one: store converted value.
View Full Code Here

  private Object[] resolvePreparedArguments(
      String beanName, RootBeanDefinition mbd, BeanWrapper bw, Member methodOrCtor, Object[] argsToResolve) {

    Class<?>[] paramTypes = (methodOrCtor instanceof Method ?
        ((Method) methodOrCtor).getParameterTypes() : ((Constructor<?>) methodOrCtor).getParameterTypes());
    TypeConverter converter = (this.beanFactory.getCustomTypeConverter() != null ?
        this.beanFactory.getCustomTypeConverter() : bw);
    BeanDefinitionValueResolver valueResolver =
        new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);
    Object[] resolvedArgs = new Object[argsToResolve.length];
    for (int argIndex = 0; argIndex < argsToResolve.length; argIndex++) {
      Object argValue = argsToResolve[argIndex];
      MethodParameter methodParam = MethodParameter.forMethodOrConstructor(methodOrCtor, argIndex);
      GenericTypeResolver.resolveParameterType(methodParam, methodOrCtor.getDeclaringClass());
      if (argValue instanceof AutowiredArgumentMarker) {
        argValue = resolveAutowiredArgument(methodParam, beanName, null, converter);
      }
      else if (argValue instanceof BeanMetadataElement) {
        argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
      }
      else if (argValue instanceof String) {
        argValue = this.beanFactory.evaluateBeanDefinitionString((String) argValue, mbd);
      }
      Class<?> paramType = paramTypes[argIndex];
      try {
        resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam);
      }
      catch (TypeMismatchException ex) {
        String methodType = (methodOrCtor instanceof Constructor ? "constructor" : "factory method");
        throw new UnsatisfiedDependencyException(
            mbd.getResourceDescription(), beanName, argIndex, paramType,
View Full Code Here

    if (this.targetMapClass != null) {
      keyType = GenericCollectionTypeResolver.getMapKeyType(this.targetMapClass);
      valueType = GenericCollectionTypeResolver.getMapValueType(this.targetMapClass);
    }
    if (keyType != null || valueType != null) {
      TypeConverter converter = getBeanTypeConverter();
      for (Map.Entry<?, ?> entry : this.sourceMap.entrySet()) {
        Object convertedKey = converter.convertIfNecessary(entry.getKey(), keyType);
        Object convertedValue = converter.convertIfNecessary(entry.getValue(), valueType);
        result.put(convertedKey, convertedValue);
      }
    }
    else {
      result.putAll(this.sourceMap);
View Full Code Here

      this.jndiObject = JndiObjectProxyFactory.createJndiObjectProxy(this);
    }
    else {
      if (this.defaultObject != null && getExpectedType() != null &&
          !getExpectedType().isInstance(this.defaultObject)) {
        TypeConverter converter = (this.beanFactory != null ?
            this.beanFactory.getTypeConverter() : new SimpleTypeConverter());
        try {
          this.defaultObject = converter.convertIfNecessary(this.defaultObject, getExpectedType());
        }
        catch (TypeMismatchException ex) {
          throw new IllegalArgumentException("Default object [" + this.defaultObject + "] of type [" +
              this.defaultObject.getClass().getName() + "] is not of expected type [" +
              getExpectedType().getName() + "] and cannot be converted either", ex);
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 (descriptor.getField() != null ?
          converter.convertIfNecessary(value, type, descriptor.getField()) :
          converter.convertIfNecessary(value, type, descriptor.getMethodParameter()));
    }

    if (type.isArray()) {
      Class<?> componentType = type.getComponentType();
      DependencyDescriptor targetDesc = new DependencyDescriptor(descriptor);
      targetDesc.increaseNestingLevel();
      Map<String, Object> matchingBeans = findAutowireCandidates(beanName, componentType, targetDesc);
      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());
      Object result = converter.convertIfNecessary(matchingBeans.values(), type);
      if (getDependencyComparator() != null && result instanceof Object[]) {
        Arrays.sort((Object[]) result, adaptDependencyComparator(matchingBeans));
      }
      return result;
    }
    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;
      }
      DependencyDescriptor targetDesc = new DependencyDescriptor(descriptor);
      targetDesc.increaseNestingLevel();
      Map<String, Object> matchingBeans = findAutowireCandidates(beanName, elementType, targetDesc);
      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());
      Object result = converter.convertIfNecessary(matchingBeans.values(), type);
      if (getDependencyComparator() != null && result instanceof List) {
        Collections.sort((List<?>) result, adaptDependencyComparator(matchingBeans));
      }
      return result;
    }
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.