package f_asm_and_javassist;
import jdk. internal .org.objectweb.asm.* ;
import java.io. * ;
import static jdk. internal .org.objectweb.asm.Opcodes.ASM5;
/* *
* @Author zhangboqing
* @Date 2020/3/26
*/
public class AsmDemo {
// Methods and fields to access the class
public static void main(String[] args) {
byte [] bytes = getBytes(); // Byte array of the MyMain.class file
ClassReader cr = new ClassReader(bytes);
ClassWriter cw = new ClassWriter( 0 );
ClassVisitor cv = new ClassVisitor(ASM5, cw) {
@Override
public FieldVisitor visitField( int access, String name, String desc, String signature, Object value) {
System. out .println( " field: " + name);
return super.visitField(access, name, desc, signature, value);
}
@Override
public MethodVisitor visitMethod( int access, String name, String desc, String signature, String[] exceptions) {
System. out .println( " method: " + name);
return super.visitMethod(access, name, desc, signature, exceptions);
}
};
cr.accept(cv, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG);
}
private static byte [] getBytes() {
StringBuilder sb = new StringBuilder();
try (FileInputStream fileInputStream = new FileInputStream( new File( " MyMain.class " ));
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
byte [] buffer = new byte [ 1024 * 8 ];
while (bufferedInputStream.available()> 0 ) {
int length = bufferedInputStream.read(buffer);
sb.append( new String(buffer, 0 ,length) );
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString().getBytes();
}
}