Java学习日记day08:reflect反射机制(IO流与Properties和reflect连用实现动态反编译.class文件)

Reflect

问题:IO流与Properties和reflect连用实现动态反编译.class文件

需求:

  • 用户只需提供配置文件即可开始反编译
  • 编译的类容输出到控制台上
下面是我所编写的代码
ClassInfomation.properties文件(配置文件)

我们只需在配置文件上获取需要反编译的类名就行

className=java.lang.String
Decompiling.java文件
package com.jvstudy.day08.text1;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Properties;
/*
 * function:反编译.class文件
 * className:存储通过反编译获取的类名
 * decompilingClass:存储反编译的基本内容
 * clName:一个Class引用
 */
public class Decompiling {
	String className;
	StringBuffer decompilingClass;
	Class<?> clName;
	/*
	 * function:从配置文件中提取出需要反编译class的类名
	 */
	public Decompiling() {
		Properties info = null;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("D:\\Java Project\\Learning\\src\\com\\jvstudy\\day08\\text1\\ClassInfomation.properties");
			info = new Properties();
			info.load(fis);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(info.get("className") instanceof String)
			this.className = (String)info.get("className");
		initDecompiling();
	}
	/**
	 * function:入口函数
	 */
	public void initDecompiling() {
		decompilingClass = new StringBuffer();
		try {
			clName = Class.forName(this.className);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		decompilingClass.append(Modifier.toString(clName.getModifiers())+" class "+clName.getName()+" {\n");
		decompilingField();
		decompilingConstructor();
		decompilingMethod();
		decompilingClass.append("\t"+"}");
		System.out.println(decompilingClass);
	}
	/*
	 * function:反编译属性
	 */
	public void decompilingField() {
		Field[] fields = clName.getDeclaredFields();
		for(Field field:fields) {		
			decompilingClass.append("\t\t"+Modifier.toString(field.getModifiers())+" "+field.getType().getSimpleName()+" "+field.getName()+";\n");	
		}
	}
	/*
	 * function:反编译构造函数
	 */
	public void decompilingConstructor() {
		Constructor<?>[] constructs = clName.getDeclaredConstructors();
		for(Constructor<?> constructor:constructs) {
			int i=1;
			Class<?>[] parameterTypes = constructor.getParameterTypes();
			decompilingClass.append("\t\t"+Modifier.toString(constructor.getModifiers())+" "+constructor.getName()+"(");
			if(parameterTypes.length==0) {
				decompilingClass.append(");\n");
			}
			else {
				for(Class<?> parameterType:parameterTypes) {
					if(i == parameterTypes.length) {
						decompilingClass.append(parameterType.getSimpleName()+");\n");
					}
					else {
						decompilingClass.append(parameterType.getSimpleName()+",");
					}
					i++;
				}
			}
		}
	}
	/**
	 * function:反编译函数方法
	 */
	public void decompilingMethod() {
		Method[] methods = clName.getDeclaredMethods();
		for(Method method:methods) {
			int i=1;
			Class<?>[] parameterTypes = method.getParameterTypes();
			decompilingClass.append("\t\t"+Modifier.toString(method.getModifiers())+" "+method.getReturnType().getSimpleName()+" "+method.getName()+"(");
			if(parameterTypes.length==0) {
				decompilingClass.append(");\n");
			}
			else {
				for(Class<?> parameterType:parameterTypes) {
					if(i == parameterTypes.length) {
						decompilingClass.append(parameterType.getSimpleName()+");\n");
					}
					else {
						decompilingClass.append(parameterType.getSimpleName()+",");
					}
					i++;
				}
			}
		}
	}

}

测试
package com.jvstudy.day08.text1;

public class test1 {
public static void main(String[] args) {
	new Decompiling();
}
}

效果图片

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值