Java Code Examples for org.springframework.beans.BeanUtils#findPrimaryConstructor()

The following examples show how to use org.springframework.beans.BeanUtils#findPrimaryConstructor() . 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: ModelAttributeMethodArgumentResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private Mono<?> createAttribute(
		String attributeName, Class<?> clazz, BindingContext context, ServerWebExchange exchange) {

	Constructor<?> ctor = BeanUtils.findPrimaryConstructor(clazz);
	if (ctor == null) {
		Constructor<?>[] ctors = clazz.getConstructors();
		if (ctors.length == 1) {
			ctor = ctors[0];
		}
		else {
			try {
				ctor = clazz.getDeclaredConstructor();
			}
			catch (NoSuchMethodException ex) {
				throw new IllegalStateException("No primary or default constructor found for " + clazz, ex);
			}
		}
	}
	return constructAttribute(ctor, attributeName, context, exchange);
}
 
Example 2
Source File: ModelAttributeMethodArgumentResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
private Mono<?> createAttribute(
		String attributeName, Class<?> clazz, BindingContext context, ServerWebExchange exchange) {

	Constructor<?> ctor = BeanUtils.findPrimaryConstructor(clazz);
	if (ctor == null) {
		Constructor<?>[] ctors = clazz.getConstructors();
		if (ctors.length == 1) {
			ctor = ctors[0];
		}
		else {
			try {
				ctor = clazz.getDeclaredConstructor();
			}
			catch (NoSuchMethodException ex) {
				throw new IllegalStateException("No primary or default constructor found for " + clazz, ex);
			}
		}
	}
	return constructAttribute(ctor, attributeName, context, exchange);
}
 
Example 3
Source File: GenericApplicationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
public Constructor<?>[] getPreferredConstructors() {
	Class<?> clazz = getBeanClass();
	Constructor<?> primaryCtor = BeanUtils.findPrimaryConstructor(clazz);
	if (primaryCtor != null) {
		return new Constructor<?>[] {primaryCtor};
	}
	Constructor<?>[] publicCtors = clazz.getConstructors();
	if (publicCtors.length > 0) {
		return publicCtors;
	}
	return null;
}
 
Example 4
Source File: ModelAttributeMethodProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Extension point to create the model attribute if not found in the model,
 * with subsequent parameter binding through bean properties (unless suppressed).
 * <p>The default implementation typically uses the unique public no-arg constructor
 * if available but also handles a "primary constructor" approach for data classes:
 * It understands the JavaBeans {@link ConstructorProperties} annotation as well as
 * runtime-retained parameter names in the bytecode, associating request parameters
 * with constructor arguments by name. If no such constructor is found, the default
 * constructor will be used (even if not public), assuming subsequent bean property
 * bindings through setter methods.
 * @param attributeName the name of the attribute (never {@code null})
 * @param parameter the method parameter declaration
 * @param binderFactory for creating WebDataBinder instance
 * @param webRequest the current request
 * @return the created model attribute (never {@code null})
 * @throws BindException in case of constructor argument binding failure
 * @throws Exception in case of constructor invocation failure
 * @see #constructAttribute(Constructor, String, MethodParameter, WebDataBinderFactory, NativeWebRequest)
 * @see BeanUtils#findPrimaryConstructor(Class)
 */
protected Object createAttribute(String attributeName, MethodParameter parameter,
		WebDataBinderFactory binderFactory, NativeWebRequest webRequest) throws Exception {

	MethodParameter nestedParameter = parameter.nestedIfOptional();
	Class<?> clazz = nestedParameter.getNestedParameterType();

	Constructor<?> ctor = BeanUtils.findPrimaryConstructor(clazz);
	if (ctor == null) {
		Constructor<?>[] ctors = clazz.getConstructors();
		if (ctors.length == 1) {
			ctor = ctors[0];
		}
		else {
			try {
				ctor = clazz.getDeclaredConstructor();
			}
			catch (NoSuchMethodException ex) {
				throw new IllegalStateException("No primary or default constructor found for " + clazz, ex);
			}
		}
	}

	Object attribute = constructAttribute(ctor, attributeName, parameter, binderFactory, webRequest);
	if (parameter != nestedParameter) {
		attribute = Optional.of(attribute);
	}
	return attribute;
}
 
Example 5
Source File: GenericApplicationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public Constructor<?>[] getPreferredConstructors() {
	Class<?> clazz = getBeanClass();
	Constructor<?> primaryCtor = BeanUtils.findPrimaryConstructor(clazz);
	if (primaryCtor != null) {
		return new Constructor<?>[] {primaryCtor};
	}
	Constructor<?>[] publicCtors = clazz.getConstructors();
	if (publicCtors.length > 0) {
		return publicCtors;
	}
	return null;
}
 
Example 6
Source File: ModelAttributeMethodProcessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Extension point to create the model attribute if not found in the model,
 * with subsequent parameter binding through bean properties (unless suppressed).
 * <p>The default implementation typically uses the unique public no-arg constructor
 * if available but also handles a "primary constructor" approach for data classes:
 * It understands the JavaBeans {@link ConstructorProperties} annotation as well as
 * runtime-retained parameter names in the bytecode, associating request parameters
 * with constructor arguments by name. If no such constructor is found, the default
 * constructor will be used (even if not public), assuming subsequent bean property
 * bindings through setter methods.
 * @param attributeName the name of the attribute (never {@code null})
 * @param parameter the method parameter declaration
 * @param binderFactory for creating WebDataBinder instance
 * @param webRequest the current request
 * @return the created model attribute (never {@code null})
 * @throws BindException in case of constructor argument binding failure
 * @throws Exception in case of constructor invocation failure
 * @see #constructAttribute(Constructor, String, MethodParameter, WebDataBinderFactory, NativeWebRequest)
 * @see BeanUtils#findPrimaryConstructor(Class)
 */
protected Object createAttribute(String attributeName, MethodParameter parameter,
		WebDataBinderFactory binderFactory, NativeWebRequest webRequest) throws Exception {

	MethodParameter nestedParameter = parameter.nestedIfOptional();
	Class<?> clazz = nestedParameter.getNestedParameterType();

	Constructor<?> ctor = BeanUtils.findPrimaryConstructor(clazz);
	if (ctor == null) {
		Constructor<?>[] ctors = clazz.getConstructors();
		if (ctors.length == 1) {
			ctor = ctors[0];
		}
		else {
			try {
				ctor = clazz.getDeclaredConstructor();
			}
			catch (NoSuchMethodException ex) {
				throw new IllegalStateException("No primary or default constructor found for " + clazz, ex);
			}
		}
	}

	Object attribute = constructAttribute(ctor, attributeName, parameter, binderFactory, webRequest);
	if (parameter != nestedParameter) {
		attribute = Optional.of(attribute);
	}
	return attribute;
}