|
20 | 20 | import static com.machinezoo.noexception.Exceptions.wrap; |
21 | 21 | import com.mrf.javadecompiler.exception.ExceptionHandler; |
22 | 22 | import com.mrf.javadecompiler.filesystems.FileSystemHelper; |
23 | | -import static com.mrf.javadecompiler.filesystems.TempDir.getTempDir; |
24 | | -import java.nio.file.Files; |
25 | | -import java.nio.file.Path; |
26 | 23 | import java.util.Map; |
27 | 24 | import org.benf.cfr.reader.Main; |
28 | 25 | import org.benf.cfr.reader.apiunreleased.ClassFileSource2; |
29 | | -import org.benf.cfr.reader.state.ClassFileSourceImpl; |
30 | 26 | import org.benf.cfr.reader.state.DCCommonState; |
31 | | -import org.benf.cfr.reader.util.AnalysisType; |
32 | 27 | import org.benf.cfr.reader.util.getopt.Options; |
33 | 28 | import org.benf.cfr.reader.util.getopt.OptionsImpl; |
34 | | -import org.benf.cfr.reader.util.output.DumperFactory; |
35 | 29 | import org.openide.filesystems.FileObject; |
36 | 30 |
|
37 | 31 | /** |
|
40 | 34 | */ |
41 | 35 | public final class DecompilerClassImpl implements Decompiler<String, FileObject> { |
42 | 36 |
|
43 | | - public static final String HEADER_COMMENT = "// Source code recreated by Apache Netbeans\n"; |
| 37 | + public static final String HEADER_COMMENT = "// Source code recreated by Apache Netbeans (NB Java Decompiler) \n"; |
44 | 38 |
|
45 | 39 | private final Options options; |
46 | | - private final ClassFileSource2 classFileSource; |
47 | | - private final DCCommonState dcCommonState; |
48 | 40 |
|
49 | 41 | public DecompilerClassImpl() { |
50 | | - options = new OptionsImpl(Map.of("comments", "false")); |
51 | | - classFileSource = new ClassFileSourceImpl(options); |
52 | | - classFileSource.informAnalysisRelativePathDetail(null, null); |
53 | | - dcCommonState = new DCCommonState(options, classFileSource); |
| 42 | + options = new OptionsImpl(Map.of("comments", "false", "innerclasses", "true")); |
54 | 43 | } |
55 | 44 |
|
56 | 45 | @Override |
57 | 46 | public String decompile(FileObject file) { |
58 | 47 | return wrap(ExceptionHandler::handleException).get(() -> { |
59 | 48 |
|
| 49 | + String className = FileSystemHelper.extractName(file); |
60 | 50 | FileSystemHelper helper = FileSystemHelper.of(file); |
61 | | - FileObject fileObject = helper.findResource(FileSystemHelper.extractName(file)); |
62 | 51 |
|
63 | | - //copy class file to temp before decompile |
64 | | - Path classFile = getTempDir().resolve(fileObject.getName() + fileObject.getExt()); |
65 | | - Files.write(classFile, fileObject.asBytes()); |
| 52 | + ClassFileSource2 classFileSource = new NetbeansClassFileSourceImpl(helper); |
66 | 53 |
|
67 | | - String decompiledClass = HEADER_COMMENT + decompile(classFile.toString()); |
68 | | - |
69 | | - //remove class file after decompile |
70 | | - Files.delete(classFile); |
| 54 | + StringBuilder out = new StringBuilder(HEADER_COMMENT); |
| 55 | + Main.doClass(new DCCommonState(options, classFileSource), className, false, new PluginDumperFactory(out, options)); |
71 | 56 |
|
72 | | - return decompiledClass; |
| 57 | + return out.toString(); |
73 | 58 | }); |
74 | 59 | } |
75 | 60 |
|
76 | | - private String decompile(String classPath) { |
77 | | - |
78 | | - StringBuilder out = new StringBuilder(); |
79 | | - |
80 | | - DumperFactory dumperFactory = new PluginDumperFactory(out, options); |
81 | | - |
82 | | - AnalysisType type = options.getOption(OptionsImpl.ANALYSE_AS); |
83 | | - |
84 | | - if (type == null || type == AnalysisType.DETECT) { |
85 | | - type = dcCommonState.detectClsJar(classPath); |
86 | | - } |
87 | | - |
88 | | - if (type == AnalysisType.CLASS) { |
89 | | - Main.doClass(dcCommonState, classPath, false, dumperFactory); |
90 | | - } |
91 | | - return out.toString(); |
92 | | - } |
93 | | - |
94 | 61 | } |
0 commit comments