Skip to content

Instantly share code, notes, and snippets.

@jsyeo
Created September 8, 2015 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsyeo/1e91207b98fc41b3bc95 to your computer and use it in GitHub Desktop.
Save jsyeo/1e91207b98fc41b3bc95 to your computer and use it in GitHub Desktop.
Jar InputStream to ASM ClassReader Generator Function
/*
* © Copyright 2015 - SourceClear Inc
*/
import com.codepoetics.protonpack.StreamUtils;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Opcodes;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.stream.Stream;
/**
*
*/
public class Main {
public static void main(String[] args) throws IOException {
InputStream is = Main.class.getClassLoader().getResourceAsStream("dotclass.jar");
StreamUtils.takeUntil(jarToClassReaders(is), x -> x == null).forEach( cr ->
cr.accept(new ReflectionClassVisitor(Opcodes.ASM5), Opcodes.ASM5)
);
}
static Stream<ClassReader> jarToClassReaders(InputStream jar) throws IOException {
JarInputStream jis = new JarInputStream(jar);
return Stream.generate(() -> {
try {
JarEntry jarEntry = jis.getNextJarEntry();
if (jarEntry != null &&
!jarEntry.isDirectory() &&
jarEntry.getName().toLowerCase().endsWith(".class")) {
ClassReader cr = new ClassReader(jis);
return cr;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment