org.benf.cfr.reader.api.CfrDriver Java Examples

The following examples show how to use org.benf.cfr.reader.api.CfrDriver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Main.java    From cfr with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    GetOptParser getOptParser = new GetOptParser();

    Options options = null;
    List<String> files = null;
    try {
        Pair<List<String>, Options> processedArgs = getOptParser.parse(args, OptionsImpl.getFactory());
        files = processedArgs.getFirst();
        options = processedArgs.getSecond();
        if (files.size() == 0) {
            throw new IllegalArgumentException("Insufficient unqualified parameters - provide at least one filename.");
        }
    } catch (Exception e) {
        getOptParser.showHelp(e);
        System.exit(1);
    }

    if (options.optionIsSet(OptionsImpl.HELP) || files.isEmpty()) {
        getOptParser.showOptionHelp(OptionsImpl.getFactory(), options, OptionsImpl.HELP);
        return;
    }

    CfrDriver cfrDriver = new CfrDriver.Builder().withBuiltOptions(options).build();
    cfrDriver.analyse(files);
}
 
Example #2
Source File: CodePane.java    From JRemapper with MIT License 4 votes vote down vote up
/**
 * Run decompiler and fetch result.
 */
private String decompile() {
	CFRResourceLookup lookupHelper = new CFRResourceLookup();
	Map<String, String> options = new HashMap<>();
	options.put("usenametable", "true");
	SinkFactory sink = new SinkFactory();
	// Setup driver
	CfrDriver driver = new CfrDriver.Builder()
			.withOptions(options)
			.withClassFileSource(new CFRSourceImpl(lookupHelper))
			.withOutputSink(sink)
			.build();
	// Decompile
	driver.analyse(Collections.singletonList(path));
	String decompilation = sink.getDecompilation();
	// Fix decompilation text
	{
		// Get rid of header comment
		if(decompilation.startsWith("/*\n" + " * Decompiled with CFR.\n" + " */"))
			decompilation = decompilation.substring(decompilation.indexOf("*/") + 3);
		// JavaParser does NOT like inline comments like this.
		decompilation = decompilation.replace("/* synthetic */ ", "");
		decompilation = decompilation.replace("/* bridge */ ", "");
		decompilation = decompilation.replace("/* enum */ ", "");
		// <clinit> with modifiers
		decompilation = decompilation.replace("public static {", "static {");
		// Fix inner class names being busted
		String simple = path.contains("/") ? path.substring(path.lastIndexOf('/') + 1) : path;
		if(simple.contains("$")) {
			// They have "." instead of "$"
			decompilation = decompilation.replace(simple.replace("$", "."), simple);
			// Inners decompiled as top-level can't have static qualifier
			String startText = decompilation.substring(0, decompilation.indexOf(simple));
			if(startText.contains("static final class") || startText.contains("static class")) {
				decompilation = decompilation.replace(startText, startText.replace("static final class", "final class"));
				decompilation = decompilation.replace(startText, startText.replace("static class", "class"));
			}
		}
	}
	return decompilation;
}