package com.javase.reflect; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ResourceBundle; /** * 反编译Method 这里我们暂时还不能获取方法体中的内容 * 通过读取属性配置文件来对不同的类进行反编译 */ public class ReflectMethodText02 { public static void main(String[] args) { //通过资源绑定器获取属性配置文件中的内容(属性配置文件dp.properties在IDEA中所处目录如下图) ResourceBundle bundle = ResourceBundle.getBundle("com\\javase\\bean\\dp"); String className = bundle.getString("className"); //创建一个字符串拼接对象 StringBuilder s = new StringBuilder(); try { //获取类 Class randomClass = Class.forName(className);//random任意,随意 组合在一起randomClass任意类 //获取类的修饰符 + " class " + 类名 + "{回车符号" s.append(Modifier.toString(randomClass.getModifiers()) + " class " + randomClass.getSimpleName() + "{\n"); //获取类中的方法 Method[] methods = randomClass.getDeclaredMethods(); //遍历 for (Method method : methods){ //拼接制表符 s.append("\t"); //获取方法的修饰符 s.append(Modifier.toString(method.getModifiers())); //添加空格 s.append(" "); //获取方法的返回值类型 s.append(method.getReturnType().getSimpleName()); //添加空格 s.append(" "); //获取方法名 s.append(method.getName()); //添加"("开始 s.append("("); //获取参数列表数据类型 Class[] parameterTypes = method.getParameterTypes(); //如果获取的参数列表不为0就遍历 if (parameterTypes.length != 0) { //遍历 for (Class parameterType : parameterTypes) { s.append(parameterType.getSimpleName()); s.append(","); } //删除最后一个","字符 s.deleteCharAt(s.length() - 1); } //添加"){}"结束 s.append("){}\n"); } //添加类的结尾 s.append("}"); //打印拼接好的字符串 System.out.println(s); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
属性配置文件dp.properties在IDEA中的目录结构
/*
程序运行结果:
public final class Integer{
public static int numberOfLeadingZeros(int){}
public static int numberOfTrailingZeros(int){}
public static int bitCount(int){}
public boolean equals(Object){}
public static String toString(int){}
public String toString(){}
public static String toString(int,int){}
public int hashCode(){}
public static int hashCode(int){}
public static int min(int,int){}
public static int max(int,int){}
public static int reverseBytes(int){}
static int getChars(int,int,byte[]){}
public volatile int compareTo(Object){}
public int compareTo(Integer){}
public byte byteValue(){}
public short shortValue(){}
public int intValue(){}
public long longValue(){}
public float floatValue(){}
public double doubleValue(){}
public static Integer valueOf(int){}
public static Integer valueOf(String,int){}
public static Integer valueOf(String){}
public static String toHexString(int){}
public static Integer decode(String){}
public static int compare(int,int){}
public volatile Object resolveConstantDesc(Lookup){}
public Integer resolveConstantDesc(Lookup){}
public Optional describeConstable(){}
public static int reverse(int){}
static int stringSize(int){}
public static long toUnsignedLong(int){}
public static int parseInt(String,int){}
public static int parseInt(CharSequence,int,int,int){}
public static int parseInt(String){}
public static int sum(int,int){}
public static int compareUnsigned(int,int){}
private static String toStringUTF16(int,int){}
public static String toUnsignedString(int,int){}
public static String toUnsignedString(int){}
private static String toUnsignedString0(int,int){}
private static void formatUnsignedInt(int,int,byte[],int){}
private static void formatUnsignedIntUTF16(int,int,byte[],int){}
public static int parseUnsignedInt(String){}
public static int parseUnsignedInt(CharSequence,int,int,int){}
public static int parseUnsignedInt(String,int){}
public static Integer getInteger(String,Integer){}
public static Integer getInteger(String,int){}
public static Integer getInteger(String){}
public static String toOctalString(int){}
public static String toBinaryString(int){}
public static int divideUnsigned(int,int){}
public static int remainderUnsigned(int,int){}
public static int highestOneBit(int){}
public static int lowestOneBit(int){}
public static int rotateLeft(int,int){}
public static int rotateRight(int,int){}
public static int signum(int){}
}
*/