Search in sources :

Example 1 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project atlas by alibaba.

the class AndFixFilterImpl method filterClass.

@Override
public boolean filterClass(ClassDiffInfo classDiffInfo) throws PatchException {
    boolean isInnerclass = false;
    DexBackedClassDef dexBackedClassDef = classDiffInfo.getClassDef();
    if (classDiffInfo.getType().equals(DiffType.ADD)) {
        return false;
    //            if (dexBackedClassDef.getAnnotations().size() > 0) {
    //                Set<? extends Annotation> annotations = dexBackedClassDef.getAnnotations();
    //                for (Annotation dexBackedAnnotation : annotations) {
    //                    if (dexBackedAnnotation.getType().equals("dalvik/annotation/EnclosingClass;"))
    //                        throw new PatchException("can't add member class:" + dexBackedClassDef.getType());
    //                }
    //            }
    //            String className = DexDiffer.getDalvikClassName(dexBackedClassDef.getType());
    //            MappingParser mappingParser = new MappingParser(APatchTool.mappingFile);
    //            String outterClassName = mappingParser.getOuterClass(className);
    //            if (!className.equals(outterClassName)) {
    //                isInnerclass = true;
    //                if (SmaliDiffUtils.diff(diffInfo, outterClassName, className, outFile)) {
    //                    classDiffInfo.setType(DiffType.NONE);
    //                    return true;
    //                } else {
    //                    throw new PatchException("can't add anonymous class;" + dexBackedClassDef.getType());
    //                }
    //            }
    //            throw new PatchException("can't add class:" + dexBackedClassDef.getType());
    } else if (classDiffInfo.getType().equals(DiffType.MODIFY)) {
        if (classDiffInfo.getName().endsWith(".R") || classDiffInfo.getName().contains(".R$")) {
            return true;
        }
        Set<MethodDiffInfo> needFilterMethod = new HashSet<MethodDiffInfo>();
        Set<FieldDiffInfo> needFilterField = new HashSet<FieldDiffInfo>();
        if (classDiffInfo.getModifyMethods().size() > 0) {
            for (MethodDiffInfo methodDiffInfo : classDiffInfo.getModifyMethods()) {
                if (filterMethod(methodDiffInfo)) {
                    System.out.println(methodDiffInfo.getBackedMethod().getDefiningClass() + ":" + methodDiffInfo.getBackedMethod().getName() + " is filtered!");
                    needFilterMethod.add(methodDiffInfo);
                }
            }
        }
        classDiffInfo.getModifyMethods().removeAll(needFilterMethod);
        if (classDiffInfo.getModifyFields().size() > 0) {
            for (FieldDiffInfo fieldDiffInfo : classDiffInfo.getModifyFields()) {
                if (filterField(fieldDiffInfo)) {
                    needFilterField.add(fieldDiffInfo);
                }
            }
        }
        classDiffInfo.getModifyFields().removeAll(needFilterField);
        if (classDiffInfo.getModifyFields().size() == 0 && classDiffInfo.getModifyMethods().size() == 0) {
            classDiffInfo.setType(DiffType.NONE);
            return true;
        }
    }
    return false;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) FieldDiffInfo(com.taobao.android.object.FieldDiffInfo) MethodDiffInfo(com.taobao.android.object.MethodDiffInfo) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef)

Example 2 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project atlas by alibaba.

the class ApkPatch method buildPrepareClass.

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));
            // 一次读入一行,直到读入null为文件结束
            String data = br.readLine();
            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("/", "."));
    }
    //        getMethodAnnotaionPrepareClasses(info,prepareClasses);
    return prepareClasses;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DexBackedMethod(org.jf.dexlib2.dexbacked.DexBackedMethod) ClassFileNameHandler(org.jf.util.ClassFileNameHandler) ArrayList(java.util.ArrayList) PatchException(com.taobao.android.differ.dex.PatchException) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) PatchException(com.taobao.android.differ.dex.PatchException) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project atlas by alibaba.

the class ApkPatch method getClassAnnotaionPrepareClasses.

public static void getClassAnnotaionPrepareClasses(DexBackedClassDef classDef, Set<String> prepareclasses, DexDiffInfo dexDiffInfo) {
    for (DexBackedClassDef modifyClasses : dexDiffInfo.getModifiedClasses()) {
        if (classDef.getType().equals(modifyClasses.getType())) {
            if (classDef.getAnnotations() != null) {
                Set<? extends DexBackedAnnotation> annotations = classDef.getAnnotations();
                for (DexBackedAnnotation 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 DexBackedAnnotationElement> elements = annotation.getElements();
                    for (DexBackedAnnotationElement 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);
                        } else if (dexBackedAnnotationElement.getValue() instanceof DexBackedAnnotationEncodedValue) {
                            String value = ((DexBackedAnnotationEncodedValue) dexBackedAnnotationElement.getValue()).getType();
                        }
                    }
                }
            }
        }
    }
}
Also used : DexBackedAnnotationElement(org.jf.dexlib2.dexbacked.DexBackedAnnotationElement) DexBackedArrayEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedArrayEncodedValue) DexBackedTypeEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedTypeEncodedValue) DexBackedArrayEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedArrayEncodedValue) DexBackedAnnotationEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedAnnotationEncodedValue) EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) DexBackedTypeEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedTypeEncodedValue) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) DexBackedAnnotationEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedAnnotationEncodedValue) DexBackedAnnotation(org.jf.dexlib2.dexbacked.DexBackedAnnotation) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) DexBackedTypeEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedTypeEncodedValue)

Example 4 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project atlas by alibaba.

the class PatchFieldTool method addField.

public static void addField(String inFile, String outFile, DexBackedClassDef dexBackedClassDef, ImmutableField immutableField) throws IOException {
    DexFile dexFile = readDexFile(inFile);
    for (ClassDef classDef : dexFile.getClasses()) {
        if (dexBackedClassDef != null && dexBackedClassDef.getType().equals(classDef.getType())) {
            dexFile = addField(dexFile, classDef.getType(), immutableField);
        } else if (dexBackedClassDef == null) {
            dexFile = addField(dexFile, classDef.getType(), immutableField);
        }
    }
    reDexFile(dexFile);
    DexFileFactory.writeDexFile(outFile, new DexFile() {

        @Nonnull
        @Override
        public Set<? extends ClassDef> getClasses() {
            return new AbstractSet<ClassDef>() {

                @Nonnull
                @Override
                public Iterator<ClassDef> iterator() {
                    return classes.iterator();
                }

                @Override
                public int size() {
                    return classes.size();
                }
            };
        }
    });
}
Also used : DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) Nonnull(javax.annotation.Nonnull)

Example 5 with DexBackedClassDef

use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project atlas by alibaba.

the class SmaliDiffUtils method disassemble.

public static File disassemble(File smaliDir, DexBackedClassDef dexBackedClassDef) throws PatchException {
    Set<DexBackedClassDef> classes = Sets.newHashSet();
    classes.add(dexBackedClassDef);
    final ClassFileNameHandler outFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
    final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
    String className = dexBackedClassDef.getType();
    AfBakSmali.disassembleClass(dexBackedClassDef, outFileNameHandler, getBuildOption(classes, 19), true, false);
    File smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
    return smaliFile;
}
Also used : ClassFileNameHandler(org.jf.util.ClassFileNameHandler) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) File(java.io.File)

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