org.tinygroup.validate.impl.ClassUtil Maven / Gradle / Ivy
/**
* Copyright (c) 1997-2013, www.tinygroup.org ([email protected]).
*
* Licensed under the GPL, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.tinygroup.validate.impl;
import java.util.Collection;
import java.util.Map;
public class ClassUtil {
/**
* 判断该类型是不是包装类型
* @param clazz
* @return
*/
public static boolean isBasicClass(Class> clazz) {
boolean isPrimitive = false;
try {
if (clazz.isPrimitive() || clazz.isAssignableFrom(String.class)) {
isPrimitive = true;
} else {
isPrimitive = ((Class>) clazz.getField("TYPE").get(null))
.isPrimitive();
}
} catch (Exception e) {
isPrimitive = false;
}
return isPrimitive;
}
/**
* 判断对象是不是集合类型
* @param
* @param object
* @return
*/
public static boolean isCollectTypes(T object) {
return object instanceof Collection;
}
/**
* 判断对象是不是映射类型
* @param
* @param object
* @return
*/
public static boolean isMapTypes(T object) {
return object instanceof Map;
}
/**
* 判断对象是不是集合类型
* @param
* @param object
* @return
*/
public static boolean isArray(Class clazz) {
return clazz.isArray();
}
/**
* 判断对象是不是集合类型
* @param
* @param object
* @return
*/
public static boolean arrayElementIsObjectType(Class clazz) {
return isArray(clazz) && !isBasicClass(clazz.getComponentType());
}
/**
* 判断对象是不是集合类型
* @param
* @param object
* @return
*/
public static boolean collectionElementIsObjectType(T object) {
if (isCollectTypes(object)) {
Class clazz = (Class) object.getClass();
Collection coll = (Collection) object;
for (Object object2 : coll) {
return isBasicClass(object2.getClass());
}
}
return false;
}
/**
* 采取驼峰规则
*
* @param simpleName
* @return
*/
public static String humpString(Class clazz) {
String simpleName = clazz.getSimpleName();
String first = simpleName.charAt(0) + "";
return first.toLowerCase() + simpleName.substring(1);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy