Search in sources :

Example 11 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project smali by JesusFreke.

the class ClassDefinition method writeDirectMethods.

private Set<String> writeDirectMethods(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();
    Iterable<? extends Method> directMethods;
    if (classDef instanceof DexBackedClassDef) {
        directMethods = ((DexBackedClassDef) classDef).getDirectMethods(false);
    } else {
        directMethods = classDef.getDirectMethods();
    }
    for (Method method : directMethods) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# direct methods");
            wroteHeader = true;
        }
        writer.write('\n');
        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getMethodDescriptor(method, true);
        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(writer);
        }
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
    return writtenMethods;
}
Also used : IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef)

Example 12 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project smali by JesusFreke.

the class ClassDefinition method writeInstanceFields.

private void writeInstanceFields(IndentingWriter writer, Set<String> staticFields) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();
    Iterable<? extends Field> instanceFields;
    if (classDef instanceof DexBackedClassDef) {
        instanceFields = ((DexBackedClassDef) classDef).getInstanceFields(false);
    } else {
        instanceFields = classDef.getInstanceFields();
    }
    for (Field field : instanceFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# instance fields");
            wroteHeader = true;
        }
        writer.write('\n');
        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(writer);
            System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
        } else if (staticFields.contains(fieldString)) {
            System.err.println(String.format("Duplicate static+instance field found: %s->%s", classDef.getType(), fieldString));
            System.err.println("You will need to rename one of these fields, including all references.");
            writer.write("# There is both a static and instance field with this signature.\n" + "# You will need to rename one of these fields, including all references.\n");
        }
        FieldDefinition.writeTo(options, fieldWriter, field, false);
    }
}
Also used : IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef)

Example 13 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project smali by JesusFreke.

the class ClassDefinition method writeVirtualMethods.

private void writeVirtualMethods(IndentingWriter writer, Set<String> directMethods) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();
    Iterable<? extends Method> virtualMethods;
    if (classDef instanceof DexBackedClassDef) {
        virtualMethods = ((DexBackedClassDef) classDef).getVirtualMethods(false);
    } else {
        virtualMethods = classDef.getVirtualMethods();
    }
    for (Method method : virtualMethods) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# virtual methods");
            wroteHeader = true;
        }
        writer.write('\n');
        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getMethodDescriptor(method, true);
        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(writer);
        } else if (directMethods.contains(methodString)) {
            writer.write("# There is both a direct and virtual method with this signature.\n" + "# You will need to rename one of these methods, including all references.\n");
            System.err.println(String.format("Duplicate direct+virtual method found: %s->%s", classDef.getType(), methodString));
            System.err.println("You will need to rename one of these methods, including all references.");
        }
        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
}
Also used : IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef)

Example 14 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project android by JetBrains.

the class DexParser method constructMethodRefTreeForDex.

@NotNull
static PackageTreeNode constructMethodRefTreeForDex(@NotNull DexBackedDexFile dexFile) {
    PackageTreeNode root = new PackageTreeNode("", "root", PackageTreeNode.NodeType.PACKAGE, null);
    Set<String> classesWithDefinition = dexFile.getClasses().stream().map(DexBackedClassDef::getType).collect(Collectors.toSet());
    Multimap<String, MethodReference> methodsByClassName = getMethodsByClassName(dexFile);
    for (String className : methodsByClassName.keySet()) {
        Collection<MethodReference> methods = methodsByClassName.get(className);
        for (MethodReference ref : methods) {
            root.insert("", DebuggerUtilsEx.signatureToName(className), ref, classesWithDefinition.contains(className));
        }
    }
    root.sortByCount();
    return root;
}
Also used : MethodReference(org.jf.dexlib2.iface.reference.MethodReference) DexBackedMethodReference(org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project dex2jar by pxb1988.

the class SmaliTest method dotest.

private void dotest(File dexFile) throws IOException {
    DexBackedDexFile dex = DexFileFactory.loadDexFile(dexFile, 14, false);
    Map<String, DexClassNode> map = readDex(dexFile);
    for (DexBackedClassDef def : dex.getClasses()) {
        String type = def.getType();
        System.out.println(type);
        DexClassNode dexClassNode = map.get(type);
        Assert.assertNotNull(dexClassNode);
        // original
        String smali = baksmali(def);
        Smali.smaliFile2Node("fake.smali", smali);
        {
            byte[] data = toDex(dexClassNode);
            DexBackedClassDef def2 = new DexBackedDexFile(new Opcodes(14, false), data).getClasses().iterator().next();
            // original
            String baksmali3 = baksmali(def2);
            Assert.assertEquals(smali, baksmali3);
        }
        String psmali = pbaksmali(dexClassNode);
        DexClassNode dexClassNode2 = Smali.smaliFile2Node("fake.smali", psmali);
        Assert.assertEquals("cmp smalip", psmali, pbaksmali(dexClassNode2));
        {
            byte[] data = toDex(dexClassNode2);
            DexBackedClassDef def2 = new DexBackedDexFile(new Opcodes(14, false), data).getClasses().iterator().next();
            // original
            String baksmali3 = baksmali(def2);
            Assert.assertEquals(smali, baksmali3);
        }
    }
}
Also used : DexClassNode(com.googlecode.d2j.node.DexClassNode) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) Opcodes(org.jf.dexlib2.Opcodes) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef)

Aggregations

DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)19 IndentingWriter (org.jf.util.IndentingWriter)9 HashSet (java.util.HashSet)7 File (java.io.File)6 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)6 ClassFileNameHandler (org.jf.util.ClassFileNameHandler)4 IOException (java.io.IOException)3 DexBackedMethod (org.jf.dexlib2.dexbacked.DexBackedMethod)3 Method (org.jf.dexlib2.iface.Method)3 MethodReplaceAnnotation (com.taobao.android.apatch.annotation.MethodReplaceAnnotation)2 PatchException (com.taobao.android.differ.dex.PatchException)2 Set (java.util.Set)2 Nonnull (javax.annotation.Nonnull)2 NotNull (org.jetbrains.annotations.NotNull)2 Field (org.jf.dexlib2.iface.Field)2 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)2 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)2 EncodedValue (org.jf.dexlib2.iface.value.EncodedValue)2 DexClassNode (com.googlecode.d2j.node.DexClassNode)1 AndFixFilterImpl (com.taobao.android.apatch.AndFixFilterImpl)1