From ff18524906fc7bcca35b8b092256b7a46bbffc74 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Thu, 13 Jan 2011 08:59:55 -0700 Subject: [PATCH] add DefineClass to test suite As reported on the discussion group, there is a problem with the ClassLoader.defineClass implementation sunch that this test is not currently passing, at least for the mode=debug and bootimage=true builds. I plan to address these failures soon, but I wanted to add a test first to make sure I could reproduce them. --- test/DefineClass.java | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 test/DefineClass.java diff --git a/test/DefineClass.java b/test/DefineClass.java new file mode 100644 index 0000000000..bfbf67b0cd --- /dev/null +++ b/test/DefineClass.java @@ -0,0 +1,52 @@ +import java.io.IOException; +import java.io.File; +import java.io.FileInputStream; + +public class DefineClass { + private static File findHello(File directory) { + File[] files = directory.listFiles(); + for (File file: directory.listFiles()) { + if (file.isFile()) { + if (file.getName().equals("Hello.class")) { + return file; + } + } else if (file.isDirectory()) { + File result = findHello(file); + if (result != null) { + return result; + } + } + } + return null; + } + + private static byte[] read(File file) throws IOException { + byte[] bytes = new byte[(int) file.length()]; + FileInputStream in = new FileInputStream(file); + try { + if (in.read(bytes) != (int) file.length()) { + throw new RuntimeException(); + } + return bytes; + } finally { + in.close(); + } + } + + public static void main(String[] args) throws Exception { + byte[] bytes = read(findHello(new File(System.getProperty("user.dir")))); + Class c = new MyClassLoader(DefineClass.class.getClassLoader()).defineClass + ("Hello", bytes); + c.getMethod("main", String[].class).invoke(null, (Object) new String[0]); + } + + private static class MyClassLoader extends ClassLoader { + public MyClassLoader(ClassLoader parent) { + super(parent); + } + + public Class defineClass(String name, byte[] bytes) { + return super.defineClass(name, bytes, 0, bytes.length); + } + } +}