import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 获取任意java类实例的方法和属性,包括父类的方法和属性
* @author weishaoxiang
*/
public class Student extends School
{
private String code;
private String name;
private Date birthday;
private int age;
private int classNumber;
private boolean isGoodStudent;
private List< School > sch;
private School sch1 = new School ( );
/**
* 测试
* @param args
*/
public static void main ( String[] args )
{
Student student = new Student ( );
// 获取属性
Map< String, Class > map = getClassFields ( student.getClass ( ), true );
for ( Object key : map.keySet ( ) )
{
System.out.println ( "<field=" + key.toString ( ) + "> <Type=" + map.get ( key ) + ">" );
}
// 获取方法
List< Method > methods = getMothds ( student.getClass ( ), true );
for ( Method method : methods )
{
System.out.println ( method.getName ( ) );
}
System.out.println ( "方法总数:" + methods.size ( ) );
}
/**
* 获取类实例的属性值
* @param clazz
* 类名
* @param includeParentClass
* 是否包括父类的属性值
* @return 类名.属性名=属性类型
*/
public static Map< String, Class > getClassFields ( Class clazz, boolean includeParentClass )
{
Map< String, Class > map = new HashMap< String, Class > ( );
Field[] fields = clazz.getDeclaredFields ( );
for ( Field field : fields )
{
map.put ( clazz.getName ( ) + "." + field.getName ( ), field.getType ( ) );
}
if ( includeParentClass )
getParentClassFields ( map, clazz.getSuperclass ( ) );
return map;
}
/**
* 获取类实例的父类的属性值
* @param map
* 类实例的属性值Map
* @param clazz
* 类名
* @return 类名.属性名=属性类型
*/
private static Map< String, Class > getParentClassFields ( Map< String, Class > map, Class clazz )
{
Field[] fields = clazz.getDeclaredFields ( );
for ( Field field : fields )
{
map.put ( clazz.getName ( ) + "." + field.getName ( ), field.getType ( ) );
}
if ( clazz.getSuperclass ( ) == null )
{
return map;
}
getParentClassFields ( map, clazz.getSuperclass ( ) );
return map;
}
/**
* 获取类实例的方法
* @param clazz
* 类名
* @param includeParentClass
* 是否包括父类的方法
* @return List
*/
public static List< Method > getMothds ( Class clazz, boolean includeParentClass )
{
List< Method > list = new ArrayList< Method > ( );
Method[] methods = clazz.getDeclaredMethods ( );
for ( Method method : methods )
{
list.add ( method );
}
if ( includeParentClass )
{
getParentClassMothds ( list, clazz.getSuperclass ( ) );
}
return list;
}
/**
* 获取类实例的父类的方法
* @param list
* 类实例的方法List
* @param clazz
* 类名
* @return List
*/
private static List< Method > getParentClassMothds ( List< Method > list, Class clazz )
{
Method[] methods = clazz.getDeclaredMethods ( );
for ( Method method : methods )
{
list.add ( method );
}
if ( clazz.getSuperclass ( ) == Object.class )
{
return list;
}
getParentClassMothds ( list, clazz.getSuperclass ( ) );
return list;
}
public int getAge ( )
{
return age;
}
public void setAge ( int age )
{
this.age = age;
}
}
class Country
{
private String countryName;
private long peopleNumber;
public long getPeopleNumber ( )
{
return peopleNumber;
}
public void setPeopleNumber ( long peopleNumber )
{
this.peopleNumber = peopleNumber;
}
}
class School extends Country
{
private String schoolName;
private String address;
public String getAddress ( )
{
return address;
}
public void setAddress ( String address )
{
this.address = address;
}
}