diff options
| author | 2019-06-16 23:49:25 +0200 | |
|---|---|---|
| committer | 2019-06-16 23:49:25 +0200 | |
| commit | e27d5967029f4f3da8889dd673ba516dcd9f3ac8 (patch) | |
| tree | 71c98afad01cafdb2884da288e494e8761c2a8ff /src/main/java/cuchaz/enigma/analysis/ClassCache.java | |
| parent | Merge remote-tracking branch 'origin/master' into proposal-tweak (diff) | |
| download | enigma-fork-e27d5967029f4f3da8889dd673ba516dcd9f3ac8.tar.gz enigma-fork-e27d5967029f4f3da8889dd673ba516dcd9f3ac8.tar.xz enigma-fork-e27d5967029f4f3da8889dd673ba516dcd9f3ac8.zip | |
Plugin rework along with API rework: Enigma split from EnigmaProject; plugins now provide services configurable via a profile
Diffstat (limited to 'src/main/java/cuchaz/enigma/analysis/ClassCache.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/analysis/ClassCache.java | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/analysis/ClassCache.java b/src/main/java/cuchaz/enigma/analysis/ClassCache.java new file mode 100644 index 0000000..0bd78b3 --- /dev/null +++ b/src/main/java/cuchaz/enigma/analysis/ClassCache.java | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | package cuchaz.enigma.analysis; | ||
| 2 | |||
| 3 | import com.google.common.cache.Cache; | ||
| 4 | import com.google.common.cache.CacheBuilder; | ||
| 5 | import com.google.common.collect.ImmutableSet; | ||
| 6 | import cuchaz.enigma.CompiledSource; | ||
| 7 | import cuchaz.enigma.ProgressListener; | ||
| 8 | import cuchaz.enigma.analysis.index.JarIndex; | ||
| 9 | import cuchaz.enigma.bytecode.translators.LocalVariableFixVisitor; | ||
| 10 | import org.objectweb.asm.ClassReader; | ||
| 11 | import org.objectweb.asm.ClassVisitor; | ||
| 12 | import org.objectweb.asm.Opcodes; | ||
| 13 | import org.objectweb.asm.tree.ClassNode; | ||
| 14 | |||
| 15 | import javax.annotation.Nullable; | ||
| 16 | import java.io.IOException; | ||
| 17 | import java.nio.file.FileSystem; | ||
| 18 | import java.nio.file.FileSystems; | ||
| 19 | import java.nio.file.Files; | ||
| 20 | import java.nio.file.Path; | ||
| 21 | import java.util.concurrent.ExecutionException; | ||
| 22 | import java.util.concurrent.TimeUnit; | ||
| 23 | import java.util.function.Supplier; | ||
| 24 | |||
| 25 | public final class ClassCache implements AutoCloseable, CompiledSource { | ||
| 26 | private final FileSystem fileSystem; | ||
| 27 | private final ImmutableSet<String> classNames; | ||
| 28 | |||
| 29 | private final Cache<String, ClassNode> nodeCache = CacheBuilder.newBuilder() | ||
| 30 | .maximumSize(128) | ||
| 31 | .expireAfterAccess(1, TimeUnit.MINUTES) | ||
| 32 | .build(); | ||
| 33 | |||
| 34 | private ClassCache(FileSystem fileSystem, ImmutableSet<String> classNames) { | ||
| 35 | this.fileSystem = fileSystem; | ||
| 36 | this.classNames = classNames; | ||
| 37 | } | ||
| 38 | |||
| 39 | public static ClassCache of(Path jarPath) throws IOException { | ||
| 40 | FileSystem fileSystem = FileSystems.newFileSystem(jarPath, null); | ||
| 41 | ImmutableSet<String> classNames = collectClassNames(fileSystem); | ||
| 42 | |||
| 43 | return new ClassCache(fileSystem, classNames); | ||
| 44 | } | ||
| 45 | |||
| 46 | private static ImmutableSet<String> collectClassNames(FileSystem fileSystem) throws IOException { | ||
| 47 | ImmutableSet.Builder<String> classNames = ImmutableSet.builder(); | ||
| 48 | for (Path root : fileSystem.getRootDirectories()) { | ||
| 49 | Files.walk(root).map(Path::toString) | ||
| 50 | .forEach(path -> { | ||
| 51 | if (path.endsWith(".class")) { | ||
| 52 | String name = path.substring(1, path.length() - ".class".length()); | ||
| 53 | classNames.add(name); | ||
| 54 | } | ||
| 55 | }); | ||
| 56 | } | ||
| 57 | |||
| 58 | return classNames.build(); | ||
| 59 | } | ||
| 60 | |||
| 61 | @Nullable | ||
| 62 | @Override | ||
| 63 | public ClassNode getClassNode(String name) { | ||
| 64 | if (!classNames.contains(name)) { | ||
| 65 | return null; | ||
| 66 | } | ||
| 67 | |||
| 68 | try { | ||
| 69 | return nodeCache.get(name, () -> parseNode(name)); | ||
| 70 | } catch (ExecutionException e) { | ||
| 71 | throw new RuntimeException(e); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | private ClassNode parseNode(String name) throws IOException { | ||
| 76 | ClassReader reader = getReader(name); | ||
| 77 | |||
| 78 | ClassNode node = new ClassNode(); | ||
| 79 | |||
| 80 | LocalVariableFixVisitor visitor = new LocalVariableFixVisitor(Opcodes.ASM5, node); | ||
| 81 | reader.accept(visitor, 0); | ||
| 82 | |||
| 83 | return node; | ||
| 84 | } | ||
| 85 | |||
| 86 | private ClassReader getReader(String name) throws IOException { | ||
| 87 | Path path = fileSystem.getPath(name + ".class"); | ||
| 88 | |||
| 89 | byte[] bytes = Files.readAllBytes(path); | ||
| 90 | return new ClassReader(bytes); | ||
| 91 | } | ||
| 92 | |||
| 93 | public int getClassCount() { | ||
| 94 | return classNames.size(); | ||
| 95 | } | ||
| 96 | |||
| 97 | public void visit(Supplier<ClassVisitor> visitorSupplier, int readFlags) { | ||
| 98 | for (String className : classNames) { | ||
| 99 | ClassVisitor visitor = visitorSupplier.get(); | ||
| 100 | |||
| 101 | ClassNode cached = nodeCache.getIfPresent(className); | ||
| 102 | if (cached != null) { | ||
| 103 | cached.accept(visitor); | ||
| 104 | continue; | ||
| 105 | } | ||
| 106 | |||
| 107 | try { | ||
| 108 | ClassReader reader = getReader(className); | ||
| 109 | reader.accept(visitor, readFlags); | ||
| 110 | } catch (IOException e) { | ||
| 111 | System.out.println("Failed to visit class " + className); | ||
| 112 | e.printStackTrace(); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | @Override | ||
| 118 | public void close() throws IOException { | ||
| 119 | this.fileSystem.close(); | ||
| 120 | } | ||
| 121 | |||
| 122 | public JarIndex index(ProgressListener progress) { | ||
| 123 | JarIndex index = JarIndex.empty(); | ||
| 124 | index.indexJar(this, progress); | ||
| 125 | return index; | ||
| 126 | } | ||
| 127 | } | ||