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

The following examples show how to use org.springframework.util.ClassUtils#isPrimitiveOrWrapper() . 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: SQLHelper.java    From syncer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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();
}
 
Example 2
Source File: SpringSessionFixFilter.java    From joinfaces with Apache License 2.0 6 votes vote down vote up
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);
	}
}
 
Example 3
Source File: CacheKeyGenerator.java    From computoser with GNU Affero General Public License v3.0 6 votes vote down vote up
@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();
}
 
Example 4
Source File: BeanUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: DataRestResponseBuilder.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: BeanUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: LegalEnumRule.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example 8
Source File: LegalEnumRule.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
@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);
		}
	}
}
 
Example 9
Source File: BeanUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 10
Source File: BeanUtils.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: BeanUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}