org.jf.dexlib2.dexbacked.DexBackedMethod Java Examples

The following examples show how to use org.jf.dexlib2.dexbacked.DexBackedMethod. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ComplexityAnalyzer.java    From apkfile with Apache License 2.0 6 votes vote down vote up
public void analyze() {
    for (DexFile dexFile : dexFiles) {
        for (DexMethod dexMethod : dexFile.getMethodDescriptorToMethod().values()) {
            if (dexMethod.getCyclomaticComplexity() >= 0) {
                continue;
            }

            DexBackedMethod method = dexMethod.getMethod();
            DexBackedMethodImplementation implementation = method.getImplementation();
            if (implementation != null) {
                int complexity = calculateComplexity(implementation, dexFiles, new HashSet<>());
                dexMethod.setCyclomaticComplexity(complexity);
            }
        }

    }

}
 
Example #2
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
private static boolean compare(DexBackedMethod dexBackedMethod, DexBackedMethod sDexBackedMethod) {
    if (dexBackedMethod == null || sDexBackedMethod == null || dexBackedMethod.getName() == null || sDexBackedMethod.getName() == null) {
        return false;
    }
    if (dexBackedMethod.getName().equals(sDexBackedMethod.getName())) {
        List<String> parameters = dexBackedMethod.getParameterTypes();
        List<String> sParameters = sDexBackedMethod.getParameterTypes();
        if (parameters.size() != sParameters.size()) {
            return false;
        }
        for (String param : parameters) {
            if (!sParameters.contains(param)) {
                return false;
            }
        }
        return true;
    }
    return false;
}
 
Example #3
Source File: AndFixFilterImpl.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Override
public boolean filterMethod(MethodDiffInfo methodDiffInfo) throws PatchException {
    DexBackedMethod dexBackedMethod = methodDiffInfo.getBackedMethod();
    if (dexBackedMethod.getName().equals("<clinit>") || dexBackedMethod.getName().contains("ajc$preClinit")||dexBackedMethod.getName().equals("<init>")||dexBackedMethod.getName().contains("access$")) {
        return true;
    }

    if (methodDiffInfo.getType().equals(DiffType.REMOVE)) {
        return true;
    } else if (methodDiffInfo.getType().equals(DiffType.ADD)){
        throw new PatchException("can't add method:" + dexBackedMethod.getName() + " in class:" + dexBackedMethod.getDefiningClass());
    }

    if (dexBackedMethod.getParameters().size() > 8) {
        throw new PatchException("can't patch method:" + dexBackedMethod.getName() + "has Parameters above 8 in class:"+dexBackedMethod.getDefiningClass());
    }
    if (dexBackedMethod.getParameterTypes().contains("J") || dexBackedMethod.getParameterTypes().contains("D") || dexBackedMethod.getParameterTypes().contains("F")) {
       throw new PatchException("can't patch method:" + dexBackedMethod.getName() + "has ParameterType long,double or float in class:"+dexBackedMethod.getDefiningClass());
    }
    return false;
}
 
Example #4
Source File: DexMethod.java    From apkfile with Apache License 2.0 5 votes vote down vote up
DexMethod(DexBackedMethod method, boolean shortMethodSignatures) {
    this.method = method;
    this.shortMethodSignatures = shortMethodSignatures;

    size = method.getSize();
    accessFlags = method.getAccessFlags();
    annotationCount = method.getAnnotations().size();
    frameworkApiCounts = new TObjectIntHashMap<>();
    frameworkFieldReferenceCounts = new TObjectIntHashMap<>();
    opCounts = new TIntIntHashMap();
    stringReferenceCounts = new TObjectIntHashMap<>();
    if (method.getImplementation() != null) {
        analyze(method.getImplementation());
    }
}
 
Example #5
Source File: ApkPatch.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static void getMethodAnnotaionPrepareClasses(DexDiffInfo dexDiffInfo, Set<String> prepareclasses){

        for (DexBackedMethod method:dexDiffInfo.getModifiedMethods()){
            Set<? extends Annotation>annotations = method.getAnnotations();
            if (annotations == null){
                continue;
            }
            for (Annotation annotation:annotations){
                String type = annotation.getType();
                if (type!= null&&type.startsWith("L")&&type.endsWith(";")){
                    prepareclasses.add(type.substring(1, type.length() - 1).replace('/', '.'));
                    System.out.println("prepare class: " + type);
                }
                Set<? extends AnnotationElement> elements = annotation.getElements();
                for (AnnotationElement dexBackedAnnotationElement:elements){
                    if (dexBackedAnnotationElement.getValue() instanceof DexBackedArrayEncodedValue){
                        List<? extends EncodedValue> values = ((DexBackedArrayEncodedValue) dexBackedAnnotationElement.getValue()).getValue();
                        for (EncodedValue encodedValue:values) {
                            if (encodedValue instanceof TypeEncodedValue) {
                                prepareclasses.add(((TypeEncodedValue) encodedValue).getValue().substring(1, ((TypeEncodedValue) encodedValue).getValue().length() - 1).replace('/', '.'));
                                System.out.println("prepare class: " + ((TypeEncodedValue) encodedValue).getValue());
                            }
                        }

                    }else if (dexBackedAnnotationElement.getValue() instanceof DexBackedTypeEncodedValue){
                        String value = ((DexBackedTypeEncodedValue) dexBackedAnnotationElement.getValue()).getValue();
                        prepareclasses.add(value.substring(1, value.length() - 1).replace('/', '.'));
                        System.out.println("prepare class: " + value);
                    }
                }
            }
        }

    }
 
Example #6
Source File: DexDiffInfo.java    From atlas with Apache License 2.0 5 votes vote down vote up
public void addModifiedMethods(DexBackedMethod method) throws PatchException {
    System.out.println("add modified Method:" + method.getReturnType()
            + "  " + method.getName() + "("
            + Formater.formatStringList(method.getParameterTypes())
            + ")  in Class:" + method.getDefiningClass());
    this.modifiedMethods.add(method);

    if (!modifiedClasses.contains(method.classDef)) {
        modifiedClasses.add(method.classDef);
        String className = method.classDef.getType();
        addManifestModifiedClass(className);
    }
}
 
Example #7
Source File: DexMethod.java    From apkfile with Apache License 2.0 4 votes vote down vote up
public DexBackedMethod getMethod() {
    return method;
}
 
Example #8
Source File: ApkPatch.java    From atlas with Apache License 2.0 4 votes vote down vote up
private static Set<String> buildPrepareClass(File smaliDir, List<File> newFiles,
                                                 DexDiffInfo info) throws PatchException {
        Set<DexBackedClassDef> classes = Sets.newHashSet();
        classes = SmaliDiffUtils.scanClasses(smaliDir, newFiles);
        ArrayList<String> methods = new ArrayList<String>();
        {
            Set<DexBackedMethod> tempSet = info.getModifiedMethods();
            for (DexBackedMethod methodRef : tempSet) {
                String template = methodRef.getDefiningClass() + "->" + methodRef.getName();
                methods.add(template);
                System.out.println("template: " + template);
                if (superClasses.containsKey(methodRef.getDefiningClass())) {
                    ArrayList<String> derivedClasses = superClasses.get(methodRef.getDefiningClass());
                    for (int i = 0; i < derivedClasses.size(); i++) {
                        template = derivedClasses.get(i) + "->" + methodRef.getName();
                        System.out.println("template: " + template);
                        methods.add(template);
                    }
                }
            }
        }

        Set<String> prepareClasses = new HashSet<String>();
        try {
            final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
            for (DexBackedClassDef classDef : classes) {
                currentClassType = null;
                String className = TypeGenUtil.newType(classDef.getType());
                // baksmali.disassembleClass(classDef, outFileNameHandler, options);
                File smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
                if (!smaliFile.exists()){
                    continue;
                }
                //增加class注解到prepare
                getClassAnnotaionPrepareClasses(classDef,prepareClasses,info);

                BufferedReader br = new BufferedReader(new FileReader(smaliFile));
                String data = br.readLine();// 一次读入一行,直到读入null为文件结束
                while (data != null) {
                    boolean find = false;
                    for (String m : methods) {
                        if (data.contains(m)) {
                            find = true;
                            break;
                        }
                    }
                    if (find) {
                        prepareClasses.add(className.substring(1, className.length() - 1).replace('/', '.'));
                        System.out.println("prepare class: " + className);
                        break;
                    }
                    data = br.readLine(); // 接着读下一行
                }
                br.close();

            }
        } catch (Exception e) {
            throw new PatchException(e);
        }
        for (DexBackedMethod method:info.getModifiedMethods()) {
            prepareClasses.add(method.getDefiningClass().substring(1, method.getDefiningClass().length() - 1).replace("/", "."));
        }
        //增加modify的anatation到prepare
//        getMethodAnnotaionPrepareClasses(info,prepareClasses);
        return prepareClasses;
    }
 
Example #9
Source File: MethodDiffInfo.java    From atlas with Apache License 2.0 4 votes vote down vote up
public DexBackedMethod getBackedMethod() {
    return backedMethod;
}
 
Example #10
Source File: MethodDiffInfo.java    From atlas with Apache License 2.0 4 votes vote down vote up
public void setBackedMethod(DexBackedMethod backedMethod) {
    this.backedMethod = backedMethod;
}
 
Example #11
Source File: DexDiffInfo.java    From atlas with Apache License 2.0 4 votes vote down vote up
public Set<DexBackedMethod> getModifiedMethods() {
    return modifiedMethods;
}
 
Example #12
Source File: DexDiffInfo.java    From atlas with Apache License 2.0 4 votes vote down vote up
public Set<DexBackedMethod> getAddedMethods() {
    return addedMethods;
}