标签:pretty local tde ted reflect alt sys tar setname
Class 对象可以调用其方法获取以下这些类的对象,这些类型分别对应着运行时类的某个结构:Field、Method、Constructor、Superclass、Interface、Annotation
通过这些类的对象,可以获取对应运行时类的:
package com.cdf.reflection;import java.lang.annotation.Annotation;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;public class TestRe {public static void main(String[] args) {Class<MyClass> clazz = MyClass.class;// 一.获取属性Field[] fields1 = clazz.getFields();// 获取运行时类及其继承的父类以 public 修饰的属性for (Field f : fields1) {System.out.println(f);}System.out.println("------------------------------------");Field[] fields2 = clazz.getDeclaredFields();// 获取运行时类的所有属性for (Field f : fields2) {// 1.获取该属性的权限修饰符System.out.print(Modifier.toString(f.getModifiers()) + "\n");// 2.获取该属性的类型System.out.print(f.getType() + "\n");// 3.获取该属性的属性名System.out.print(f.getName() + "\n");// 4.获取完整的该属性描述System.out.println(f);System.out.println("------------------------------------");}System.out.println("------------------------------------");// 二.获取方法Method[] methods1 = clazz.getMethods();// 获取运行时类及其继承的父类以 public 修饰的方法for (Method m : methods1) {System.out.println(m);}System.out.println("------------------------------------");Method[] methods2 = clazz.getDeclaredMethods();// 获取运行时类的所有方法for (Method m : methods2) {// 1.获取该方法的注释Annotation[] annotation = m.getAnnotations();for (Annotation a : annotation) {System.out.println(a.annotationType().getName() + " ");}// 2.获取该方法的权限修饰符System.out.print(Modifier.toString(m.getModifiers()) + " ");// 3.获取该方法的返回值类型System.out.print(m.getReturnType().getName() + " ");// 4.获取该方法的方法名System.out.print(m.getName() + " ");// 5.获取该方法的形参列表Class[] params = m.getParameterTypes();System.out.print("(");for (Class p : params) {System.out.print(p.getName() + " ");}System.out.print(")");// 6.获取该方法的异常类型Class[] exps = m.getExceptionTypes();if (exps.length != 0) {System.out.print(" throws ");}for (Class e : exps) {System.out.print(e.getName() + " ");}System.out.println();// 7.获取完整的该方法描述System.out.println(m);System.out.println("------------------------------------");}System.out.println("------------------------------------");// 三.获取构造器Constructor[] constructors = clazz.getConstructors();for (Constructor c : constructors) {System.out.println(c);}System.out.println("------------------------------------");System.out.println("------------------------------------");// 四.获取父类Class superClass = clazz.getSuperclass();System.out.println(superClass);Type genericSuperClassType = clazz.getGenericSuperclass();// 带泛型System.out.println(genericSuperClassType);ParameterizedType parameterizedType = (ParameterizedType) genericSuperClassType;Type[] type = parameterizedType.getActualTypeArguments();for (Type t : type) {System.out.println(t);}System.out.println("------------------------------------");System.out.println("------------------------------------");// 五.获取实现的接口Class[] interfaces = clazz.getInterfaces();for (Class i : interfaces) {System.out.println(i);}System.out.println("------------------------------------");System.out.println("------------------------------------");// 六.获取所在的包Package pack = clazz.getPackage();System.out.println(pack);System.out.println("------------------------------------");System.out.println("------------------------------------");// 七.获取注释Annotation[] annotations = clazz.getAnnotations();for (Annotation a : annotations) {System.out.println(a);}}}interface MyInterface extends Comparable<Object> {}class MySuperClass<String> {public String superName;@Overridepublic java.lang.String toString() {return "MySuperClass [superName=" + superName + "]";}public String getSuperName() {return superName;}public void setSuperName(String superName) {this.superName = superName;}public MySuperClass(String superName) {super();this.superName = superName;}public MySuperClass() {super();}private void superMethod() {System.out.println("This is my superMethod in MySuperClass");}}@MyAnnotation(value = "cdf")class MyClass extends MySuperClass<String> implements MyInterface {public int id;String name;private double balance;public MyClass() {super();}public MyClass(int id, String name, double balance) {super();this.id = id;this.name = name;this.balance = balance;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getBalance() {return balance;}public void setBalance(double balance) {this.balance = balance;}@Overridepublic String toString() {return "MyClass [id=" + id + ", name=" + name + ", balance=" + balance + "]";}@MyAnnotation(value = "123")public void method() throws Exception {System.out.println("This is my method in MyClass");}@Overridepublic int compareTo(Object o) {return 0;}}
package com.cdf.reflection;import static java.lang.annotation.ElementType.CONSTRUCTOR;import static java.lang.annotation.ElementType.FIELD;import static java.lang.annotation.ElementType.LOCAL_VARIABLE;import static java.lang.annotation.ElementType.METHOD;import static java.lang.annotation.ElementType.PARAMETER;import static java.lang.annotation.ElementType.TYPE;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE })@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {String value();}
标签:pretty local tde ted reflect alt sys tar setname
原文地址:http://www.cnblogs.com/chendifan/p/6576243.html