import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.swing.JButton;
public class Test {
private static byte[] readAll(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (;;) {
int i = is.read();
if (i < 0)
break;
baos.write(i);
}
return baos.toByteArray();
}
public static void main(String[] args) throws ClassNotFoundException,
MalformedURLException, InstantiationException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException {
final String jreRt = "c:\\Program Files\\Java\\jdk1.7.0_45\\jre\\lib\\rt.jar";
ClassLoader loader = new ClassLoader() {
@Override
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException {
if (name.equals("javax.swing.JButton"))
try {
@SuppressWarnings("resource")
ZipFile file = new ZipFile(new File(jreRt));
ZipEntry entry = file.getEntry(name.replace('.', '/')
+ ".class");
byte[] data = readAll(file.getInputStream(entry));
return defineClass(data, 0, data.length);
} catch (Throwable t) {
}
return Class.forName(name);
}
};
Object button = new javax.swing.JButton();
Class originalClass = javax.swing.JButton.class;
Class artificialClass = Class.forName("javax.swing.JButton", false,
loader);
Object artificialButton = artificialClass.getConstructor()
.newInstance();
System.out.println("class hashCode");
System.out.println(originalClass.hashCode());
System.out.println(artificialClass.hashCode());
System.out.println("----------------------");
System.out.println("check if new JButton().getClass() == our class");
System.out.println("Original class: "
+ (button.getClass() == originalClass));
System.out.println("Artificial class: "
+ (button.getClass() == artificialClass));
System.out.println("----------------------");
System.out.println("toString for classes");
System.out.println("originalClass: " + originalClass);
System.out.println("artificialClass: " + artificialClass);
System.out.println("------------------");
System.out.println("Check if classes are the same...");
System.out.println("artificialButton.getClass() == button.getClass(): "
+ (artificialButton.getClass() == button.getClass()));
System.out.println("syntheticObject instanceof JButton: "
+ (artificialButton instanceof JButton));
try {
button = (JButton) artificialButton;
} catch (ClassCastException e) {
System.out.println(e);
System.out.println("Cannot cast from " + JButton.class + " to "
+ artificialButton.getClass() + "!?!");
}
System.out.println("This is the genuine button toString(): " + button);
try {
System.out.println("This is the artificial button toString(): "
+ artificialButton);
} catch (Throwable t) {
System.out.println(t);
System.out
.println("Cannot run toString for the artificial button because:");
System.out
.println("AbstractButton.defaultCapable is hidden for classes that don't inherit the AbstractButton.");
System.out
.println("And our artificialButton JButton tried to access the AbstractButton protected field.");
System.out
.println("And that AbstractButton is the JVM standard class loader - remember if (name.equals(\"javax.swing.JButton\"))?");
}
}
}