|
|
|
|
Here's an example of how we can reconstruct a class using reflection. This program is a modification of the eariler program that used just the class Class. First, for purposes of exposition, I've added another class which adds additional features to be reconstructed:
So here's the example program that reconstructs a class using reflection:
The program isn't quite complete, since it does not handle the following features:
Adding support for these features (and others?) is left as an exercise for the reader. The above program produces the following output: ---- toString(): class java.lang.String ----
package java.lang;
public final synchronized class String
extends java.lang.Object
implements java.io.Serializable
{
// Constructors
public String() { /* ??? */ }
public String(java.lang.String p1) { /* ??? */ }
public String(char[] p1) { /* ??? */ }
public String(char[] p1, int p2, int p3) { /* ??? */ }
public String(byte[] p1, int p2, int p3, int p4) { /* ??? */ }
public String(byte[] p1, int p2) { /* ??? */ }
private String(byte[] p1, int p2, int p3, sun.io.ByteToCharConverter p4) { /* ??? */ }
public String(byte[] p1, int p2, int p3, java.lang.String p4) { /* ??? */ }
public String(byte[] p1, java.lang.String p2) { /* ??? */ }
public String(byte[] p1, int p2, int p3) { /* ??? */ }
public String(byte[] p1) { /* ??? */ }
public String(java.lang.StringBuffer p1) { /* ??? */ }
private String(int p1, int p2, char[] p3) { /* ??? */ }
// Methods
public int length() { /* ??? */ }
public char charAt(int p1) { /* ??? */ }
public void getChars(int p1, int p2, char[] p3, int p4) { /* ??? */ }
public void getBytes(int p1, int p2, byte[] p3, int p4) { /* ??? */ }
private byte[] getBytes(sun.io.CharToByteConverter p1) { /* ??? */ }
public byte[] getBytes(java.lang.String p1) { /* ??? */ }
public byte[] getBytes() { /* ??? */ }
public boolean equals(java.lang.Object p1) { /* ??? */ }
public boolean equalsIgnoreCase(java.lang.String p1) { /* ??? */ }
public int compareTo(java.lang.String p1) { /* ??? */ }
public boolean regionMatches(int p1, java.lang.String p2, int p3, int p4) { /* ??? */ }
public boolean regionMatches(boolean p1, int p2, java.lang.String p3, int p4, int p5) { /* ??? */ }
public boolean startsWith(java.lang.String p1, int p2) { /* ??? */ }
public boolean startsWith(java.lang.String p1) { /* ??? */ }
public boolean endsWith(java.lang.String p1) { /* ??? */ }
public int hashCode() { /* ??? */ }
public int indexOf(int p1) { /* ??? */ }
public int indexOf(int p1, int p2) { /* ??? */ }
public int lastIndexOf(int p1) { /* ??? */ }
public int lastIndexOf(int p1, int p2) { /* ??? */ }
public int indexOf(java.lang.String p1) { /* ??? */ }
public int indexOf(java.lang.String p1, int p2) { /* ??? */ }
public int lastIndexOf(java.lang.String p1) { /* ??? */ }
public int lastIndexOf(java.lang.String p1, int p2) { /* ??? */ }
public java.lang.String substring(int p1) { /* ??? */ }
public java.lang.String substring(int p1, int p2) { /* ??? */ }
public java.lang.String concat(java.lang.String p1) { /* ??? */ }
public java.lang.String replace(char p1, char p2) { /* ??? */ }
public java.lang.String toLowerCase(java.util.Locale p1) { /* ??? */ }
public java.lang.String toLowerCase() { /* ??? */ }
public java.lang.String toUpperCase(java.util.Locale p1) { /* ??? */ }
public java.lang.String toUpperCase() { /* ??? */ }
public java.lang.String trim() { /* ??? */ }
public java.lang.String toString() { /* ??? */ }
public char[] toCharArray() { /* ??? */ }
public static java.lang.String valueOf(java.lang.Object p1) { /* ??? */ }
public static java.lang.String valueOf(char[] p1) { /* ??? */ }
public static java.lang.String valueOf(char[] p1, int p2, int p3) { /* ??? */ }
public static java.lang.String copyValueOf(char[] p1, int p2, int p3) { /* ??? */ }
public static java.lang.String copyValueOf(char[] p1) { /* ??? */ }
public static java.lang.String valueOf(boolean p1) { /* ??? */ }
public static java.lang.String valueOf(char p1) { /* ??? */ }
public static java.lang.String valueOf(int p1) { /* ??? */ }
public static java.lang.String valueOf(long p1) { /* ??? */ }
public static java.lang.String valueOf(float p1) { /* ??? */ }
public static java.lang.String valueOf(double p1) { /* ??? */ }
public native java.lang.String intern() { /* ??? */ }
int utfLength() { /* ??? */ }
// Fields
private char[] value;
private int offset;
private int count;
private static final long serialVersionUID;
}
---- toString(): int ----
Type int
---- toString(): class [Ljava.lang.Double; ----
Array of java.lang.Double
---- toString(): class [I ----
Array of int
---- toString(): class [S ----
Array of short
---- toString(): class reflection.Person ----
package reflection;
public synchronized class Person
extends java.lang.Object
{
// Constructors
public Person() { /* ??? */ }
public Person(java.lang.String p1, int p2) { /* ??? */ }
// Methods
public int getAge() { /* ??? */ }
public java.lang.String getName() { /* ??? */ }
public void init(java.lang.String p1, int p2) { /* ??? */ }
// Fields
private java.lang.String m_name;
private int m_age;
}
---- toString(): class reflection.Employee ----
package reflection;
public synchronized class Employee
extends reflection.Person
{
// Constructors
public Employee() { /* ??? */ }
public Employee(java.lang.String p1, int p2, double p3, long p4) { /* ??? */ }
// Methods
public long getId() { /* ??? */ }
public double getSalary() { /* ??? */ }
public void init(java.lang.String p1, int p2, double p3, long p4) { /* ??? */ }
// Fields
private double m_salary;
private long m_id;
}
---- toString(): class reflection.Manager ----
package reflection;
public synchronized class Manager
extends reflection.Employee
implements reflection.Hirer, reflection.Firer
{
// Constructors
public Manager() { /* ??? */ }
public Manager(java.lang.String p1, int p2, double p3, long p4) { /* ??? */ }
// Methods
public void fire(reflection.Employee p1) { /* ??? */ }
public void hire(reflection.Employee p1) { /* ??? */ }
}
---- toString(): interface reflection.Hirer ----
package reflection;
public abstract interface interface Hirer
{
// Methods
public abstract void hire(reflection.Employee p1);
}
---- toString(): interface reflection.HirerFirer ----
package reflection;
public interface interface HirerFirer
extends reflection.Hirer, reflection.Firer
{
}
---- toString(): class reflection.Company ----
package reflection;
synchronized class Company
extends java.lang.Object
implements reflection.HirerFirer
{
// Constructors
public Company() { /* ??? */ }
public Company(java.lang.String p1, int p2) { /* ??? */ }
// Methods
public void fire(reflection.Employee p1) { /* ??? */ }
public reflection.Employee[] getEmployees() { /* ??? */ }
public void hire(reflection.Employee p1) { /* ??? */ }
public void init(java.lang.String p1, int p2) { /* ??? */ }
public void setEmployees(reflection.Employee[] p1) { /* ??? */ }
// Fields
private java.lang.String m_name;
private reflection.Employee[] m_employees;
private int m_empCount;
private int m_lastIndex;
}
---- toString(): class reflection.ClassTestClass2 ----
package reflection;
public synchronized class ClassTestClass2
extends java.lang.Object
{
// Constructors
public ClassTestClass2() { /* ??? */ }
// Methods
private static java.lang.String getArrayType(java.lang.Class p1) { /* ??? */ }
private static java.lang.String[] getPackageAndName(java.lang.String p1) { /* ??? */ }
public static void main(java.lang.String[] p1) { /* ??? */ }
private static void printArrayInfo(java.lang.Class p1) { /* ??? */ }
private static void printClassInfo(java.lang.Class p1) { /* ??? */ }
private static void printConstructors(java.lang.Class p1) { /* ??? */ }
private static void printFields(java.lang.Class p1) { /* ??? */ }
private static void printInfo(java.lang.Class p1) { /* ??? */ }
private static void printInterfaceInfo(java.lang.Class p1) { /* ??? */ }
private static void printMembers(java.lang.Class p1) { /* ??? */ }
private static void printMethods(java.lang.Class p1) { /* ??? */ }
private static void printParams(java.lang.Class[] p1) { /* ??? */ }
private static void printPrimitiveInfo(java.lang.Class p1) { /* ??? */ }
static java.lang.Class class$(java.lang.String p1) { /* ??? */ }
// Fields
private static final java.lang.String UNKNOWN_IMPLEMENTATION;
private static final java.lang.String INDENT;
static java.lang.Class array$Ljava$lang$Double;
static java.lang.Class array$I;
static java.lang.Class class$reflection$Person;
static java.lang.Class class$reflection$Employee;
static java.lang.Class class$reflection$Manager;
static java.lang.Class class$reflection$Hirer;
static java.lang.Class class$reflection$HirerFirer;
static java.lang.Class class$reflection$Company;
static java.lang.Class class$reflection$ClassTestClass2;
}
(Note, in particular, the output for the class ClassTestClass2 itself is a little strange; it appears to have some extra static fields with strange names.)
|
| The page was last updated February 19, 2008 |