summaryrefslogtreecommitdiff
path: root/enigma/src/main/java/cuchaz/enigma/source/cfr/CfrDecompiler.java
blob: cd7b2aa2f12919a077a1b800bd7605ac491c5548 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cuchaz.enigma.source.cfr;

import cuchaz.enigma.classprovider.ClassProvider;
import cuchaz.enigma.source.Decompiler;
import cuchaz.enigma.source.Source;
import cuchaz.enigma.source.SourceSettings;
import cuchaz.enigma.translation.mapping.EntryRemapper;
import cuchaz.enigma.utils.AsmUtil;
import org.benf.cfr.reader.apiunreleased.ClassFileSource2;
import org.benf.cfr.reader.apiunreleased.JarContent;
import org.benf.cfr.reader.bytecode.analysis.parse.utils.Pair;
import org.benf.cfr.reader.util.AnalysisType;
import org.benf.cfr.reader.util.getopt.Options;
import org.benf.cfr.reader.util.getopt.OptionsImpl;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.objectweb.asm.tree.ClassNode;

import java.util.Collection;
import java.util.Map;

public class CfrDecompiler implements Decompiler {
    // cfr doesn't add final on params so final setting is ignored
    private final SourceSettings settings;
    private final Options options;
    private final ClassFileSource2 classFileSource;

    public CfrDecompiler(ClassProvider classProvider, SourceSettings sourceSettings) {
        this.options = OptionsImpl.getFactory().create( Map.of("trackbytecodeloc", "true"));
        this.settings = sourceSettings;
        this.classFileSource = new ClassFileSource(classProvider);
    }

    @Override
    public Source getSource(String className, @Nullable EntryRemapper mapper) {
        return new CfrSource(className, settings, this.options, this.classFileSource, mapper);
    }

    private record ClassFileSource(ClassProvider classProvider) implements ClassFileSource2 {
        @Override
        public JarContent addJarContent(String s, AnalysisType analysisType) {
            return null;
        }

        @Override
        public void informAnalysisRelativePathDetail(String usePath, String classFilePath) {
        }

        @Override
        public Collection<String> addJar(String jarPath) {
            return null;
        }

        @Override
        public String getPossiblyRenamedPath(String path) {
            return path;
        }

        @Override
        public Pair<byte[], String> getClassFileContent(String path) {
            ClassNode node = classProvider.get(path.substring(0, path.lastIndexOf('.')));

            if (node == null) {
                return null;
            }

            return new Pair<>(AsmUtil.nodeToBytes(node), path);
        }
    }
}