Search in sources :

Example 6 with Dex

use of com.tencent.tinker.android.dex.Dex in project tinker by Tencent.

the class DexClassesComparator method startCheck.

public void startCheck(DexGroup oldDexGroup, DexGroup newDexGroup) {
    // Init assist structures.
    addedClassInfoList.clear();
    deletedClassInfoList.clear();
    changedClassDescToClassInfosMap.clear();
    oldDescriptorOfClassesToCheck.clear();
    newDescriptorOfClassesToCheck.clear();
    oldClassDescriptorToClassInfoMap.clear();
    newClassDescriptorToClassInfoMap.clear();
    refAffectedClassDescs.clear();
    // and collect typeIndex of classes to check in oldDexes.
    for (Dex oldDex : oldDexGroup.dexes) {
        int classDefIndex = 0;
        for (ClassDef oldClassDef : oldDex.classDefs()) {
            String desc = oldDex.typeNames().get(oldClassDef.typeIndex);
            if (Utils.isStringMatchesPatterns(desc, patternsOfClassDescToCheck)) {
                if (!oldDescriptorOfClassesToCheck.add(desc)) {
                    throw new IllegalStateException(String.format("duplicate class descriptor [%s] in different old dexes.", desc));
                }
            }
            DexClassInfo classInfo = new DexClassInfo(desc, classDefIndex, oldClassDef, oldDex);
            ++classDefIndex;
            oldClassDescriptorToClassInfoMap.put(desc, classInfo);
        }
    }
    // and collect typeIndex of classes to check in newDexes.
    for (Dex newDex : newDexGroup.dexes) {
        int classDefIndex = 0;
        for (ClassDef newClassDef : newDex.classDefs()) {
            String desc = newDex.typeNames().get(newClassDef.typeIndex);
            if (Utils.isStringMatchesPatterns(desc, patternsOfClassDescToCheck)) {
                if (!newDescriptorOfClassesToCheck.add(desc)) {
                    throw new IllegalStateException(String.format("duplicate class descriptor [%s] in different new dexes.", desc));
                }
            }
            DexClassInfo classInfo = new DexClassInfo(desc, classDefIndex, newClassDef, newDex);
            ++classDefIndex;
            newClassDescriptorToClassInfoMap.put(desc, classInfo);
        }
    }
    Set<String> deletedClassDescs = new HashSet<>(oldDescriptorOfClassesToCheck);
    deletedClassDescs.removeAll(newDescriptorOfClassesToCheck);
    for (String desc : deletedClassDescs) {
        // from result.
        if (Utils.isStringMatchesPatterns(desc, patternsOfIgnoredRemovedClassDesc)) {
            logger.i(TAG, "Ignored deleted class: %s", desc);
        } else {
            logger.i(TAG, "Deleted class: %s", desc);
            deletedClassInfoList.add(oldClassDescriptorToClassInfoMap.get(desc));
        }
    }
    Set<String> addedClassDescs = new HashSet<>(newDescriptorOfClassesToCheck);
    addedClassDescs.removeAll(oldDescriptorOfClassesToCheck);
    for (String desc : addedClassDescs) {
        if (Utils.isStringMatchesPatterns(desc, patternsOfIgnoredRemovedClassDesc)) {
            logger.i(TAG, "Ignored added class: %s", desc);
        } else {
            logger.i(TAG, "Added class: %s", desc);
            addedClassInfoList.add(newClassDescriptorToClassInfoMap.get(desc));
        }
    }
    Set<String> mayBeChangedClassDescs = new HashSet<>(oldDescriptorOfClassesToCheck);
    mayBeChangedClassDescs.retainAll(newDescriptorOfClassesToCheck);
    for (String desc : mayBeChangedClassDescs) {
        DexClassInfo oldClassInfo = oldClassDescriptorToClassInfoMap.get(desc);
        DexClassInfo newClassInfo = newClassDescriptorToClassInfoMap.get(desc);
        switch(compareMode) {
            case COMPARE_MODE_NORMAL:
                {
                    if (!isSameClass(oldClassInfo.owner, newClassInfo.owner, oldClassInfo.classDef, newClassInfo.classDef)) {
                        if (Utils.isStringMatchesPatterns(desc, patternsOfIgnoredRemovedClassDesc)) {
                            logger.i(TAG, "Ignored changed class: %s", desc);
                        } else {
                            logger.i(TAG, "Changed class: %s", desc);
                            changedClassDescToClassInfosMap.put(desc, new DexClassInfo[] { oldClassInfo, newClassInfo });
                        }
                    }
                    break;
                }
            case COMPARE_MODE_REFERRER_AFFECTED_CHANGE_ONLY:
                {
                    if (isClassChangeAffectedToReferrer(oldClassInfo.owner, newClassInfo.owner, oldClassInfo.classDef, newClassInfo.classDef)) {
                        if (Utils.isStringMatchesPatterns(desc, patternsOfIgnoredRemovedClassDesc)) {
                            logger.i(TAG, "Ignored referrer-affected changed class: %s", desc);
                        } else {
                            logger.i(TAG, "Referrer-affected change class: %s", desc);
                            changedClassDescToClassInfosMap.put(desc, new DexClassInfo[] { oldClassInfo, newClassInfo });
                        }
                    }
                    break;
                }
            default:
                {
                    break;
                }
        }
    }
}
Also used : ClassDef(com.tencent.tinker.android.dex.ClassDef) Dex(com.tencent.tinker.android.dex.Dex) HashSet(java.util.HashSet)

Aggregations

Dex (com.tencent.tinker.android.dex.Dex)6 HashSet (java.util.HashSet)4 ClassDef (com.tencent.tinker.android.dex.ClassDef)3 TinkerPatchException (com.tencent.tinker.build.util.TinkerPatchException)2 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 JarFile (java.util.jar.JarFile)2 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)2 DexFile (org.jf.dexlib2.iface.DexFile)2 TableOfContents (com.tencent.tinker.android.dex.TableOfContents)1 DexPatchGenerator (com.tencent.tinker.build.dexpatcher.DexPatchGenerator)1 ChangedClassesDexClassInfoCollector (com.tencent.tinker.build.dexpatcher.util.ChangedClassesDexClassInfoCollector)1 DexClassInfo (com.tencent.tinker.build.util.DexClassesComparator.DexClassInfo)1 DexGroup (com.tencent.tinker.build.util.DexClassesComparator.DexGroup)1 DexPatchApplier (com.tencent.tinker.commons.dexpatcher.DexPatchApplier)1 AnnotationSectionPatchAlgorithm (com.tencent.tinker.commons.dexpatcher.algorithms.patch.AnnotationSectionPatchAlgorithm)1 AnnotationSetRefListSectionPatchAlgorithm (com.tencent.tinker.commons.dexpatcher.algorithms.patch.AnnotationSetRefListSectionPatchAlgorithm)1 AnnotationSetSectionPatchAlgorithm (com.tencent.tinker.commons.dexpatcher.algorithms.patch.AnnotationSetSectionPatchAlgorithm)1