Skip to content

Commit 14f2cff

Browse files
committed
make Internal Class decompiled related
1 parent 7dd333a commit 14f2cff

File tree

6 files changed

+70
-27
lines changed

6 files changed

+70
-27
lines changed

src/main/java/cn/enaium/joe/dialog/CallTreeDialog.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ private void recursion(MethodTreeNode methodTreeNode) {
7272
Map<String, Pair<ClassNode, MethodNode>> map = new HashMap<>();
7373

7474
for (AbstractInsnNode instruction : methodNode.instructions) {
75-
if (instruction instanceof MethodInsnNode) {
76-
MethodInsnNode methodInsnNode = (MethodInsnNode) instruction;
75+
if (instruction instanceof MethodInsnNode methodInsnNode) {
7776
if (!(methodTreeNode.classNode.getInternalName() + "." + methodNode.name + methodNode.desc).equals(methodInsnNode.owner + "." + methodInsnNode.name + methodInsnNode.desc)) {
7877
Map<String, ClassNode> classes = JavaOctetEditor.getInstance().getJar().classes;
7978
String key = methodInsnNode.owner + ".class";

src/main/java/cn/enaium/joe/service/decompiler/CFRDecompiler.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import cn.enaium.joe.config.util.CachedGlobalValue;
2121
import cn.enaium.joe.util.MessageUtil;
2222
import cn.enaium.joe.util.classes.ClassNode;
23+
import cn.enaium.joe.util.classes.JarHelper;
2324
import org.benf.cfr.reader.Main;
2425
import org.benf.cfr.reader.apiunreleased.ClassFileSource2;
2526
import org.benf.cfr.reader.apiunreleased.JarContent;
@@ -34,6 +35,7 @@
3435

3536
import java.util.Collection;
3637
import java.util.Collections;
38+
import java.util.HashMap;
3739

3840
/**
3941
* @author Enaium
@@ -43,14 +45,16 @@ public class CFRDecompiler implements IDecompiler {
4345

4446
public static final CachedGlobalValue<Options> options = new CachedGlobalValue<>(config -> OptionsImpl.getFactory().create(JavaOctetEditor.getInstance().config.getConfigMapStrings(config)));
4547

48+
public HashMap<String, ClassNode> activeNodes;
4649
@Override
4750
public String decompile(final ClassNode classNode) {
51+
this.activeNodes = JarHelper.getAllNodes(classNode);
4852
DCCommonState state = new DCCommonState(options.getValue(), new ClassFileSource2() {
4953
@Override
5054
public Pair<byte[], String> getClassFileContent(String path) {
5155
String name = path.substring(0, path.length() - 6);
52-
if (name.equals(classNode.getInternalName())) {
53-
return Pair.make(classNode.getClassBytes(), name);
56+
if (activeNodes.containsKey(name)) {
57+
return Pair.make(activeNodes.get(name).getClassBytes(), name);
5458
} else {
5559
if (JavaOctetEditor.getInstance().getJar().classes.get(path) != null) {
5660
return Pair.make(JavaOctetEditor.getInstance().getJar().classes.get(path).getClassBytes(), name);
@@ -63,12 +67,12 @@ public Pair<byte[], String> getClassFileContent(String path) {
6367
@Override public String getPossiblyRenamedPath(String path) {return path;}
6468
@Override public Collection<String> addJar(String arg0) {return Collections.emptySet();}
6569
});
66-
return decompile(state, classNode);
70+
return decompile(state, classNode.getInternalName());
6771
}
6872

69-
public static String decompile(DCCommonState state, ClassNode classNode){
73+
public static String decompile(DCCommonState state, String internalName){
7074
try {
71-
Main.doClass(state, classNode.getInternalName(), false, PluginDumperFactory.INSTANCE);
75+
Main.doClass(state, internalName, false, PluginDumperFactory.INSTANCE);
7276
return PluginDumperFactory.INSTANCE.getResult();
7377
} catch (Exception e) {
7478
MessageUtil.error(e);

src/main/java/cn/enaium/joe/service/decompiler/ProcyonDecompiler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import cn.enaium.joe.JavaOctetEditor;
2020
import cn.enaium.joe.config.util.CachedGlobalValue;
2121
import cn.enaium.joe.util.classes.ClassNode;
22+
import cn.enaium.joe.util.classes.JarHelper;
2223
import cn.enaium.joe.util.reflection.FieldAccessor;
2324
import cn.enaium.joe.util.reflection.ReflectionHelper;
2425
import com.strobel.assembler.InputTypeLoader;
@@ -32,6 +33,7 @@
3233
import org.pmw.tinylog.Logger;
3334

3435
import java.io.StringWriter;
36+
import java.util.HashMap;
3537

3638
/**
3739
* @author Enaium
@@ -54,16 +56,18 @@ public class ProcyonDecompiler implements IDecompiler {
5456
return aDefault;
5557
});
5658

59+
public HashMap<String, ClassNode> activeNodes;
5760
@Override
5861
public String decompile(ClassNode classNode) {
5962
DecompilerSettings decompilerSettings = new DecompilerSettings();
63+
this.activeNodes = JarHelper.getAllNodes(classNode);
6064
MetadataSystem metadataSystem = new MetadataSystem(new ITypeLoader() {
6165
private final InputTypeLoader backLoader = new InputTypeLoader();
6266

6367
@Override
6468
public boolean tryLoadType(String s, Buffer buffer) {
65-
if (s.equals(classNode.getInternalName())) {
66-
byte[] b = classNode.getClassBytes();
69+
if (ProcyonDecompiler.this.activeNodes.containsKey(s)) {
70+
byte[] b = ProcyonDecompiler.this.activeNodes.get(s).getClassBytes();
6771
buffer.putByteArray(b, 0, b.length);
6872
buffer.position(0);
6973
return true;

src/main/java/cn/enaium/joe/service/decompiler/VineFlowerDecompiler.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import cn.enaium.joe.config.util.CachedGlobalValue;
2121
import cn.enaium.joe.util.MessageUtil;
2222
import cn.enaium.joe.util.classes.ClassNode;
23+
import cn.enaium.joe.util.classes.JarHelper;
2324
import org.jetbrains.java.decompiler.main.decompiler.BaseDecompiler;
2425
import org.jetbrains.java.decompiler.main.extern.IContextSource;
2526
import org.jetbrains.java.decompiler.main.extern.IFernflowerLogger;
@@ -37,7 +38,7 @@
3738
*/
3839
public class VineFlowerDecompiler extends IFernflowerLogger implements IDecompiler, IResultSaver, IContextSource, IContextSource.IOutputSink {
3940
private String returned;
40-
private ClassNode activeClass;
41+
private HashMap<String, ClassNode> activeClass;
4142
public static final CachedGlobalValue<Map<String, Object>> customProperties = new CachedGlobalValue<>(config -> {
4243
Map<String, String> map = JavaOctetEditor.getInstance().config.getConfigMapStrings(config);
4344
HashMap<String, Object> hashMap = new HashMap<>(map.size());
@@ -55,7 +56,7 @@ public class VineFlowerDecompiler extends IFernflowerLogger implements IDecompil
5556
@Override
5657
public String decompile(final ClassNode classNode) {
5758
returned = null;
58-
activeClass = classNode;
59+
activeClass = JarHelper.getAllNodes(classNode);
5960
BaseDecompiler baseDecompiler = new BaseDecompiler(this, customProperties.getValue(), this);
6061
baseDecompiler.addSource(this);
6162
baseDecompiler.decompileContext();
@@ -101,7 +102,7 @@ public String getName() {
101102

102103
@Override
103104
public Entries getEntries() {
104-
return new Entries(List.of(Entry.atBase(activeClass.getInternalName())), List.of(), List.of());
105+
return new Entries(activeClass.keySet().stream().map(Entry::atBase).toList(), List.of(), List.of());
105106
}
106107

107108
@Override
@@ -111,17 +112,19 @@ public boolean isLazy() {
111112

112113
@Override
113114
public InputStream getInputStream(String resource) {
114-
return new ByteArrayInputStream(activeClass.getClassBytes());
115+
if (activeClass.containsKey(resource)) return new ByteArrayInputStream(activeClass.get(resource).getClassBytes());
116+
return null;
115117
}
116118

117119
@Override
118120
public byte[] getClassBytes(String className) {
119-
return activeClass.getClassBytes();
121+
if (activeClass.containsKey(className)) return activeClass.get(className).getClassBytes();
122+
return null;
120123
}
121124

122125
@Override
123126
public boolean hasClass(String className) {
124-
return className.equals(activeClass.getInternalName()) || className.equals(activeClass.getCanonicalName());
127+
return activeClass.containsKey(className);
125128
}
126129

127130
@Override

src/main/java/cn/enaium/joe/util/classes/ClassNode.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
import java.util.Set;
1313

1414
public interface ClassNode {
15-
String getInternalName();
16-
String getCanonicalName();
15+
default String getInternalName(){
16+
return this.getNodeInternal().name;
17+
}
18+
default String getCanonicalName(){
19+
return this.getNodeInternal().name.replace('/', '.');
20+
}
1721
byte[] getClassBytes();
1822
org.objectweb.asm.tree.ClassNode getClassNode();
1923
void accept(ClassNode newNode);
@@ -30,6 +34,14 @@ default String getInternalPackageName(){
3034
return this.getInternalName().substring(0, this.getInternalName().lastIndexOf('/') - 1);
3135
}
3236

37+
default String[] getInnerClassesInternalName(){
38+
return this.getNodeInternal().innerClasses.stream().map((v) -> v.name).toArray(String[]::new);
39+
}
40+
41+
default String[] getInnerClassesCanonicalName(){
42+
return this.getNodeInternal().innerClasses.stream().map((v) -> v.name.replace('/', '.')).toArray(String[]::new);
43+
}
44+
3345
default String getSuperClass(){
3446
return this.getNodeInternal().superName;
3547
}
@@ -67,6 +79,8 @@ default void editVisitor(ClassVisitor classVisitor){
6779
this.updateBytes();
6880
}
6981

82+
void mkdir();
83+
7084
org.objectweb.asm.tree.ClassNode getNodeInternal();
7185

7286
default Set<String> getParents(){
@@ -112,16 +126,6 @@ public Impl(String internalName, String canonicalName, byte[] clazz, org.objectw
112126
this.node = node;
113127
}
114128

115-
@Override
116-
public String getInternalName() {
117-
return this.node.name;
118-
}
119-
120-
@Override
121-
public String getCanonicalName() {
122-
return this.node.name.replace('/', '.');
123-
}
124-
125129
@Override
126130
public byte[] getClassBytes() {
127131
if (this.isDir) updateBytes();
@@ -158,5 +162,10 @@ public ClassNode clone() {
158162
public org.objectweb.asm.tree.ClassNode getNodeInternal() {
159163
return this.node;
160164
}
165+
166+
@Override
167+
public void mkdir() {
168+
this.isDir = true;
169+
}
161170
}
162171
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.enaium.joe.util.classes;
2+
3+
import cn.enaium.joe.JavaOctetEditor;
4+
import cn.enaium.joe.jar.Jar;
5+
6+
import java.util.*;
7+
8+
public class JarHelper {
9+
public static HashMap<String, ClassNode> getAllNodes(ClassNode classNode){
10+
return getAllNodes(JavaOctetEditor.getInstance().getJar(), classNode);
11+
}
12+
13+
public static HashMap<String, ClassNode> getAllNodes(Jar jar, ClassNode classNode){
14+
HashMap<String, ClassNode> classNodes = new HashMap<>();
15+
classNodes.put(classNode.getInternalName(), classNode);
16+
for(String clazz : classNode.getInnerClassesInternalName()){
17+
ClassNode cn = jar.classes.get(clazz + ".class");
18+
if (cn != null) {
19+
classNodes.put(cn.getInternalName(), cn);
20+
}
21+
}
22+
return classNodes;
23+
}
24+
}

0 commit comments

Comments
 (0)