Java Code Examples for org.springframework.core.convert.TypeDescriptor#isPrimitive()

The following examples show how to use org.springframework.core.convert.TypeDescriptor#isPrimitive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReflectionHelper.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
@Nullable
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		// The user may supply null - and that will be ok unless a primitive is expected
		if (suppliedArg == null) {
			if (expectedArg.isPrimitive()) {
				match = null;
			}
		}
		else if (!expectedArg.equals(suppliedArg))  {
			if (suppliedArg.isAssignableTo(expectedArg)) {
				if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
					match = ArgumentsMatchKind.CLOSE;
				}
			}
			else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
				match = ArgumentsMatchKind.REQUIRES_CONVERSION;
			}
			else {
				match = null;
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}
 
Example 2
Source File: ReflectionHelper.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Based on {@link MethodInvoker#getTypeDifferenceWeight(Class[], Object[])} but operates on TypeDescriptors.
 */
public static int getTypeDifferenceWeight(List<TypeDescriptor> paramTypes, List<TypeDescriptor> argTypes) {
	int result = 0;
	for (int i = 0; i < paramTypes.size(); i++) {
		TypeDescriptor paramType = paramTypes.get(i);
		TypeDescriptor argType = (i < argTypes.size() ? argTypes.get(i) : null);
		if (argType == null) {
			if (paramType.isPrimitive()) {
				return Integer.MAX_VALUE;
			}
		}
		else {
			Class<?> paramTypeClazz = paramType.getType();
			if (!ClassUtils.isAssignable(paramTypeClazz, argType.getType())) {
				return Integer.MAX_VALUE;
			}
			if (paramTypeClazz.isPrimitive()) {
				paramTypeClazz = Object.class;
			}
			Class<?> superClass = argType.getType().getSuperclass();
			while (superClass != null) {
				if (paramTypeClazz.equals(superClass)) {
					result = result + 2;
					superClass = null;
				}
				else if (ClassUtils.isAssignable(paramTypeClazz, superClass)) {
					result = result + 2;
					superClass = superClass.getSuperclass();
				}
				else {
					superClass = null;
				}
			}
			if (paramTypeClazz.isInterface()) {
				result = result + 1;
			}
		}
	}
	return result;
}
 
Example 3
Source File: ReflectionHelper.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
@Nullable
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		// The user may supply null - and that will be ok unless a primitive is expected
		if (suppliedArg == null) {
			if (expectedArg.isPrimitive()) {
				match = null;
			}
		}
		else if (!expectedArg.equals(suppliedArg))  {
			if (suppliedArg.isAssignableTo(expectedArg)) {
				if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
					match = ArgumentsMatchKind.CLOSE;
				}
			}
			else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
				match = ArgumentsMatchKind.REQUIRES_CONVERSION;
			}
			else {
				match = null;
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}
 
Example 4
Source File: ReflectionHelper.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Based on {@link MethodInvoker#getTypeDifferenceWeight(Class[], Object[])} but operates on TypeDescriptors.
 */
public static int getTypeDifferenceWeight(List<TypeDescriptor> paramTypes, List<TypeDescriptor> argTypes) {
	int result = 0;
	for (int i = 0; i < paramTypes.size(); i++) {
		TypeDescriptor paramType = paramTypes.get(i);
		TypeDescriptor argType = (i < argTypes.size() ? argTypes.get(i) : null);
		if (argType == null) {
			if (paramType.isPrimitive()) {
				return Integer.MAX_VALUE;
			}
		}
		else {
			Class<?> paramTypeClazz = paramType.getType();
			if (!ClassUtils.isAssignable(paramTypeClazz, argType.getType())) {
				return Integer.MAX_VALUE;
			}
			if (paramTypeClazz.isPrimitive()) {
				paramTypeClazz = Object.class;
			}
			Class<?> superClass = argType.getType().getSuperclass();
			while (superClass != null) {
				if (paramTypeClazz.equals(superClass)) {
					result = result + 2;
					superClass = null;
				}
				else if (ClassUtils.isAssignable(paramTypeClazz, superClass)) {
					result = result + 2;
					superClass = superClass.getSuperclass();
				}
				else {
					superClass = null;
				}
			}
			if (paramTypeClazz.isInterface()) {
				result = result + 1;
			}
		}
	}
	return result;
}
 
Example 5
Source File: ReflectionHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		if (!expectedArg.equals(suppliedArg)) {
			// The user may supply null - and that will be ok unless a primitive is expected
			if (suppliedArg == null) {
				if (expectedArg.isPrimitive()) {
					match = null;
				}
			}
			else {
				if (suppliedArg.isAssignableTo(expectedArg)) {
					if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
						match = ArgumentsMatchKind.CLOSE;
					}
				}
				else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
					match = ArgumentsMatchKind.REQUIRES_CONVERSION;
				}
				else {
					match = null;
				}
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}
 
Example 6
Source File: ReflectionHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Based on {@link MethodInvoker#getTypeDifferenceWeight(Class[], Object[])} but operates on TypeDescriptors.
 */
public static int getTypeDifferenceWeight(List<TypeDescriptor> paramTypes, List<TypeDescriptor> argTypes) {
	int result = 0;
	for (int i = 0; i < paramTypes.size(); i++) {
		TypeDescriptor paramType = paramTypes.get(i);
		TypeDescriptor argType = (i < argTypes.size() ? argTypes.get(i) : null);
		if (argType == null) {
			if (paramType.isPrimitive()) {
				return Integer.MAX_VALUE;
			}
		}
		else {
			Class<?> paramTypeClazz = paramType.getType();
			if (!ClassUtils.isAssignable(paramTypeClazz, argType.getType())) {
				return Integer.MAX_VALUE;
			}
			if (paramTypeClazz.isPrimitive()) {
				paramTypeClazz = Object.class;
			}
			Class<?> superClass = argType.getType().getSuperclass();
			while (superClass != null) {
				if (paramTypeClazz.equals(superClass)) {
					result = result + 2;
					superClass = null;
				}
				else if (ClassUtils.isAssignable(paramTypeClazz, superClass)) {
					result = result + 2;
					superClass = superClass.getSuperclass();
				}
				else {
					superClass = null;
				}
			}
			if (paramTypeClazz.isInterface()) {
				result = result + 1;
			}
		}
	}
	return result;
}
 
Example 7
Source File: ReflectionHelper.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		if (!expectedArg.equals(suppliedArg)) {
			// The user may supply null - and that will be ok unless a primitive is expected
			if (suppliedArg == null) {
				if (expectedArg.isPrimitive()) {
					match = null;
				}
			}
			else {
				if (suppliedArg.isAssignableTo(expectedArg)) {
					if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
						match = ArgumentsMatchKind.CLOSE;
					}
				}
				else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
					match = ArgumentsMatchKind.REQUIRES_CONVERSION;
				}
				else {
					match = null;
				}
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}
 
Example 8
Source File: ReflectionHelper.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Based on {@link MethodInvoker#getTypeDifferenceWeight(Class[], Object[])} but operates on TypeDescriptors.
 */
public static int getTypeDifferenceWeight(List<TypeDescriptor> paramTypes, List<TypeDescriptor> argTypes) {
	int result = 0;
	for (int i = 0; i < paramTypes.size(); i++) {
		TypeDescriptor paramType = paramTypes.get(i);
		TypeDescriptor argType = (i < argTypes.size() ? argTypes.get(i) : null);
		if (argType == null) {
			if (paramType.isPrimitive()) {
				return Integer.MAX_VALUE;
			}
		}
		else {
			Class<?> paramTypeClazz = paramType.getType();
			if (!ClassUtils.isAssignable(paramTypeClazz, argType.getType())) {
				return Integer.MAX_VALUE;
			}
			if (paramTypeClazz.isPrimitive()) {
				paramTypeClazz = Object.class;
			}
			Class<?> superClass = argType.getType().getSuperclass();
			while (superClass != null) {
				if (paramTypeClazz.equals(superClass)) {
					result = result + 2;
					superClass = null;
				}
				else if (ClassUtils.isAssignable(paramTypeClazz, superClass)) {
					result = result + 2;
					superClass = superClass.getSuperclass();
				}
				else {
					superClass = null;
				}
			}
			if (paramTypeClazz.isInterface()) {
				result = result + 1;
			}
		}
	}
	return result;
}
 
Example 9
Source File: GenericConversionService.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void assertNotPrimitiveTargetType(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (targetType.isPrimitive()) {
		throw new ConversionFailedException(sourceType, targetType, null,
				new IllegalArgumentException("A null value cannot be assigned to a primitive type"));
	}
}
 
Example 10
Source File: GenericConversionService.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void assertNotPrimitiveTargetType(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (targetType.isPrimitive()) {
		throw new ConversionFailedException(sourceType, targetType, null,
				new IllegalArgumentException("A null value cannot be assigned to a primitive type"));
	}
}
 
Example 11
Source File: GenericConversionService.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void assertNotPrimitiveTargetType(TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (targetType.isPrimitive()) {
		throw new ConversionFailedException(sourceType, targetType, null,
				new IllegalArgumentException("A null value cannot be assigned to a primitive type"));
	}
}
 
Example 12
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private void assertNotPrimitiveTargetType(TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (targetType.isPrimitive()) {
		throw new ConversionFailedException(sourceType, targetType, null,
				new IllegalArgumentException("A null value cannot be assigned to a primitive type"));
	}
}