| 3 | import java.io.IOException; |
| 4 | |
| 5 | public class LazyLoading { |
| 6 | public static boolean loadLazy; |
| 7 | |
| 8 | public static void expect(boolean v) { |
| 9 | if (! v) throw new RuntimeException(); |
| 10 | } |
| 11 | |
| 12 | private static File findClass(String name, File directory) { |
| 13 | for (File file: directory.listFiles()) { |
| 14 | if (file.isFile()) { |
| 15 | if (file.getName().equals(name + ".class")) { |
| 16 | return file; |
| 17 | } |
| 18 | } else if (file.isDirectory()) { |
| 19 | File result = findClass(name, file); |
| 20 | if (result != null) { |
| 21 | return result; |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | return null; |
| 26 | } |
| 27 | |
| 28 | private static byte[] read(File file) throws IOException { |
| 29 | byte[] bytes = new byte[(int) file.length()]; |
| 30 | FileInputStream in = new FileInputStream(file); |
| 31 | try { |
| 32 | if (in.read(bytes) != (int) file.length()) { |
| 33 | throw new RuntimeException(); |
| 34 | } |
| 35 | return bytes; |
| 36 | } finally { |
| 37 | in.close(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public static void main(String[] args) throws Exception { |
| 42 | Class c = new MyClassLoader(LazyLoading.class.getClassLoader()).loadClass |
| 43 | ("LazyLoading$Test"); |
| 44 | |
| 45 | c.getMethod("test").invoke(null); |
| 46 | } |
| 47 | |
| 48 | private static class MyClassLoader extends ClassLoader { |
| 49 | public MyClassLoader(ClassLoader parent) { |
| 50 | super(parent); |
| 51 | } |
| 52 | |
| 53 | protected Class findClass(String name) throws ClassNotFoundException { |
| 54 | try { |
| 55 | return defineClass |
| 56 | (name, read |
| 57 | (LazyLoading.findClass |
| 58 | (name, new File(System.getProperty("user.dir"))))); |
| 59 | } catch (IOException e) { |
| 60 | throw new RuntimeException(e); |
| 61 | } |
| 62 | } |
nothing calls this directly
no outgoing calls
no test coverage detected