content: package org.example.common;
  
  import org.benf.cfr.reader.api.CfrDriver;
  import org.benf.cfr.reader.api.OutputSinkFactory;
  import org.example.utils.StringUtils;
  
  import java.util.*;
  
  /**
   *
   * referer https://github.com/alibaba/arthas/blob/master/core/src/main/java/com/taobao/arthas/core/util/Decompiler.java
   *
   */
  
  public class Decompiler {
      public static String decompile(String classFilePath, String methodName, boolean hideUnicode) {
          final StringBuilder result = new StringBuilder(8192);
  
          OutputSinkFactory mySink = new OutputSinkFactory() {
              @Override
              public List<SinkClass> getSupportedSinks(SinkType sinkType, Collection<SinkClass> collection) {
                  return Arrays.asList(SinkClass.STRING, SinkClass.DECOMPILED, SinkClass.DECOMPILED_MULTIVER,
                          SinkClass.EXCEPTION_MESSAGE);
              }
  
              @Override
              public <T> Sink<T> getSink(final SinkType sinkType, SinkClass sinkClass) {
                  return new Sink<T>() {
                      @Override
                      public void write(T sinkable) {
                          // skip message like: Analysing type demo.MathGame
                          if (sinkType == SinkType.PROGRESS) {
                              return;
                          }
                          result.append(sinkable);
                      }
                  };
              }
          };
  
          HashMap<String, String> options = new HashMap<String, String>();
          /**
           * @see org.benf.cfr.reader.util.MiscConstants.Version.getVersion() Currently,
           *      the cfr version is wrong. so disable show cfr version.
           */
          options.put("showversion", "false");
          options.put("hideutf", String.valueOf(hideUnicode));
          if (!StringUtils.isBlank(methodName)) {
              options.put("methodname", methodName);
          }
  
          CfrDriver driver = new CfrDriver.Builder().withOptions(options).withOutputSink(mySink).build();
          List<String> toAnalyse = new ArrayList<String>();
          toAnalyse.add(classFilePath);
          driver.analyse(toAnalyse);
  
          return result.toString();
      }
  
  }
  
  