Search in sources :

Example 36 with Method

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

use of org.jf.dexlib2.iface.Method 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)

Example 38 with Method

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

the class MetaObjectDex method getDeclaredMethods.

@Override
public MethodInfo[] getDeclaredMethods() {
    Iterable<? extends Method> implMethods = classDef.getMethods();
    List<MethodInfo> result = new ArrayList<>();
    for (Method method : implMethods) {
        if (!isConstructor(method)) {
            MethodInfo mi = new MethodInfo();
            mi.parameterTypes = convertParameters(method.getParameters());
            mi.annotations = convertAnnotations(method.getAnnotations());
            mi.modifiers = method.getAccessFlags();
            mi.name = method.getName();
            mi.exceptionTypes = new ExceptionInfo[0];
            mi.returnType = DexlibAdapter.getTypeName(method.getReturnType());
            result.add(mi);
        }
    }
    MethodInfo[] array = new MethodInfo[result.size()];
    return result.toArray(array);
}
Also used : ArrayList(java.util.ArrayList) Method(org.jf.dexlib2.iface.Method)

Example 39 with Method

use of org.jf.dexlib2.iface.Method in project atlas by alibaba.

the class DexCompareUtils method equalsInstruction.

/**
     * 比较method重的tryBlock块
     *
     * @param a
     * @param b
     * @return
     */
private static boolean equalsInstruction(List<Instruction> a, List<Instruction> b) {
    if (a.size() != b.size()) {
        return false;
    }
    Instruction at, bt;
    for (int i = 0; i < a.size(); i++) {
        at = a.get(i);
        bt = b.get(i);
        if (!at.getOpcode().equals(bt.getOpcode()) || !(at.getCodeUnits() == bt.getCodeUnits())) {
            return false;
        }
    }
    return true;
}
Also used : Instruction(org.jf.dexlib2.iface.instruction.Instruction)

Example 40 with Method

use of org.jf.dexlib2.iface.Method in project atlas by alibaba.

the class DexDiffInfo method writeToFile.

public void writeToFile(String bundleName, File diffFile, File diffJsonFile) throws IOException {
    JSONArray jsonArray = new JSONArray();
    if (diffJsonFile.exists()) {
        String content = FileUtils.readFileToString(diffJsonFile);
        jsonArray = JSONArray.parseArray(content);
    }
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("bundleName", bundleName);
    if (addedMethods.size() > 0) {
        List<String> addMethods = new ArrayList<String>();
        List<MethodDiffInfoObject> diffInfoObjects = new ArrayList<MethodDiffInfoObject>();
        for (DexBackedMethod method : addedMethods) {
            addMethods.add("[add new Method:]" + method.getReturnType() + "  " + method.getName() + "(" + Formater.formatStringList(method.getParameterTypes()) + ")  in Class:" + method.getDefiningClass());
            MethodDiffInfoObject diffInfoObject = new MethodDiffInfoObject();
            diffInfoObject.setDiffType(DiffType.ADD);
            diffInfoObject.setReturnType(method.getReturnType());
            diffInfoObject.setMethodDeclaration(method.getName() + "(" + Formater.formatStringList(method.getParameterTypes()) + ")");
            diffInfoObject.setInClass(method.getDefiningClass());
            diffInfoObjects.add(diffInfoObject);
        }
        FileUtils.writeLines(diffFile, addMethods, true);
        jsonObject.put("addedMethods", diffInfoObjects);
    }
    if (modifiedMethods.size() > 0) {
        List<String> modifyMethods = new ArrayList<String>();
        List<MethodDiffInfoObject> diffInfoObjects = new ArrayList<MethodDiffInfoObject>();
        for (DexBackedMethod method : modifiedMethods) {
            modifyMethods.add("[modify Method:]" + method.getReturnType() + "  " + method.getName() + "(" + Formater.formatStringList(method.getParameterTypes()) + ")  in Class:" + method.getDefiningClass());
            MethodDiffInfoObject diffInfoObject = new MethodDiffInfoObject();
            diffInfoObject.setDiffType(DiffType.MODIFY);
            diffInfoObject.setReturnType(method.getReturnType());
            diffInfoObject.setMethodDeclaration(method.getName() + "(" + Formater.formatStringList(method.getParameterTypes()) + ")");
            diffInfoObject.setInClass(method.getDefiningClass());
            diffInfoObjects.add(diffInfoObject);
        }
        FileUtils.writeLines(diffFile, modifyMethods, true);
        jsonObject.put("modifiedMethods", diffInfoObjects);
    }
    jsonArray.add(jsonObject);
    FileUtils.writeStringToFile(diffJsonFile, JSON.toJSONString(jsonArray));
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) DexBackedMethod(org.jf.dexlib2.dexbacked.DexBackedMethod) JSONArray(com.alibaba.fastjson.JSONArray)

Aggregations

ClassDef (org.jf.dexlib2.iface.ClassDef)21 Method (org.jf.dexlib2.iface.Method)21 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)19 Test (org.junit.Test)16 Instruction (org.jf.dexlib2.iface.instruction.Instruction)15 DexFile (org.jf.dexlib2.iface.DexFile)13 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)13 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)11 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)10 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)7 MutableMethodImplementation (org.jf.dexlib2.builder.MutableMethodImplementation)7 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)7 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)7 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)7 Nonnull (javax.annotation.Nonnull)6 Opcode (org.jf.dexlib2.Opcode)6 BuilderInstruction21t (org.jf.dexlib2.builder.instruction.BuilderInstruction21t)6 BuilderInstruction22c (org.jf.dexlib2.builder.instruction.BuilderInstruction22c)6 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)6 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)6