Java Code Examples for org.springframework.util.ClassUtils#resolvePrimitiveIfNecessary()

The following examples show how to use org.springframework.util.ClassUtils#resolvePrimitiveIfNecessary() . 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: ListTableModel.java    From jdal with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Class<?> getColumnClass(int columnIndex) {
	Class clazz = Object.class;

	if (isCheckColum(columnIndex)) {
		clazz = Boolean.class;
	} else if (isPropertyColumn(columnIndex)) {
		if (pds.size() > 0) {
			clazz = pds.get(columnToPropertyIndex(columnIndex)).getPropertyType();
		}
	} else if (isActionColumn(columnIndex)) {
		clazz = actions.get(columntoToActionIndex(columnIndex)).getClass();
	}
	
	// JTable hangs if we return a primitive type here
	return ClassUtils.resolvePrimitiveIfNecessary(clazz);
}
 
Example 2
Source File: TypeMappedAnnotation.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Object adaptForAttribute(Method attribute, Object value) {
	Class<?> attributeType = ClassUtils.resolvePrimitiveIfNecessary(attribute.getReturnType());
	if (attributeType.isArray() && !value.getClass().isArray()) {
		Object array = Array.newInstance(value.getClass(), 1);
		Array.set(array, 0, value);
		return adaptForAttribute(attribute, array);
	}
	if (attributeType.isAnnotation()) {
		return adaptToMergedAnnotation(value,(Class<? extends Annotation>) attributeType);
	}
	if (attributeType.isArray() && attributeType.getComponentType().isAnnotation() &&
			value.getClass().isArray()) {
		MergedAnnotation<?>[] result = new MergedAnnotation<?>[Array.getLength(value)];
		for (int i = 0; i < result.length; i++) {
			result[i] = adaptToMergedAnnotation(Array.get(value, i),
					(Class<? extends Annotation>) attributeType.getComponentType());
		}
		return result;
	}
	if ((attributeType == Class.class && value instanceof String) ||
			(attributeType == Class[].class && value instanceof String[]) ||
			(attributeType == String.class && value instanceof Class) ||
			(attributeType == String[].class && value instanceof Class[])) {
		return value;
	}
	if (attributeType.isArray() && isEmptyObjectArray(value)) {
		return emptyArray(attributeType.getComponentType());
	}
	if (!attributeType.isInstance(value)) {
		throw new IllegalStateException("Attribute '" + attribute.getName() +
				"' in annotation " + getType().getName() + " should be compatible with " +
				attributeType.getName() + " but a " + value.getClass().getName() +
				" value was returned");
	}
	return value;
}
 
Example 3
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an ordered class hierarchy for the given type.
 * @param type the type
 * @return an ordered list of all classes that the given type extends or implements
 */
private List<Class<?>> getClassHierarchy(Class<?> type) {
	List<Class<?>> hierarchy = new ArrayList<Class<?>>(20);
	Set<Class<?>> visited = new HashSet<Class<?>>(20);
	addToClassHierarchy(0, ClassUtils.resolvePrimitiveIfNecessary(type), false, hierarchy, visited);
	boolean array = type.isArray();

	int i = 0;
	while (i < hierarchy.size()) {
		Class<?> candidate = hierarchy.get(i);
		candidate = (array ? candidate.getComponentType() : ClassUtils.resolvePrimitiveIfNecessary(candidate));
		Class<?> superclass = candidate.getSuperclass();
		if (superclass != null && superclass != Object.class && superclass != Enum.class) {
			addToClassHierarchy(i + 1, candidate.getSuperclass(), array, hierarchy, visited);
		}
		addInterfacesToClassHierarchy(candidate, array, hierarchy, visited);
		i++;
	}

	if (Enum.class.isAssignableFrom(type)) {
		addToClassHierarchy(hierarchy.size(), Enum.class, array, hierarchy, visited);
		addToClassHierarchy(hierarchy.size(), Enum.class, false, hierarchy, visited);
		addInterfacesToClassHierarchy(Enum.class, array, hierarchy, visited);
	}

	addToClassHierarchy(hierarchy.size(), Object.class, array, hierarchy, visited);
	addToClassHierarchy(hierarchy.size(), Object.class, false, hierarchy, visited);
	return hierarchy;
}
 
Example 4
Source File: GenericConversionService.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an ordered class hierarchy for the given type.
 * @param type the type
 * @return an ordered list of all classes that the given type extends or implements
 */
private List<Class<?>> getClassHierarchy(Class<?> type) {
	List<Class<?>> hierarchy = new ArrayList<Class<?>>(20);
	Set<Class<?>> visited = new HashSet<Class<?>>(20);
	addToClassHierarchy(0, ClassUtils.resolvePrimitiveIfNecessary(type), false, hierarchy, visited);
	boolean array = type.isArray();

	int i = 0;
	while (i < hierarchy.size()) {
		Class<?> candidate = hierarchy.get(i);
		candidate = (array ? candidate.getComponentType() : ClassUtils.resolvePrimitiveIfNecessary(candidate));
		Class<?> superclass = candidate.getSuperclass();
		if (superclass != null && superclass != Object.class && superclass != Enum.class) {
			addToClassHierarchy(i + 1, candidate.getSuperclass(), array, hierarchy, visited);
		}
		addInterfacesToClassHierarchy(candidate, array, hierarchy, visited);
		i++;
	}

	if (Enum.class.isAssignableFrom(type)) {
		addToClassHierarchy(hierarchy.size(), Enum.class, array, hierarchy, visited);
		addToClassHierarchy(hierarchy.size(), Enum.class, false, hierarchy, visited);
		addInterfacesToClassHierarchy(Enum.class, array, hierarchy, visited);
	}

	addToClassHierarchy(hierarchy.size(), Object.class, array, hierarchy, visited);
	addToClassHierarchy(hierarchy.size(), Object.class, false, hierarchy, visited);
	return hierarchy;
}
 
Example 5
Source File: GenericConversionService.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Returns an ordered class hierarchy for the given type.
 * @param type the type
 * @return an ordered list of all classes that the given type extends or implements
 */
private List<Class<?>> getClassHierarchy(Class<?> type) {
	List<Class<?>> hierarchy = new ArrayList<>(20);
	Set<Class<?>> visited = new HashSet<>(20);
	addToClassHierarchy(0, ClassUtils.resolvePrimitiveIfNecessary(type), false, hierarchy, visited);
	boolean array = type.isArray();

	int i = 0;
	while (i < hierarchy.size()) {
		Class<?> candidate = hierarchy.get(i);
		candidate = (array ? candidate.getComponentType() : ClassUtils.resolvePrimitiveIfNecessary(candidate));
		Class<?> superclass = candidate.getSuperclass();
		if (superclass != null && superclass != Object.class && superclass != Enum.class) {
			addToClassHierarchy(i + 1, candidate.getSuperclass(), array, hierarchy, visited);
		}
		addInterfacesToClassHierarchy(candidate, array, hierarchy, visited);
		i++;
	}

	if (Enum.class.isAssignableFrom(type)) {
		addToClassHierarchy(hierarchy.size(), Enum.class, array, hierarchy, visited);
		addToClassHierarchy(hierarchy.size(), Enum.class, false, hierarchy, visited);
		addInterfacesToClassHierarchy(Enum.class, array, hierarchy, visited);
	}

	addToClassHierarchy(hierarchy.size(), Object.class, array, hierarchy, visited);
	addToClassHierarchy(hierarchy.size(), Object.class, false, hierarchy, visited);
	return hierarchy;
}
 
Example 6
Source File: GenericConversionService.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Returns an ordered class hierarchy for the given type.
 * @param type the type
 * @return an ordered list of all classes that the given type extends or implements
 */
private List<Class<?>> getClassHierarchy(Class<?> type) {
	List<Class<?>> hierarchy = new ArrayList<>(20);
	Set<Class<?>> visited = new HashSet<>(20);
	addToClassHierarchy(0, ClassUtils.resolvePrimitiveIfNecessary(type), false, hierarchy, visited);
	boolean array = type.isArray();

	int i = 0;
	while (i < hierarchy.size()) {
		Class<?> candidate = hierarchy.get(i);
		candidate = (array ? candidate.getComponentType() : ClassUtils.resolvePrimitiveIfNecessary(candidate));
		Class<?> superclass = candidate.getSuperclass();
		if (superclass != null && superclass != Object.class && superclass != Enum.class) {
			addToClassHierarchy(i + 1, candidate.getSuperclass(), array, hierarchy, visited);
		}
		addInterfacesToClassHierarchy(candidate, array, hierarchy, visited);
		i++;
	}

	if (Enum.class.isAssignableFrom(type)) {
		addToClassHierarchy(hierarchy.size(), Enum.class, array, hierarchy, visited);
		addToClassHierarchy(hierarchy.size(), Enum.class, false, hierarchy, visited);
		addInterfacesToClassHierarchy(Enum.class, array, hierarchy, visited);
	}

	addToClassHierarchy(hierarchy.size(), Object.class, array, hierarchy, visited);
	addToClassHierarchy(hierarchy.size(), Object.class, false, hierarchy, visited);
	return hierarchy;
}
 
Example 7
Source File: TypeMappedAnnotation.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <T> Class<T> getAdaptType(Method attribute, Class<T> type) {
	if (type != Object.class) {
		return type;
	}
	Class<?> attributeType = attribute.getReturnType();
	if (attributeType.isAnnotation()) {
		return (Class<T>) MergedAnnotation.class;
	}
	if (attributeType.isArray() && attributeType.getComponentType().isAnnotation()) {
		return (Class<T>) MergedAnnotation[].class;
	}
	return (Class<T>) ClassUtils.resolvePrimitiveIfNecessary(attributeType);
}
 
Example 8
Source File: SynthesizedMergedAnnotationInvocationHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Object getAttributeValue(Method method) {
	String name = method.getName();
	Class<?> type = ClassUtils.resolvePrimitiveIfNecessary(method.getReturnType());
	return this.annotation.getValue(name, type).orElseThrow(
			() -> new NoSuchElementException("No value found for attribute named '" + name +
					"' in merged annotation " + this.annotation.getType().getName()));
}
 
Example 9
Source File: BeanWrapperProperty.java    From jdal with Apache License 2.0 4 votes vote down vote up
@Override
public Class<? extends T> getType() {
	Class<?> clazz =  this.beanWrapper.getPropertyDescriptor(this.propertyName).getPropertyType();
	
	return (Class<? extends T>) ClassUtils.resolvePrimitiveIfNecessary(clazz);
}
 
Example 10
Source File: SingleColumnRowMapper.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Set the type that each result object is expected to match.
 * <p>If not specified, the column value will be exposed as
 * returned by the JDBC driver.
 */
public void setRequiredType(Class<T> requiredType) {
	this.requiredType = ClassUtils.resolvePrimitiveIfNecessary(requiredType);
}
 
Example 11
Source File: TypeDescriptor.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Variation of {@link #getType()} that accounts for a primitive type by
 * returning its object wrapper type.
 * <p>This is useful for conversion service implementations that wish to
 * normalize to object-based types and not work with primitive types directly.
 */
public Class<?> getObjectType() {
	return ClassUtils.resolvePrimitiveIfNecessary(getType());
}
 
Example 12
Source File: SingleColumnRowMapper.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Set the type that each result object is expected to match.
 * <p>If not specified, the column value will be exposed as
 * returned by the JDBC driver.
 */
public void setRequiredType(Class<T> requiredType) {
	this.requiredType = ClassUtils.resolvePrimitiveIfNecessary(requiredType);
}
 
Example 13
Source File: SingleColumnRowMapper.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Set the type that each result object is expected to match.
 * <p>If not specified, the column value will be exposed as
 * returned by the JDBC driver.
 */
public void setRequiredType(Class<T> requiredType) {
	this.requiredType = ClassUtils.resolvePrimitiveIfNecessary(requiredType);
}
 
Example 14
Source File: TypeDescriptor.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Variation of {@link #getType()} that accounts for a primitive type by
 * returning its object wrapper type.
 * <p>This is useful for conversion service implementations that wish to
 * normalize to object-based types and not work with primitive types directly.
 */
public Class<?> getObjectType() {
	return ClassUtils.resolvePrimitiveIfNecessary(getType());
}
 
Example 15
Source File: TypeDescriptor.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Variation of {@link #getType()} that accounts for a primitive type by
 * returning its object wrapper type.
 * <p>This is useful for conversion service implementations that wish to
 * normalize to object-based types and not work with primitive types directly.
 */
public Class<?> getObjectType() {
	return ClassUtils.resolvePrimitiveIfNecessary(getType());
}
 
Example 16
Source File: TypeDescriptor.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Variation of {@link #getType()} that accounts for a primitive type by
 * returning its object wrapper type.
 * <p>This is useful for conversion service implementations that wish to
 * normalize to object-based types and not work with primitive types directly.
 */
public Class<?> getObjectType() {
	return ClassUtils.resolvePrimitiveIfNecessary(getType());
}
 
Example 17
Source File: SingleColumnRowMapper.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Set the type that each result object is expected to match.
 * <p>If not specified, the column value will be exposed as
 * returned by the JDBC driver.
 */
public void setRequiredType(Class<T> requiredType) {
	this.requiredType = ClassUtils.resolvePrimitiveIfNecessary(requiredType);
}