Skip to content
This repository was archived by the owner on Nov 28, 2020. It is now read-only.

Commit 0ac0108

Browse files
committed
Replace deprecated CFR runner with public API
1 parent da0d80c commit 0ac0108

File tree

1 file changed

+52
-38
lines changed

1 file changed

+52
-38
lines changed

src/me/coley/jremapper/ui/CodePane.java

+52-38
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
import java.awt.Toolkit;
44
import java.io.IOException;
55
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.*;
117
import javafx.event.EventHandler;
128
import javafx.geometry.Side;
139
import javafx.scene.control.Label;
@@ -23,11 +19,10 @@
2319
import javafx.util.Duration;
2420
import jregex.Matcher;
2521
import jregex.Pattern;
26-
27-
import org.benf.cfr.reader.PluginRunner;
22+
import org.benf.cfr.reader.api.CfrDriver;
2823
import org.benf.cfr.reader.api.ClassFileSource;
24+
import org.benf.cfr.reader.api.OutputSinkFactory;
2925
import org.benf.cfr.reader.bytecode.analysis.parse.utils.Pair;
30-
import org.benf.cfr.reader.state.DCCommonState;
3126
import org.controlsfx.control.HiddenSidesPane;
3227
import org.controlsfx.control.textfield.CustomTextField;
3328
import org.fxmisc.flowless.VirtualizedScrollPane;
@@ -470,11 +465,16 @@ private void setupRegions(String decompile) {
470465
private String decompile() {
471466
CFRResourceLookup lookupHelper = new CFRResourceLookup();
472467
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();
478478
// JavaParser does NOT like inline comments like this.
479479
decompilation = decompilation.replace("/* synthetic */ ", "");
480480
decompilation = decompilation.replace("/* bridge */ ", "");
@@ -558,31 +558,6 @@ private String getStyleClass(Matcher matcher) {
558558
//@formatter:on
559559
}
560560

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-
586561
/**
587562
* Extension of CFR's front-end for looking up resources. Successful lookups
588563
* allow more accurate decompilations.
@@ -636,6 +611,45 @@ public byte[] get(String path) {
636611
}
637612
}
638613
}
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+
};
639653

640654
/**
641655
* CFR option map.

0 commit comments

Comments
 (0)