4

I'm trying to use the Java ASM library, and after quite a bit of research, I haven't found the solutions to a problem I'm having. I'm trying to make an application that does the following:

  • Reads classes from an external Jar file
  • Remove completely certain methods from specified classes
  • Print to console or store the modified class' compiled bytecode or compiled class contents, like from new String(cw.toByteArray()), cw being an instance of ClassWriter (Similar to how you would see if you edited a class file with notepad)

I have been able to read bytecode and the compiled class contents, but not remove methods. I haven't been able to find any working code, only partial code with very little explanation of how to implement it.

RubbaBoy
  • 41
  • 2
  • 5
  • 1
    `new String(cw.toByteArray())` doesn’t make any sense. – Holger Feb 01 '17 at 20:16
  • Instead of `new String(cw.toByteArray())` to view contents, why not use a Textifier? It makes reading the class way easier as it prints out the bytecode of a given class in a nicely formatted way. – Display Name Feb 05 '17 at 03:16

1 Answers1

3

You can implement a ClassVisitor that overrides the visitMethod method and return null from this method without invoking super.visitMethod when you discover a certain pattern.

This way, the method in question is skipped and not added to the created class file.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192