获取类的元数据信息

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Exec {
	public static void main(String[] args) {
		Calendar birthday = Calendar.getInstance();
		birthday.set(1983, 5, 13, 18, 30, 05);
		Student s1 = new Student("000005", "张三", true, new Timestamp(birthday
				.getTimeInMillis()), 178.3);
		System.out.println(s1);

		System.out.println("\n==========================================");
		System.out.println("通过反射获取的信息:");
		System.out.println("==========================================");
		ClassInfo classInfo = new ClassInfo();
		System.out.println(classInfo.getClassInfo(s1));
	}
}

class ClassInfo {
	public String getClassInfo(Object obj) {
		StringBuffer result = new StringBuffer();
		Class cls = obj.getClass();

		// 获取参数类变量的属性信息
		Field[] fields = cls.getDeclaredFields();
		String fullName = cls.getName();
		String className = "类的名称:"
				+ fullName.substring(fullName.lastIndexOf(".") + 1);

		// 如果有包名,获取包名
		int packagePosition = fullName.lastIndexOf('.');
		String packageName = null;
		if (packagePosition < 0) {
			packageName = "";
		} else {
			packageName = "包的名称:"
					+ fullName.substring(0, fullName.lastIndexOf('.'));
		}

		// 输出包名和类名
		result.append(packageName + "\n");
		result.append(className + "\n");

		// 遍历变量
		for (Field field : fields) {
			field.setAccessible(true);
			try {
				if (field.getModifiers() == Modifier.PRIVATE) {
					result.append("私有属性:" + field.getName() + ":值为:"
							+ field.get(obj) + "\n");
				} else if (field.getModifiers() == Modifier.PROTECTED) {
					result.append("受保护的属性:" + field.getName() + ":值为:"
							+ field.get(obj) + "\n");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result.toString();
	}
}

class Student {
	private String number = null;
	private String name = null;
	protected boolean sex = false;
	private Date birthday = null;
	protected double height = 0;

	public Student() {
	}

	public Student(String number, String name, boolean sex, Date birthday,
			double height) {
		this.number = number;
		this.name = name;
		this.sex = sex;
		this.birthday = birthday;
		this.height = height;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isSex() {
		return sex;
	}

	public void setSex(boolean sex) {
		this.sex = sex;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	@Override
	public String toString() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd E a HH:mm:ss ");
		return "学号:" + number + "\n姓名:" + name + "\n是否为男生:" + sex + "\n生日:"
				+ sdf.format(birthday) + "\n身高:" + height;

	}

}

你可能感兴趣的:(java,sql)