被编译的类:
package cn.itcast.day04.demo01.Kt1.test2;
public class Srudentt {
int no;
String name ;
String birth;
boolean sex;
public Srudentt(){
}
public Srudentt(int no) {
this.no = no;
}
public Srudentt(int no, String name) {
this.no = no;
this.name = name;
}
public Srudentt(int no, String name, String birth) {
this.no = no;
this.name = name;
this.birth = birth;
}
public Srudentt(int no, String name, String birth, boolean sex) {
this.no = no;
this.name = name;
this.birth = birth;
this.sex = sex;
}
}
测试编译类:
package cn.itcast.day04.demo01.Kt1.test2;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
/*
反编译一个类的Constructor构造方法
*/
public class Reflect {
public static void main(String[] args) throws Exception{
StringBuilder s =new StringBuilder();
Class vipClass = Class.forName("cn.itcast.day04.demo01.Kt1.test2.Srudentt");
s.append(Modifier.toString(vipClass.getModifiers()));
s.append(" class ");
s.append(vipClass.getSimpleName());
s.append("{\n");
Constructor[] constructors = vipClass.getConstructors();
for (Constructor constructor:constructors){
s.append("\t");
s.append(Modifier.toString(constructor.getModifiers()));
s.append(" ");
s.append(vipClass.getSimpleName());
s.append("(");
Class[] parameterTypes = constructor.getParameterTypes();
for (Class parameterType : parameterTypes){
s.append(parameterType.getSimpleName());
s.append(",");
}
//删除最后下标位置上的字符。
if (parameterTypes.length > 0){
s.deleteCharAt(s.length() - 1);
}
s.append("){}\n");
}
s.append("}");
System.out.println(s);
}
}
编译结果:
public class Srudentt{
public Srudentt(int,String,String,boolean){}
public Srudentt(int,String,String){}
public Srudentt(int,String){}
public Srudentt(int){}
public Srudentt(){}
}