Skip to content

Commit b5fe9b5

Browse files
authored
Merge pull request #21 from moacirrf/15-test-the-possibility-to-replace-jd-core-by-cfr-another-java-decompiler
15 test the possibility to replace jd core by cfr another java decompiler
2 parents 39189d7 + 0fe1013 commit b5fe9b5

File tree

13 files changed

+241
-208
lines changed

13 files changed

+241
-208
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
Java Decompiler for Netbeans ![master](https://github.com/moacirrf/nb-java-decompiler/actions/workflows/maven-publish.yml/badge.svg)
22
=====================================
3-
Java Decompiler for Apache Netbeans, it is based on [jd-core](https://github.com/java-decompiler/jd-core), same core of [jd-gui](http://java-decompiler.github.io).
3+
Java Decompiler for Apache Netbeans uses [CFR - another java decompiler](https://www.benf.org/other/cfr/).
44

55
## Description
6-
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.
6+
This a very simple plugin, with only basic features. If you want to help, contact-me or make a pull-request.
77

88
- Apache Netbeans 13
99
- JDK 11+
10+
11+
## Licenses
12+
- NB Java Decompiler - GNU General Public License 3.0
13+
- CFR - another java decompiler - MIT license
1014

1115
## Screenshots
1216

13-
![NetbeansDecompileFoto11](https://user-images.githubusercontent.com/950706/146479624-4b1ce1a5-b08f-4358-9b0f-e7895d4a8a0d.png) ![NetbeansDecompileFoto12](https://user-images.githubusercontent.com/950706/146624779-b005e8b6-eb88-48a0-a48b-6ddd15e0efb5.png)
14-
15-
![NetbeansDecompileFoto13](https://user-images.githubusercontent.com/950706/146479949-0241a1bc-60cc-4fe9-a2e0-d4f93e0b0e03.png)
17+
<img src="https://user-images.githubusercontent.com/950706/172032392-3a52cbbe-b55f-4784-a1cb-d259a0df02ea.png" width="50%" /><img src="https://user-images.githubusercontent.com/950706/172032681-edfbe954-a11a-4594-a5f1-99f37293a16c.png" width="49.5%"/>
18+
<img src="https://user-images.githubusercontent.com/950706/172033148-4d7060f1-4370-4940-b3c8-cf1e45b7ba58.png" />

nb-configuration.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
10+
<!--
11+
Properties that influence various parts of the IDE, especially code formatting and the like.
12+
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
13+
That way multiple projects can share the same settings (useful for formatting rules for example).
14+
Any value defined here will override the pom.xml file value but is only applicable to the current project.
15+
-->
16+
<netbeans.hint.license>gpl30</netbeans.hint.license>
17+
</properties>
18+
</project-shared-configuration>

pom.xml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<plugin>
1111
<groupId>org.apache.netbeans.utilities</groupId>
1212
<artifactId>nbm-maven-plugin</artifactId>
13-
<version>4.5</version>
13+
<version>4.7</version>
1414
<extensions>true</extensions>
1515
<configuration>
1616
<author>Moacir da Roza Flores-moacirrf@gmail.com</author>
@@ -47,21 +47,21 @@
4747
</plugin>
4848
</plugins>
4949
</build>
50-
<dependencies>
51-
<!-- jd-core -->
50+
<dependencies>
51+
<!-- cfr -->
5252
<dependency>
53-
<groupId>org.jd</groupId>
54-
<artifactId>jd-core</artifactId>
55-
<version>1.1.3</version>
53+
<groupId>org.benf</groupId>
54+
<artifactId>cfr</artifactId>
55+
<version>0.152</version>
5656
</dependency>
57-
57+
5858
<!-- noexception -->
5959
<dependency>
6060
<groupId>com.machinezoo.noexception</groupId>
6161
<artifactId>noexception</artifactId>
6262
<version>1.8.0</version>
6363
</dependency>
64-
64+
6565
<!-- Junit -->
6666
<dependency>
6767
<groupId>org.junit.jupiter</groupId>
@@ -138,6 +138,12 @@
138138
<artifactId>org-openide-modules</artifactId>
139139
<version>RELEASE130</version>
140140
</dependency>
141+
142+
<dependency>
143+
<groupId>org.netbeans.api</groupId>
144+
<artifactId>org-netbeans-api-java-classpath</artifactId>
145+
<version>RELEASE130</version>
146+
</dependency>
141147
</dependencies>
142148

143149
<repositories>

src/main/java/com/mrf/javadecompiler/decompiler/DecompilerFactory.java

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

19+
import com.mrf.javadecompiler.decompiler.cfr.DecompilerClassImpl;
20+
1921
/**
2022
*
2123
* @author Moacir da Roza Flores <moacirrf@gmail.com>

src/main/java/com/mrf/javadecompiler/decompiler/DecompilerClassImpl.java renamed to src/main/java/com/mrf/javadecompiler/decompiler/cfr/DecompilerClassImpl.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
* You should have received a copy of the GNU General Public License
1515
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
17-
package com.mrf.javadecompiler.decompiler;
17+
package com.mrf.javadecompiler.decompiler.cfr;
1818

19-
import com.mrf.javadecompiler.decompiler.jdcore.PrinterImpl;
20-
import com.mrf.javadecompiler.decompiler.jdcore.LoaderImpl;
19+
import com.mrf.javadecompiler.decompiler.Decompiler;
2120
import static com.machinezoo.noexception.Exceptions.wrap;
2221
import com.mrf.javadecompiler.exception.ExceptionHandler;
2322
import com.mrf.javadecompiler.filesystems.FileSystemHelper;
24-
import org.jd.core.v1.ClassFileToJavaSourceDecompiler;
23+
import java.util.Map;
24+
import org.benf.cfr.reader.Main;
25+
import org.benf.cfr.reader.apiunreleased.ClassFileSource2;
26+
import org.benf.cfr.reader.state.DCCommonState;
27+
import org.benf.cfr.reader.util.getopt.Options;
28+
import org.benf.cfr.reader.util.getopt.OptionsImpl;
2529
import org.openide.filesystems.FileObject;
2630

2731
/**
@@ -30,21 +34,27 @@
3034
*/
3135
public final class DecompilerClassImpl implements Decompiler<String, FileObject> {
3236

33-
public static final String HEADER_COMMENT = "//\n"
34-
+ "// Source code recreated by Apache Netbeans\n"
35-
+ "// (powered by Java Decompiler http://java-decompiler.github.io )\n"
36-
+ "//\n";
37+
public static final String HEADER_COMMENT = "// Source code recreated by Apache Netbeans (NB Java Decompiler) \n";
38+
39+
private final Options options;
40+
41+
public DecompilerClassImpl() {
42+
options = new OptionsImpl(Map.of("comments", "false", "innerclasses", "true"));
43+
}
3744

3845
@Override
3946
public String decompile(FileObject file) {
4047
return wrap(ExceptionHandler::handleException).get(() -> {
41-
LoaderImpl loader = new LoaderImpl(FileSystemHelper.of(file));
42-
PrinterImpl printer = new PrinterImpl();
4348

44-
ClassFileToJavaSourceDecompiler decompiler = new ClassFileToJavaSourceDecompiler();
45-
decompiler.decompile(loader, printer, FileSystemHelper.extractName(file));
49+
String className = FileSystemHelper.extractName(file);
50+
FileSystemHelper helper = FileSystemHelper.of(file);
51+
52+
ClassFileSource2 classFileSource = new NetbeansClassFileSourceImpl(helper);
53+
54+
StringBuilder out = new StringBuilder(HEADER_COMMENT);
55+
Main.doClass(new DCCommonState(options, classFileSource), className, false, new PluginDumperFactory(out, options));
4656

47-
return HEADER_COMMENT + printer.toString();
57+
return out.toString();
4858
});
4959
}
5060

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (C) 2022 moacirrf
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package com.mrf.javadecompiler.decompiler.cfr;
18+
19+
import com.mrf.javadecompiler.filesystems.FileSystemHelper;
20+
import java.io.IOException;
21+
import java.util.Collection;
22+
import java.util.List;
23+
import static java.util.Objects.isNull;
24+
import org.benf.cfr.reader.apiunreleased.ClassFileSource2;
25+
import org.benf.cfr.reader.apiunreleased.JarContent;
26+
import org.benf.cfr.reader.bytecode.analysis.parse.utils.Pair;
27+
import org.benf.cfr.reader.util.AnalysisType;
28+
import org.openide.filesystems.FileObject;
29+
30+
/**
31+
*
32+
* @author moacirrf
33+
*/
34+
public class NetbeansClassFileSourceImpl implements ClassFileSource2 {
35+
36+
private final FileSystemHelper helper;
37+
38+
public NetbeansClassFileSourceImpl(FileSystemHelper helper) {
39+
this.helper = helper;
40+
}
41+
42+
@Override
43+
public JarContent addJarContent(String jarPath, AnalysisType analysisType) {
44+
//we dont decompile complete jars
45+
return null;
46+
}
47+
48+
@Override
49+
public void informAnalysisRelativePathDetail(String usePath, String classFilePath) {
50+
// not used
51+
}
52+
53+
@Override
54+
public Collection<String> addJar(String jarPath) {
55+
// not used
56+
return List.of();
57+
}
58+
59+
@Override
60+
public String getPossiblyRenamedPath(String path) {
61+
return path;
62+
}
63+
64+
@Override
65+
public Pair<byte[], String> getClassFileContent(String path) throws IOException {
66+
FileObject fileObj = helper.findResource(path);
67+
if (isNull(fileObj)) {
68+
return null;
69+
}
70+
71+
return Pair.make(fileObj.asBytes(), path);
72+
}
73+
74+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (C) 2022 moacirrf
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package com.mrf.javadecompiler.decompiler.cfr;
18+
19+
import org.benf.cfr.reader.bytecode.analysis.types.JavaTypeInstance;
20+
import org.benf.cfr.reader.state.TypeUsageInformation;
21+
import org.benf.cfr.reader.util.getopt.Options;
22+
import org.benf.cfr.reader.util.getopt.OptionsImpl;
23+
import org.benf.cfr.reader.util.output.Dumper;
24+
import org.benf.cfr.reader.util.output.DumperFactory;
25+
import org.benf.cfr.reader.util.output.ExceptionDumper;
26+
import org.benf.cfr.reader.util.output.FileSummaryDumper;
27+
import org.benf.cfr.reader.util.output.IllegalIdentifierDump;
28+
import org.benf.cfr.reader.util.output.MethodErrorCollector;
29+
import org.benf.cfr.reader.util.output.NopSummaryDumper;
30+
import org.benf.cfr.reader.util.output.ProgressDumper;
31+
import org.benf.cfr.reader.util.output.ProgressDumperNop;
32+
import org.benf.cfr.reader.util.output.StringStreamDumper;
33+
import org.benf.cfr.reader.util.output.SummaryDumper;
34+
35+
/*
36+
* Class entirely copied from cfr.jar
37+
*/
38+
public class PluginDumperFactory implements DumperFactory {
39+
40+
private final IllegalIdentifierDump illegalIdentifierDump = new IllegalIdentifierDump.Nop();
41+
42+
private final StringBuilder outBuffer;
43+
private final Options options;
44+
45+
public PluginDumperFactory(StringBuilder out, Options options) {
46+
this.outBuffer = out;
47+
this.options = options;
48+
}
49+
50+
@Override
51+
public Dumper getNewTopLevelDumper(JavaTypeInstance classType, SummaryDumper summaryDumper, TypeUsageInformation typeUsageInformation, IllegalIdentifierDump illegalIdentifierDump) {
52+
return new StringStreamDumper(new MethodErrorCollector.SummaryDumperMethodErrorCollector(classType, summaryDumper), outBuffer, typeUsageInformation, options, this.illegalIdentifierDump);
53+
}
54+
55+
@Override
56+
public Dumper wrapLineNoDumper(Dumper dumper) {
57+
return dumper;
58+
}
59+
60+
/*
61+
* A summary dumper will receive errors. Generally, it's only of value when dumping jars to file.
62+
*/
63+
@Override
64+
public SummaryDumper getSummaryDumper() {
65+
if (!options.optionIsSet(OptionsImpl.OUTPUT_DIR)) {
66+
return new NopSummaryDumper();
67+
}
68+
69+
return new FileSummaryDumper(options.getOption(OptionsImpl.OUTPUT_DIR), options, null);
70+
}
71+
72+
@Override
73+
public ProgressDumper getProgressDumper() {
74+
return ProgressDumperNop.INSTANCE;
75+
}
76+
77+
@Override
78+
public ExceptionDumper getExceptionDumper() {
79+
return null;
80+
}
81+
82+
@Override
83+
public DumperFactory getFactoryWithPrefix(String prefix, int version) {
84+
return this;
85+
}
86+
}

src/main/java/com/mrf/javadecompiler/decompiler/jdcore/LoaderImpl.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)