通过java反射得到javabean的属性名称和值
Field fields[]=().getDeclaredFields();//cHis 是实体类名称
String[] name=new String[];
Object[] value=new Object[];
try {
(fields, true);
for (int i = 0; i < ; i++) {
name[i] = fields[i].getName();
(name[i] + "-> ");
value[i] = fields[i].get(cHis);//cHis 是实体类名称
(value[i]);
}
} catch (Exception e) {
();
}
通过反射获取类属性字段 以及 调用类方法 以下方法未测试应该可以
public class ModelClassHelper {
public static HashMap<String,Class> init(String classPath)
{
try {
//""
HashMap<String,Class> fieldHashMap=new HashMap<String,Class>();
Class cls = (classPath); //
Field[] fieldlist = ();
for (int i = 0; i < ; i++) {
Field fld = fieldlist[i];
((), ());
// ("name = " + ());
// ("decl class = " + ());
// ("type = " + ());
// ("-----");
}
return fieldHashMap;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
();
}
return null;
}
public static String getTableName(String classPath)
{
try {
Class cls = (classPath);
Method test=("getTableName" );
Object invoke = (());
return ();
//();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
();
} catch (SecurityException e) {
// TODO Auto-generated catch block
();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
();
}
return "";
}}
利用反射技术动态获取任意Java类实例的属性值
package ;
import ;
public class TestEntity {
private String code;
private String name;
public void setCode(String code)
{
= code;
}
public String getCode()
{
return ;
}
public void setName(String name)
{
= name;
}
public String getName()
{
return ;
}
public static void main(String[] args) throws SecurityException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException
{
TestEntity obj = new TestEntity();
("name value");
("code value");
Field[] fds =
("").getDeclaredFields();
();
for(int i=0;i<;i++)
{
(fds[i].get(obj));
}
}
}