概述
public class ClassMappings {
private ClassMappings(){}
static final Set<Class<?>> SUPPORTED_SQL_OBJECTS = new HashSet<Class<?>>();
static {
//只要这里写了的,默认支持自动类型转换
Class<?>[] classes = {
boolean.class, Boolean.class,
short.class, Short.class,
int.class, Integer.class,
long.class, Long.class,
float.class, Float.class,
double.class, Double.class,
String.class,
Date.class,
Timestamp.class,
BigDecimal.class
};
SUPPORTED_SQL_OBJECTS.addAll(Arrays.asList(classes));
}
static boolean isSupportedSQLObject(Class<?> clazz) {
return clazz.isEnum() || SUPPORTED_SQL_OBJECTS.contains(clazz);
}
public static Map<String, Method> findPublicGetters(Class<?> clazz) {
Map<String, Method> map = new HashMap<String, Method>();
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (Modifier.isStatic(method.getModifiers()))
continue;
if (method.getParameterTypes().length != 0)
continue;
if (method.getName().equals("getClass"))
continue;
Class<?> returnType = method.getReturnType();
if (void.class.equals(returnType))
continue;
if(!isSupportedSQLObject(returnType)){
continue;
}
if ((returnType.equals(boolean.class)
|| returnType.equals(Boolean.class))
&& method.getName().startsWith("is")
&& method.getName().length() > 2) {
map.put(getGetterName(method), method);
continue;
}
if ( ! method.getName().startsWith("get"))
continue;
if (method.getName().length() < 4)
continue;
map.put(getGetterName(method), method);
}
return map;
}
public static Field[] findFields(Class<?> clazz){
return clazz.getDeclaredFields();
}
public static Map<String, Method> findPublicSetters(Class<?> clazz) {
Map<String, Method> map = new HashMap<String, Method>();
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (Modifier.isStatic(method.getModifiers()))
continue;
if ( ! void.class.equals(method.getReturnType()))
continue;
if (method.getParameterTypes().length != 1)
continue;
if ( ! method.getName().startsWith("set"))
continue;
if (method.getName().length() < 4)
continue;
if(!isSupportedSQLObject(method.getParameterTypes()[0])){
continue;
}
map.put(getSetterName(method), method);
}
return map;
}
public static String getGetterName(Method getter) {
String name = getter.getName();
if (name.startsWith("is"))
name = name.substring(2);
else
name = name.substring(3);
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
}
private static String getSetterName(Method setter) {
String name = setter.getName().substring(3);
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
}
}
最后
以上就是丰富钢笔为你收集整理的基于SpringJDBC 实现关键功能-ClassMappings的全部内容,希望文章能够帮你解决基于SpringJDBC 实现关键功能-ClassMappings所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复