Resolve class name from bytecode

后端 未结 5 584
陌清茗
陌清茗 2021-01-14 23:46

Is it possible to dig up a classes name from bytecode which is formed from the class\' source code?

The situation is this: I get a classes bytecode remotely from so

5条回答
  •  無奈伤痛
    2021-01-15 00:17

    The easiest way is probably using something like ASM:

    import org.objectweb.asm.ClassReader;
    import org.objectweb.asm.commons.EmptyVisitor;
    
    public class PrintClassName {
      public static void main(String[] args) throws IOException {
        class ClassNamePrinter extends EmptyVisitor {
          @Override
          public void visit(int version, int access, String name, String signature,
              String superName, String[] interfaces) {
            System.out.println("Class name: " + name);
          }
        }
    
        InputStream binary = new FileInputStream(args[0]);
        try {
          ClassReader reader = new ClassReader(binary);
          reader.accept(new ClassNamePrinter(), 0);
        } finally {
          binary.close();
        }
      }
    }
    

    If you can't use a 3rd party library, you could read the class file format yourself.

提交回复
热议问题