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

The following examples show how to use org.springframework.beans.BeanUtils#isSimpleProperty() . These examples are extracted from open source projects. 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
@Override
public boolean supports(HandlerResult result) {
	if (hasModelAnnotation(result.getReturnTypeSource())) {
		return true;
	}

	Class<?> type = result.getReturnType().toClass();
	ReactiveAdapter adapter = getAdapter(result);
	if (adapter != null) {
		if (adapter.isNoValue()) {
			return true;
		}
		type = result.getReturnType().getGeneric().toClass();
	}

	return (CharSequence.class.isAssignableFrom(type) || Rendering.class.isAssignableFrom(type) ||
			Model.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type) ||
			void.class.equals(type) || View.class.isAssignableFrom(type) ||
			!BeanUtils.isSimpleProperty(type));
}
 
Example 2
/**
 * 
 * ִ��һ�������Լ�飬�������ع�������ѱ�����
 * <p>
 * Perform a dependency check that all properties exposed have been set, if
 * desired. Dependency checks can be objects (collaborating beans), simple
 * (primitives and String), or all (both).
 * 
 * @param beanName
 *            the name of the bean
 * @param mbd
 *            the merged bean definition the bean was created with
 * @param pds
 *            the relevant property descriptors for the target bean
 * @param pvs
 *            the property values to be applied to the bean
 * @see #isExcludedFromDependencyCheck(java.beans.PropertyDescriptor)
 */
protected void checkDependencies(
		String beanName, AbstractBeanDefinition mbd, PropertyDescriptor[] pds, PropertyValues pvs)
		throws UnsatisfiedDependencyException {

	int dependencyCheck = mbd.getDependencyCheck();
	for (PropertyDescriptor pd : pds) {
		
		if (pd.getWriteMethod() != null && !pvs.contains(pd.getName())) {
			
			boolean isSimple = BeanUtils.isSimpleProperty(pd.getPropertyType());
			
			boolean unsatisfied = (dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_ALL) ||
					(isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_SIMPLE) ||
					(!isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
			// �����㣬���쳣
			if (unsatisfied) {
				throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, pd.getName(),
						"Set this property value or disable dependency checking for this bean.");
			}
		}
	}
}
 
Example 3
/**
 * Perform a dependency check that all properties exposed have been set,
 * if desired. Dependency checks can be objects (collaborating beans),
 * simple (primitives and String), or all (both).
 * @param beanName the name of the bean
 * @param mbd the merged bean definition the bean was created with
 * @param pds the relevant property descriptors for the target bean
 * @param pvs the property values to be applied to the bean
 * @see #isExcludedFromDependencyCheck(PropertyDescriptor)
 */
protected void checkDependencies(
		String beanName, AbstractBeanDefinition mbd, PropertyDescriptor[] pds, PropertyValues pvs)
		throws UnsatisfiedDependencyException {

	int dependencyCheck = mbd.getDependencyCheck();
	for (PropertyDescriptor pd : pds) {
		if (pd.getWriteMethod() != null && !pvs.contains(pd.getName())) {
			boolean isSimple = BeanUtils.isSimpleProperty(pd.getPropertyType());
			boolean unsatisfied = (dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_ALL) ||
					(isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_SIMPLE) ||
					(!isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
			if (unsatisfied) {
				throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, pd.getName(),
						"Set this property value or disable dependency checking for this bean.");
			}
		}
	}
}
 
Example 4
/**
 * Perform a dependency check that all properties exposed have been set,
 * if desired. Dependency checks can be objects (collaborating beans),
 * simple (primitives and String), or all (both).
 * @param beanName the name of the bean
 * @param mbd the merged bean definition the bean was created with
 * @param pds the relevant property descriptors for the target bean
 * @param pvs the property values to be applied to the bean
 * @see #isExcludedFromDependencyCheck(java.beans.PropertyDescriptor)
 */
protected void checkDependencies(
		String beanName, AbstractBeanDefinition mbd, PropertyDescriptor[] pds, PropertyValues pvs)
		throws UnsatisfiedDependencyException {

	int dependencyCheck = mbd.getDependencyCheck();
	for (PropertyDescriptor pd : pds) {
		if (pd.getWriteMethod() != null && !pvs.contains(pd.getName())) {
			boolean isSimple = BeanUtils.isSimpleProperty(pd.getPropertyType());
			boolean unsatisfied = (dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_ALL) ||
					(isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_SIMPLE) ||
					(!isSimple && dependencyCheck == RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
			if (unsatisfied) {
				throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName, pd.getName(),
						"Set this property value or disable dependency checking for this bean.");
			}
		}
	}
}
 
Example 5
private void resolveApiParam(ParameterContext context) {
	ResolvedType resolvedType = context.resolvedMethodParameter().getParameterType();
	Class<?> parameterClass = resolveParamType(resolvedType);
	if (parameterClass == null) {
		log.warn(StrUtil.concat(resolvedType.getBriefDescription(), "的类型无法被DefaultParamPlugin解析"));
		@SuppressWarnings("unused") int a = 1;
		return;
	}
	ApiModel apiModel = parameterClass.getAnnotation(ApiModel.class);
	if (apiModel == null) {
		if (!BeanUtils.isSimpleProperty(parameterClass)) {
			warn(context, parameterClass);
		}
		return;
	}
	ParameterBuilder builder = context.parameterBuilder();
	builder.name(apiModel.description());
	builder.description(descriptions.resolve(apiModel.description()));
	builder.allowMultiple(false);
	builder.allowEmptyValue(false);
	builder.hidden(false);
	builder.collectionFormat("");
	builder.order(SWAGGER_PLUGIN_ORDER);
}
 
Example 6
/**
 * Return an array of non-simple bean properties that are unsatisfied.
 * These are probably unsatisfied references to other beans in the
 * factory. Does not include simple properties like primitives or Strings.
 * @param mbd the merged bean definition the bean was created with
 * @param bw the BeanWrapper the bean was created with
 * @return an array of bean property names
 * @see BeanUtils#isSimpleProperty
 */
protected String[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) {
	Set<String> result = new TreeSet<String>();
	PropertyValues pvs = mbd.getPropertyValues();
	PropertyDescriptor[] pds = bw.getPropertyDescriptors();
	for (PropertyDescriptor pd : pds) {
		if (pd.getWriteMethod() != null && !isExcludedFromDependencyCheck(pd) && !pvs.contains(pd.getName()) &&
				!BeanUtils.isSimpleProperty(pd.getPropertyType())) {
			result.add(pd.getName());
		}
	}
	return StringUtils.toStringArray(result);
}
 
Example 7
/**
 * Return an array of non-simple bean properties that are unsatisfied.
 * These are probably unsatisfied references to other beans in the
 * factory. Does not include simple properties like primitives or Strings.
 * @param mbd the merged bean definition the bean was created with
 * @param bw the BeanWrapper the bean was created with
 * @return an array of bean property names
 * @see org.springframework.beans.BeanUtils#isSimpleProperty
 */
protected String[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) {
	Set<String> result = new TreeSet<String>();
	PropertyValues pvs = mbd.getPropertyValues();
	PropertyDescriptor[] pds = bw.getPropertyDescriptors();
	for (PropertyDescriptor pd : pds) {
		if (pd.getWriteMethod() != null && !isExcludedFromDependencyCheck(pd) && !pvs.contains(pd.getName()) &&
				!BeanUtils.isSimpleProperty(pd.getPropertyType())) {
			result.add(pd.getName());
		}
	}
	return StringUtils.toStringArray(result);
}
 
Example 8
@Override
public boolean supportsParameter(MethodParameter param) {
	if (checkAnnotatedParamNoReactiveWrapper(param, RequestParam.class, this::singleParam)) {
		return true;
	}
	else if (this.useDefaultResolution) {
		return checkParameterTypeNoReactiveWrapper(param, BeanUtils::isSimpleProperty) ||
				BeanUtils.isSimpleProperty(param.nestedIfOptional().getNestedParameterType());
	}
	return false;
}
 
Example 9
/**
 * Supports the following:
 * <ul>
 * <li>@RequestParam-annotated method arguments.
 * This excludes {@link Map} params where the annotation does not specify a name.
 * See {@link RequestParamMapMethodArgumentResolver} instead for such params.
 * <li>Arguments of type {@link MultipartFile} unless annotated with @{@link RequestPart}.
 * <li>Arguments of type {@code Part} unless annotated with @{@link RequestPart}.
 * <li>In default resolution mode, simple type arguments even if not with @{@link RequestParam}.
 * </ul>
 */
@Override
public boolean supportsParameter(MethodParameter parameter) {
	if (parameter.hasParameterAnnotation(RequestParam.class)) {
		if (Map.class.isAssignableFrom(parameter.nestedIfOptional().getNestedParameterType())) {
			RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
			return (requestParam != null && StringUtils.hasText(requestParam.name()));
		}
		else {
			return true;
		}
	}
	else {
		if (parameter.hasParameterAnnotation(RequestPart.class)) {
			return false;
		}
		parameter = parameter.nestedIfOptional();
		if (MultipartResolutionDelegate.isMultipartArgument(parameter)) {
			return true;
		}
		else if (this.useDefaultResolution) {
			return BeanUtils.isSimpleProperty(parameter.getNestedParameterType());
		}
		else {
			return false;
		}
	}
}
 
Example 10
/**
 * Return an array of non-simple bean properties that are unsatisfied.
 * These are probably unsatisfied references to other beans in the
 * factory. Does not include simple properties like primitives or Strings.
 * @param mbd the merged bean definition the bean was created with
 * @param bw the BeanWrapper the bean was created with
 * @return an array of bean property names
 * @see org.springframework.beans.BeanUtils#isSimpleProperty
 */
protected String[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) {
	Set<String> result = new TreeSet<>();
	PropertyValues pvs = mbd.getPropertyValues();
	PropertyDescriptor[] pds = bw.getPropertyDescriptors();
	for (PropertyDescriptor pd : pds) {
		if (pd.getWriteMethod() != null && !isExcludedFromDependencyCheck(pd) && !pvs.contains(pd.getName()) &&
				!BeanUtils.isSimpleProperty(pd.getPropertyType())) {
			result.add(pd.getName());
		}
	}
	return StringUtils.toStringArray(result);
}
 
Example 11
/**
 * Supports the following:
 * <ul>
 * <li>@RequestParam-annotated method arguments.
 * This excludes {@link Map} params where the annotation doesn't
 * specify a name.	See {@link RequestParamMapMethodArgumentResolver}
 * instead for such params.
 * <li>Arguments of type {@link MultipartFile}
 * unless annotated with @{@link RequestPart}.
 * <li>Arguments of type {@code javax.servlet.http.Part}
 * unless annotated with @{@link RequestPart}.
 * <li>In default resolution mode, simple type arguments
 * even if not with @{@link RequestParam}.
 * </ul>
 */
@Override
public boolean supportsParameter(MethodParameter parameter) {
	if (parameter.hasParameterAnnotation(RequestParam.class)) {
		if (Map.class.isAssignableFrom(parameter.nestedIfOptional().getNestedParameterType())) {
			String paramName = parameter.getParameterAnnotation(RequestParam.class).name();
			return StringUtils.hasText(paramName);
		}
		else {
			return true;
		}
	}
	else {
		if (parameter.hasParameterAnnotation(RequestPart.class)) {
			return false;
		}
		parameter = parameter.nestedIfOptional();
		if (MultipartResolutionDelegate.isMultipartArgument(parameter)) {
			return true;
		}
		else if (this.useDefaultResolution) {
			return BeanUtils.isSimpleProperty(parameter.getNestedParameterType());
		}
		else {
			return false;
		}
	}
}
 
Example 12
/**
 * Return {@code true} if there is a method-level {@code @ModelAttribute}
 * or, in default resolution mode, for any return value type that is not
 * a simple type.
 */
@Override
public boolean supportsReturnType(MethodParameter returnType) {
	return (returnType.hasMethodAnnotation(ModelAttribute.class) ||
			(this.annotationNotRequired && !BeanUtils.isSimpleProperty(returnType.getParameterType())));
}
 
Example 13
private boolean isRequestParamType(Type type, Map<Class<?>, Annotation> annotations) {
    RequestParam requestParam = (RequestParam) annotations.get(RequestParam.class);
    return requestParam != null || (BeanUtils.isSimpleProperty(TypeUtils.getRawType(type, type)) && !hasClassStartingWith(annotations.keySet(), "org.springframework.web.bind.annotation"));
}
 
Example 14
private Object[] resolveInitBinderArguments(Object handler, Method initBinderMethod,
		WebDataBinder binder, NativeWebRequest webRequest) throws Exception {

	Class<?>[] initBinderParams = initBinderMethod.getParameterTypes();
	Object[] initBinderArgs = new Object[initBinderParams.length];

	for (int i = 0; i < initBinderArgs.length; i++) {
		MethodParameter methodParam = new SynthesizingMethodParameter(initBinderMethod, i);
		methodParam.initParameterNameDiscovery(this.parameterNameDiscoverer);
		GenericTypeResolver.resolveParameterType(methodParam, handler.getClass());
		String paramName = null;
		boolean paramRequired = false;
		String paramDefaultValue = null;
		String pathVarName = null;
		Annotation[] paramAnns = methodParam.getParameterAnnotations();

		for (Annotation paramAnn : paramAnns) {
			if (RequestParam.class.isInstance(paramAnn)) {
				RequestParam requestParam = (RequestParam) paramAnn;
				paramName = requestParam.name();
				paramRequired = requestParam.required();
				paramDefaultValue = parseDefaultValueAttribute(requestParam.defaultValue());
				break;
			}
			else if (ModelAttribute.class.isInstance(paramAnn)) {
				throw new IllegalStateException(
						"@ModelAttribute is not supported on @InitBinder methods: " + initBinderMethod);
			}
			else if (PathVariable.class.isInstance(paramAnn)) {
				PathVariable pathVar = (PathVariable) paramAnn;
				pathVarName = pathVar.value();
			}
		}

		if (paramName == null && pathVarName == null) {
			Object argValue = resolveCommonArgument(methodParam, webRequest);
			if (argValue != WebArgumentResolver.UNRESOLVED) {
				initBinderArgs[i] = argValue;
			}
			else {
				Class<?> paramType = initBinderParams[i];
				if (paramType.isInstance(binder)) {
					initBinderArgs[i] = binder;
				}
				else if (BeanUtils.isSimpleProperty(paramType)) {
					paramName = "";
				}
				else {
					throw new IllegalStateException("Unsupported argument [" + paramType.getName() +
							"] for @InitBinder method: " + initBinderMethod);
				}
			}
		}

		if (paramName != null) {
			initBinderArgs[i] =
					resolveRequestParam(paramName, paramRequired, paramDefaultValue, methodParam, webRequest, null);
		}
		else if (pathVarName != null) {
			initBinderArgs[i] = resolvePathVariable(pathVarName, methodParam, webRequest, null);
		}
	}

	return initBinderArgs;
}
 
Example 15
/**
 * Return {@code true} if there is a method-level {@code @ModelAttribute}
 * or, in default resolution mode, for any return value type that is not
 * a simple type.
 */
@Override
public boolean supportsReturnType(MethodParameter returnType) {
	return (returnType.hasMethodAnnotation(ModelAttribute.class) ||
			(this.annotationNotRequired && !BeanUtils.isSimpleProperty(returnType.getParameterType())));
}
 
Example 16
/**
 * Looks at the object passed in and recursively expands any @EmbeddedEntityResource annotations or related relationship.
 * {@link org.alfresco.rest.framework.resource.EmbeddedEntityResource EmbeddedEntityResource} is expanded by calling the ReadById method for this entity.
 * 
 * Either returns a ExecutionResult object or a CollectionWithPagingInfo containing a collection of ExecutionResult objects.
 * 
 * @param api Api
 * @param entityCollectionName String
 * @param params  Params
 * @param objectToWrap Object
 * @return Object - Either ExecutionResult or CollectionWithPagingInfo<ExecutionResult>
 */
public Object processAdditionsToTheResponse(WebScriptResponse res, Api api, String entityCollectionName, Params params, Object objectToWrap)
{
    PropertyCheck.mandatory(this, null, params);
    if (objectToWrap == null ) return null;
    if (objectToWrap instanceof CollectionWithPagingInfo<?>)
    {
        CollectionWithPagingInfo<?> collectionToWrap = (CollectionWithPagingInfo<?>) objectToWrap;
        Object sourceEntity = executeIncludedSource(api, params, entityCollectionName, collectionToWrap);
        Collection<Object> resultCollection = new ArrayList(collectionToWrap.getCollection().size());
        if (!collectionToWrap.getCollection().isEmpty())
        {
            for (Object obj : collectionToWrap.getCollection())
            {
                resultCollection.add(processAdditionsToTheResponse(res, api,entityCollectionName,params,obj));
            }
        }
        return CollectionWithPagingInfo.asPaged(collectionToWrap.getPaging(), resultCollection, collectionToWrap.hasMoreItems(),
                                                collectionToWrap.getTotalItems(), sourceEntity, collectionToWrap.getContext());
    }
    else
    {           
        if (BeanUtils.isSimpleProperty(objectToWrap.getClass())  || objectToWrap instanceof Collection)
        {
            //Simple property or Collection that can't be embedded so just return it.
            return objectToWrap;
        }

        final ExecutionResult execRes = new ExecutionResult(objectToWrap, params.getFilter());
        
        Map<String,Pair<String,Method>> embeddded = ResourceInspector.findEmbeddedResources(objectToWrap.getClass());
        if (embeddded != null && !embeddded.isEmpty())
        {
            Map<String, Object> results = executeEmbeddedResources(api, params,objectToWrap, embeddded);
            execRes.addEmbedded(results);
        }
        
        if (params.getRelationsFilter() != null && !params.getRelationsFilter().isEmpty())
        {
            Map<String, ResourceWithMetadata> relationshipResources = locator.locateRelationResource(api,entityCollectionName, params.getRelationsFilter().keySet(), HttpMethod.GET);
            String uniqueEntityId = ResourceInspector.findUniqueId(objectToWrap);
            Map<String,Object> relatedResources = executeRelatedResources(api, params, relationshipResources, uniqueEntityId);
            execRes.addRelated(relatedResources);
        }

        return execRes; 

    }
}
 
Example 17
/**
 * Return {@code true} if there is a method-level {@code @ModelAttribute}
 * or, in default resolution mode, for any return value type that is not
 * a simple type.
 */
@Override
public boolean supportsReturnType(MethodParameter returnType) {
	return (returnType.hasMethodAnnotation(ModelAttribute.class) ||
			(this.annotationNotRequired && !BeanUtils.isSimpleProperty(returnType.getParameterType())));
}
 
Example 18
private Object[] resolveInitBinderArguments(Object handler, Method initBinderMethod,
		WebDataBinder binder, NativeWebRequest webRequest) throws Exception {

	Class<?>[] initBinderParams = initBinderMethod.getParameterTypes();
	Object[] initBinderArgs = new Object[initBinderParams.length];

	for (int i = 0; i < initBinderArgs.length; i++) {
		MethodParameter methodParam = new SynthesizingMethodParameter(initBinderMethod, i);
		methodParam.initParameterNameDiscovery(this.parameterNameDiscoverer);
		GenericTypeResolver.resolveParameterType(methodParam, handler.getClass());
		String paramName = null;
		boolean paramRequired = false;
		String paramDefaultValue = null;
		String pathVarName = null;
		Annotation[] paramAnns = methodParam.getParameterAnnotations();

		for (Annotation paramAnn : paramAnns) {
			if (RequestParam.class.isInstance(paramAnn)) {
				RequestParam requestParam = (RequestParam) paramAnn;
				paramName = requestParam.name();
				paramRequired = requestParam.required();
				paramDefaultValue = parseDefaultValueAttribute(requestParam.defaultValue());
				break;
			}
			else if (ModelAttribute.class.isInstance(paramAnn)) {
				throw new IllegalStateException(
						"@ModelAttribute is not supported on @InitBinder methods: " + initBinderMethod);
			}
			else if (PathVariable.class.isInstance(paramAnn)) {
				PathVariable pathVar = (PathVariable) paramAnn;
				pathVarName = pathVar.value();
			}
		}

		if (paramName == null && pathVarName == null) {
			Object argValue = resolveCommonArgument(methodParam, webRequest);
			if (argValue != WebArgumentResolver.UNRESOLVED) {
				initBinderArgs[i] = argValue;
			}
			else {
				Class<?> paramType = initBinderParams[i];
				if (paramType.isInstance(binder)) {
					initBinderArgs[i] = binder;
				}
				else if (BeanUtils.isSimpleProperty(paramType)) {
					paramName = "";
				}
				else {
					throw new IllegalStateException("Unsupported argument [" + paramType.getName() +
							"] for @InitBinder method: " + initBinderMethod);
				}
			}
		}

		if (paramName != null) {
			initBinderArgs[i] =
					resolveRequestParam(paramName, paramRequired, paramDefaultValue, methodParam, webRequest, null);
		}
		else if (pathVarName != null) {
			initBinderArgs[i] = resolvePathVariable(pathVarName, methodParam, webRequest, null);
		}
	}

	return initBinderArgs;
}
 
Example 19
Source Project: spring-boot   File: DtoUtils.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 找到 bean 的非简单属性名称(isSimpleProperty 进行判断),包括 :
 * primitive (byte,short,int,long,float,double,boolean,char) Primitive Data Types 基本数据类型,共 8 种
 * String or other CharSequence
 * Number
 * Date
 * URI
 * URL
 * Locale
 * Class
 * corresponding array.
 *
 * @param fromClass
 * @return
 */
private List<String> getNonSimplePropertyNames(Class fromClass) {
    List<String> names = new ArrayList<String>();
    for (Field field : fromClass.getDeclaredFields()) {
        if (!BeanUtils.isSimpleProperty(field.getType())) {
            names.add(field.getName());
        }
    }
    return names;
}
 
Example 20
Source Project: framework   File: BeanUtil.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author yang.zhipeng <br>
 * @taskId <br>
 * @param clazz <br>
 * @return <br>
 */
public static boolean isSimpleProperty(final Class<?> clazz) {
    return BeanUtils.isSimpleProperty(clazz);
}