|
3 | 3 | import java.awt.Toolkit;
|
4 | 4 | import java.io.IOException;
|
5 | 5 | import java.lang.reflect.Field;
|
6 |
| -import java.util.Collection; |
7 |
| -import java.util.Collections; |
8 |
| -import java.util.HashMap; |
9 |
| -import java.util.List; |
10 |
| -import java.util.Map; |
| 6 | +import java.util.*; |
11 | 7 | import javafx.event.EventHandler;
|
12 | 8 | import javafx.geometry.Side;
|
13 | 9 | import javafx.scene.control.Label;
|
|
23 | 19 | import javafx.util.Duration;
|
24 | 20 | import jregex.Matcher;
|
25 | 21 | import jregex.Pattern;
|
26 |
| - |
27 |
| -import org.benf.cfr.reader.PluginRunner; |
| 22 | +import org.benf.cfr.reader.api.CfrDriver; |
28 | 23 | import org.benf.cfr.reader.api.ClassFileSource;
|
| 24 | +import org.benf.cfr.reader.api.OutputSinkFactory; |
29 | 25 | import org.benf.cfr.reader.bytecode.analysis.parse.utils.Pair;
|
30 |
| -import org.benf.cfr.reader.state.DCCommonState; |
31 | 26 | import org.controlsfx.control.HiddenSidesPane;
|
32 | 27 | import org.controlsfx.control.textfield.CustomTextField;
|
33 | 28 | import org.fxmisc.flowless.VirtualizedScrollPane;
|
@@ -470,11 +465,16 @@ private void setupRegions(String decompile) {
|
470 | 465 | private String decompile() {
|
471 | 466 | CFRResourceLookup lookupHelper = new CFRResourceLookup();
|
472 | 467 | Map<String, String> options = CFROpts.toStringMap();
|
473 |
| - CFRPluginRunner runner = new CFRPluginRunner(options, new CFRSourceImpl(lookupHelper)); |
474 |
| - String decompilation = runner.getDecompilationFor(path); |
475 |
| - if (decompilation.startsWith("/")) { |
476 |
| - decompilation = decompilation.substring(decompilation.indexOf("*/") + 3); |
477 |
| - } |
| 468 | + SinkFactory sink = new SinkFactory(); |
| 469 | + // Setup driver |
| 470 | + CfrDriver driver = new CfrDriver.Builder() |
| 471 | + .withClassFileSource(new CFRSourceImpl(lookupHelper)) |
| 472 | + .withOutputSink(sink) |
| 473 | + .withOptions(options) |
| 474 | + .build(); |
| 475 | + // Decompile |
| 476 | + driver.analyse(Collections.singletonList(path)); |
| 477 | + String decompilation = sink.getDecompilation(); |
478 | 478 | // JavaParser does NOT like inline comments like this.
|
479 | 479 | decompilation = decompilation.replace("/* synthetic */ ", "");
|
480 | 480 | decompilation = decompilation.replace("/* bridge */ ", "");
|
@@ -558,31 +558,6 @@ private String getStyleClass(Matcher matcher) {
|
558 | 558 | //@formatter:on
|
559 | 559 | }
|
560 | 560 |
|
561 |
| - /** |
562 |
| - * Extension of CFR's front-end. <br> |
563 |
| - * Uses reflection to expose internal components. Currently unused but may be |
564 |
| - * useful later. |
565 |
| - * |
566 |
| - * @author Matt |
567 |
| - */ |
568 |
| - private static class CFRPluginRunner extends PluginRunner { |
569 |
| - |
570 |
| - public CFRPluginRunner(Map<String, String> options, CFRSourceImpl src) { |
571 |
| - super(options, src); |
572 |
| - } |
573 |
| - |
574 |
| - @SuppressWarnings("unused") |
575 |
| - public DCCommonState getState() { |
576 |
| - try { |
577 |
| - return (DCCommonState) PluginRunner.class.getDeclaredField("dcCommonState").get(this); |
578 |
| - } catch (Exception e) { |
579 |
| - e.printStackTrace(); |
580 |
| - } |
581 |
| - return null; |
582 |
| - } |
583 |
| - |
584 |
| - } |
585 |
| - |
586 | 561 | /**
|
587 | 562 | * Extension of CFR's front-end for looking up resources. Successful lookups
|
588 | 563 | * allow more accurate decompilations.
|
@@ -636,6 +611,45 @@ public byte[] get(String path) {
|
636 | 611 | }
|
637 | 612 | }
|
638 | 613 | }
|
| 614 | + |
| 615 | + /** |
| 616 | + * CFR decompile output sink. |
| 617 | + * |
| 618 | + * @author Matt |
| 619 | + */ |
| 620 | + private static class SinkFactory implements OutputSinkFactory { |
| 621 | + private String decompile = "Failed to get CFR output"; |
| 622 | + |
| 623 | + @Override |
| 624 | + public List<SinkClass> getSupportedSinks(SinkType sinkType, Collection<SinkClass> collection) { |
| 625 | + return Arrays.asList(SinkClass.STRING); |
| 626 | + } |
| 627 | + |
| 628 | + @Override |
| 629 | + public <T> Sink<T> getSink(SinkType sinkType, SinkClass sinkClass) { |
| 630 | + switch (sinkType) { |
| 631 | + case EXCEPTION: |
| 632 | + return sinkable -> { |
| 633 | + Logging.error("CFR: " + sinkable); |
| 634 | + }; |
| 635 | + case JAVA: |
| 636 | + return sinkable -> { |
| 637 | + decompile = sinkable.toString(); |
| 638 | + }; |
| 639 | + case PROGRESS: |
| 640 | + return sinkable -> { |
| 641 | + Logging.info("CFR: " + sinkable); |
| 642 | + }; |
| 643 | + default: |
| 644 | + break; |
| 645 | + } |
| 646 | + return ignore -> {}; |
| 647 | + } |
| 648 | + |
| 649 | + public String getDecompilation() { |
| 650 | + return decompile; |
| 651 | + } |
| 652 | + }; |
639 | 653 |
|
640 | 654 | /**
|
641 | 655 | * CFR option map.
|
|
0 commit comments