下面列出了org.springframework.util.ClassUtils#isPrimitiveOrWrapper ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Find search return type type.
*
* @param handlerMethod the handler method
* @param methodResourceMapping the method resource mapping
* @param domainType the domain type
* @return the type
*/
private Type findSearchReturnType(HandlerMethod handlerMethod, MethodResourceMapping methodResourceMapping, Class<?> domainType) {
Type returnType = null;
Type returnRepoType = ReturnTypeParser.resolveType(methodResourceMapping.getMethod().getGenericReturnType(), methodResourceMapping.getMethod().getDeclaringClass());
if (methodResourceMapping.isPagingResource()) {
returnType = ResolvableType.forClassWithGenerics(PagedModel.class, domainType).getType();
}
else if (Iterable.class.isAssignableFrom(ResolvableType.forType(returnRepoType).getRawClass())) {
returnType = ResolvableType.forClassWithGenerics(CollectionModel.class, domainType).getType();
}
else if (!ClassUtils.isPrimitiveOrWrapper(domainType)) {
returnType = ResolvableType.forClassWithGenerics(EntityModel.class, domainType).getType();
}
if (returnType == null) {
returnType = ReturnTypeParser.resolveType(handlerMethod.getMethod().getGenericReturnType(), handlerMethod.getBeanType());
returnType = getType(returnType, domainType);
}
return returnType;
}
public static String inSQL(Object value) {
if (value == null) {
return "NULL";
}
Class<?> aClass = value.getClass();
if (ClassUtils.isPrimitiveOrWrapper(aClass)
|| CharSequence.class.isAssignableFrom(aClass)
|| value instanceof Timestamp
|| value instanceof BigDecimal) {
if (value instanceof String) {
// TODO 2019/3/3 http://www.jguru.com/faq/view.jsp?EID=8881 {escape '/'} ?
String replace = StringUtils.replace(StringUtils.replace(value.toString(), "'", "''"), "\\", "\\\\");
value = "'" + replace + "'";
} else if (value instanceof Timestamp) {
value = "'" + value.toString() + "'";
}
} else if (SQLFunction.class.isAssignableFrom(aClass)) {
value = value.toString();
} else {
logger.error("Unhandled complex type: {}, value: {}", aClass, value);
}
return value.toString();
}
private void reSetAttributes(SessionWrapper sessionWrapper) {
HttpSession realSession = sessionWrapper.delegate;
Enumeration<String> attributeNames = realSession.getAttributeNames();
while (attributeNames.hasMoreElements()) {
String attributeName = attributeNames.nextElement();
if (!sessionWrapper.readAttributeNames.contains(attributeName)) {
//Attribute was not read, so we don't need to re-set it.
continue;
}
Object attributeValue = realSession.getAttribute(attributeName);
if (ClassUtils.isPrimitiveOrWrapper(attributeValue.getClass())) {
//Attribute is primitive (immutable), so we don't need to re-set it.
continue;
}
realSession.setAttribute(attributeName, attributeValue);
}
}
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder key = new StringBuilder();
key.append(target.getClass().getSimpleName()).append(".").append(method.getName()).append(":");
if (params.length == 0) {
key.append(NO_PARAM_KEY).toString();
}
for (Object param : params) {
if (param == null) {
key.append(NULL_PARAM_KEY);
} else if (ClassUtils.isPrimitiveOrWrapper(param.getClass()) || param instanceof String) {
key.append(param);
} else if (param instanceof CacheKey) {
key.append(((CacheKey) param).getCacheKey());
} else {
logger.warn("Using object " + param + " as cache key. Either use key='..' or implement CacheKey. Method is " + target.getClass() + "#" + method.getName());
key.append(param.hashCode());
}
}
return key.toString();
}
/**
* Check if the given type represents a "simple" value type:
* a primitive, an enum, a String or other CharSequence, a Number, a Date,
* a Temporal, a URI, a URL, a Locale or a Class.
* @param clazz the type to check
* @return whether the given type represents a "simple" value type
*/
public static boolean isSimpleValueType(Class<?> clazz) {
return (ClassUtils.isPrimitiveOrWrapper(clazz) ||
Enum.class.isAssignableFrom(clazz) ||
CharSequence.class.isAssignableFrom(clazz) ||
Number.class.isAssignableFrom(clazz) ||
Date.class.isAssignableFrom(clazz) ||
Temporal.class.isAssignableFrom(clazz) ||
URI.class == clazz || URL.class == clazz ||
Locale.class == clazz || Class.class == clazz);
}
/**
* Check if the given type represents a "simple" value type:
* a primitive, an enum, a String or other CharSequence, a Number, a Date,
* a URI, a URL, a Locale or a Class.
* @param clazz the type to check
* @return whether the given type represents a "simple" value type
*/
public static boolean isSimpleValueType(Class<?> clazz) {
return (ClassUtils.isPrimitiveOrWrapper(clazz) ||
Enum.class.isAssignableFrom(clazz) ||
CharSequence.class.isAssignableFrom(clazz) ||
Number.class.isAssignableFrom(clazz) ||
Date.class.isAssignableFrom(clazz) ||
URI.class == clazz || URL.class == clazz ||
Locale.class == clazz || Class.class == clazz);
}
@Override
public void check(Field field) throws CodeCheckException {
MapEnum annotation = field.getAnnotation(MapEnum.class);
if (annotation != null) {
if (!ClassUtils.isPrimitiveOrWrapper(field.getType())) {
throw new CodeCheckException(ReflectUtil.fullName(field), "不是基本类型或者其装箱类,不能使用@", MapEnum.class.getCanonicalName(), "注解");
}
checkLegalEnum(annotation);
}
}
@Override
public void check(Method method) throws CodeCheckException {
for (Parameter parameter : method.getParameters()) {
MapEnum annotation = parameter.getAnnotation(MapEnum.class);
if (annotation != null) {
if (!ClassUtils.isPrimitiveOrWrapper(parameter.getType())) {
throw new CodeCheckException("方法", ReflectUtil.fullName(method), "的参数", parameter.getName(), "不是基本类型或者其装箱类,不能使用@",
MapEnum.class.getCanonicalName(), "注解");
}
checkLegalEnum(annotation);
}
}
}
/**
* Check if the given type represents a "simple" value type:
* a primitive, a String or other CharSequence, a Number, a Date,
* a URI, a URL, a Locale or a Class.
* @param clazz the type to check
* @return whether the given type represents a "simple" value type
*/
public static boolean isSimpleValueType(Class<?> clazz) {
return (ClassUtils.isPrimitiveOrWrapper(clazz) || clazz.isEnum() ||
CharSequence.class.isAssignableFrom(clazz) ||
Number.class.isAssignableFrom(clazz) ||
Date.class.isAssignableFrom(clazz) ||
URI.class == clazz || URL.class == clazz ||
Locale.class == clazz || Class.class == clazz);
}
/**
* Check if the given type represents a "simple" value type:
* a primitive, a String or other CharSequence, a Number, a Date,
* a URI, a URL, a Locale or a Class.
* @param clazz the type to check
* @return whether the given type represents a "simple" value type
*/
public static boolean isSimpleValueType(Class<?> clazz) {
return ClassUtils.isPrimitiveOrWrapper(clazz) || clazz.isEnum() ||
CharSequence.class.isAssignableFrom(clazz) ||
Number.class.isAssignableFrom(clazz) ||
Date.class.isAssignableFrom(clazz) ||
clazz.equals(URI.class) || clazz.equals(URL.class) ||
clazz.equals(Locale.class) || clazz.equals(Class.class);
}
/**
* Check if the given type represents a "simple" value type:
* a primitive, a String or other CharSequence, a Number, a Date,
* a URI, a URL, a Locale or a Class.
* @param clazz the type to check
* @return whether the given type represents a "simple" value type
*/
public static boolean isSimpleValueType(Class<?> clazz) {
return (ClassUtils.isPrimitiveOrWrapper(clazz) || clazz.isEnum() ||
CharSequence.class.isAssignableFrom(clazz) ||
Number.class.isAssignableFrom(clazz) ||
Date.class.isAssignableFrom(clazz) ||
URI.class == clazz || URL.class == clazz ||
Locale.class == clazz || Class.class == clazz);
}