java反编译获取源码

最近在研究反射,想做一个东西,把运行的java程序饭编译(Decompile)成.java文件。现思路如下:

1.写出程序反编译一个类

2.将所有类反编译

3.java代码注入一个正在运行的java程序中

现已实现第一步:

代码如下:

//这是一个要反编译的类
public
class Reflect { private int num; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getString() { return string; } public void setString(String string) { this.string = string; } private String string; public void printProperty(){ System.out.println(getNum()); System.out.println(string); } public void println(String s){ System.out.println(s); } }
//实现反编译的类,现在可以判断具体有哪些方法,但是方法里面的具体细节还不能输出
import
java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Parameter; public class Decompile { private Class<?> c; private Reflect reflect; private String className; public Decompile(String className){ try { this.className = className; c = Class.forName(className); reflect=(Reflect)c.newInstance(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //获得构造方法 public void getConstructFuntion(){ Constructor[] constructors = c.getConstructors(); for(Constructor constructor:constructors){ System.out.println( Modifier.toString(constructor.getModifiers()) +" " +constructor.getName() +"(" +getParameter(constructor) +"){}"); } } //获得类信息 public void getClassInfo(){ try { System.out.println( Modifier.toString(c.getModifiers()) +" class" +className +"{"); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //获得类属性信息 public void getProperty(){ Field[] fields = c.getDeclaredFields(); for(Field field:fields){ field.setAccessible(true); try { System.out.println( Modifier.toString(field.getModifiers()) +" " +field.getType().getSimpleName() +" " +field.getName() +" = " +field.get(reflect) +";"); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //获得类方法信息 public void getFuntion(){ Method[] methods=c.getDeclaredMethods(); for(Method method:methods){ System.out.println( Modifier.toString(method.getModifiers()) +" " +method.getReturnType().getSimpleName() +" " +method.getName().toString() +"(" +getParameter(method) +"){}"); } } //获得参数字符串 public String getParameter(Object obj){ Parameter[] parameters=null; if("Method".equals(obj.getClass().getSimpleName())){ parameters = ((Method)obj).getParameters(); } else{ parameters = ((Constructor)obj).getParameters(); } StringBuffer sb = new StringBuffer(); int count = 0; for(Parameter parameter:parameters){ if(count!=0){ sb.append(","); } sb.append(parameter.getType().getSimpleName() +" " +parameter.getName().toString()); count++; } return sb.toString(); } }
//测试代码
public
class TestReflect { public static void main(String[] args) { Decompile decompile=new Decompile("Reflect"); decompile.getClassInfo(); decompile.getConstructFuntion(); decompile.getProperty(); decompile.getFuntion(); System.out.println("}"); } }

下次再完成剩下

posted @ 2016-09-26 21:23  麦子TMAC  阅读(3755)  评论(0编辑  收藏  举报