use of com.googlecode.d2j.reader.DexFileReader in project dex2jar by pxb1988.
the class SkipDupMethod method test.
@Test
public void test() throws IOException {
InputStream is = SkipDupMethod.class.getClassLoader().getResourceAsStream("i200.dex");
Assert.assertNotNull(is);
DexFileReader reader = new DexFileReader(is);
DexFileNode dfn1 = new DexFileNode();
reader.accept(dfn1, DexFileReader.KEEP_ALL_METHODS);
DexFileNode dfn2 = new DexFileNode();
reader.accept(dfn2, 0);
Assert.assertTrue(dfn1.clzs.get(0).methods.size() > dfn2.clzs.get(0).methods.size());
}
use of com.googlecode.d2j.reader.DexFileReader in project dex2jar by pxb1988.
the class DexWeaverCmd method doCommandLine.
@Override
protected void doCommandLine() throws Exception {
if (remainingArgs.length == 0) {
throw new HelpException("no odex");
}
final Map<String, Method> map = new HashMap<>();
for (String ln : Files.readAllLines(config, StandardCharsets.UTF_8)) {
if (ln.startsWith("#") || ln.length() == 0) {
continue;
}
String[] x = ln.split("=");
map.put(x[0], parseMethod(x[1]));
}
DexFileWriter out = new DexFileWriter();
DexFileVisitor fv = new DexFileVisitor(out) {
@Override
public DexClassVisitor visit(int access_flags, String className, String superClass, String[] interfaceNames) {
DexClassVisitor dcv = super.visit(access_flags, className, superClass, interfaceNames);
if (dcv != null) {
return new DexClassVisitor(dcv) {
@Override
public DexMethodVisitor visitMethod(int accessFlags, Method method) {
DexMethodVisitor dmv = super.visitMethod(accessFlags, method);
if (dmv != null) {
return new DexMethodVisitor(dmv) {
@Override
public DexCodeVisitor visitCode() {
DexCodeVisitor code = super.visitCode();
if (code != null) {
return new DexCodeVisitor(code) {
@Override
public void visitMethodStmt(Op op, int[] args, Method method) {
Method replaceTo = map.get(method.toString());
if (replaceTo != null) {
switch(op) {
case INVOKE_DIRECT:
case INVOKE_INTERFACE:
case INVOKE_STATIC:
case INVOKE_SUPER:
case INVOKE_VIRTUAL:
super.visitMethodStmt(Op.INVOKE_STATIC, args, replaceTo);
break;
case INVOKE_DIRECT_RANGE:
case INVOKE_INTERFACE_RANGE:
case INVOKE_STATIC_RANGE:
case INVOKE_SUPER_RANGE:
case INVOKE_VIRTUAL_RANGE:
super.visitMethodStmt(Op.INVOKE_STATIC_RANGE, args, replaceTo);
break;
default:
}
} else {
super.visitMethodStmt(op, args, method);
}
}
};
}
return code;
}
};
}
return dmv;
}
};
}
return dcv;
}
@Override
public void visitEnd() {
}
};
for (String f : remainingArgs) {
byte[] data = ZipUtil.readDex(new File(f).toPath());
DexFileReader r = new DexFileReader(data);
r.accept(fv);
}
if (stub != null) {
byte[] data = ZipUtil.readDex(stub);
DexFileReader r = new DexFileReader(data);
r.accept(new DexFileVisitor(out) {
@Override
public void visitEnd() {
}
});
}
out.visitEnd();
byte[] data = out.toByteArray();
Files.write(output, data);
}
use of com.googlecode.d2j.reader.DexFileReader in project dex2jar by pxb1988.
the class AppWriterTest method test4.
@Test
public void test4() throws IOException {
DexFileWriter w = new DexFileWriter();
DexFileReader dexFileReader = new DexFileReader(new File("../dex-translator/src/test/resources/dexes/i_jetty.dex"));
dexFileReader.accept(w);
w.toByteArray();
}
use of com.googlecode.d2j.reader.DexFileReader in project dex2jar by pxb1988.
the class SmaliTest method readDex.
Map<String, DexClassNode> readDex(File path) throws IOException {
DexFileReader dexFileReader = new DexFileReader(ZipUtil.readDex(path));
DexFileNode dexFileNode = new DexFileNode();
dexFileReader.accept(dexFileNode);
Map<String, DexClassNode> map = new HashMap<>();
for (DexClassNode c : dexFileNode.clzs) {
map.put(c.className, c);
}
return map;
}
use of com.googlecode.d2j.reader.DexFileReader in project dex2jar by pxb1988.
the class GenerateCompileStubFromOdex method doDex.
private void doDex(ByteBuffer bs, final Path out) {
DexFileReader reader = new DexFileReader(bs);
DexFileNode fileNode = new DexFileNode();
reader.accept(fileNode, DexFileReader.SKIP_CODE);
Dex2Asm dex2Asm = new Dex2Asm();
dex2Asm.convertDex(fileNode, new ClassVisitorFactory() {
@Override
public ClassVisitor create(final String classInternalName) {
final Path target = out.resolve(classInternalName + ".class");
if (Files.exists(target)) {
System.err.println("class " + classInternalName + " is already exists, skipping.");
return null;
}
return new ClassVisitor(Opcodes.ASM4, new ClassWriter(ClassWriter.COMPUTE_MAXS)) {
@Override
public void visitEnd() {
super.visitEnd();
ClassWriter cw = (ClassWriter) cv;
byte[] data = cw.toByteArray();
try {
BaseCmd.createParentDirectories(target);
Files.write(target, data);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
if (noPrivate && 0 != (access & Opcodes.ACC_PRIVATE)) {
return null;
}
return super.visitField(access, name, desc, signature, value);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
if (noPrivate && 0 != (access & Opcodes.ACC_PRIVATE)) {
return null;
}
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
if (0 != ((Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT) & access)) {
return mv;
}
mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException");
mv.visitInsn(Opcodes.DUP);
mv.visitLdcInsn("stub");
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V");
mv.visitInsn(Opcodes.ATHROW);
return mv;
}
};
}
});
}
Aggregations