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