Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15 test the possibility to replace jd core by cfr another java decompiler #21

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
Java Decompiler for Netbeans ![master](https://github.com/moacirrf/nb-java-decompiler/actions/workflows/maven-publish.yml/badge.svg)
=====================================
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).
Java Decompiler for Apache Netbeans uses [CFR - another java decompiler](https://www.benf.org/other/cfr/).

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

- Apache Netbeans 13
- JDK 11+

## Licenses
- NB Java Decompiler - GNU General Public License 3.0
- CFR - another java decompiler - MIT license

## Screenshots

![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)

![NetbeansDecompileFoto13](https://user-images.githubusercontent.com/950706/146479949-0241a1bc-60cc-4fe9-a2e0-d4f93e0b0e03.png)
<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%"/>
<img src="https://user-images.githubusercontent.com/950706/172033148-4d7060f1-4370-4940-b3c8-cf1e45b7ba58.png" />
18 changes: 18 additions & 0 deletions nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.license>gpl30</netbeans.hint.license>
</properties>
</project-shared-configuration>
22 changes: 14 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<plugin>
<groupId>org.apache.netbeans.utilities</groupId>
<artifactId>nbm-maven-plugin</artifactId>
<version>4.5</version>
<version>4.7</version>
<extensions>true</extensions>
<configuration>
<author>Moacir da Roza Flores-moacirrf@gmail.com</author>
Expand Down Expand Up @@ -47,21 +47,21 @@
</plugin>
</plugins>
</build>
<dependencies>
<!-- jd-core -->
<dependencies>
<!-- cfr -->
<dependency>
<groupId>org.jd</groupId>
<artifactId>jd-core</artifactId>
<version>1.1.3</version>
<groupId>org.benf</groupId>
<artifactId>cfr</artifactId>
<version>0.152</version>
</dependency>

<!-- noexception -->
<dependency>
<groupId>com.machinezoo.noexception</groupId>
<artifactId>noexception</artifactId>
<version>1.8.0</version>
</dependency>

<!-- Junit -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down Expand Up @@ -138,6 +138,12 @@
<artifactId>org-openide-modules</artifactId>
<version>RELEASE130</version>
</dependency>

<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-api-java-classpath</artifactId>
<version>RELEASE130</version>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.mrf.javadecompiler.decompiler;

import com.mrf.javadecompiler.decompiler.cfr.DecompilerClassImpl;

/**
*
* @author Moacir da Roza Flores <moacirrf@gmail.com>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.mrf.javadecompiler.decompiler;
package com.mrf.javadecompiler.decompiler.cfr;

import com.mrf.javadecompiler.decompiler.jdcore.PrinterImpl;
import com.mrf.javadecompiler.decompiler.jdcore.LoaderImpl;
import com.mrf.javadecompiler.decompiler.Decompiler;
import static com.machinezoo.noexception.Exceptions.wrap;
import com.mrf.javadecompiler.exception.ExceptionHandler;
import com.mrf.javadecompiler.filesystems.FileSystemHelper;
import org.jd.core.v1.ClassFileToJavaSourceDecompiler;
import java.util.Map;
import org.benf.cfr.reader.Main;
import org.benf.cfr.reader.apiunreleased.ClassFileSource2;
import org.benf.cfr.reader.state.DCCommonState;
import org.benf.cfr.reader.util.getopt.Options;
import org.benf.cfr.reader.util.getopt.OptionsImpl;
import org.openide.filesystems.FileObject;

/**
Expand All @@ -30,21 +34,27 @@
*/
public final class DecompilerClassImpl implements Decompiler<String, FileObject> {

public static final String HEADER_COMMENT = "//\n"
+ "// Source code recreated by Apache Netbeans\n"
+ "// (powered by Java Decompiler http://java-decompiler.github.io )\n"
+ "//\n";
public static final String HEADER_COMMENT = "// Source code recreated by Apache Netbeans (NB Java Decompiler) \n";

private final Options options;

public DecompilerClassImpl() {
options = new OptionsImpl(Map.of("comments", "false", "innerclasses", "true"));
}

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

ClassFileToJavaSourceDecompiler decompiler = new ClassFileToJavaSourceDecompiler();
decompiler.decompile(loader, printer, FileSystemHelper.extractName(file));
String className = FileSystemHelper.extractName(file);
FileSystemHelper helper = FileSystemHelper.of(file);

ClassFileSource2 classFileSource = new NetbeansClassFileSourceImpl(helper);

StringBuilder out = new StringBuilder(HEADER_COMMENT);
Main.doClass(new DCCommonState(options, classFileSource), className, false, new PluginDumperFactory(out, options));

return HEADER_COMMENT + printer.toString();
return out.toString();
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2022 moacirrf
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.mrf.javadecompiler.decompiler.cfr;

import com.mrf.javadecompiler.filesystems.FileSystemHelper;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import static java.util.Objects.isNull;
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.openide.filesystems.FileObject;

/**
*
* @author moacirrf
*/
public class NetbeansClassFileSourceImpl implements ClassFileSource2 {

private final FileSystemHelper helper;

public NetbeansClassFileSourceImpl(FileSystemHelper helper) {
this.helper = helper;
}

@Override
public JarContent addJarContent(String jarPath, AnalysisType analysisType) {
//we dont decompile complete jars
return null;
}

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

@Override
public Collection<String> addJar(String jarPath) {
// not used
return List.of();
}

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

@Override
public Pair<byte[], String> getClassFileContent(String path) throws IOException {
FileObject fileObj = helper.findResource(path);
if (isNull(fileObj)) {
return null;
}

return Pair.make(fileObj.asBytes(), path);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2022 moacirrf
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.mrf.javadecompiler.decompiler.cfr;

import org.benf.cfr.reader.bytecode.analysis.types.JavaTypeInstance;
import org.benf.cfr.reader.state.TypeUsageInformation;
import org.benf.cfr.reader.util.getopt.Options;
import org.benf.cfr.reader.util.getopt.OptionsImpl;
import org.benf.cfr.reader.util.output.Dumper;
import org.benf.cfr.reader.util.output.DumperFactory;
import org.benf.cfr.reader.util.output.ExceptionDumper;
import org.benf.cfr.reader.util.output.FileSummaryDumper;
import org.benf.cfr.reader.util.output.IllegalIdentifierDump;
import org.benf.cfr.reader.util.output.MethodErrorCollector;
import org.benf.cfr.reader.util.output.NopSummaryDumper;
import org.benf.cfr.reader.util.output.ProgressDumper;
import org.benf.cfr.reader.util.output.ProgressDumperNop;
import org.benf.cfr.reader.util.output.StringStreamDumper;
import org.benf.cfr.reader.util.output.SummaryDumper;

/*
* Class entirely copied from cfr.jar
*/
public class PluginDumperFactory implements DumperFactory {

private final IllegalIdentifierDump illegalIdentifierDump = new IllegalIdentifierDump.Nop();

private final StringBuilder outBuffer;
private final Options options;

public PluginDumperFactory(StringBuilder out, Options options) {
this.outBuffer = out;
this.options = options;
}

@Override
public Dumper getNewTopLevelDumper(JavaTypeInstance classType, SummaryDumper summaryDumper, TypeUsageInformation typeUsageInformation, IllegalIdentifierDump illegalIdentifierDump) {
return new StringStreamDumper(new MethodErrorCollector.SummaryDumperMethodErrorCollector(classType, summaryDumper), outBuffer, typeUsageInformation, options, this.illegalIdentifierDump);
}

@Override
public Dumper wrapLineNoDumper(Dumper dumper) {
return dumper;
}

/*
* A summary dumper will receive errors. Generally, it's only of value when dumping jars to file.
*/
@Override
public SummaryDumper getSummaryDumper() {
if (!options.optionIsSet(OptionsImpl.OUTPUT_DIR)) {
return new NopSummaryDumper();
}

return new FileSummaryDumper(options.getOption(OptionsImpl.OUTPUT_DIR), options, null);
}

@Override
public ProgressDumper getProgressDumper() {
return ProgressDumperNop.INSTANCE;
}

@Override
public ExceptionDumper getExceptionDumper() {
return null;
}

@Override
public DumperFactory getFactoryWithPrefix(String prefix, int version) {
return this;
}
}

This file was deleted.

Loading