diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/analysis/ParsedJar.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/analysis/ParsedJar.java | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/analysis/ParsedJar.java b/src/main/java/cuchaz/enigma/analysis/ParsedJar.java new file mode 100644 index 0000000..78ef722 --- /dev/null +++ b/src/main/java/cuchaz/enigma/analysis/ParsedJar.java | |||
| @@ -0,0 +1,72 @@ | |||
| 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 | |||
| 12 | package cuchaz.enigma.analysis; | ||
| 13 | |||
| 14 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 15 | import org.objectweb.asm.ClassReader; | ||
| 16 | import org.objectweb.asm.tree.ClassNode; | ||
| 17 | |||
| 18 | import java.io.IOException; | ||
| 19 | import java.io.InputStream; | ||
| 20 | import java.util.*; | ||
| 21 | import java.util.function.Consumer; | ||
| 22 | import java.util.jar.JarEntry; | ||
| 23 | import java.util.jar.JarFile; | ||
| 24 | |||
| 25 | public class ParsedJar { | ||
| 26 | private final Map<String, ClassNode> nodes = new LinkedHashMap<>(); | ||
| 27 | |||
| 28 | public ParsedJar(JarFile jar) throws IOException { | ||
| 29 | try { | ||
| 30 | // get the jar entries that correspond to classes | ||
| 31 | Enumeration<JarEntry> entries = jar.entries(); | ||
| 32 | while (entries.hasMoreElements()) { | ||
| 33 | JarEntry entry = entries.nextElement(); | ||
| 34 | // is this a class file? | ||
| 35 | if (entry.getName().endsWith(".class")) { | ||
| 36 | try (InputStream input = jar.getInputStream(entry)) { | ||
| 37 | // read the ClassNode from the jar | ||
| 38 | ClassReader reader = new ClassReader(input); | ||
| 39 | ClassNode node = new ClassNode(); | ||
| 40 | reader.accept(node, 0); | ||
| 41 | String path = entry.getName().substring(0, entry.getName().length() - ".class".length()); | ||
| 42 | nodes.put(path, node); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } finally { | ||
| 47 | jar.close(); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | public void visit(Consumer<ClassNode> visitor) { | ||
| 52 | for (ClassNode node : nodes.values()) { | ||
| 53 | visitor.accept(node); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | public int getClassCount() { | ||
| 58 | return nodes.size(); | ||
| 59 | } | ||
| 60 | |||
| 61 | public List<ClassEntry> getClassEntries() { | ||
| 62 | List<ClassEntry> entries = new ArrayList<>(nodes.size()); | ||
| 63 | for (ClassNode node : nodes.values()) { | ||
| 64 | entries.add(new ClassEntry(node.name)); | ||
| 65 | } | ||
| 66 | return entries; | ||
| 67 | } | ||
| 68 | |||
| 69 | public ClassNode getClassNode(String name) { | ||
| 70 | return nodes.get(name); | ||
| 71 | } | ||
| 72 | } | ||