Search code examples
javainputstreaminstrumentationjava-bytecode-asm

using asm (java bytecode) classreader on a class outside of my project packages?


I'm getting by input the path of a class (lets say "C:\Temp\Test.class). What i'd like to do is to intialize a ClassReader to this class and start instrumentation it using ClassAdapter and ClassWriter ( i know how to do the instrumentation).

What I dont know- is how do I "fecth" that class into class reader?

I tried using the constructor ClassReader(String) but it was in vain

thanks ! (and sorry for my bad english)


Solution

  • I don't know the library you're using — apparently it's ASM. Anyway I see that the constructor you're trying to use does not take a file name, but the fully qualified name of a class. However, there's another constructor that takes an InputStream. So you must be able to solve your problem passing a FileInputStream to this constructor:

    ClassReader r = new ClassReader(new FileInputStream(filename));