com.tmsps.ne4spring.orm.ClassUtil Maven / Gradle / Ivy
package com.tmsps.ne4spring.orm;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.tmsps.ne4spring.annotation.Column;
import com.tmsps.ne4spring.annotation.NotMap;
import com.tmsps.ne4spring.annotation.PK;
import com.tmsps.ne4spring.annotation.Table;
import com.tmsps.ne4spring.orm.model.DataModel;
import com.tmsps.ne4spring.utils.ChkUtil;
/**
*
* @author zhangwei [email protected]
*
*/
public class ClassUtil {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String getClassName(Class clazz) {
if (!clazz.isAnnotationPresent(Table.class)) {
return clazz.getSimpleName().toString();
} else if (!"".equals(((Table) clazz.getAnnotation(Table.class)).value().toString())) {
return ((Table) clazz.getAnnotation(Table.class)).value().toString();
} else if ("".equals(((Table) clazz.getAnnotation(Table.class)).TableName().toString())) {
return clazz.getSimpleName().toString();
} else {
return ((Table) clazz.getAnnotation(Table.class)).TableName().toString();
}
}
public static String getTableName(Class extends DataModel> clazz){
if (!clazz.isAnnotationPresent(Table.class)) {
return clazz.getSimpleName().toString();
} else if (!"".equals(((Table) clazz.getAnnotation(Table.class)).value().toString())) {
return ((Table) clazz.getAnnotation(Table.class)).value().toString();
} else if ("".equals(((Table) clazz.getAnnotation(Table.class)).TableName().toString())) {
return clazz.getSimpleName().toString();
} else {
return ((Table) clazz.getAnnotation(Table.class)).TableName().toString();
}
}
public static List getPropertyName(Class> clazz) {
List list = new ArrayList();
List fields = getClassFields(clazz);
for (Field field : fields) {
if (field.isAnnotationPresent((Class extends Annotation>) NotMap.class)) {
continue;
}
if (field.isAnnotationPresent((Class extends Annotation>) Column.class)) {
list.add(field.getAnnotation(Column.class).name());
continue;
}
if(field.isSynthetic()) {//如果类中字段为合成字段,剔除。
continue;
}
list.add(field.getName());
}
return list;
}
public static List getClassFields(Class> clazz) {
List clazzField = new ArrayList();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(NotMap.class)) {
continue;
}
if(field.isSynthetic()) {//如果类中字段为合成字段,剔除。
continue;
}
clazzField.add(field);
} // #for
return clazzField;
}
public static List