Add methods for manipulating and iterating over the classes in a library

master
Graham 5 years ago
parent 77fc441a07
commit ca79e234bd
  1. 26
      asm/src/main/java/dev/openrs2/asm/Library.java

@ -5,6 +5,7 @@ import java.io.IOException;
import java.io.SequenceInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.jar.JarEntry;
@ -21,7 +22,7 @@ import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.util.CheckClassAdapter;
public final class Library {
public final class Library implements Iterable<ClassNode> {
private static final String CLASS_SUFFIX = ".class";
private static final String TEMP_PREFIX = "tmp";
private static final String JAR_SUFFIX = ".jar";
@ -41,7 +42,7 @@ public final class Library {
var reader = new ClassReader(in);
reader.accept(clazz, ClassReader.SKIP_DEBUG);
library.classes.put(clazz.name, clazz);
library.add(clazz);
}
}
@ -69,6 +70,27 @@ public final class Library {
/* empty */
}
public boolean contains(String name) {
return classes.containsKey(name);
}
public ClassNode get(String name) {
return classes.get(name);
}
public ClassNode add(ClassNode clazz) {
return classes.put(clazz.name, clazz);
}
public ClassNode remove(String name) {
return classes.remove(name);
}
@Override
public Iterator<ClassNode> iterator() {
return classes.values().iterator();
}
public void writeJar(Path path) throws IOException {
try (var out = new DeterministicJarOutputStream(Files.newOutputStream(path))) {
for (var entry : classes.entrySet()) {

Loading…
Cancel
Save