反编译一个类
    首先写一个配置文件test.properties

    1. class = java.lang.String

    然后进行反编译

    1. /**
    2. * 反编译一个类
    3. * 从配置文件中读取到全类名 然后反编译这个类
    4. */
    5. public class Reflect05 {
    6. public static void main(String[] args) {
    7. try {
    8. //创建流对象
    9. FileReader test = new FileReader("");
    10. //创建集合对象
    11. Properties properties = new Properties();
    12. properties.load(test);
    13. System.out.println(properties.getProperty("class"));
    14. //开始反编译
    15. StringBuilder str = new StringBuilder();
    16. //获得字节码对象
    17. Class aClass = Class.forName(properties.getProperty("class"));
    18. str.append("class "+aClass.getSimpleName());
    19. str.append("{\n");
    20. //获得属性
    21. Field[] declaredFields = aClass.getDeclaredFields();
    22. for (Field declaredField : declaredFields) {
    23. str.append("\t");
    24. str.append(Modifier.toString(declaredField.getModifiers())+" ");
    25. str.append(declaredField.getType()+" ");
    26. str.append(declaredField.getName()+"\n");
    27. }
    28. //反编译构造
    29. Constructor[] declaredConstructors = aClass.getDeclaredConstructors();
    30. //遍历
    31. for (Constructor declaredConstructor : declaredConstructors) {
    32. str.append("\n");
    33. str.append("\t");
    34. str.append(Modifier.toString(declaredConstructor.getModifiers())+" ");
    35. str.append(declaredConstructor.getName()+" (");
    36. //获取参数列表
    37. Class[] parameterTypes = declaredConstructor.getParameterTypes();
    38. //遍历参数列表
    39. for (Class parameterType : parameterTypes) {
    40. str.append(parameterType.getSimpleName()+",");
    41. //deleteCharAt()删除指定下标的字符
    42. str.deleteCharAt(str.length()-1);
    43. }
    44. str.append("){\n");
    45. str.append("\n\t}\n");
    46. //反编译方法
    47. Method[] declaredMethods = aClass.getDeclaredMethods();
    48. for (Method declaredMethod : declaredMethods) {
    49. str.append("\n");
    50. str.append("\t");
    51. str.append(Modifier.toString(declaredMethod.getModifiers())+" ");
    52. str.append(declaredMethod.getReturnType()+" ");
    53. str.append(declaredMethod.getName()+"(");
    54. //获取参数列表
    55. Class[] parameterTypes1 = declaredMethod.getParameterTypes();
    56. for (Class parameterType : parameterTypes1) {
    57. str.append(parameterType.getSimpleName()+",");
    58. //切割最后一个逗号
    59. str.deleteCharAt(str.length()-1);
    60. }
    61. str.append("){\n");
    62. str.append("\n\t}");
    63. }
    64. }
    65. str.append("}");
    66. System.out.println(str);
    67. } catch (FileNotFoundException e) {
    68. e.printStackTrace();
    69. } catch (IOException e) {
    70. e.printStackTrace();
    71. } catch (ClassNotFoundException e) {
    72. e.printStackTrace();
    73. }
    74. }
    75. }