1.首先创建一个员工类(可以反编译员工类,也可以编译String类或者其他类都可)
public class Employee { static { System.out.println("Employee静态语句块执行。。。"); } private String name; private int age; private boolean gender; public Employee(){} public Employee(String name){} public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isGender() { return gender; } public void setGender(boolean gender) { this.gender = gender; } }
2.编写反编译代码
public class ReflectTest01 { public static void main(String[] args) { // 拿到Employee的class类 Class c1 = String.class; // 获取Employee的所有属性<数组> Field[] fields = c1.getDeclaredFields(); // 获取Employee的所有方法 Method[] methods = c1.getDeclaredMethods(); StringBuffer sb = new StringBuffer(); // 类的修饰符+类的简单类名 sb.append(Modifier.toString(c1.getModifiers()) + " class " + c1.getSimpleName() + " {\n"); // 遍历所有属性 for (Field field : fields) { // 属性的修饰符 sb.append("\t" + Modifier.toString(field.getModifiers()) + " "); // 属性的简单类名 sb.append(field.getType().getSimpleName() + " "); // 属性的名称 sb.append(field.getName() + ";\n"); } sb.append("\t===================分割线===========================\n"); // 遍历所有方法 for (Method method : methods) { // 方法的修饰符 sb.append("\t" + Modifier.toString(method.getModifiers()) + " "); // 返回值类型的简单类名 sb.append(method.getReturnType().getSimpleName() + " "); // 方法的方法名 sb.append(method.getName() + "("); // 形式参数列表 Class[] parameterTypes = method.getParameterTypes(); for (int i = 0; i < parameterTypes.length; i++) { if (i == parameterTypes.length - 1) { sb.append(parameterTypes[i].getSimpleName() + " args"); } else { sb.append(parameterTypes[i].getSimpleName() + " args,"); } } sb.append("){ }\n"); } sb.append("}"); System.out.println(sb); } }
3.控制台输出
public final class String { private final char[] value; private int hash; private static final long serialVersionUID; private static final ObjectStreamField[] serialPersistentFields; public static final Comparator CASE_INSENSITIVE_ORDER; ===================分割线=========================== public boolean equals(Object args){ } public String toString(){ } public int hashCode(){ } public volatile int compareTo(Object args){ } public int compareTo(String args){ } public int indexOf(String args,int args){ } static int indexOf(char[] args,int args,int args,String args,int args){ } static int indexOf(char[] args,int args,int args,char[] args,int args,int args,int args){ } public int indexOf(int args){ } public int indexOf(String args){ } public int indexOf(int args,int args){ } public static String valueOf(char args){ } public static String valueOf(Object args){ } public static String valueOf(boolean args){ } public static String valueOf(char[] args,int args,int args){ } public static String valueOf(char[] args){ } public static String valueOf(double args){ } public static String valueOf(float args){ } public static String valueOf(long args){ } public static String valueOf(int args){ } private static void checkBounds(byte[] args,int args,int args){ } public int length(){ } public boolean isEmpty(){ } public char charAt(int args){ } public int codePointAt(int args){ } public int codePointBefore(int args){ } public int codePointCount(int args,int args){ } public int offsetByCodePoints(int args,int args){ } public void getChars(int args,int args,char[] args,int args){ } void getChars(char[] args,int args){ } public byte[] getBytes(){ } public byte[] getBytes(String args){ } public void getBytes(int args,int args,byte[] args,int args){ } public byte[] getBytes(Charset args){ } public boolean contentEquals(StringBuffer args){ } public boolean contentEquals(CharSequence args){ } private boolean nonSyncContentEquals(AbstractStringBuilder args){ } public boolean equalsIgnoreCase(String args){ } public int compareToIgnoreCase(String args){ } public boolean regionMatches(int args,String args,int args,int args){ } public boolean regionMatches(boolean args,int args,String args,int args,int args){ } public boolean startsWith(String args){ } public boolean startsWith(String args,int args){ } public boolean endsWith(String args){ } private int indexOfSupplementary(int args,int args){ } public int lastIndexOf(int args,int args){ } static int lastIndexOf(char[] args,int args,int args,char[] args,int args,int args,int args){ } static int lastIndexOf(char[] args,int args,int args,String args,int args){ } public int lastIndexOf(String args,int args){ } public int lastIndexOf(int args){ } public int lastIndexOf(String args){ } private int lastIndexOfSupplementary(int args,int args){ } public String substring(int args){ } public String substring(int args,int args){ } public CharSequence subSequence(int args,int args){ } public String concat(String args){ } public String replace(char args,char args){ } public String replace(CharSequence args,CharSequence args){ } public boolean matches(String args){ } public boolean contains(CharSequence args){ } public String replaceFirst(String args,String args){ } public String replaceAll(String args,String args){ } public String[] split(String args,int args){ } public String[] split(String args){ } public static transient String join(CharSequence args,CharSequence[] args){ } public static String join(CharSequence args,Iterable args){ } public String toLowerCase(Locale args){ } public String toLowerCase(){ } public String toUpperCase(){ } public String toUpperCase(Locale args){ } public String trim(){ } public char[] toCharArray(){ } public static transient String format(Locale args,String args,Object[] args){ } public static transient String format(String args,Object[] args){ } public static String copyValueOf(char[] args,int args,int args){ } public static String copyValueOf(char[] args){ } public native String intern(){ } }
将String类的全部属性和方法都反编译出来了。。。挺有意思