summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/analysis/ParsedJar.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cuchaz/enigma/analysis/ParsedJar.java')
-rw-r--r--src/main/java/cuchaz/enigma/analysis/ParsedJar.java129
1 files changed, 0 insertions, 129 deletions
diff --git a/src/main/java/cuchaz/enigma/analysis/ParsedJar.java b/src/main/java/cuchaz/enigma/analysis/ParsedJar.java
deleted file mode 100644
index ddcda3e..0000000
--- a/src/main/java/cuchaz/enigma/analysis/ParsedJar.java
+++ /dev/null
@@ -1,129 +0,0 @@
1/*******************************************************************************
2 * Copyright (c) 2015 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser General Public
5 * License v3.0 which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 * <p>
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11
12package cuchaz.enigma.analysis;
13
14import com.google.common.io.ByteStreams;
15import cuchaz.enigma.CompiledSource;
16import cuchaz.enigma.bytecode.translators.LocalVariableFixVisitor;
17import cuchaz.enigma.translation.representation.entry.ClassEntry;
18import org.objectweb.asm.ClassReader;
19import org.objectweb.asm.ClassVisitor;
20import org.objectweb.asm.Opcodes;
21import org.objectweb.asm.tree.ClassNode;
22
23import javax.annotation.Nullable;
24import java.io.BufferedInputStream;
25import java.io.IOException;
26import java.io.InputStream;
27import java.util.*;
28import java.util.function.Consumer;
29import java.util.function.Function;
30import java.util.jar.JarEntry;
31import java.util.jar.JarFile;
32import java.util.jar.JarInputStream;
33
34public class ParsedJar implements CompiledSource {
35 private final Map<String, byte[]> classBytes;
36 private final Map<String, ClassNode> nodeCache = new HashMap<>();
37
38 public ParsedJar(JarFile jar) throws IOException {
39 Map<String, byte[]> uClassBytes = new LinkedHashMap<>();
40 try {
41 // get the jar entries that correspond to classes
42 Enumeration<JarEntry> entries = jar.entries();
43 while (entries.hasMoreElements()) {
44 JarEntry entry = entries.nextElement();
45 // is this a class file?
46 if (entry.getName().endsWith(".class")) {
47 try (InputStream input = new BufferedInputStream(jar.getInputStream(entry))) {
48 String path = entry.getName().substring(0, entry.getName().length() - ".class".length());
49 uClassBytes.put(path, ByteStreams.toByteArray(input));
50 }
51 }
52 }
53 } finally {
54 jar.close();
55 classBytes = Collections.unmodifiableMap(uClassBytes);
56 }
57 }
58
59 public ParsedJar(JarInputStream jar) throws IOException {
60 Map<String, byte[]> uClassBytes = new LinkedHashMap<>();
61 try {
62 // get the jar entries that correspond to classes
63 JarEntry entry;
64 while ((entry = jar.getNextJarEntry()) != null) {
65 // is this a class file?
66 if (entry.getName().endsWith(".class")) {
67 String path = entry.getName().substring(0, entry.getName().length() - ".class".length());
68 uClassBytes.put(path, ByteStreams.toByteArray(jar));
69 jar.closeEntry();
70 }
71 }
72 } finally {
73 jar.close();
74 classBytes = Collections.unmodifiableMap(uClassBytes);
75 }
76 }
77
78 public void visitReader(Function<String, ClassVisitor> visitorFunction, int options) {
79 for (String s : classBytes.keySet()) {
80 ClassNode nodeCached = nodeCache.get(s);
81 if (nodeCached != null) {
82 nodeCached.accept(visitorFunction.apply(s));
83 } else {
84 new ClassReader(classBytes.get(s)).accept(visitorFunction.apply(s), options);
85 }
86 }
87 }
88
89 public void visitNode(Consumer<ClassNode> consumer) {
90 for (String s : classBytes.keySet()) {
91 consumer.accept(getClassNode(s));
92 }
93 }
94
95 public int getClassCount() {
96 return classBytes.size();
97 }
98
99 @Nullable
100 @Override
101 public ClassNode getClassNode(String name) {
102 return nodeCache.computeIfAbsent(name, (n) -> {
103 byte[] bytes = classBytes.get(name);
104 if (bytes == null) {
105 return null;
106 }
107
108 ClassReader reader = new ClassReader(bytes);
109 ClassNode node = new ClassNode();
110
111 LocalVariableFixVisitor visitor = new LocalVariableFixVisitor(Opcodes.ASM5, node);
112 reader.accept(visitor, 0);
113
114 return node;
115 });
116 }
117
118 public List<ClassEntry> getClassEntries() {
119 List<ClassEntry> entries = new ArrayList<>(classBytes.size());
120 for (String s : classBytes.keySet()) {
121 entries.add(new ClassEntry(s));
122 }
123 return entries;
124 }
125
126 public Map<String, byte[]> getClassDataMap() {
127 return classBytes;
128 }
129}