Created
September 8, 2015 08:00
-
-
Save jsyeo/1e91207b98fc41b3bc95 to your computer and use it in GitHub Desktop.
Jar InputStream to ASM ClassReader Generator Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* © 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