|
17 | 17 | package io.github.moacirrf.javadecompiler; |
18 | 18 |
|
19 | 19 | import static com.machinezoo.noexception.Exceptions.wrap; |
| 20 | + |
20 | 21 | import static java.util.Objects.nonNull; |
| 22 | + |
21 | 23 | import io.github.moacirrf.javadecompiler.files.TempDir; |
22 | 24 | import io.github.moacirrf.javadecompiler.validator.FileValidator; |
| 25 | + |
23 | 26 | import java.awt.event.ActionEvent; |
24 | 27 | import java.awt.event.ActionListener; |
25 | 28 | import java.nio.file.Files; |
26 | 29 | import java.nio.file.Path; |
| 30 | + |
27 | 31 | import org.netbeans.api.java.source.UiUtils; |
28 | 32 | import org.openide.loaders.DataObject; |
29 | 33 | import org.openide.awt.ActionID; |
|
35 | 39 | import org.openide.util.NbBundle.Messages; |
36 | 40 |
|
37 | 41 | @ActionID( |
38 | | - category = "Tools", |
39 | | - id = "io.github.moacirrf.javadecompiler.DecompileAction" |
| 42 | + category = "Tools", |
| 43 | + id = "io.github.moacirrf.javadecompiler.DecompileFernflowerAction" |
40 | 44 | ) |
41 | 45 | @ActionRegistration( |
42 | | - displayName = "#CTL_DecompileAction" |
| 46 | + displayName = "#CTL_DecompileWithFernflower", |
| 47 | + iconInMenu = false |
43 | 48 | ) |
44 | | -@ActionReferences(value = { |
45 | | - @ActionReference(path = "Editors/Popup", position = 1425), |
46 | | - @ActionReference(path = "UI/ToolActions", position = 2950) |
| 49 | +@ActionReferences({ |
| 50 | + @ActionReference(path = "Editors/Popup/Decompile"), |
| 51 | + @ActionReference(path = "UI/ToolActions/Decompile", position = 2) |
| 52 | +}) |
| 53 | +@Messages({ |
| 54 | + "CTL_DecompileWithFernflower=Decompile With Fernflower" |
47 | 55 | }) |
48 | | -@Messages("CTL_DecompileAction=Decompile") |
49 | | -public final class DecompileAction implements ActionListener { |
| 56 | +public final class DecompileFernflowerAction implements ActionListener { |
| 57 | + |
| 58 | + private static final String DECOMPILER = "fernflower"; |
50 | 59 |
|
51 | 60 | private final DataObject context; |
52 | 61 | private final Path decompilerDir; |
53 | 62 |
|
54 | | - public DecompileAction(DataObject context) { |
55 | | - this.context = context; |
56 | | - this.decompilerDir = TempDir.getTempDir(); |
| 63 | + public DecompileFernflowerAction(DataObject context) { |
| 64 | + this.context = context; |
| 65 | + this.decompilerDir = Path.of(TempDir.getTempDir().toString(), DECOMPILER); |
| 66 | + wrap(ExceptionHandler::handleException).run(() -> { |
| 67 | + if (!Files.exists(decompilerDir)) { |
| 68 | + Files.createDirectory(decompilerDir); |
| 69 | + } |
| 70 | + }); |
57 | 71 | } |
58 | 72 |
|
59 | 73 | @Override |
60 | 74 | public void actionPerformed(ActionEvent ev) { |
61 | | - FileObject file = context.getPrimaryFile(); |
62 | | - if (FileValidator.validate(file)) { |
63 | | - Decompiler<String, FileObject> decompiler = DecompilerFactory.create(); |
64 | | - writeToNewClass(file, decompiler.decompile(file)); |
65 | | - } |
| 75 | + FileObject file = context.getPrimaryFile(); |
| 76 | + if (FileValidator.validate(file)) { |
| 77 | + Decompiler<String, FileObject> decompiler = DecompilerFactory.create(DecompilerFactory.DecompilerType.FERNFLOWER); |
| 78 | + writeToNewClass(file, decompiler.decompile(file)); |
| 79 | + } |
66 | 80 | } |
67 | 81 |
|
68 | 82 | private void writeToNewClass(FileObject file, String decompiled) { |
69 | | - if (nonNull(decompiled) && !decompiled.isEmpty()) { |
70 | | - wrap(ExceptionHandler::handleException).run(() -> { |
71 | | - Path newFile = Path.of(decompilerDir.toString(), file.getName().concat(".java")); |
72 | | - if (Files.exists(newFile)) { |
73 | | - newFile.toFile().setWritable(true); |
74 | | - Files.delete(newFile); |
75 | | - } |
76 | | - Files.write(newFile, decompiled.getBytes()); |
77 | | - newFile.toFile().setReadOnly(); |
| 83 | + if (nonNull(decompiled) && !decompiled.isEmpty()) { |
| 84 | + wrap(ExceptionHandler::handleException).run(() -> { |
| 85 | + Path newFile = Path.of(decompilerDir.toString(), file.getName().concat(".java")); |
| 86 | + if (Files.exists(newFile)) { |
| 87 | + newFile.toFile().setWritable(true); |
| 88 | + Files.delete(newFile); |
| 89 | + } |
| 90 | + Files.write(newFile, decompiled.getBytes()); |
| 91 | + newFile.toFile().setReadOnly(); |
78 | 92 |
|
79 | | - FileObject newFileObject = FileUtil.createData(newFile.toFile()); |
80 | | - newFileObject.setAttribute("disable-java-errors", true); |
81 | | - UiUtils.open(newFileObject, 1); |
82 | | - }); |
83 | | - } |
| 93 | + FileObject newFileObject = FileUtil.createData(newFile.toFile()); |
| 94 | + newFileObject.setAttribute("disable-java-errors", true); |
| 95 | + UiUtils.open(newFileObject, 1); |
| 96 | + }); |
| 97 | + } |
84 | 98 | } |
85 | 99 | } |
0 commit comments