Search in sources :

Example 6 with AnnotationAnnotationElem

use of soot.tagkit.AnnotationAnnotationElem in project soot by Sable.

the class DexPrinter method buildClassAnnotations.

private Set<Annotation> buildClassAnnotations(SootClass c) {
    Set<String> skipList = new HashSet<String>();
    Set<Annotation> annotations = buildCommonAnnotations(c, skipList);
    // so we test for enclosing methods first.
    if (c.hasTag("EnclosingMethodTag")) {
        EnclosingMethodTag eMethTag = (EnclosingMethodTag) c.getTag("EnclosingMethodTag");
        Annotation enclosingMethodItem = buildEnclosingMethodTag(eMethTag, skipList);
        if (enclosingMethodItem != null)
            annotations.add(enclosingMethodItem);
    } else if (c.hasOuterClass()) {
        if (skipList.add("Ldalvik/annotation/EnclosingClass;")) {
            // EnclosingClass annotation
            ImmutableAnnotationElement enclosingElement = new ImmutableAnnotationElement("value", new ImmutableTypeEncodedValue(SootToDexUtils.getDexClassName(c.getOuterClass().getName())));
            annotations.add(new ImmutableAnnotation(AnnotationVisibility.SYSTEM, "Ldalvik/annotation/EnclosingClass;", Collections.singleton(enclosingElement)));
        }
    }
    // respective inner classes.
    if (c.hasOuterClass()) {
        InnerClassAttribute icTag = (InnerClassAttribute) c.getOuterClass().getTag("InnerClassAttribute");
        if (icTag != null) {
            List<Annotation> innerClassItem = buildInnerClassAttribute(c, icTag, skipList);
            if (innerClassItem != null)
                annotations.addAll(innerClassItem);
        }
    }
    // Write the MemberClasses tag
    InnerClassAttribute icTag = (InnerClassAttribute) c.getTag("InnerClassAttribute");
    if (icTag != null) {
        List<Annotation> memberClassesItem = buildMemberClassesAttribute(c, icTag, skipList);
        if (memberClassesItem != null)
            annotations.addAll(memberClassesItem);
    }
    for (Tag t : c.getTags()) {
        if (t.getName().equals("VisibilityAnnotationTag")) {
            List<ImmutableAnnotation> visibilityItems = buildVisibilityAnnotationTag((VisibilityAnnotationTag) t, skipList);
            annotations.addAll(visibilityItems);
        }
    }
    // Write default-annotation tags
    List<AnnotationElem> defaults = new ArrayList<AnnotationElem>();
    for (SootMethod method : c.getMethods()) {
        AnnotationDefaultTag tag = (AnnotationDefaultTag) method.getTag("AnnotationDefaultTag");
        if (tag != null) {
            tag.getDefaultVal().setName(method.getName());
            defaults.add(tag.getDefaultVal());
        }
    }
    if (defaults.size() > 0) {
        VisibilityAnnotationTag defaultAnnotationTag = new VisibilityAnnotationTag(AnnotationConstants.RUNTIME_INVISIBLE);
        AnnotationTag a = new AnnotationTag("Ldalvik/annotation/AnnotationDefault;");
        defaultAnnotationTag.addAnnotation(a);
        AnnotationTag at = new AnnotationTag(SootToDexUtils.getDexClassName(c.getName()));
        AnnotationAnnotationElem ae = new AnnotationAnnotationElem(at, '@', "value");
        a.addElem(ae);
        for (AnnotationElem aelem : defaults) at.addElem(aelem);
        List<ImmutableAnnotation> visibilityItems = buildVisibilityAnnotationTag(defaultAnnotationTag, skipList);
        annotations.addAll(visibilityItems);
    }
    return annotations;
}
Also used : InnerClassAttribute(soot.tagkit.InnerClassAttribute) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem) ImmutableAnnotationElement(org.jf.dexlib2.immutable.ImmutableAnnotationElement) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) ArrayList(java.util.ArrayList) ImmutableTypeEncodedValue(org.jf.dexlib2.immutable.value.ImmutableTypeEncodedValue) ImmutableAnnotation(org.jf.dexlib2.immutable.ImmutableAnnotation) Annotation(org.jf.dexlib2.iface.Annotation) ImmutableAnnotation(org.jf.dexlib2.immutable.ImmutableAnnotation) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationTag(soot.tagkit.AnnotationTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) EnclosingMethodTag(soot.tagkit.EnclosingMethodTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) SootMethod(soot.SootMethod) ConstantValueTag(soot.tagkit.ConstantValueTag) SourceFileTag(soot.tagkit.SourceFileTag) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) InnerClassTag(soot.tagkit.InnerClassTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) AnnotationTag(soot.tagkit.AnnotationTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) EnclosingMethodTag(soot.tagkit.EnclosingMethodTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) Tag(soot.tagkit.Tag) LineNumberTag(soot.tagkit.LineNumberTag) ParamNamesTag(soot.tagkit.ParamNamesTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) SignatureTag(soot.tagkit.SignatureTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) AnnotationElem(soot.tagkit.AnnotationElem) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem) HashSet(java.util.HashSet)

Example 7 with AnnotationAnnotationElem

use of soot.tagkit.AnnotationAnnotationElem in project soot by Sable.

the class DexAnnotation method handleAnnotationElement.

private ArrayList<AnnotationElem> handleAnnotationElement(AnnotationElement ae, List<? extends EncodedValue> evList) {
    ArrayList<AnnotationElem> aelemList = new ArrayList<AnnotationElem>();
    for (EncodedValue ev : evList) {
        int type = ev.getValueType();
        AnnotationElem elem = null;
        switch(type) {
            case // BYTE
            0x00:
                {
                    ByteEncodedValue v = (ByteEncodedValue) ev;
                    elem = new AnnotationIntElem(v.getValue(), 'B', ae.getName());
                    break;
                }
            case // SHORT
            0x02:
                {
                    ShortEncodedValue v = (ShortEncodedValue) ev;
                    elem = new AnnotationIntElem(v.getValue(), 'S', ae.getName());
                    break;
                }
            case // CHAR
            0x03:
                {
                    CharEncodedValue v = (CharEncodedValue) ev;
                    elem = new AnnotationIntElem(v.getValue(), 'C', ae.getName());
                    break;
                }
            case // INT
            0x04:
                {
                    IntEncodedValue v = (IntEncodedValue) ev;
                    elem = new AnnotationIntElem(v.getValue(), 'I', ae.getName());
                    break;
                }
            case // LONG
            0x06:
                {
                    LongEncodedValue v = (LongEncodedValue) ev;
                    elem = new AnnotationLongElem(v.getValue(), 'J', ae.getName());
                    break;
                }
            case // FLOAT
            0x10:
                {
                    FloatEncodedValue v = (FloatEncodedValue) ev;
                    elem = new AnnotationFloatElem(v.getValue(), 'F', ae.getName());
                    break;
                }
            case // DOUBLE
            0x11:
                {
                    DoubleEncodedValue v = (DoubleEncodedValue) ev;
                    elem = new AnnotationDoubleElem(v.getValue(), 'D', ae.getName());
                    break;
                }
            case // STRING
            0x17:
                {
                    StringEncodedValue v = (StringEncodedValue) ev;
                    elem = new AnnotationStringElem(v.getValue(), 's', ae.getName());
                    break;
                }
            case // TYPE
            0x18:
                {
                    TypeEncodedValue v = (TypeEncodedValue) ev;
                    elem = new AnnotationClassElem(v.getValue(), 'c', ae.getName());
                    break;
                }
            case // FIELD (Dalvik specific?)
            0x19:
                {
                    FieldEncodedValue v = (FieldEncodedValue) ev;
                    FieldReference fr = v.getValue();
                    String fieldSig = "";
                    fieldSig += DexType.toSootAT(fr.getDefiningClass()) + ": ";
                    fieldSig += DexType.toSootAT(fr.getType()) + " ";
                    fieldSig += fr.getName();
                    elem = new AnnotationStringElem(fieldSig, 'f', ae.getName());
                    break;
                }
            case // METHOD (Dalvik specific?)
            0x1a:
                {
                    MethodEncodedValue v = (MethodEncodedValue) ev;
                    MethodReference mr = v.getValue();
                    String className = DexType.toSootICAT(mr.getDefiningClass());
                    String returnType = DexType.toSootAT(mr.getReturnType());
                    String methodName = mr.getName();
                    String parameters = "";
                    for (CharSequence p : mr.getParameterTypes()) {
                        parameters += DexType.toSootAT(p.toString());
                    }
                    String mSig = className + " |" + methodName + " |" + parameters + " |" + returnType;
                    elem = new AnnotationStringElem(mSig, 'M', ae.getName());
                    break;
                }
            case // ENUM : Warning -> encoding Dalvik specific!
            0x1b:
                {
                    EnumEncodedValue v = (EnumEncodedValue) ev;
                    FieldReference fr = v.getValue();
                    elem = new AnnotationEnumElem(DexType.toSootAT(fr.getType()).toString(), fr.getName(), 'e', ae.getName());
                    break;
                }
            case // ARRAY
            0x1c:
                {
                    ArrayEncodedValue v = (ArrayEncodedValue) ev;
                    ArrayList<AnnotationElem> l = handleAnnotationElement(ae, v.getValue());
                    if (l != null)
                        elem = new AnnotationArrayElem(l, '[', ae.getName());
                    break;
                }
            case // ANNOTATION
            0x1d:
                {
                    AnnotationEncodedValue v = (AnnotationEncodedValue) ev;
                    AnnotationTag t = new AnnotationTag(DexType.toSootAT(v.getType()).toString());
                    for (AnnotationElement newElem : v.getElements()) {
                        List<EncodedValue> l = new ArrayList<EncodedValue>();
                        l.add(newElem.getValue());
                        List<AnnotationElem> aList = handleAnnotationElement(newElem, l);
                        if (aList != null)
                            for (AnnotationElem e : aList) t.addElem(e);
                    }
                    elem = new AnnotationAnnotationElem(t, '@', ae.getName());
                    break;
                }
            case // NULL (Dalvik specific?)
            0x1e:
                {
                    elem = new AnnotationStringElem(null, 'N', ae.getName());
                    break;
                }
            case // BOOLEAN
            0x1f:
                {
                    BooleanEncodedValue v = (BooleanEncodedValue) ev;
                    elem = new AnnotationBooleanElem(v.getValue(), 'Z', ae.getName());
                    break;
                }
            default:
                {
                    throw new RuntimeException("Unknown annotation element 0x" + Integer.toHexString(type));
                }
        }
        if (elem != null)
            aelemList.add(elem);
    }
    return aelemList;
}
Also used : AnnotationStringElem(soot.tagkit.AnnotationStringElem) AnnotationEncodedValue(org.jf.dexlib2.iface.value.AnnotationEncodedValue) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem) FloatEncodedValue(org.jf.dexlib2.iface.value.FloatEncodedValue) ArrayList(java.util.ArrayList) ArrayEncodedValue(org.jf.dexlib2.iface.value.ArrayEncodedValue) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationTag(soot.tagkit.AnnotationTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) ShortEncodedValue(org.jf.dexlib2.iface.value.ShortEncodedValue) FieldEncodedValue(org.jf.dexlib2.iface.value.FieldEncodedValue) EnumEncodedValue(org.jf.dexlib2.iface.value.EnumEncodedValue) AnnotationElement(org.jf.dexlib2.iface.AnnotationElement) AnnotationFloatElem(soot.tagkit.AnnotationFloatElem) ByteEncodedValue(org.jf.dexlib2.iface.value.ByteEncodedValue) StringEncodedValue(org.jf.dexlib2.iface.value.StringEncodedValue) List(java.util.List) ArrayList(java.util.ArrayList) CharEncodedValue(org.jf.dexlib2.iface.value.CharEncodedValue) AnnotationElem(soot.tagkit.AnnotationElem) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) MethodEncodedValue(org.jf.dexlib2.iface.value.MethodEncodedValue) AnnotationBooleanElem(soot.tagkit.AnnotationBooleanElem) AnnotationLongElem(soot.tagkit.AnnotationLongElem) AnnotationArrayElem(soot.tagkit.AnnotationArrayElem) AnnotationEnumElem(soot.tagkit.AnnotationEnumElem) AnnotationEncodedValue(org.jf.dexlib2.iface.value.AnnotationEncodedValue) DoubleEncodedValue(org.jf.dexlib2.iface.value.DoubleEncodedValue) ShortEncodedValue(org.jf.dexlib2.iface.value.ShortEncodedValue) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) ArrayEncodedValue(org.jf.dexlib2.iface.value.ArrayEncodedValue) LongEncodedValue(org.jf.dexlib2.iface.value.LongEncodedValue) BooleanEncodedValue(org.jf.dexlib2.iface.value.BooleanEncodedValue) MethodEncodedValue(org.jf.dexlib2.iface.value.MethodEncodedValue) IntEncodedValue(org.jf.dexlib2.iface.value.IntEncodedValue) CharEncodedValue(org.jf.dexlib2.iface.value.CharEncodedValue) ByteEncodedValue(org.jf.dexlib2.iface.value.ByteEncodedValue) FieldEncodedValue(org.jf.dexlib2.iface.value.FieldEncodedValue) StringEncodedValue(org.jf.dexlib2.iface.value.StringEncodedValue) FloatEncodedValue(org.jf.dexlib2.iface.value.FloatEncodedValue) EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) EnumEncodedValue(org.jf.dexlib2.iface.value.EnumEncodedValue) DoubleEncodedValue(org.jf.dexlib2.iface.value.DoubleEncodedValue) AnnotationDoubleElem(soot.tagkit.AnnotationDoubleElem) AnnotationIntElem(soot.tagkit.AnnotationIntElem) BooleanEncodedValue(org.jf.dexlib2.iface.value.BooleanEncodedValue) AnnotationClassElem(soot.tagkit.AnnotationClassElem) LongEncodedValue(org.jf.dexlib2.iface.value.LongEncodedValue) MethodReference(org.jf.dexlib2.iface.reference.MethodReference) IntEncodedValue(org.jf.dexlib2.iface.value.IntEncodedValue)

Example 8 with AnnotationAnnotationElem

use of soot.tagkit.AnnotationAnnotationElem in project soot by Sable.

the class DexAnnotation method handleClassAnnotation.

/**
 * Converts Class annotations from Dexlib to Jimple.
 *
 * @param h
 * @param classDef
 */
// .annotation "Ldalvik/annotation/AnnotationDefault;"
// .annotation "Ldalvik/annotation/EnclosingClass;"
// .annotation "Ldalvik/annotation/EnclosingMethod;"
// .annotation "Ldalvik/annotation/InnerClass;"
// .annotation "Ldalvik/annotation/MemberClasses;"
// .annotation "Ldalvik/annotation/Signature;"
// .annotation "Ldalvik/annotation/Throws;"
public void handleClassAnnotation(ClassDef classDef) {
    Set<? extends Annotation> aSet = classDef.getAnnotations();
    if (aSet == null || aSet.isEmpty())
        return;
    List<Tag> tags = handleAnnotation(aSet, classDef.getType());
    if (tags == null)
        return;
    InnerClassAttribute ica = null;
    for (Tag t : tags) if (t != null) {
        if (t instanceof InnerClassTag) {
            if (ica == null) {
                // Do we already have an InnerClassAttribute?
                ica = (InnerClassAttribute) clazz.getTag("InnerClassAttribute");
                // If not, create one
                if (ica == null) {
                    ica = new InnerClassAttribute();
                    clazz.addTag(ica);
                }
            }
            ica.add((InnerClassTag) t);
        } else if (t instanceof VisibilityAnnotationTag) {
            // If a dalvik/annotation/AnnotationDefault tag is present
            // in a class, its AnnotationElements must be propagated
            // to methods through the creation of new
            // AnnotationDefaultTag.
            VisibilityAnnotationTag vt = (VisibilityAnnotationTag) t;
            for (AnnotationTag a : vt.getAnnotations()) {
                if (a.getType().equals("Ldalvik/annotation/AnnotationDefault;")) {
                    for (AnnotationElem ae : a.getElems()) {
                        if (ae instanceof AnnotationAnnotationElem) {
                            AnnotationAnnotationElem aae = (AnnotationAnnotationElem) ae;
                            AnnotationTag at = aae.getValue();
                            // extract default elements
                            Map<String, AnnotationElem> defaults = new HashMap<String, AnnotationElem>();
                            for (AnnotationElem aelem : at.getElems()) {
                                defaults.put(aelem.getName(), aelem);
                            }
                            // and add tags on methods
                            for (SootMethod sm : clazz.getMethods()) {
                                String methodName = sm.getName();
                                if (defaults.containsKey(methodName)) {
                                    AnnotationElem e = defaults.get(methodName);
                                    // Okay, the name is the same, but
                                    // is it actually the same type?
                                    Type annotationType = getSootType(e);
                                    boolean isCorrectType = false;
                                    if (annotationType == null) {
                                        // we do not know the type of
                                        // the annotation, so we guess
                                        // it's the correct type.
                                        isCorrectType = true;
                                    } else {
                                        if (annotationType.equals(sm.getReturnType())) {
                                            isCorrectType = true;
                                        } else if (annotationType.equals(ARRAY_TYPE)) {
                                            if (sm.getReturnType() instanceof ArrayType)
                                                isCorrectType = true;
                                        }
                                    }
                                    if (isCorrectType && sm.getParameterCount() == 0) {
                                        e.setName("default");
                                        AnnotationDefaultTag d = new AnnotationDefaultTag(e);
                                        sm.addTag(d);
                                        // In case there is more than
                                        // one matching method, we only
                                        // use the first one
                                        defaults.remove(sm.getName());
                                    }
                                }
                            }
                            for (Entry<String, AnnotationElem> leftOverEntry : defaults.entrySet()) {
                                // We were not able to find a matching
                                // method for the tag, because the
                                // return signature
                                // does not match
                                SootMethod found = clazz.getMethodByNameUnsafe(leftOverEntry.getKey());
                                AnnotationElem element = leftOverEntry.getValue();
                                if (found != null) {
                                    element.setName("default");
                                    AnnotationDefaultTag d = new AnnotationDefaultTag(element);
                                    found.addTag(d);
                                }
                            }
                        }
                    }
                }
            }
            if (!(vt.getVisibility() == AnnotationConstants.RUNTIME_INVISIBLE))
                clazz.addTag(vt);
        } else {
            clazz.addTag(t);
        }
    }
}
Also used : InnerClassAttribute(soot.tagkit.InnerClassAttribute) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem) HashMap(java.util.HashMap) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) ArrayType(soot.ArrayType) InnerClassTag(soot.tagkit.InnerClassTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationTag(soot.tagkit.AnnotationTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) RefType(soot.RefType) Type(soot.Type) ArrayType(soot.ArrayType) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) SootMethod(soot.SootMethod) Tag(soot.tagkit.Tag) ParamNamesTag(soot.tagkit.ParamNamesTag) DeprecatedTag(soot.tagkit.DeprecatedTag) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) InnerClassTag(soot.tagkit.InnerClassTag) SignatureTag(soot.tagkit.SignatureTag) AnnotationTag(soot.tagkit.AnnotationTag) EnclosingMethodTag(soot.tagkit.EnclosingMethodTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) AnnotationElem(soot.tagkit.AnnotationElem) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem)

Aggregations

AnnotationAnnotationElem (soot.tagkit.AnnotationAnnotationElem)8 AnnotationElem (soot.tagkit.AnnotationElem)6 AnnotationTag (soot.tagkit.AnnotationTag)6 AnnotationArrayElem (soot.tagkit.AnnotationArrayElem)5 VisibilityAnnotationTag (soot.tagkit.VisibilityAnnotationTag)5 VisibilityParameterAnnotationTag (soot.tagkit.VisibilityParameterAnnotationTag)5 ArrayList (java.util.ArrayList)4 AnnotationClassElem (soot.tagkit.AnnotationClassElem)4 AnnotationDoubleElem (soot.tagkit.AnnotationDoubleElem)4 AnnotationEnumElem (soot.tagkit.AnnotationEnumElem)4 AnnotationFloatElem (soot.tagkit.AnnotationFloatElem)4 AnnotationIntElem (soot.tagkit.AnnotationIntElem)4 AnnotationLongElem (soot.tagkit.AnnotationLongElem)4 AnnotationStringElem (soot.tagkit.AnnotationStringElem)4 SootMethod (soot.SootMethod)2 AnnotationDefaultTag (soot.tagkit.AnnotationDefaultTag)2 EnclosingMethodTag (soot.tagkit.EnclosingMethodTag)2 InnerClassAttribute (soot.tagkit.InnerClassAttribute)2 InnerClassTag (soot.tagkit.InnerClassTag)2 ParamNamesTag (soot.tagkit.ParamNamesTag)2