use of com.googlecode.d2j.dex.Dex2Asm in project dex2jar by pxb1988.
the class TestUtils method translateAndCheck.
public static byte[] translateAndCheck(DexFileNode fileNode, DexClassNode clzNode) throws AnalyzerException, IllegalAccessException {
// 1. convert to .class
Dex2Asm dex2Asm = new Dex2Asm() {
@Override
public void convertCode(DexMethodNode methodNode, MethodVisitor mv) {
try {
super.convertCode(methodNode, mv);
} catch (Exception ex) {
BaksmaliDumper d = new BaksmaliDumper();
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.err, "UTF-8"));
d.baksmaliMethod(methodNode, out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
throw new DexException(ex, "fail convert code %s", methodNode.method);
}
}
};
final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassVisitorFactory cvf = new ClassVisitorFactory() {
@Override
public ClassVisitor create(String classInternalName) {
return cw;
}
};
if (fileNode != null) {
dex2Asm.convertClass(clzNode, cvf, fileNode);
} else {
dex2Asm.convertClass(clzNode, cvf);
}
byte[] data = cw.toByteArray();
// 2. verify .class
ClassReader cr = new ClassReader(data);
TestUtils.verify(cr);
// 3. convert back to dex
CfOptions cfOptions = new CfOptions();
cfOptions.strictNameCheck = false;
DexOptions dexOptions = new DexOptions();
DirectClassFile dcf = new DirectClassFile(data, clzNode.className.substring(1, clzNode.className.length() - 1) + ".class", true);
dcf.setAttributeFactory(new StdAttributeFactory());
com.android.dx.dex.file.DexFile dxFile = new com.android.dx.dex.file.DexFile(dexOptions);
CfTranslator.translate(dcf, data, cfOptions, dexOptions, dxFile);
return data;
}
use of com.googlecode.d2j.dex.Dex2Asm 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