Search in sources :

Example 36 with ClassDef

use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.

the class ListVtablesCommand method run.

@Override
public void run() {
    if (help || inputList == null || inputList.isEmpty()) {
        usage();
        return;
    }
    if (inputList.size() > 1) {
        System.err.println("Too many files specified");
        usage();
        return;
    }
    String input = inputList.get(0);
    loadDexFile(input);
    BaksmaliOptions options = getOptions();
    if (options == null) {
        return;
    }
    try {
        if (classes != null && !classes.isEmpty()) {
            for (String cls : classes) {
                listClassVtable((ClassProto) options.classPath.getClass(cls));
            }
            return;
        }
        for (ClassDef classDef : dexFile.getClasses()) {
            if (!AccessFlags.INTERFACE.isSet(classDef.getAccessFlags())) {
                listClassVtable((ClassProto) options.classPath.getClass(classDef));
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : ClassDef(org.jf.dexlib2.iface.ClassDef) IOException(java.io.IOException)

Example 37 with ClassDef

use of org.jf.dexlib2.iface.ClassDef 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 38 with ClassDef

use of org.jf.dexlib2.iface.ClassDef 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 39 with ClassDef

use of org.jf.dexlib2.iface.ClassDef 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 40 with ClassDef

use of org.jf.dexlib2.iface.ClassDef in project android-classyshark by google.

the class SyntheticAccessorsInspector method getSyntheticAccessors.

public List<String> getSyntheticAccessors() {
    LinkedList<String> result = new LinkedList<>();
    Set<? extends ClassDef> allClasses = dxFile.getClasses();
    for (ClassDef classDef : allClasses) {
        Iterator<? extends Method> allMethodsIter = classDef.getMethods().iterator();
        while (allMethodsIter.hasNext()) {
            Method element = allMethodsIter.next();
            String name = element.getName();
            String nClassName = classDef.getType();
            if (name.contains("access$")) {
                String cleanClassName = DexlibAdapter.getClassStringFromDex(nClassName);
                if (!result.contains(cleanClassName)) {
                    result.add(cleanClassName);
                }
            }
        }
    }
    return result;
}
Also used : ClassDef(org.jf.dexlib2.iface.ClassDef) Method(org.jf.dexlib2.iface.Method) LinkedList(java.util.LinkedList)

Aggregations

ClassDef (org.jf.dexlib2.iface.ClassDef)47 DexFile (org.jf.dexlib2.iface.DexFile)23 Test (org.junit.Test)21 Method (org.jf.dexlib2.iface.Method)18 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)15 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)14 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)14 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)13 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)12 IndentingWriter (org.jf.util.IndentingWriter)11 File (java.io.File)10 IOException (java.io.IOException)10 HashSet (java.util.HashSet)8 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)8 Instruction (org.jf.dexlib2.iface.instruction.Instruction)8 Nonnull (javax.annotation.Nonnull)7 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)7 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)7 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)7 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)6