Search in sources :

Example 1 with AnnotationElement

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

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

the class AnnotationEncodedValueAdaptor method writeElementsTo.

public static void writeElementsTo(@Nonnull IndentingWriter writer, @Nonnull Collection<? extends AnnotationElement> annotationElements, @Nullable String containingClass) throws IOException {
    writer.indent(4);
    for (AnnotationElement annotationElement : annotationElements) {
        writer.write(annotationElement.getName());
        writer.write(" = ");
        EncodedValueAdaptor.writeTo(writer, annotationElement.getValue(), containingClass);
        writer.write('\n');
    }
    writer.deindent(4);
}
Also used : AnnotationElement(org.jf.dexlib2.iface.AnnotationElement)

Example 3 with AnnotationElement

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

the class ClassReIClassDef method reAnnotation.

@Override
protected Annotation reAnnotation(Annotation annotation) {
    String type = annotation.getType();
    boolean isArray = false;
    if (type.startsWith("[")) {
        isArray = true;
    }
    String newType = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(type)).className, isArray);
    Set<? extends AnnotationElement> sets = annotation.getElements();
    Set<ImmutableAnnotationElement> newAnnotationElement = new HashSet<ImmutableAnnotationElement>();
    for (AnnotationElement annotationElement : sets) {
        String name = annotationElement.getName();
        EncodedValue encodedValue = annotationElement.getValue();
        if (encodedValue instanceof ArrayEncodedValue) {
            List<EncodedValue> lists = new ArrayList<EncodedValue>();
            for (EncodedValue encodedValueSub : ((ArrayEncodedValue) encodedValue).getValue()) {
                if (encodedValueSub instanceof StringEncodedValue) {
                    String newValue = null;
                    boolean isArray1 = false;
                    String value = ((StringEncodedValue) encodedValueSub).getValue();
                    if (value.startsWith("[")) {
                        isArray1 = true;
                    }
                    if (basicValue.contains(value)) {
                        newValue = value;
                    } else if (value.startsWith("Ljava/util/") || value.startsWith("Ljava/lang/") || !value.endsWith(";")) {
                        newValue = value;
                    } else {
                        newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray1);
                    }
                    ImmutableStringEncodedValue immutableStringEncodedValue = new ImmutableStringEncodedValue(newValue);
                    lists.add(immutableStringEncodedValue);
                } else if (encodedValueSub instanceof TypeEncodedValue) {
                    String newValueSub = null;
                    String value = ((TypeEncodedValue) encodedValueSub).getValue();
                    boolean isArray2 = false;
                    if (value.startsWith("[")) {
                        isArray2 = true;
                    }
                    if (basicValue.contains(value)) {
                        newValueSub = value;
                    } else if (value.startsWith("Ljava/util/") || !value.endsWith(";")) {
                        newValueSub = value;
                    } else {
                        newValueSub = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray2);
                    }
                    ImmutableTypeEncodedValue immutableTypeEncodedValue = new ImmutableTypeEncodedValue(newValueSub);
                    lists.add(immutableTypeEncodedValue);
                } else {
                    lists.add(encodedValue);
                }
            }
            ImmutableArrayEncodedValue immutableArrayEncodedValue = new ImmutableArrayEncodedValue(lists);
            ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableArrayEncodedValue);
            newAnnotationElement.add(immutableAnnotationElement);
        } else if (encodedValue instanceof StringEncodedValue) {
            String value = ((StringEncodedValue) encodedValue).getValue();
            String newValue = null;
            isArray = false;
            if (value.startsWith("[")) {
                isArray = true;
            }
            if (basicValue.contains(value)) {
                newValue = value;
            } else if (value.startsWith("Ljava/util/") || !value.endsWith(";")) {
                newValue = value;
            } else {
                newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray);
            }
            ImmutableStringEncodedValue immutableStringEncodedValue = new ImmutableStringEncodedValue(newValue);
            ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableStringEncodedValue);
            newAnnotationElement.add(immutableAnnotationElement);
        } else if (encodedValue instanceof TypeEncodedValue) {
            String newValue = null;
            String value = ((TypeEncodedValue) encodedValue).getValue();
            boolean isArray2 = false;
            if (value.startsWith("[")) {
                isArray2 = true;
            }
            if (basicValue.contains(value)) {
                newValue = value;
            } else if (value.startsWith("Ljava/util/") || !value.endsWith(";")) {
                newValue = value;
            } else {
                newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray2);
            }
            ImmutableTypeEncodedValue immutableTypeEncodedValue = new ImmutableTypeEncodedValue(newValue);
            ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableTypeEncodedValue);
            newAnnotationElement.add(immutableAnnotationElement);
        } else if (encodedValue instanceof MethodEncodedValue) {
            MethodReference methodReference = ((MethodEncodedValue) encodedValue).getValue();
            String returnType = methodReference.getReturnType();
            boolean isBasic = false;
            List<? extends CharSequence> paramTypes = methodReference.getParameterTypes();
            List<CharSequence> dalvikParamTypes = new ArrayList<CharSequence>();
            List<CharSequence> newParamTypes = new ArrayList<CharSequence>();
            for (CharSequence charSequence : paramTypes) {
                if (basicType.containsKey(charSequence.toString())) {
                    newParamTypes.add(charSequence);
                    dalvikParamTypes.add(basicType.get(charSequence.toString()));
                    continue;
                }
                dalvikParamTypes.add(DefineUtils.getDalvikClassName(charSequence.toString()) + (isArray ? "[]" : ""));
                newParamTypes.add(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(charSequence.toString())).className, isArray));
            }
            final ImmutableMethodReference immutableReference = new ImmutableMethodReference(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass())).className, false), classProcessor.methodProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass()), methodReference.getName(), isBasic ? basicType.get(methodReference.getReturnType()) : DefineUtils.getDalvikClassName(methodReference.getReturnType()) + (isArray ? "[]" : ""), StringUtils.join(dalvikParamTypes.toArray(), ",")).methodName, newParamTypes, isBasic ? returnType : DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getReturnType())).className, methodReference.getReturnType().startsWith("[")));
            ImmutableMethodEncodedValue immutableMethodEncodedValue = new ImmutableMethodEncodedValue(immutableReference);
            ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableMethodEncodedValue);
            newAnnotationElement.add(immutableAnnotationElement);
        } else {
            newAnnotationElement.add(ImmutableAnnotationElement.of(annotationElement));
        }
    }
    return new ImmutableAnnotation(annotation.getVisibility(), newType, newAnnotationElement);
}
Also used : ImmutableAnnotationElement(org.jf.dexlib2.immutable.ImmutableAnnotationElement) ImmutableTypeEncodedValue(org.jf.dexlib2.immutable.value.ImmutableTypeEncodedValue) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) ArrayList(java.util.ArrayList) MethodEncodedValue(org.jf.dexlib2.iface.value.MethodEncodedValue) ImmutableMethodEncodedValue(org.jf.dexlib2.immutable.value.ImmutableMethodEncodedValue) ArrayEncodedValue(org.jf.dexlib2.iface.value.ArrayEncodedValue) ImmutableArrayEncodedValue(org.jf.dexlib2.immutable.value.ImmutableArrayEncodedValue) ImmutableTypeEncodedValue(org.jf.dexlib2.immutable.value.ImmutableTypeEncodedValue) ImmutableMethodEncodedValue(org.jf.dexlib2.immutable.value.ImmutableMethodEncodedValue) ImmutableAnnotation(org.jf.dexlib2.immutable.ImmutableAnnotation) MethodEncodedValue(org.jf.dexlib2.iface.value.MethodEncodedValue) ImmutableTypeEncodedValue(org.jf.dexlib2.immutable.value.ImmutableTypeEncodedValue) StringEncodedValue(org.jf.dexlib2.iface.value.StringEncodedValue) ImmutableMethodEncodedValue(org.jf.dexlib2.immutable.value.ImmutableMethodEncodedValue) ImmutableStringEncodedValue(org.jf.dexlib2.immutable.value.ImmutableStringEncodedValue) EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) ArrayEncodedValue(org.jf.dexlib2.iface.value.ArrayEncodedValue) ImmutableArrayEncodedValue(org.jf.dexlib2.immutable.value.ImmutableArrayEncodedValue) ImmutableAnnotationElement(org.jf.dexlib2.immutable.ImmutableAnnotationElement) AnnotationElement(org.jf.dexlib2.iface.AnnotationElement) ImmutableStringEncodedValue(org.jf.dexlib2.immutable.value.ImmutableStringEncodedValue) StringEncodedValue(org.jf.dexlib2.iface.value.StringEncodedValue) ImmutableStringEncodedValue(org.jf.dexlib2.immutable.value.ImmutableStringEncodedValue) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableArrayEncodedValue(org.jf.dexlib2.immutable.value.ImmutableArrayEncodedValue) HashSet(java.util.HashSet)

Example 4 with AnnotationElement

use of org.jf.dexlib2.iface.AnnotationElement in project smali by JesusFreke.

the class DexWriterTest method testEncodedAnnotationElementOrder.

@Test
public void testEncodedAnnotationElementOrder() {
    // Elements are out of order wrt to the element name
    ImmutableSet<ImmutableAnnotationElement> encodedElements = ImmutableSet.of(new ImmutableAnnotationElement("zabaglione", ImmutableNullEncodedValue.INSTANCE), new ImmutableAnnotationElement("blah", ImmutableNullEncodedValue.INSTANCE));
    ImmutableAnnotationEncodedValue encodedAnnotations = new ImmutableAnnotationEncodedValue("Lan/encoded/annotation", encodedElements);
    ImmutableSet<ImmutableAnnotationElement> elements = ImmutableSet.of(new ImmutableAnnotationElement("encoded_annotation", encodedAnnotations));
    ImmutableAnnotation annotation = new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lorg/test/anno;", elements);
    ImmutableClassDef classDef = new ImmutableClassDef("Lorg/test/blah;", 0, "Ljava/lang/Object;", null, null, ImmutableSet.of(annotation), null, null);
    MemoryDataStore dataStore = new MemoryDataStore();
    try {
        DexPool.writeTo(dataStore, new ImmutableDexFile(Opcodes.getDefault(), ImmutableSet.of(classDef)));
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    DexBackedDexFile dexFile = new DexBackedDexFile(Opcodes.getDefault(), dataStore.getData());
    ClassDef dbClassDef = Iterables.getFirst(dexFile.getClasses(), null);
    Assert.assertNotNull(dbClassDef);
    Annotation dbAnnotation = Iterables.getFirst(dbClassDef.getAnnotations(), null);
    Assert.assertNotNull(dbAnnotation);
    AnnotationElement element = Iterables.getFirst(dbAnnotation.getElements(), null);
    AnnotationEncodedValue dbAnnotationEncodedValue = (AnnotationEncodedValue) element.getValue();
    List<AnnotationElement> dbElements = Lists.newArrayList(dbAnnotationEncodedValue.getElements());
    // Ensure that the elements were written out in sorted order
    Assert.assertEquals(2, dbElements.size());
    Assert.assertEquals("blah", dbElements.get(0).getName());
    Assert.assertEquals("zabaglione", dbElements.get(1).getName());
}
Also used : DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) ImmutableAnnotationEncodedValue(org.jf.dexlib2.immutable.value.ImmutableAnnotationEncodedValue) AnnotationEncodedValue(org.jf.dexlib2.iface.value.AnnotationEncodedValue) ImmutableAnnotationElement(org.jf.dexlib2.immutable.ImmutableAnnotationElement) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) MemoryDataStore(org.jf.dexlib2.writer.io.MemoryDataStore) IOException(java.io.IOException) ImmutableAnnotation(org.jf.dexlib2.immutable.ImmutableAnnotation) Annotation(org.jf.dexlib2.iface.Annotation) ImmutableAnnotation(org.jf.dexlib2.immutable.ImmutableAnnotation) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) ClassDef(org.jf.dexlib2.iface.ClassDef) ImmutableAnnotationElement(org.jf.dexlib2.immutable.ImmutableAnnotationElement) AnnotationElement(org.jf.dexlib2.iface.AnnotationElement) ImmutableAnnotationEncodedValue(org.jf.dexlib2.immutable.value.ImmutableAnnotationEncodedValue) ImmutableDexFile(org.jf.dexlib2.immutable.ImmutableDexFile) Test(org.junit.Test)

Example 5 with AnnotationElement

use of org.jf.dexlib2.iface.AnnotationElement in project smali by JesusFreke.

the class EncodedValueWriter method writeAnnotation.

public void writeAnnotation(TypeKey annotationType, Collection<? extends AnnotationElement> elements) throws IOException {
    writer.writeEncodedValueHeader(ValueType.ANNOTATION, 0);
    writer.writeUleb128(typeSection.getItemIndex(annotationType));
    writer.writeUleb128(elements.size());
    Collection<? extends AnnotationElement> sortedElements = Ordering.from(BaseAnnotationElement.BY_NAME).immutableSortedCopy(elements);
    for (AnnotationElement element : sortedElements) {
        writer.writeUleb128(stringSection.getItemIndex(annotationSection.getElementName(element)));
        writeEncodedValue(annotationSection.getElementValue(element));
    }
}
Also used : BaseAnnotationElement(org.jf.dexlib2.base.BaseAnnotationElement)

Aggregations

AnnotationElement (org.jf.dexlib2.iface.AnnotationElement)10 Annotation (org.jf.dexlib2.iface.Annotation)5 EncodedValue (org.jf.dexlib2.iface.value.EncodedValue)4 ImmutableAnnotation (org.jf.dexlib2.immutable.ImmutableAnnotation)4 ImmutableAnnotationElement (org.jf.dexlib2.immutable.ImmutableAnnotationElement)4 ArrayEncodedValue (org.jf.dexlib2.iface.value.ArrayEncodedValue)3 StringEncodedValue (org.jf.dexlib2.iface.value.StringEncodedValue)3 TypeEncodedValue (org.jf.dexlib2.iface.value.TypeEncodedValue)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 BaseAnnotationElement (org.jf.dexlib2.base.BaseAnnotationElement)2 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)2 ClassDef (org.jf.dexlib2.iface.ClassDef)2 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)2 MethodEncodedValue (org.jf.dexlib2.iface.value.MethodEncodedValue)2 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)2 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)2 ImmutableMethodReference (org.jf.dexlib2.immutable.reference.ImmutableMethodReference)2 ImmutableArrayEncodedValue (org.jf.dexlib2.immutable.value.ImmutableArrayEncodedValue)2