package com.jzlife.nurse.util;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import com.jzlife.nurse.entity.servant.dto.MatchingDto;
public class EntityToList {
/**
* List<String> entiyList =EntityToList.getList(VBonusReportStore.class);
*
* @param c
* @return
*/
public static List<String> getList(Class<?> c) {
return getList(c, null);
}
/**
* type:如String ,double,int...
*
* @param c
* @param type
* @return
*/
public static List<String> getList(Class<?> c, String type) {
List<String> entiyList = new ArrayList<String>();
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
if (type != null) {
String name = field.getName();
if (name.indexOf("ajc$tjp") < 0 && name.indexOf("entityManager") < 0) {
String myType = field.getType().toString();
if (myType.contains(type)) {
entiyList.add(field.getName());
}
}
} else {
String name = field.getName();
if (name.indexOf("ajc$tjp") < 0 && name.indexOf("entityManager") < 0) {
entiyList.add(field.getName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return entiyList;
}
public static String getString(Object o, Class<?> c) {
String result = c.getSimpleName() + ":";
// 获取父类,判断是否为实体类
if (c.getSuperclass().getName().indexOf("entity") >= 0) {
result += "\n<" + getString(o, c.getSuperclass()) + ">,\n";
}
// 获取类中的所有定义字段
Field[] fields = c.getDeclaredFields();
// 循环遍历字段,获取字段对应的属性值
for (Field field : fields) {
// 如果不为空,设置可见性,然后返回
field.setAccessible(true);
try {
// 设置字段可见,即可用get方法获取属性值。
result += field.getName() + "=" + field.get(o) + ",\n";
} catch (Exception e) {
// System.out.println("error--------"+methodName+".Reason is:"+e.getMessage());
}
}
if (result.indexOf(",") >= 0)
result = result.substring(0, result.length() - 2);
return result;
}
/**
* 获取对象中字段的值
*
* @param cs 对象
* @param filed 字段
* @return
* @throws Exception
*/
public static Object getFieldValue(Object cs, String filed) throws Exception {
if (null == filed) {
return null;
}
Field name = cs.getClass().getDeclaredField(filed);
name.setAccessible(true);
Object object = name.get(cs);
name.setAccessible(false);
return object;
}
/**
* 获取对象中有值的字段
*
* @param cs 对象
* @throws Exception
*/
public static List<String> getValueField(Object cs) throws Exception {
List<String> entiyList = EntityToList.getList(cs.getClass());
List<String> valueField = new ArrayList<String>();
for (String s : entiyList) {
Object value = getFieldValue(cs, s);
if (value != null) {
valueField.add(s);
}
}
return valueField;
}
/**
* 去掉对象中的值,不能有int类型
*
* @param cs 对象
* @param filed 要去掉值的字段
* @return
* @return
* @throws Exception
*/
public static void cleanFieldValue(Object cs, String filed) throws Exception {
if(null!=filed) {
Field name = cs.getClass().getDeclaredField(filed);
name.setAccessible(true);
name.set(cs,null);
name.setAccessible(false);
}
}
public static void main(String[] args) throws Exception {
MatchingDto md=new MatchingDto();
md.setCountSize(100);
md.setServiceType("呵呵");
md.setPageNum(20);
md.setPageSize(25);
List<String> valueField = getValueField(md);//对象中哪些有值
for(String s:valueField) {
cleanFieldValue(md,s);//去掉
System.out.println(s);
}
}
}