Skip to content

Commit f86bf4c

Browse files
authored
Merge pull request #5 from moacirrf/ft-improve-editor
Ft improve editor
2 parents 78afc0a + de56c7d commit f86bf4c

File tree

8 files changed

+90
-41
lines changed

8 files changed

+90
-41
lines changed

.github/workflows/maven-publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ jobs:
88

99
steps:
1010
- uses: actions/checkout@v2
11-
- name: Set up JDK 8.0.312
11+
- name: Set up JDK 11
1212
uses: actions/setup-java@v2
1313
with:
14-
java-version: '8.0.312+7'
14+
java-version: '11'
1515
distribution: 'adopt'
1616
- name: NBM
1717
run: mvn clean install

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Java Decompiler for Apache Netbeans, it is based on [jd-core](https://github.com
55
## Description
66
This a very simple plugin, with only basic features, if you have some tips or want to help, make a fork and open a pull-request.
77

8-
- Apache Netbeans 9+
9-
- JDK 8+
8+
- Apache Netbeans 13
9+
- JDK 11+
1010

1111
## Screenshots
1212

pom.xml

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.mrf.javadecompiler</groupId>
55
<artifactId>nb-java-decompiler</artifactId>
6-
<version>0.0.1-beta</version>
6+
<version>0.0.2-beta</version>
77
<packaging>nbm</packaging>
88
<build>
99
<plugins>
@@ -12,20 +12,27 @@
1212
<artifactId>nbm-maven-plugin</artifactId>
1313
<version>4.5</version>
1414
<extensions>true</extensions>
15+
<configuration>
16+
<publicPackages>
17+
<publicPackage>com.mrf.javadecompiler</publicPackage>
18+
</publicPackages>
19+
<netbeansInstallation>/home/PROGRAMAS/instalados/ides/netbeans/</netbeansInstallation>
20+
<!-- <netbeansInstallation>${netbeansInstalationPath}</netbeansInstallation>-->
21+
</configuration>
1522
</plugin>
1623
<plugin>
1724
<groupId>org.apache.maven.plugins</groupId>
1825
<artifactId>maven-compiler-plugin</artifactId>
1926
<version>3.8.1</version>
2027
<configuration>
21-
<source>1.8</source>
22-
<target>1.8</target>
28+
<source>11</source>
29+
<target>11</target>
2330
</configuration>
2431
</plugin>
2532
<plugin>
2633
<groupId>org.apache.maven.plugins</groupId>
2734
<artifactId>maven-jar-plugin</artifactId>
28-
<version>3.1.2</version>
35+
<version>3.2.2</version>
2936
<configuration>
3037
<archive>
3138
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
@@ -46,70 +53,81 @@
4653
<dependency>
4754
<groupId>com.machinezoo.noexception</groupId>
4855
<artifactId>noexception</artifactId>
49-
<version>1.6.2</version>
56+
<version>1.8.0</version>
5057
</dependency>
5158

5259
<!-- Netbeans modules -->
5360
<dependency>
5461
<groupId>org.netbeans.api</groupId>
5562
<artifactId>org-netbeans-api-annotations-common</artifactId>
56-
<version>RELEASE90</version>
63+
<version>RELEASE130</version>
5764
</dependency>
5865

5966
<dependency>
6067
<groupId>org.netbeans.api</groupId>
6168
<artifactId>org-openide-filesystems</artifactId>
62-
<version>RELEASE90</version>
69+
<version>RELEASE130</version>
6370
</dependency>
6471

6572
<dependency>
6673
<groupId>org.netbeans.api</groupId>
6774
<artifactId>org-openide-awt</artifactId>
68-
<version>RELEASE90</version>
75+
<version>RELEASE130</version>
6976
</dependency>
7077

7178
<dependency>
7279
<groupId>org.netbeans.api</groupId>
7380
<artifactId>org-openide-util</artifactId>
74-
<version>RELEASE90</version>
81+
<version>RELEASE130</version>
7582
</dependency>
7683

7784
<dependency>
7885
<groupId>org.netbeans.api</groupId>
7986
<artifactId>org-openide-loaders</artifactId>
80-
<version>RELEASE90</version>
87+
<version>RELEASE130</version>
8188
</dependency>
8289

8390
<dependency>
8491
<groupId>org.netbeans.api</groupId>
8592
<artifactId>org-openide-windows</artifactId>
86-
<version>RELEASE90</version>
93+
<version>RELEASE130</version>
8794
</dependency>
8895

8996
<dependency>
9097
<groupId>org.netbeans.api</groupId>
9198
<artifactId>org-openide-util-ui</artifactId>
92-
<version>RELEASE90</version>
99+
<version>RELEASE130</version>
93100
</dependency>
94101

95102
<dependency>
96103
<groupId>org.netbeans.api</groupId>
97104
<artifactId>org-openide-util-lookup</artifactId>
98-
<version>RELEASE90</version>
105+
<version>RELEASE130</version>
99106
</dependency>
100107

101108
<dependency>
102109
<groupId>org.netbeans.api</groupId>
103110
<artifactId>org-netbeans-modules-settings</artifactId>
104-
<version>RELEASE90</version>
111+
<version>RELEASE130</version>
105112
</dependency>
106113

107114
<dependency>
108115
<groupId>org.netbeans.api</groupId>
109116
<artifactId>org-openide-text</artifactId>
110-
<version>RELEASE90</version>
117+
<version>RELEASE130</version>
118+
</dependency>
119+
120+
<dependency>
121+
<groupId>org.netbeans.api</groupId>
122+
<artifactId>org-openide-explorer</artifactId>
123+
<version>RELEASE130</version>
111124
</dependency>
112125

126+
<dependency>
127+
<groupId>org.netbeans.api</groupId>
128+
<artifactId>org-netbeans-modules-java-source</artifactId>
129+
<version>RELEASE130</version>
130+
</dependency>
113131
</dependencies>
114132

115133
<repositories>

src/main/java/com/mrf/javadecompiler/builder/SourceWindowBuilder.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.mrf.javadecompiler.constants.Constants.CLASS_EXT;
2222
import static com.mrf.javadecompiler.constants.Constants.EXCLAM;
2323
import static com.mrf.javadecompiler.constants.Constants.FILE;
24+
import static com.mrf.javadecompiler.constants.Constants.HEADER_COMMENT;
2425
import static com.mrf.javadecompiler.constants.Constants.JAR;
2526
import static com.mrf.javadecompiler.exception.ExceptionHandler.handleException;
2627
import static java.io.File.separator;
@@ -37,16 +38,11 @@
3738
*/
3839
public final class SourceWindowBuilder {
3940

40-
public static final String COMMENT = "//\n"
41-
+ "// Source code recreated by Apache Netbeans\n"
42-
+ "// (powered by Java Decompiler http://java-decompiler.github.io )\n"
43-
+ "//\n";
44-
4541
public static SourceWindowTopComponent build(FileObject file, String decompiledSource) {
4642
SourceWindowTopComponent window = new SourceWindowTopComponent();
4743
window.setName(getName(file));
4844
window.setToolTipText(getTooltip(file));
49-
window.setDecompiledSource(COMMENT + decompiledSource);
45+
window.setDecompiledSource(HEADER_COMMENT + decompiledSource);
5046
return window;
5147
}
5248

src/main/java/com/mrf/javadecompiler/constants/Constants.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@
1616
*/
1717
package com.mrf.javadecompiler.constants;
1818

19+
import static java.lang.System.getProperty;
20+
1921
/**
2022
*
2123
* @author Moacir da Roza Flores <[email protected]>
2224
*/
2325
public final class Constants {
2426

27+
public static final String HEADER_COMMENT = "//\n"
28+
+ "// Source code recreated by Apache Netbeans\n"
29+
+ "// (powered by Java Decompiler http://java-decompiler.github.io )\n"
30+
+ "//\n";
31+
32+
public static final String TEMP_DIR_PLUGIN = getProperty("java.io.tmpdir") + "/nb_java_decompiler";
2533
public static final String CLASSFILE_BINARY_NAME = "classfile-binaryName";
2634
public static final String CLASSFILE_ROOT = "classfile-root";
2735
public static final String CLASS_EXT = ".class";

src/main/java/com/mrf/javadecompiler/openapi/action/DecompileAction.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 moacirrf
2+
* Copyright (C) 2021 Moacir da Roza Flores <moacirrf@gmail.com>
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -16,28 +16,36 @@
1616
*/
1717
package com.mrf.javadecompiler.openapi.action;
1818

19+
import static com.machinezoo.noexception.Exceptions.wrap;
20+
import static com.mrf.javadecompiler.constants.Constants.HEADER_COMMENT;
1921
import static java.util.Objects.nonNull;
20-
import com.mrf.javadecompiler.builder.SourceWindowBuilder;
22+
import static com.mrf.javadecompiler.constants.Constants.TEMP_DIR_PLUGIN;
2123
import com.mrf.javadecompiler.decompiler.Decompiler;
24+
import static com.mrf.javadecompiler.exception.ExceptionHandler.handleException;
2225
import com.mrf.javadecompiler.factory.DecompilerFactory;
2326
import com.mrf.javadecompiler.validator.FileValidator;
2427
import java.awt.event.ActionEvent;
2528
import java.awt.event.ActionListener;
29+
import java.nio.file.Files;
30+
import java.nio.file.Path;
31+
import java.nio.file.Paths;
32+
import org.netbeans.api.java.source.UiUtils;
2633
import org.openide.loaders.DataObject;
2734
import org.openide.awt.ActionID;
2835
import org.openide.awt.ActionReference;
2936
import org.openide.awt.ActionReferences;
3037
import org.openide.awt.ActionRegistration;
3138
import org.openide.filesystems.FileObject;
39+
import org.openide.filesystems.FileUtil;
3240
import org.openide.util.NbBundle.Messages;
3341

3442
@ActionID(
35-
category = "Tools",
36-
id = "com.mrf.javadecompiler.openapi.DecompileAction"
43+
category = "Tools",
44+
id = "com.mrf.javadecompiler.openapi.DecompileAction"
3745
)
3846
@ActionRegistration(
39-
iconBase = "com/mrf/javadecompiler/openapi/jd_icon_16.png",
40-
displayName = "#CTL_DecompileAction"
47+
iconBase = "com/mrf/javadecompiler/openapi/jd_icon_16.png",
48+
displayName = "#CTL_DecompileAction"
4149
)
4250
@ActionReferences(value = {
4351
@ActionReference(path = "Editors/Popup", position = 1425),
@@ -47,21 +55,38 @@
4755
public final class DecompileAction implements ActionListener {
4856

4957
private final DataObject context;
58+
private final Path decompilerDir;
5059

5160
public DecompileAction(DataObject context) {
5261
this.context = context;
62+
this.decompilerDir = Paths.get(TEMP_DIR_PLUGIN);
63+
if (!Files.exists(decompilerDir)) {
64+
wrap(e -> handleException(e))
65+
.run(() -> Files.createDirectory(decompilerDir));
66+
}
5367
}
5468

5569
@Override
5670
public void actionPerformed(ActionEvent ev) {
5771
FileObject file = context.getPrimaryFile();
5872
if (FileValidator.validate(file)) {
5973
Decompiler<String> decompiler = DecompilerFactory.create();
60-
String decompiled = decompiler.decompile(file);
74+
writeToNewClass(file, decompiler.decompile(file));
75+
}
76+
}
6177

62-
if (nonNull(decompiled) && !decompiled.isEmpty()) {
63-
SourceWindowBuilder.build(file, decompiled).open();
64-
}
78+
private void writeToNewClass(FileObject file, String decompiled) {
79+
if (nonNull(decompiled) && !decompiled.isEmpty()) {
80+
wrap(e -> handleException(e)).run(() -> {
81+
Path newFile = Path.of(decompilerDir.toString(), file.getName().concat(".java"));
82+
if (Files.exists(newFile)) {
83+
Files.delete(newFile);
84+
}
85+
Files.write(newFile, HEADER_COMMENT.concat(decompiled).getBytes());
86+
newFile.toFile().setReadOnly();
87+
FileObject newFileObject = FileUtil.createData(newFile.toFile());
88+
UiUtils.open(newFileObject, 1);
89+
});
6590
}
6691
}
6792
}

src/main/java/com/mrf/javadecompiler/openapi/ui/SourceWindowTopComponent.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021 moacirrf
2+
* Copyright (C) 2021 Moacir da Roza Flores <moacirrf@gmail.com>
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -46,11 +46,17 @@
4646
@Messages({
4747
"CTL_SourceWindowAction=Java Decompiler"
4848
})
49+
@Deprecated
50+
/**
51+
* Actualy not used, maybe will be used again on the future.
52+
*
53+
*/
4954
public final class SourceWindowTopComponent extends TopComponent {
50-
55+
5156
public SourceWindowTopComponent() {
5257
initComponents();
5358
initEditorPane();
59+
5460
}
5561

5662
/**

src/main/java/com/mrf/javadecompiler/validator/FileValidator.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
import org.openide.awt.StatusDisplayer;
2525
import org.openide.filesystems.FileObject;
2626

27-
/**
28-
*
29-
* @author Moacir da Roza Flores <[email protected]>
30-
*/
3127
public final class FileValidator {
3228

3329
private static final String INVALID_FILE = "Invalid file.";

0 commit comments

Comments
 (0)