diff options
| author | 2018-07-23 13:20:47 +0800 | |
|---|---|---|
| committer | 2018-07-23 13:20:47 +0800 | |
| commit | e98dae3df050f180b1dfef8576d32df4c3c64663 (patch) | |
| tree | 5e6004b3c8f9565a58593ac5820620f8ddb5b7b0 /src/main/java/oml | |
| parent | remove injected DUP + getClass() + POP instructions (diff) | |
| download | enigma-e98dae3df050f180b1dfef8576d32df4c3c64663.tar.gz enigma-e98dae3df050f180b1dfef8576d32df4c3c64663.tar.xz enigma-e98dae3df050f180b1dfef8576d32df4c3c64663.zip | |
add classloader for adding deps
Diffstat (limited to 'src/main/java/oml')
| -rw-r--r-- | src/main/java/oml/ExtraClasspathTypeLoader.java | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/main/java/oml/ExtraClasspathTypeLoader.java b/src/main/java/oml/ExtraClasspathTypeLoader.java new file mode 100644 index 00000000..f8ec2e0c --- /dev/null +++ b/src/main/java/oml/ExtraClasspathTypeLoader.java | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | package oml; | ||
| 2 | |||
| 3 | import com.strobel.assembler.metadata.Buffer; | ||
| 4 | import com.strobel.assembler.metadata.ITypeLoader; | ||
| 5 | |||
| 6 | import java.io.File; | ||
| 7 | import java.io.IOException; | ||
| 8 | import java.io.InputStream; | ||
| 9 | import java.net.MalformedURLException; | ||
| 10 | import java.net.URL; | ||
| 11 | import java.net.URLClassLoader; | ||
| 12 | import java.util.Arrays; | ||
| 13 | |||
| 14 | /** | ||
| 15 | * Copy of ClasspathTypeLoader supporting a classpath constructor. | ||
| 16 | */ | ||
| 17 | public class ExtraClasspathTypeLoader implements ITypeLoader { | ||
| 18 | private final ClassLoader _loader; | ||
| 19 | |||
| 20 | public ExtraClasspathTypeLoader(String extraClasspath){ | ||
| 21 | _loader = new URLClassLoader(Arrays.stream(extraClasspath.split(File.pathSeparator)).map(path-> { | ||
| 22 | try { | ||
| 23 | return new File(path).toURI().toURL(); | ||
| 24 | } catch (MalformedURLException e) { | ||
| 25 | throw new RuntimeException(e); | ||
| 26 | } | ||
| 27 | }).toArray(URL[]::new)); | ||
| 28 | } | ||
| 29 | |||
| 30 | @Override | ||
| 31 | public boolean tryLoadType(final String internalName, final Buffer buffer) { | ||
| 32 | |||
| 33 | final String path = internalName.concat(".class"); | ||
| 34 | final URL resource = _loader.getResource(path); | ||
| 35 | |||
| 36 | if (resource == null) { | ||
| 37 | return false; | ||
| 38 | } | ||
| 39 | |||
| 40 | try (final InputStream stream = _loader.getResourceAsStream(path)) { | ||
| 41 | final byte[] temp = new byte[4096]; | ||
| 42 | |||
| 43 | int bytesRead; | ||
| 44 | |||
| 45 | while ((bytesRead = stream.read(temp, 0, temp.length)) > 0) { | ||
| 46 | //buffer.ensureWriteableBytes(bytesRead); | ||
| 47 | buffer.putByteArray(temp, 0, bytesRead); | ||
| 48 | } | ||
| 49 | |||
| 50 | buffer.flip(); | ||
| 51 | |||
| 52 | |||
| 53 | return true; | ||
| 54 | } | ||
| 55 | catch (final IOException ignored) { | ||
| 56 | return false; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||