提交 6bc2d332 编写于 作者: S Skylot

code refactoring

上级 c9521192
...@@ -35,20 +35,20 @@ subprojects { ...@@ -35,20 +35,20 @@ subprojects {
} }
} }
task copyArtifacts (type: Sync, dependsOn: ['jadx-cli:installApp', 'jadx-gui:installApp']) { task copyArtifacts(type: Sync, dependsOn: ['jadx-cli:installApp', 'jadx-gui:installApp']) {
destinationDir file("$buildDir/jadx") destinationDir file("$buildDir/jadx")
['jadx-cli', 'jadx-gui'].each { ['jadx-cli', 'jadx-gui'].each {
from tasks.getByPath(":${it}:installApp").destinationDir from tasks.getByPath(":${it}:installApp").destinationDir
} }
} }
task pack (type: Zip, dependsOn: copyArtifacts) { task pack(type: Zip, dependsOn: copyArtifacts) {
destinationDir buildDir destinationDir buildDir
archiveName "jadx-${jadxVersion}.zip" archiveName "jadx-${jadxVersion}.zip"
from copyArtifacts.destinationDir from copyArtifacts.destinationDir
} }
task build (dependsOn: pack) { task build(dependsOn: pack) {
description = 'Build jadx distribution zip' description = 'Build jadx distribution zip'
} }
......
...@@ -2,7 +2,6 @@ package jadx.cli; ...@@ -2,7 +2,6 @@ package jadx.cli;
import jadx.api.Decompiler; import jadx.api.Decompiler;
import jadx.core.utils.ErrorsCounter; import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.exceptions.JadxException;
import java.io.File; import java.io.File;
...@@ -17,7 +16,7 @@ public class JadxCLI { ...@@ -17,7 +16,7 @@ public class JadxCLI {
JadxCLIArgs jadxArgs = new JadxCLIArgs(args); JadxCLIArgs jadxArgs = new JadxCLIArgs(args);
checkArgs(jadxArgs); checkArgs(jadxArgs);
processAndSave(jadxArgs); processAndSave(jadxArgs);
} catch (JadxException e) { } catch (Exception e) {
LOG.error(e.getMessage()); LOG.error(e.getMessage());
System.exit(1); System.exit(1);
} }
...@@ -40,26 +39,29 @@ public class JadxCLI { ...@@ -40,26 +39,29 @@ public class JadxCLI {
System.exit(errorsCount); System.exit(errorsCount);
} }
private static void checkArgs(JadxCLIArgs jadxArgs) throws JadxException { private static void checkArgs(JadxCLIArgs jadxArgs) throws Exception {
if (jadxArgs.getInput().isEmpty()) if (jadxArgs.getInput().isEmpty()) {
throw new JadxException("Please specify input file"); LOG.error("Please specify input file");
jadxArgs.printUsage();
System.exit(1);
}
File outputDir = jadxArgs.getOutDir(); File outputDir = jadxArgs.getOutDir();
if (outputDir == null) { if (outputDir == null) {
String outDirName; String outDirName;
File file = jadxArgs.getInput().get(0); File file = jadxArgs.getInput().get(0);
String name = file.getName(); String name = file.getName();
int pos = name.lastIndexOf('.'); int pos = name.lastIndexOf('.');
if (pos != -1) if (pos != -1) {
outDirName = name.substring(0, pos); outDirName = name.substring(0, pos);
else } else {
outDirName = name + "-jadx-out"; outDirName = name + "-jadx-out";
}
LOG.info("output directory: " + outDirName); LOG.info("output directory: " + outDirName);
outputDir = new File(outDirName); outputDir = new File(outDirName);
jadxArgs.setOutputDir(outputDir); jadxArgs.setOutputDir(outputDir);
} }
if (outputDir.exists() && !outputDir.isDirectory()) if (outputDir.exists() && !outputDir.isDirectory()) {
throw new JadxException("Output directory exists as file " + outputDir); throw new Exception("Output directory exists as file " + outputDir);
}
} }
} }
...@@ -20,7 +20,7 @@ import com.beust.jcommander.ParameterException; ...@@ -20,7 +20,7 @@ import com.beust.jcommander.ParameterException;
public final class JadxCLIArgs implements IJadxArgs { public final class JadxCLIArgs implements IJadxArgs {
@Parameter(description = "<input file> (.dex, .apk, .jar or .class)") @Parameter(description = "<input file> (.dex, .apk or .jar)")
protected List<String> files; protected List<String> files;
@Parameter(names = {"-d", "--output-dir"}, description = "output directory") @Parameter(names = {"-d", "--output-dir"}, description = "output directory")
...@@ -32,7 +32,7 @@ public final class JadxCLIArgs implements IJadxArgs { ...@@ -32,7 +32,7 @@ public final class JadxCLIArgs implements IJadxArgs {
@Parameter(names = {"-f", "--fallback"}, description = "make simple dump (using goto instead of 'if', 'for', etc)", help = true) @Parameter(names = {"-f", "--fallback"}, description = "make simple dump (using goto instead of 'if', 'for', etc)", help = true)
protected boolean fallbackMode = false; protected boolean fallbackMode = false;
@Parameter(names = {"--cfg"}, description = "save methods control flow graph") @Parameter(names = {"--cfg"}, description = "save methods control flow graph to dot file")
protected boolean cfgOutput = false; protected boolean cfgOutput = false;
@Parameter(names = {"--raw-cfg"}, description = "save methods control flow graph (use raw instructions)") @Parameter(names = {"--raw-cfg"}, description = "save methods control flow graph (use raw instructions)")
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder> <encoder>
<pattern>%d{HH:mm:ss} %-5level - %msg%n</pattern> <pattern>%-5level - %msg%n</pattern>
</encoder> </encoder>
</appender> </appender>
......
...@@ -8,9 +8,9 @@ import jadx.core.dex.info.FieldInfo; ...@@ -8,9 +8,9 @@ import jadx.core.dex.info.FieldInfo;
import jadx.core.dex.info.MethodInfo; import jadx.core.dex.info.MethodInfo;
import jadx.core.dex.instructions.ArithNode; import jadx.core.dex.instructions.ArithNode;
import jadx.core.dex.instructions.ArithOp; import jadx.core.dex.instructions.ArithOp;
import jadx.core.dex.instructions.ConstClassInsn; import jadx.core.dex.instructions.ConstClassNode;
import jadx.core.dex.instructions.ConstStringInsn; import jadx.core.dex.instructions.ConstStringNode;
import jadx.core.dex.instructions.FillArrayOp; import jadx.core.dex.instructions.FillArrayNode;
import jadx.core.dex.instructions.GotoNode; import jadx.core.dex.instructions.GotoNode;
import jadx.core.dex.instructions.IfNode; import jadx.core.dex.instructions.IfNode;
import jadx.core.dex.instructions.IndexInsnNode; import jadx.core.dex.instructions.IndexInsnNode;
...@@ -209,12 +209,12 @@ public class InsnGen { ...@@ -209,12 +209,12 @@ public class InsnGen {
private void makeInsnBody(CodeWriter code, InsnNode insn, EnumSet<IGState> state) throws CodegenException { private void makeInsnBody(CodeWriter code, InsnNode insn, EnumSet<IGState> state) throws CodegenException {
switch (insn.getType()) { switch (insn.getType()) {
case CONST_STR: case CONST_STR:
String str = ((ConstStringInsn) insn).getString(); String str = ((ConstStringNode) insn).getString();
code.add(StringUtils.unescapeString(str)); code.add(StringUtils.unescapeString(str));
break; break;
case CONST_CLASS: case CONST_CLASS:
ArgType clsType = ((ConstClassInsn) insn).getClsType(); ArgType clsType = ((ConstClassNode) insn).getClsType();
code.add(useType(clsType)).add(".class"); code.add(useType(clsType)).add(".class");
break; break;
...@@ -311,7 +311,7 @@ public class InsnGen { ...@@ -311,7 +311,7 @@ public class InsnGen {
break; break;
case FILL_ARRAY: case FILL_ARRAY:
fillArray((FillArrayOp) insn, code); fillArray((FillArrayNode) insn, code);
break; break;
case FILLED_NEW_ARRAY: case FILLED_NEW_ARRAY:
...@@ -449,7 +449,7 @@ public class InsnGen { ...@@ -449,7 +449,7 @@ public class InsnGen {
code.add('}'); code.add('}');
} }
private void fillArray(FillArrayOp insn, CodeWriter code) throws CodegenException { private void fillArray(FillArrayNode insn, CodeWriter code) throws CodegenException {
ArgType elType = insn.getResult().getType().getArrayElement(); ArgType elType = insn.getResult().getType().getArrayElement();
if (elType.getPrimitiveType() == null) { if (elType.getPrimitiveType() == null) {
elType = elType.selectFirst(); elType = elType.selectFirst();
......
...@@ -80,7 +80,7 @@ public final class ClassInfo { ...@@ -80,7 +80,7 @@ public final class ClassInfo {
char firstChar = name.charAt(0); char firstChar = name.charAt(0);
if (Character.isDigit(firstChar)) { if (Character.isDigit(firstChar)) {
name = "InnerClass_" + name; name = "AnonymousClass_" + name;
} else if (firstChar == '$') { } else if (firstChar == '$') {
name = "_" + name; name = "_" + name;
} }
......
...@@ -3,11 +3,11 @@ package jadx.core.dex.instructions; ...@@ -3,11 +3,11 @@ package jadx.core.dex.instructions;
import jadx.core.dex.instructions.args.ArgType; import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.InsnNode;
public class ConstClassInsn extends InsnNode { public class ConstClassNode extends InsnNode {
private final ArgType clsType; private final ArgType clsType;
public ConstClassInsn(ArgType clsType) { public ConstClassNode(ArgType clsType) {
super(InsnType.CONST_CLASS, 0); super(InsnType.CONST_CLASS, 0);
this.clsType = clsType; this.clsType = clsType;
} }
......
...@@ -2,11 +2,11 @@ package jadx.core.dex.instructions; ...@@ -2,11 +2,11 @@ package jadx.core.dex.instructions;
import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.InsnNode;
public class ConstStringInsn extends InsnNode { public class ConstStringNode extends InsnNode {
private final String str; private final String str;
public ConstStringInsn(String str) { public ConstStringNode(String str) {
super(InsnType.CONST_STR, 0); super(InsnType.CONST_STR, 0);
this.str = str; this.str = str;
} }
......
...@@ -7,11 +7,11 @@ import jadx.core.dex.nodes.InsnNode; ...@@ -7,11 +7,11 @@ import jadx.core.dex.nodes.InsnNode;
import com.android.dx.io.instructions.FillArrayDataPayloadDecodedInstruction; import com.android.dx.io.instructions.FillArrayDataPayloadDecodedInstruction;
public class FillArrayOp extends InsnNode { public class FillArrayNode extends InsnNode {
private final Object data; private final Object data;
public FillArrayOp(int resReg, FillArrayDataPayloadDecodedInstruction payload) { public FillArrayNode(int resReg, FillArrayDataPayloadDecodedInstruction payload) {
super(InsnType.FILL_ARRAY, 0); super(InsnType.FILL_ARRAY, 0);
this.data = payload.getData(); this.data = payload.getData();
......
...@@ -96,13 +96,13 @@ public class InsnDecoder { ...@@ -96,13 +96,13 @@ public class InsnDecoder {
case Opcodes.CONST_STRING: case Opcodes.CONST_STRING:
case Opcodes.CONST_STRING_JUMBO: { case Opcodes.CONST_STRING_JUMBO: {
InsnNode node = new ConstStringInsn(dex.getString(insn.getIndex())); InsnNode node = new ConstStringNode(dex.getString(insn.getIndex()));
node.setResult(InsnArg.reg(insn, 0, ArgType.STRING)); node.setResult(InsnArg.reg(insn, 0, ArgType.STRING));
return node; return node;
} }
case Opcodes.CONST_CLASS: { case Opcodes.CONST_CLASS: {
InsnNode node = new ConstClassInsn(dex.getType(insn.getIndex())); InsnNode node = new ConstClassNode(dex.getType(insn.getIndex()));
node.setResult(InsnArg.reg(insn, 0, ArgType.CLASS)); node.setResult(InsnArg.reg(insn, 0, ArgType.CLASS));
return node; return node;
} }
...@@ -595,7 +595,7 @@ public class InsnDecoder { ...@@ -595,7 +595,7 @@ public class InsnDecoder {
private InsnNode fillArray(DecodedInstruction insn) { private InsnNode fillArray(DecodedInstruction insn) {
DecodedInstruction payload = insnArr[insn.getTarget()]; DecodedInstruction payload = insnArr[insn.getTarget()];
return new FillArrayOp(insn.getA(), (FillArrayDataPayloadDecodedInstruction) payload); return new FillArrayNode(insn.getA(), (FillArrayDataPayloadDecodedInstruction) payload);
} }
private InsnNode filledNewArray(DecodedInstruction insn, int offset, boolean isRange) { private InsnNode filledNewArray(DecodedInstruction insn, int offset, boolean isRange) {
......
package jadx.core.dex.instructions.args; package jadx.core.dex.instructions.args;
import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.InsnNode;
import jadx.core.utils.InsnUtils; import jadx.core.utils.InsnUtils;
...@@ -63,40 +62,45 @@ public abstract class InsnArg extends Typed { ...@@ -63,40 +62,45 @@ public abstract class InsnArg extends Typed {
} }
public InsnArg wrapInstruction(InsnNode insn) { public InsnArg wrapInstruction(InsnNode insn) {
assert parentInsn != insn : "Can't wrap instruction info itself"; InsnNode parent = parentInsn;
int count = parentInsn.getArgsCount(); assert parent != insn : "Can't wrap instruction info itself";
int count = parent.getArgsCount();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
if (parentInsn.getArg(i) == this) { if (parent.getArg(i) == this) {
InsnArg arg; InsnArg arg = wrapArg(insn);
InsnType insnType = insn.getType(); parent.setArg(i, arg);
switch (insnType) {
case MOVE:
case CONST:
arg = insn.getArg(0);
String name = insn.getResult().getTypedVar().getName();
if (name != null) {
arg.getTypedVar().setName(name);
}
break;
case CONST_STR:
arg = wrap(insn);
arg.getTypedVar().forceSetType(ArgType.STRING);
break;
case CONST_CLASS:
arg = wrap(insn);
arg.getTypedVar().forceSetType(ArgType.CLASS);
break;
default:
arg = wrap(insn);
break;
}
parentInsn.setArg(i, arg);
return arg; return arg;
} }
} }
return null; return null;
} }
private static InsnArg wrapArg(InsnNode insn) {
InsnArg arg;
switch (insn.getType()) {
case MOVE:
case CONST:
arg = insn.getArg(0);
String name = insn.getResult().getTypedVar().getName();
if (name != null) {
arg.getTypedVar().setName(name);
}
break;
case CONST_STR:
arg = wrap(insn);
arg.getTypedVar().forceSetType(ArgType.STRING);
break;
case CONST_CLASS:
arg = wrap(insn);
arg.getTypedVar().forceSetType(ArgType.CLASS);
break;
default:
arg = wrap(insn);
break;
}
return arg;
}
public boolean isThis() { public boolean isThis() {
// must be implemented in RegisterArg // must be implemented in RegisterArg
return false; return false;
......
package jadx.core.dex.instructions.args; package jadx.core.dex.instructions.args;
import jadx.core.dex.instructions.ConstClassInsn; import jadx.core.dex.instructions.ConstClassNode;
import jadx.core.dex.instructions.ConstStringInsn; import jadx.core.dex.instructions.ConstStringNode;
import jadx.core.dex.instructions.InsnType; import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.nodes.InsnNode; import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.visitors.InstructionRemover; import jadx.core.dex.visitors.InstructionRemover;
...@@ -53,9 +53,9 @@ public class RegisterArg extends InsnArg { ...@@ -53,9 +53,9 @@ public class RegisterArg extends InsnArg {
case CONST: case CONST:
return parInsn.getArg(0); return parInsn.getArg(0);
case CONST_STR: case CONST_STR:
return ((ConstStringInsn) parInsn).getString(); return ((ConstStringNode) parInsn).getString();
case CONST_CLASS: case CONST_CLASS:
return ((ConstClassInsn) parInsn).getClsType(); return ((ConstClassNode) parInsn).getClsType();
} }
} }
return null; return null;
......
...@@ -3,8 +3,8 @@ package jadx.core.dex.visitors; ...@@ -3,8 +3,8 @@ package jadx.core.dex.visitors;
import jadx.core.deobf.NameMapper; import jadx.core.deobf.NameMapper;
import jadx.core.dex.attributes.AttributeType; import jadx.core.dex.attributes.AttributeType;
import jadx.core.dex.info.MethodInfo; import jadx.core.dex.info.MethodInfo;
import jadx.core.dex.instructions.ConstClassInsn; import jadx.core.dex.instructions.ConstClassNode;
import jadx.core.dex.instructions.ConstStringInsn; import jadx.core.dex.instructions.ConstStringNode;
import jadx.core.dex.instructions.IndexInsnNode; import jadx.core.dex.instructions.IndexInsnNode;
import jadx.core.dex.instructions.InsnType; import jadx.core.dex.instructions.InsnType;
import jadx.core.dex.instructions.InvokeNode; import jadx.core.dex.instructions.InvokeNode;
...@@ -104,10 +104,10 @@ public class ModVisitor extends AbstractVisitor { ...@@ -104,10 +104,10 @@ public class ModVisitor extends AbstractVisitor {
ClassNode parentClass = mth.getParentClass(); ClassNode parentClass = mth.getParentClass();
FieldNode f = null; FieldNode f = null;
if (insn.getType() == InsnType.CONST_STR) { if (insn.getType() == InsnType.CONST_STR) {
String s = ((ConstStringInsn) insn).getString(); String s = ((ConstStringNode) insn).getString();
f = parentClass.getConstField(s); f = parentClass.getConstField(s);
} else if (insn.getType() == InsnType.CONST_CLASS) { } else if (insn.getType() == InsnType.CONST_CLASS) {
ArgType t = ((ConstClassInsn) insn).getClsType(); ArgType t = ((ConstClassNode) insn).getClsType();
f = parentClass.getConstField(t); f = parentClass.getConstField(t);
} else { } else {
LiteralArg arg = (LiteralArg) insn.getArg(0); LiteralArg arg = (LiteralArg) insn.getArg(0);
......
...@@ -40,10 +40,10 @@ task samplesJadxRun(type: JavaExec, dependsOn: samplesJadxCompile) { ...@@ -40,10 +40,10 @@ task samplesJadxRun(type: JavaExec, dependsOn: samplesJadxCompile) {
main = mainSamplesClass main = mainSamplesClass
} }
task samples (dependsOn: samplesJadxRun) { task samples(dependsOn: samplesJadxRun) {
} }
task cleanGeneratedFiles (type: Delete) { task cleanGeneratedFiles(type: Delete) {
delete samplesJadxSrcDir delete samplesJadxSrcDir
delete samplesJadxOutDir delete samplesJadxOutDir
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册