|
23 | 23 | */ |
24 | 24 | package com.sun.jna; |
25 | 25 |
|
| 26 | +import java.io.ByteArrayOutputStream; |
26 | 27 | import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.lang.ref.WeakReference; |
27 | 31 | import java.lang.reflect.Method; |
28 | 32 | import java.nio.charset.Charset; |
29 | 33 | import java.util.Arrays; |
|
38 | 42 | //@SuppressWarnings("unused") |
39 | 43 | public class NativeTest extends TestCase { |
40 | 44 |
|
| 45 | + |
| 46 | + public interface CollectibleLibrary extends Library { |
| 47 | + int abs(int value); |
| 48 | + |
| 49 | + class Mapper extends DefaultTypeMapper { |
| 50 | + } |
| 51 | + |
| 52 | + @Structure.FieldOrder({"value"}) |
| 53 | + class Value extends Structure { |
| 54 | + public int value; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static class CollectibleClassLoader extends ClassLoader { |
| 59 | + CollectibleClassLoader() { |
| 60 | + super(NativeTest.class.getClassLoader()); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected synchronized Class<?> loadClass(String name, boolean resolve) |
| 65 | + throws ClassNotFoundException { |
| 66 | + if (!name.startsWith(CollectibleLibrary.class.getName())) { |
| 67 | + return super.loadClass(name, resolve); |
| 68 | + } |
| 69 | + Class<?> type = findLoadedClass(name); |
| 70 | + if (type == null) { |
| 71 | + String resource = "/" + name.replace('.', '/') + ".class"; |
| 72 | + try (InputStream stream = NativeTest.class.getResourceAsStream(resource)) { |
| 73 | + if (stream == null) { |
| 74 | + throw new ClassNotFoundException(name); |
| 75 | + } |
| 76 | + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 77 | + byte[] buffer = new byte[1024]; |
| 78 | + int length; |
| 79 | + while ((length = stream.read(buffer)) != -1) { |
| 80 | + bytes.write(buffer, 0, length); |
| 81 | + } |
| 82 | + byte[] content = bytes.toByteArray(); |
| 83 | + type = defineClass(name, content, 0, content.length); |
| 84 | + } catch (IOException e) { |
| 85 | + throw new ClassNotFoundException(name, e); |
| 86 | + } |
| 87 | + } |
| 88 | + if (resolve) { |
| 89 | + resolveClass(type); |
| 90 | + } |
| 91 | + return type; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @SuppressWarnings("unchecked") |
| 96 | + private WeakReference<ClassLoader> loadCollectibleLibrary(boolean withMapper, |
| 97 | + boolean withLoader) throws Exception { |
| 98 | + ClassLoader loader = new CollectibleClassLoader(); |
| 99 | + Class<? extends Library> type = (Class<? extends Library>) |
| 100 | + loader.loadClass(CollectibleLibrary.class.getName()); |
| 101 | + Map<String, Object> options = new HashMap<>(); |
| 102 | + if (withMapper) { |
| 103 | + Object mapper = loader.loadClass(CollectibleLibrary.Mapper.class.getName()) |
| 104 | + .getDeclaredConstructor().newInstance(); |
| 105 | + assertSame(loader, mapper.getClass().getClassLoader()); |
| 106 | + options.put(Library.OPTION_TYPE_MAPPER, mapper); |
| 107 | + } |
| 108 | + if (withLoader) { |
| 109 | + options.put(Library.OPTION_CLASSLOADER, loader); |
| 110 | + } |
| 111 | + Library library = Native.load(Platform.C_LIBRARY_NAME, type, options); |
| 112 | + assertEquals(7, type.getMethod("abs", int.class).invoke(library, -7)); |
| 113 | + Class<?> nested = loader.loadClass(CollectibleLibrary.Value.class.getName()); |
| 114 | + assertSame(loader, type.getClassLoader()); |
| 115 | + assertSame(loader, nested.getClassLoader()); |
| 116 | + assertSame(Native.getLibraryOptions(type), Native.getLibraryOptions(nested)); |
| 117 | + assertSame(type, Native.findEnclosingLibraryClass(nested)); |
| 118 | + assertSame(options.get(Library.OPTION_TYPE_MAPPER), Native.getTypeMapper(nested)); |
| 119 | + return new WeakReference<>(loader); |
| 120 | + } |
| 121 | + |
| 122 | + private void assertClassLoaderCollected(WeakReference<ClassLoader> loader) throws Exception { |
| 123 | + for (int i = 0; i < 100 && loader.get() != null; i++) { |
| 124 | + System.gc(); |
| 125 | + Thread.sleep(50); |
| 126 | + } |
| 127 | + assertNull("Discarded library class loader is retained by cached options", loader.get()); |
| 128 | + } |
| 129 | + |
| 130 | + public void testDiscardedLibraryClassLoader() throws Exception { |
| 131 | + assertClassLoaderCollected(loadCollectibleLibrary(false, false)); |
| 132 | + } |
| 133 | + |
| 134 | + public void testDiscardedLibraryClassLoaderWithTypeMapper() throws Exception { |
| 135 | + assertClassLoaderCollected(loadCollectibleLibrary(true, false)); |
| 136 | + } |
| 137 | + |
| 138 | + public void testDiscardedLibraryClassLoaderWithLoaderOption() throws Exception { |
| 139 | + assertClassLoaderCollected(loadCollectibleLibrary(false, true)); |
| 140 | + } |
| 141 | + |
| 142 | + @SuppressWarnings("unchecked") |
| 143 | + public void testLibraryOptionsRetainedWhileClassIsLive() throws Exception { |
| 144 | + ClassLoader loader = new CollectibleClassLoader(); |
| 145 | + Class<? extends Library> type = (Class<? extends Library>) |
| 146 | + loader.loadClass(CollectibleLibrary.class.getName()); |
| 147 | + Object mapper = loader.loadClass(CollectibleLibrary.Mapper.class.getName()) |
| 148 | + .getDeclaredConstructor().newInstance(); |
| 149 | + Map<String, Object> options = new HashMap<>(); |
| 150 | + options.put(Library.OPTION_TYPE_MAPPER, mapper); |
| 151 | + Library library = Native.load(Platform.C_LIBRARY_NAME, type, options); |
| 152 | + Class<?> nested = loader.loadClass(CollectibleLibrary.Value.class.getName()); |
| 153 | + Map<String, Object> cached = Native.getLibraryOptions(type); |
| 154 | + assertSame(cached, Native.getLibraryOptions(nested)); |
| 155 | + for (int i = 0; i < 3; i++) { |
| 156 | + System.gc(); |
| 157 | + assertSame(cached, Native.getLibraryOptions(type)); |
| 158 | + assertSame(cached, Native.getLibraryOptions(nested)); |
| 159 | + assertSame(mapper, Native.getTypeMapper(nested)); |
| 160 | + assertSame(type, Native.findEnclosingLibraryClass(nested)); |
| 161 | + assertEquals(7, type.getMethod("abs", int.class).invoke(library, -7)); |
| 162 | + } |
| 163 | + } |
| 164 | + |
41 | 165 | private static final String UNICODE = "[\u0444]"; |
42 | 166 |
|
43 | 167 | public void testLoadLibraryMethods() throws Exception { |
|
0 commit comments