Search in sources :

Example 6 with AnnotationElement

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

the class BaseMethodParameter method getSignature.

@Nullable
@Override
public String getSignature() {
    Annotation signatureAnnotation = null;
    for (Annotation annotation : getAnnotations()) {
        if (annotation.getType().equals("Ldalvik/annotation/Signature;")) {
            signatureAnnotation = annotation;
            break;
        }
    }
    if (signatureAnnotation == null) {
        return null;
    }
    ArrayEncodedValue signatureValues = null;
    for (AnnotationElement annotationElement : signatureAnnotation.getElements()) {
        if (annotationElement.getName().equals("value")) {
            EncodedValue encodedValue = annotationElement.getValue();
            if (encodedValue.getValueType() != ValueType.ARRAY) {
                return null;
            }
            signatureValues = (ArrayEncodedValue) encodedValue;
            break;
        }
    }
    if (signatureValues == null) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    for (EncodedValue signatureValue : signatureValues.getValue()) {
        if (signatureValue.getValueType() != ValueType.STRING) {
            return null;
        }
        sb.append(((StringEncodedValue) signatureValue).getValue());
    }
    return sb.toString();
}
Also used : StringEncodedValue(org.jf.dexlib2.iface.value.StringEncodedValue) EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) ArrayEncodedValue(org.jf.dexlib2.iface.value.ArrayEncodedValue) AnnotationElement(org.jf.dexlib2.iface.AnnotationElement) ArrayEncodedValue(org.jf.dexlib2.iface.value.ArrayEncodedValue) Annotation(org.jf.dexlib2.iface.Annotation) Nullable(javax.annotation.Nullable)

Example 7 with AnnotationElement

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

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 8 with AnnotationElement

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

the class ClassReIClassDef method getAnnotation.

public Set<? extends Annotation> getAnnotation(Set<? extends Annotation> annotations) {
    Set<ImmutableAnnotation> newAnnotations = new HashSet<ImmutableAnnotation>();
    for (Annotation annotation : annotations) {
        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.endsWith(";") || value.startsWith("Ljava/lang/")) {
                            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.startsWith("Ljava/lang/") || !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();
                boolean isArray3 = false;
                if (value.startsWith("[")) {
                    isArray3 = true;
                }
                String newValue = null;
                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, isArray3);
                }
                ImmutableStringEncodedValue immutableStringEncodedValue = new ImmutableStringEncodedValue(newValue);
                ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableStringEncodedValue);
                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 if (encodedValue instanceof TypeEncodedValue) {
                String newValueSub = null;
                String value = ((TypeEncodedValue) encodedValue).getValue();
                boolean isArray2 = false;
                if (value.startsWith("[")) {
                    isArray2 = true;
                }
                if (basicValue.contains(value)) {
                    newValueSub = value;
                } else if (value.startsWith("Ljava/util/") || value.startsWith("Ljava/lang/") || !value.endsWith(";")) {
                    newValueSub = value;
                } else {
                    newValueSub = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray2);
                }
                ImmutableTypeEncodedValue immutableTypeEncodedValue = new ImmutableTypeEncodedValue(newValueSub);
                ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableTypeEncodedValue);
                newAnnotationElement.add(immutableAnnotationElement);
            } else {
                newAnnotationElement.add(ImmutableAnnotationElement.of(annotationElement));
            }
        }
        ImmutableAnnotation immutableAnnotation = new ImmutableAnnotation(annotation.getVisibility(), newType, newAnnotationElement);
        newAnnotations.add(immutableAnnotation);
    }
    return newAnnotations;
}
Also used : ImmutableAnnotationElement(org.jf.dexlib2.immutable.ImmutableAnnotationElement) ArrayList(java.util.ArrayList) 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) ImmutableArrayEncodedValue(org.jf.dexlib2.immutable.value.ImmutableArrayEncodedValue) HashSet(java.util.HashSet) ImmutableTypeEncodedValue(org.jf.dexlib2.immutable.value.ImmutableTypeEncodedValue) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) MethodEncodedValue(org.jf.dexlib2.iface.value.MethodEncodedValue) ImmutableMethodEncodedValue(org.jf.dexlib2.immutable.value.ImmutableMethodEncodedValue) ImmutableTypeEncodedValue(org.jf.dexlib2.immutable.value.ImmutableTypeEncodedValue) ImmutableMethodEncodedValue(org.jf.dexlib2.immutable.value.ImmutableMethodEncodedValue) ImmutableAnnotation(org.jf.dexlib2.immutable.ImmutableAnnotation) Annotation(org.jf.dexlib2.iface.Annotation) 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) ImmutableMethodReference(org.jf.dexlib2.immutable.reference.ImmutableMethodReference) MethodReference(org.jf.dexlib2.iface.reference.MethodReference)

Example 9 with AnnotationElement

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

the class DexWriter method writeAnnotations.

private void writeAnnotations(@Nonnull DexDataWriter writer) throws IOException {
    InternalEncodedValueWriter encodedValueWriter = new InternalEncodedValueWriter(writer);
    annotationSectionOffset = writer.getPosition();
    for (Map.Entry<? extends AnnotationKey, Integer> entry : annotationSection.getItems()) {
        entry.setValue(writer.getPosition());
        AnnotationKey key = entry.getKey();
        writer.writeUbyte(annotationSection.getVisibility(key));
        writer.writeUleb128(typeSection.getItemIndex(annotationSection.getType(key)));
        Collection<? extends AnnotationElement> elements = Ordering.from(BaseAnnotationElement.BY_NAME).immutableSortedCopy(annotationSection.getElements(key));
        writer.writeUleb128(elements.size());
        for (AnnotationElement element : elements) {
            writer.writeUleb128(stringSection.getItemIndex(annotationSection.getElementName(element)));
            writeEncodedValue(encodedValueWriter, annotationSection.getElementValue(element));
        }
    }
}
Also used : BaseAnnotationElement(org.jf.dexlib2.base.BaseAnnotationElement)

Example 10 with AnnotationElement

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

the class AnnotationPool method intern.

public void intern(@Nonnull Annotation annotation) {
    Integer prev = internedItems.put(annotation, 0);
    if (prev == null) {
        dexPool.typeSection.intern(annotation.getType());
        for (AnnotationElement element : annotation.getElements()) {
            dexPool.stringSection.intern(element.getName());
            dexPool.internEncodedValue(element.getValue());
        }
    }
}
Also used : AnnotationElement(org.jf.dexlib2.iface.AnnotationElement)

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