public class ReflectEntityTest {
private byte aByte;
private char aChar;
private int anInt;
private short aShort;
private long aLong;
private double aDouble;
private float aFloat;
private boolean aBoolean;
private String string;
public String publicString;
public String getPublicString() {
return publicString;
}
public void setPublicString(String publicString) {
this.publicString = publicString;
}
public byte getaByte() {
return aByte;
}
public void setaByte(byte aByte) {
this.aByte = aByte;
}
public char getaChar() {
return aChar;
}
public void setaChar(char aChar) {
this.aChar = aChar;
}
public int getAnInt() {
return anInt;
}
public void setAnInt(int anInt) {
this.anInt = anInt;
}
public short getaShort() {
return aShort;
}
public void setaShort(short aShort) {
this.aShort = aShort;
}
public long getaLong() {
return aLong;
}
public void setaLong(long aLong) {
this.aLong = aLong;
}
public double getaDouble() {
return aDouble;
}
public void setaDouble(double aDouble) {
this.aDouble = aDouble;
}
public float getaFloat() {
return aFloat;
}
public void setaFloat(float aFloat) {
this.aFloat = aFloat;
}
public boolean isaBoolean() {
return aBoolean;
}
public void setaBoolean(boolean aBoolean) {
this.aBoolean = aBoolean;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public ReflectEntityTest() {
}
public ReflectEntityTest(String string) {
this.string = string;
}
public ReflectEntityTest(byte aByte, char aChar, int anInt, short aShort, long aLong, double aDouble, float aFloat, boolean aBoolean, String string) {
this.aByte = aByte;
this.aChar = aChar;
this.anInt = anInt;
this.aShort = aShort;
this.aLong = aLong;
this.aDouble = aDouble;
this.aFloat = aFloat;
this.aBoolean = aBoolean;
this.string = string;
}
@Override
public String toString() {
return "ReflectEntityTest{" +
"aByte=" + aByte +
", aChar=" + aChar +
", anInt=" + anInt +
", aShort=" + aShort +
", aLong=" + aLong +
", aDouble=" + aDouble +
", aFloat=" + aFloat +
", aBoolean=" + aBoolean +
", string='" + string + '\'' +
'}';
}
}
public class ReflectTest {
private static final String classPath = "ReflectEntityTest";
private Class clazz;
{
try {
clazz = Class.forName(classPath);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void getClassInfo() throws Exception {
System.out.println(clazz.getPackage());//类路径类
System.out.println(clazz.getName());//类名称继承类
System.out.println(clazz.getSuperclass());//集成类接口
System.out.println(Arrays.toString(clazz.getInterfaces()));//所有的接口构造方法
System.out.println(Arrays.toString(clazz.getConstructors()));//public的构造方法构造方法
System.out.println(clazz.getConstructor(new Class[]{String.class}));//public的有参构造方法构造方法
System.out.println(Arrays.toString(clazz.getDeclaredConstructors()));//所有的构造方法构造方法
Class[] cArg = new Class[1];
cArg[0] = String.class;
System.out.println(clazz.getDeclaredConstructor(cArg));//所有的有参构造方法方法
System.out.println(Arrays.toString(clazz.getMethods()));//public的方法方法
System.out.println(clazz.getMethod("setString", String.class));//public的有参方法方法
System.out.println(Arrays.toString(clazz.getDeclaredMethods()));//所有的方法方法
System.out.println(clazz.getDeclaredMethod("setString", String.class));//所有的有参方法成员变量
System.out.println(Arrays.toString(clazz.getFields()));//public的成员变量成员变量
/*设置字段属性可见*/
for (int i = 0; i < clazz.getFields().length; i++) {
clazz.getFields()[i].setAccessible(true);
}
System.out.println(clazz.getField("publicString"));//public的有参变量成员变量
System.out.println(Arrays.toString(clazz.getDeclaredFields()));//所有的成员变量成员变量
System.out.println(clazz.getDeclaredField("string"));//所有的有参变量内部类
System.out.println(Arrays.toString(clazz.getClasses()));//public的内部类内部类
System.out.println(Arrays.toString(clazz.getDeclaredClasses()));//所有的内部类内部类的声明类
System.out.println(clazz.getDeclaringClass());//
}
/*构造器常用的方法*/
@Test
public void testConstructor() throws Exception {
Constructor constructor = clazz.getDeclaredConstructor(new Class[]{String.class});
/*可变数量的参数*/
System.out.println(constructor.isVarArgs());
/*获取参数类型*/
System.out.println(Arrays.toString(constructor.getParameterTypes()));
/*获取异常类型*/
System.out.println(Arrays.toString(constructor.getExceptionTypes()));
/*新建一个实例*/
// constructor.newInstance(String.class);
/*设置可见*/
constructor.setAccessible(true);
/*获取构造方法采用的修饰符的整数*/
int modifier = constructor.getModifiers();
/**/
System.out.println(Modifier.isPublic(modifier));
System.out.println(Modifier.isPrivate(modifier));
System.out.println(Modifier.isProtected(modifier));
System.out.println(Modifier.isStatic(modifier));
System.out.println(Modifier.isFinal(modifier));
System.out.println(Modifier.isAbstract(modifier));
System.out.println(Modifier.toString(modifier));
}
@Test
public void testField() throws Exception {
Field field = clazz.getField("publicString");
System.out.println(field.getName());
System.out.println(field.getType());
field.setAccessible(true);
System.out.println(field.getModifiers());
}
@Test
public void getMethod() throws Exception {
Method method = clazz.getMethod("setString", String.class);
System.out.println(method.getName());
System.out.println(method.getParameterTypes());
System.out.println(method.getReturnType());
System.out.println(method.getExceptionTypes());
System.out.println(method.isVarArgs());
System.out.println(method.getModifiers());
/*调用回调方法设置参数*/
method.invoke(new ReflectEntityTest(),"1");
}
}