Skip to content

Commit dd7c67b

Browse files
moacirrfmoacirrf
authored andcommitted
Fix decompilation of innerclasses, improve th support of cfr
1 parent f16eeeb commit dd7c67b

File tree

3 files changed

+84
-40
lines changed

3 files changed

+84
-40
lines changed

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

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,12 @@
2020
import static com.machinezoo.noexception.Exceptions.wrap;
2121
import com.mrf.javadecompiler.exception.ExceptionHandler;
2222
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;
2623
import java.util.Map;
2724
import org.benf.cfr.reader.Main;
2825
import org.benf.cfr.reader.apiunreleased.ClassFileSource2;
29-
import org.benf.cfr.reader.state.ClassFileSourceImpl;
3026
import org.benf.cfr.reader.state.DCCommonState;
31-
import org.benf.cfr.reader.util.AnalysisType;
3227
import org.benf.cfr.reader.util.getopt.Options;
3328
import org.benf.cfr.reader.util.getopt.OptionsImpl;
34-
import org.benf.cfr.reader.util.output.DumperFactory;
3529
import org.openide.filesystems.FileObject;
3630

3731
/**
@@ -40,55 +34,28 @@
4034
*/
4135
public final class DecompilerClassImpl implements Decompiler<String, FileObject> {
4236

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";
4438

4539
private final Options options;
46-
private final ClassFileSource2 classFileSource;
47-
private final DCCommonState dcCommonState;
4840

4941
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"));
5443
}
5544

5645
@Override
5746
public String decompile(FileObject file) {
5847
return wrap(ExceptionHandler::handleException).get(() -> {
5948

49+
String className = FileSystemHelper.extractName(file);
6050
FileSystemHelper helper = FileSystemHelper.of(file);
61-
FileObject fileObject = helper.findResource(FileSystemHelper.extractName(file));
6251

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);
6653

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));
7156

72-
return decompiledClass;
57+
return out.toString();
7358
});
7459
}
7560

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-
9461
}
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+
}

src/main/java/com/mrf/javadecompiler/decompiler/cfr/PluginDumperFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
import org.benf.cfr.reader.util.output.StringStreamDumper;
3333
import org.benf.cfr.reader.util.output.SummaryDumper;
3434

35+
/*
36+
* Class entirely copied from cfr.jar
37+
*/
3538
public class PluginDumperFactory implements DumperFactory {
3639

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

0 commit comments

Comments
 (0)