github.com/xyproto/orbiton/v2@v2.65.12-0.20240516144430-e10a419274ec/filter.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"os/exec"
     7  
     8  	"github.com/xyproto/mode"
     9  )
    10  
    11  // LoadClass attempts to convert a .class file with "jad" and then
    12  // save the output as a new filename, so that .class files can be
    13  // opened as the disassembled version.
    14  func (e *Editor) LoadClass(filename string) ([]byte, error) {
    15  	jadCommand := exec.Command("jad", "-b", "-dead", "-f", "-ff", "-o", "-p", "-s", "-space", ".decompiled.java", filename)
    16  	saveCommand(jadCommand)
    17  	output, err := jadCommand.Output() // ignore warnings on stderr
    18  	if err != nil {
    19  		return []byte{}, err
    20  	}
    21  	e.mode = mode.Java
    22  	e.filename = filename[:len(filename)-len(".class")] + ".decompiled.java"
    23  
    24  	// Remove "java.lang." qualifiers that are not needed
    25  	data := bytes.ReplaceAll(output, []byte("java.lang."), []byte{})
    26  
    27  	if err := os.WriteFile(e.filename, data, 0o644); err != nil {
    28  		return data, err
    29  	}
    30  	return data, nil
    31  }