From ca79e234bd5cfcb9940acd7c7043c429e37d156b Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 29 Jul 2019 13:57:57 +0100 Subject: [PATCH] Add methods for manipulating and iterating over the classes in a library --- .../main/java/dev/openrs2/asm/Library.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/asm/src/main/java/dev/openrs2/asm/Library.java b/asm/src/main/java/dev/openrs2/asm/Library.java index 05d8980f..4d80755d 100644 --- a/asm/src/main/java/dev/openrs2/asm/Library.java +++ b/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 { 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 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()) {