Search in sources :

Example 1 with Annotation

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

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

the class ApkPatch method getMethodAnnotaionPrepareClasses.

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);
                }
            }
        }
    }
}
Also used : 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) DexBackedAnnotationElement(org.jf.dexlib2.dexbacked.DexBackedAnnotationElement) AnnotationElement(org.jf.dexlib2.iface.AnnotationElement) DexBackedMethod(org.jf.dexlib2.dexbacked.DexBackedMethod) Annotation(org.jf.dexlib2.iface.Annotation) DexBackedAnnotation(org.jf.dexlib2.dexbacked.DexBackedAnnotation) DexBackedTypeEncodedValue(org.jf.dexlib2.dexbacked.value.DexBackedTypeEncodedValue)

Example 3 with Annotation

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

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

the class MethodDefinition method writeParameters.

private static void writeParameters(IndentingWriter writer, Method method, List<? extends MethodParameter> parameters, baksmaliOptions options) throws IOException {
    boolean isStatic = AccessFlags.STATIC.isSet(method.getAccessFlags());
    int registerNumber = isStatic ? 0 : 1;
    for (MethodParameter parameter : parameters) {
        String parameterType = parameter.getType();
        String parameterName = parameter.getName();
        Collection<? extends Annotation> annotations = parameter.getAnnotations();
        if (parameterName != null || annotations.size() != 0) {
            writer.write(".param p");
            writer.printSignedIntAsDec(registerNumber);
            if (parameterName != null && options.outputDebugInfo) {
                writer.write(", ");
                ReferenceFormatter.writeStringReference(writer, parameterName);
            }
            writer.write("    # ");
            writer.write(parameterType);
            writer.write("\n");
            if (annotations.size() > 0) {
                writer.indent(4);
                String containingClass = null;
                if (options.useImplicitReferences) {
                    containingClass = method.getDefiningClass();
                }
                AnnotationFormatter.writeTo(writer, annotations, containingClass);
                writer.deindent(4);
                writer.write(".end param\n");
            }
        }
        registerNumber++;
        if (TypeUtils.isWideType(parameterType)) {
            registerNumber++;
        }
    }
}
Also used : MethodParameter(org.jf.dexlib2.iface.MethodParameter)

Example 5 with Annotation

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

the class AnnotationFormatter method writeTo.

public static void writeTo(@Nonnull IndentingWriter writer, @Nonnull Collection<? extends Annotation> annotations, @Nullable String containingClass, MethodReplaceAnnotation methodReplaceAnnotation) throws IOException {
    boolean first = true;
    for (Annotation annotation : annotations) {
        if (!first) {
            writer.write('\n');
        }
        first = false;
        writeTo(writer, annotation, containingClass);
    }
    if (null != methodReplaceAnnotation) {
        writeTo(writer, methodReplaceAnnotation, containingClass);
    }
}
Also used : Annotation(org.jf.dexlib2.iface.Annotation) MethodReplaceAnnotation(com.taobao.android.apatch.annotation.MethodReplaceAnnotation)

Aggregations

Annotation (org.jf.dexlib2.iface.Annotation)10 AnnotationElement (org.jf.dexlib2.iface.AnnotationElement)7 EncodedValue (org.jf.dexlib2.iface.value.EncodedValue)7 HashSet (java.util.HashSet)4 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)4 Instruction (org.jf.dexlib2.iface.instruction.Instruction)4 ImmutableAnnotation (org.jf.dexlib2.immutable.ImmutableAnnotation)4 ImmutableAnnotationElement (org.jf.dexlib2.immutable.ImmutableAnnotationElement)4 ArrayList (java.util.ArrayList)3 Nonnull (javax.annotation.Nonnull)3 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)3 ClassDef (org.jf.dexlib2.iface.ClassDef)3 ArrayEncodedValue (org.jf.dexlib2.iface.value.ArrayEncodedValue)3 TypeEncodedValue (org.jf.dexlib2.iface.value.TypeEncodedValue)3 MemoryDataStore (org.jf.dexlib2.writer.io.MemoryDataStore)3 Test (org.junit.Test)3 MethodReplaceAnnotation (com.taobao.android.apatch.annotation.MethodReplaceAnnotation)2 IOException (java.io.IOException)2 Nullable (javax.annotation.Nullable)2 BuilderInstruction21c (org.jf.dexlib2.builder.instruction.BuilderInstruction21c)2