反射使用(二)
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
public class ReflectionTest1 {
@Test
public void test1() {
Class clazz = Person.class;
Field[] fields1 = clazz.getFields();
Field[] fields2 = clazz.getDeclaredFields();
for (Field f : fields2) {
int modifiers = f.getModifiers();
System.out.print(Modifier.toString(modifiers) + "\t");
Class type = f.getType();
System.out.print(type.getName() + "\t");
String name = f.getName();
System.out.println(name);
}
}
@Test
public void test2() {
Class clazz = Person.class;
Method[] methods1 = clazz.getMethods();
Method[] methods2 = clazz.getDeclaredMethods();
for (Method method : methods2) {
Annotation[] annotations = method.getAnnotations();
for (Annotation a : annotations) {
System.out.println(a);
}
int modifiers = method.getModifiers();
System.out.print(Modifier.toString(modifiers) + "\t");
Class type = method.getReturnType();
System.out.print(type.getName() + "\t");
String name = method.getName();
System.out.print(name + "(");
Class[] types = method.getParameterTypes();
if (types != null || types.length != 0) {
for (int i = 0; i < types.length; i++) {
if (i == types.length - 1) {
System.out.print(types[i].getName() + "\t" + "args_" + i);
break;
}
System.out.print(types[i].getName() + "\t" + "args_" + i + "," + "\t");
}
}
System.out.print(")" + "\t");
Class[] exceptionTypes = method.getExceptionTypes();
if (exceptionTypes.length > 0) {
System.out.print("throws" + "\t");
for (int i = 0; i < exceptionTypes.length; i++) {
if (i == exceptionTypes.length - 1) {
System.out.print(exceptionTypes[i].getName());
break;
}
System.out.print(exceptionTypes[i].getName() + ',');
}
}
System.out.println();
}
}
@Test
public void test3(){
Class clazz = Person.class;
Constructor[] constructor = clazz.getConstructors();
Constructor[] constructors = clazz.getDeclaredConstructors();
}
@Test
public void test4(){
Class clazz = Person.class;
Class superclass = clazz.getSuperclass();
System.out.println(superclass);
Type genericSuperclass = clazz.getGenericSuperclass();
System.out.println(genericSuperclass);
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
Type[] types = parameterizedType.getActualTypeArguments();
System.out.println(types[0].getTypeName());
System.out.println(((Class)types[0]).getName());
}
@Test
public void test5(){
Class clazz = Person.class;
Class[] interfaces1 = clazz.getInterfaces();
for (Class i : interfaces1) {
System.out.println(i);
}
Class[] interfaces2 = clazz.getSuperclass().getInterfaces();
for (Class i : interfaces2) {
System.out.println(i);
}
Package aPackage = clazz.getPackage();
System.out.println(aPackage);
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
}
@Test
public void test6() throws Exception {
Class clazz = Person.class;
Object o = clazz.newInstance();
Field id = clazz.getField("id");
Field name = clazz.getDeclaredField("name");
name.setAccessible(true);
id.set(o,1001);
name.set(o,"tom");
System.out.println(id.get(o));
System.out.println(name.get(o));
Method show = clazz.getDeclaredMethod("show", String.class);
show.setAccessible(true);
Object china = show.invoke(o, "China");
System.out.println(china);
Method showDesc = clazz.getDeclaredMethod("showDesc");
showDesc.setAccessible(true);
Object invoke = showDesc.invoke(null);
System.out.println(invoke);
Constructor name1 = clazz.getDeclaredConstructor(String.class);
name1.setAccessible(true);
Object tom = name1.newInstance("tom");
System.out.println(tom);
}
}