java利用反射机制反编译一个类的Constructor构造方法

被编译的类:

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(){}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值