Package org.springframework.beans

Examples of org.springframework.beans.TypeConverter


   * @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 (int i = 0; i < candidates.length; i++) {
        Method candidate = candidates[i];
        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

  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

        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");
        }
        return null;
      }
      Map matchingBeans = findAutowireCandidates(beanName, elementType, descriptor);
      if (matchingBeans.isEmpty()) {
        if (descriptor.isRequired()) {
          throw new NoSuchBeanDefinitionException(elementType,
              "Unsatisfied dependency of type [" + elementType.getName() + "]: expected at least 1 matching bean");
        }
        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

   * @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 autowiredBeanNames = new LinkedHashSet(4);
    String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw);
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

        // Found a cached constructor...
        argsToUse = mbd.resolvedConstructorArguments;
        if (argsToUse == null) {
          Class[] paramTypes = constructorToUse.getParameterTypes();
          Object[] argsToResolve = mbd.preparedConstructorArguments;
          TypeConverter converter = (this.typeConverter != null ? this.typeConverter : bw);
          BeanDefinitionValueResolver valueResolver =
              new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);
          argsToUse = new Object[argsToResolve.length];
          for (int i = 0; i < argsToResolve.length; i++) {
            Object argValue = argsToResolve[i];
            if (argValue instanceof AutowiredArgumentMarker) {
              argValue = resolveAutowiredArgument(
                  new MethodParameter(constructorToUse, i), beanName, null, converter);
            }
            else if (argValue instanceof BeanMetadataElement) {
              argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
            }
            argsToUse[i] = converter.convertIfNecessary(argValue, paramTypes[i],
                new MethodParameter(constructorToUse, i));
          }
        }
      }
    }
View Full Code Here

        // Found a cached factory method...
        argsToUse = mbd.resolvedConstructorArguments;
        if (argsToUse == null) {
          Class[] paramTypes = factoryMethodToUse.getParameterTypes();
          Object[] argsToResolve = mbd.preparedConstructorArguments;
          TypeConverter converter = (this.typeConverter != null ? this.typeConverter : bw);
          BeanDefinitionValueResolver valueResolver =
              new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);
          argsToUse = new Object[argsToResolve.length];
          for (int i = 0; i < argsToResolve.length; i++) {
            Object argValue = argsToResolve[i];
            if (argValue instanceof AutowiredArgumentMarker) {
              argValue = resolveAutowiredArgument(
                  new MethodParameter(factoryMethodToUse, i), beanName, null, converter);
            }
            else if (argValue instanceof BeanMetadataElement) {
              argValue = valueResolver.resolveValueIfNecessary("factory method argument", argValue);
            }
            argsToUse[i] = converter.convertIfNecessary(argValue, paramTypes[i],
                new MethodParameter(factoryMethodToUse, i));
          }
        }
      }
    }
View Full Code Here

   */
  private int resolveConstructorArguments(
      String beanName, RootBeanDefinition mbd, BeanWrapper bw,
      ConstructorArgumentValues cargs, ConstructorArgumentValues resolvedValues) {

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

    int minNrOfArgs = cargs.getArgumentCount();

View Full Code Here

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

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

    ArgumentsHolder args = new ArgumentsHolder(paramTypes.length);
    Set usedValueHolders = new HashSet(paramTypes.length);
    Set autowiredBeanNames = new LinkedHashSet(4);
    boolean resolveNecessary = false;

    for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) {
      Class paramType = paramTypes[paramIndex];
      // Try to find matching constructor argument value, either indexed or generic.
      ConstructorArgumentValues.ValueHolder valueHolder =
          resolvedValues.getArgumentValue(paramIndex, paramType, 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, 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);
        args.rawArguments[paramIndex] = valueHolder.getValue();
        if (valueHolder.isConverted()) {
          Object convertedValue = valueHolder.getConvertedValue();
          args.arguments[paramIndex] = convertedValue;
          args.preparedArguments[paramIndex] = convertedValue;
        }
        else {
          try {
            Object originalValue = valueHolder.getValue();
            Object convertedValue = converter.convertIfNecessary(originalValue, paramType,
                MethodParameter.forMethodOrConstructor(methodOrCtor, paramIndex));
            args.arguments[paramIndex] = convertedValue;
            ConstructorArgumentValues.ValueHolder sourceHolder =
                (ConstructorArgumentValues.ValueHolder) valueHolder.getSource();
            Object sourceValue = sourceHolder.getValue();
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.