fix: use correct type printer in json output mode (#2053)

This commit is contained in:
Skylot 2023-12-23 18:46:39 +00:00
parent 3e57dacfd3
commit e723c245ee
No known key found for this signature in database
GPG Key ID: 47866607B16F25C8
2 changed files with 26 additions and 16 deletions

View File

@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory;
import jadx.api.JadxArgs; import jadx.api.JadxArgs;
import jadx.api.JadxDecompiler; import jadx.api.JadxDecompiler;
import jadx.api.impl.AnnotatedCodeWriter;
import jadx.api.impl.NoOpCodeCache; import jadx.api.impl.NoOpCodeCache;
import jadx.api.impl.SimpleCodeWriter; import jadx.api.impl.SimpleCodeWriter;
import jadx.cli.LogHelper.LogLevelEnum; import jadx.cli.LogHelper.LogLevelEnum;
@ -44,8 +45,8 @@ public class JadxCLI {
LogHelper.setLogLevelsForLoadingStage(); LogHelper.setLogLevelsForLoadingStage();
JadxArgs jadxArgs = cliArgs.toJadxArgs(); JadxArgs jadxArgs = cliArgs.toJadxArgs();
jadxArgs.setCodeCache(new NoOpCodeCache()); jadxArgs.setCodeCache(new NoOpCodeCache());
jadxArgs.setCodeWriterProvider(SimpleCodeWriter::new);
jadxArgs.setPluginLoader(new JadxExternalPluginsLoader()); jadxArgs.setPluginLoader(new JadxExternalPluginsLoader());
initCodeWriterProvider(jadxArgs);
try (JadxDecompiler jadx = new JadxDecompiler(jadxArgs)) { try (JadxDecompiler jadx = new JadxDecompiler(jadxArgs)) {
jadx.load(); jadx.load();
if (checkForErrors(jadx)) { if (checkForErrors(jadx)) {
@ -66,6 +67,18 @@ public class JadxCLI {
return 0; return 0;
} }
private static void initCodeWriterProvider(JadxArgs jadxArgs) {
switch (jadxArgs.getOutputFormat()) {
case JAVA:
jadxArgs.setCodeWriterProvider(SimpleCodeWriter::new);
break;
case JSON:
// needed for code offsets and source lines
jadxArgs.setCodeWriterProvider(AnnotatedCodeWriter::new);
break;
}
}
private static boolean checkForErrors(JadxDecompiler jadx) { private static boolean checkForErrors(JadxDecompiler jadx) {
if (jadx.getRoot().getClasses().isEmpty()) { if (jadx.getRoot().getClasses().isEmpty()) {
if (jadx.getArgs().isSkipResources()) { if (jadx.getArgs().isSkipResources()) {

View File

@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -77,11 +76,14 @@ public class JsonCodeGen {
} }
jsonCls.setType(getClassTypeStr(cls)); jsonCls.setType(getClassTypeStr(cls));
jsonCls.setAccessFlags(cls.getAccessFlags().rawValue()); jsonCls.setAccessFlags(cls.getAccessFlags().rawValue());
if (!Objects.equals(cls.getSuperClass(), ArgType.OBJECT)) { ArgType superClass = cls.getSuperClass();
jsonCls.setSuperClass(getTypeAlias(cls.getSuperClass())); if (superClass != null
&& !superClass.equals(ArgType.OBJECT)
&& !cls.contains(AFlag.REMOVE_SUPER_CLASS)) {
jsonCls.setSuperClass(getTypeAlias(classGen, superClass));
} }
if (!cls.getInterfaces().isEmpty()) { if (!cls.getInterfaces().isEmpty()) {
jsonCls.setInterfaces(Utils.collectionMap(cls.getInterfaces(), this::getTypeAlias)); jsonCls.setInterfaces(Utils.collectionMap(cls.getInterfaces(), clsType -> getTypeAlias(classGen, clsType)));
} }
ICodeWriter cw = new SimpleCodeWriter(); ICodeWriter cw = new SimpleCodeWriter();
@ -148,8 +150,8 @@ public class JsonCodeGen {
jsonMth.setAlias(mth.getAlias()); jsonMth.setAlias(mth.getAlias());
} }
jsonMth.setSignature(mth.getMethodInfo().getShortId()); jsonMth.setSignature(mth.getMethodInfo().getShortId());
jsonMth.setReturnType(getTypeAlias(mth.getReturnType())); jsonMth.setReturnType(getTypeAlias(classGen, mth.getReturnType()));
jsonMth.setArguments(Utils.collectionMap(mth.getMethodInfo().getArgumentsTypes(), this::getTypeAlias)); jsonMth.setArguments(Utils.collectionMap(mth.getMethodInfo().getArgumentsTypes(), clsType -> getTypeAlias(classGen, clsType)));
MethodGen mthGen = new MethodGen(classGen, mth); MethodGen mthGen = new MethodGen(classGen, mth);
ICodeWriter cw = new AnnotatedCodeWriter(); ICodeWriter cw = new AnnotatedCodeWriter();
@ -205,15 +207,10 @@ public class JsonCodeGen {
return codeLines; return codeLines;
} }
private String getTypeAlias(ArgType clsType) { private String getTypeAlias(ClassGen classGen, ArgType clsType) {
if (Objects.equals(clsType, ArgType.OBJECT)) { ICodeWriter code = new SimpleCodeWriter();
return ArgType.OBJECT.getObject(); classGen.useType(code, clsType);
} return code.getCodeStr();
if (clsType.isObject()) {
ClassInfo classInfo = ClassInfo.fromType(root, clsType);
return classInfo.getAliasFullName();
}
return clsType.toString();
} }
private String getClassTypeStr(ClassNode cls) { private String getClassTypeStr(ClassNode cls) {