diff options
Diffstat (limited to 'src/cuchaz/enigma/analysis/JarClassIterator.java')
| -rw-r--r-- | src/cuchaz/enigma/analysis/JarClassIterator.java | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/analysis/JarClassIterator.java b/src/cuchaz/enigma/analysis/JarClassIterator.java new file mode 100644 index 0000000..aa58e9e --- /dev/null +++ b/src/cuchaz/enigma/analysis/JarClassIterator.java | |||
| @@ -0,0 +1,137 @@ | |||
| 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 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.analysis; | ||
| 12 | |||
| 13 | import java.io.ByteArrayOutputStream; | ||
| 14 | import java.io.IOException; | ||
| 15 | import java.io.InputStream; | ||
| 16 | import java.util.Enumeration; | ||
| 17 | import java.util.Iterator; | ||
| 18 | import java.util.List; | ||
| 19 | import java.util.jar.JarEntry; | ||
| 20 | import java.util.jar.JarFile; | ||
| 21 | |||
| 22 | import javassist.ByteArrayClassPath; | ||
| 23 | import javassist.ClassPool; | ||
| 24 | import javassist.CtClass; | ||
| 25 | import javassist.NotFoundException; | ||
| 26 | import javassist.bytecode.Descriptor; | ||
| 27 | |||
| 28 | import com.google.common.collect.Lists; | ||
| 29 | |||
| 30 | import cuchaz.enigma.Constants; | ||
| 31 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 32 | |||
| 33 | public class JarClassIterator implements Iterator<CtClass> { | ||
| 34 | |||
| 35 | private JarFile m_jar; | ||
| 36 | private Iterator<JarEntry> m_iter; | ||
| 37 | |||
| 38 | public JarClassIterator(JarFile jar) { | ||
| 39 | m_jar = jar; | ||
| 40 | |||
| 41 | // get the jar entries that correspond to classes | ||
| 42 | List<JarEntry> classEntries = Lists.newArrayList(); | ||
| 43 | Enumeration<JarEntry> entries = m_jar.entries(); | ||
| 44 | while (entries.hasMoreElements()) { | ||
| 45 | JarEntry entry = entries.nextElement(); | ||
| 46 | |||
| 47 | // is this a class file? | ||
| 48 | if (entry.getName().endsWith(".class")) { | ||
| 49 | classEntries.add(entry); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | m_iter = classEntries.iterator(); | ||
| 53 | } | ||
| 54 | |||
| 55 | @Override | ||
| 56 | public boolean hasNext() { | ||
| 57 | return m_iter.hasNext(); | ||
| 58 | } | ||
| 59 | |||
| 60 | @Override | ||
| 61 | public CtClass next() { | ||
| 62 | JarEntry entry = m_iter.next(); | ||
| 63 | try { | ||
| 64 | return getClass(m_jar, entry); | ||
| 65 | } catch (IOException | NotFoundException ex) { | ||
| 66 | throw new Error("Unable to load class: " + entry.getName()); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | @Override | ||
| 71 | public void remove() { | ||
| 72 | throw new UnsupportedOperationException(); | ||
| 73 | } | ||
| 74 | |||
| 75 | public static List<ClassEntry> getClassEntries(JarFile jar) { | ||
| 76 | List<ClassEntry> classEntries = Lists.newArrayList(); | ||
| 77 | Enumeration<JarEntry> entries = jar.entries(); | ||
| 78 | while (entries.hasMoreElements()) { | ||
| 79 | JarEntry entry = entries.nextElement(); | ||
| 80 | |||
| 81 | // is this a class file? | ||
| 82 | if (!entry.isDirectory() && entry.getName().endsWith(".class")) { | ||
| 83 | classEntries.add(getClassEntry(entry)); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | return classEntries; | ||
| 87 | } | ||
| 88 | |||
| 89 | public static Iterable<CtClass> classes(final JarFile jar) { | ||
| 90 | return new Iterable<CtClass>() { | ||
| 91 | @Override | ||
| 92 | public Iterator<CtClass> iterator() { | ||
| 93 | return new JarClassIterator(jar); | ||
| 94 | } | ||
| 95 | }; | ||
| 96 | } | ||
| 97 | |||
| 98 | public static CtClass getClass(JarFile jar, ClassEntry classEntry) { | ||
| 99 | try { | ||
| 100 | return getClass(jar, new JarEntry(classEntry.getName() + ".class")); | ||
| 101 | } catch (IOException | NotFoundException ex) { | ||
| 102 | throw new Error("Unable to load class: " + classEntry.getName()); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | private static CtClass getClass(JarFile jar, JarEntry entry) throws IOException, NotFoundException { | ||
| 107 | // read the class into a buffer | ||
| 108 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
| 109 | byte[] buf = new byte[Constants.KiB]; | ||
| 110 | int totalNumBytesRead = 0; | ||
| 111 | InputStream in = jar.getInputStream(entry); | ||
| 112 | while (in.available() > 0) { | ||
| 113 | int numBytesRead = in.read(buf); | ||
| 114 | if (numBytesRead < 0) { | ||
| 115 | break; | ||
| 116 | } | ||
| 117 | bos.write(buf, 0, numBytesRead); | ||
| 118 | |||
| 119 | // sanity checking | ||
| 120 | totalNumBytesRead += numBytesRead; | ||
| 121 | if (totalNumBytesRead > Constants.MiB) { | ||
| 122 | throw new Error("Class file " + entry.getName() + " larger than 1 MiB! Something is wrong!"); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | // get a javassist handle for the class | ||
| 127 | String className = Descriptor.toJavaName(getClassEntry(entry).getName()); | ||
| 128 | ClassPool classPool = new ClassPool(); | ||
| 129 | classPool.appendSystemPath(); | ||
| 130 | classPool.insertClassPath(new ByteArrayClassPath(className, bos.toByteArray())); | ||
| 131 | return classPool.get(className); | ||
| 132 | } | ||
| 133 | |||
| 134 | private static ClassEntry getClassEntry(JarEntry entry) { | ||
| 135 | return new ClassEntry(entry.getName().substring(0, entry.getName().length() - ".class".length())); | ||
| 136 | } | ||
| 137 | } | ||