Search in sources :

Example 31 with AnnotationTag

use of soot.tagkit.AnnotationTag 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 32 with AnnotationTag

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

the class DexPrinter method buildVisibilityParameterAnnotationTag.

private List<ImmutableAnnotation> buildVisibilityParameterAnnotationTag(VisibilityParameterAnnotationTag t, Set<String> skipList, int paramIdx) {
    if (t.getVisibilityAnnotations() == null)
        return Collections.emptyList();
    int paramTagIdx = 0;
    List<ImmutableAnnotation> annotations = new ArrayList<ImmutableAnnotation>();
    for (VisibilityAnnotationTag vat : t.getVisibilityAnnotations()) {
        if (paramTagIdx == paramIdx && vat != null && vat.getAnnotations() != null)
            for (AnnotationTag at : vat.getAnnotations()) {
                String type = at.getType();
                if (!skipList.add(type))
                    continue;
                Set<String> alreadyWritten = new HashSet<String>();
                List<AnnotationElement> elements = null;
                if (!at.getElems().isEmpty()) {
                    elements = new ArrayList<AnnotationElement>();
                    for (AnnotationElem ae : at.getElems()) {
                        if (ae.getName() == null || ae.getName().isEmpty())
                            throw new RuntimeException("Null or empty annotation name encountered");
                        if (!alreadyWritten.add(ae.getName()))
                            throw new RuntimeException("Duplicate annotation attribute: " + ae.getName());
                        EncodedValue value = buildEncodedValueForAnnotation(ae);
                        ImmutableAnnotationElement element = new ImmutableAnnotationElement(ae.getName(), value);
                        elements.add(element);
                    }
                }
                ImmutableAnnotation ann = new ImmutableAnnotation(getVisibility(vat.getVisibility()), SootToDexUtils.getDexClassName(at.getType()), elements);
                annotations.add(ann);
            }
        paramTagIdx++;
    }
    return annotations;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ImmutableAnnotationElement(org.jf.dexlib2.immutable.ImmutableAnnotationElement) ArrayList(java.util.ArrayList) ImmutableAnnotation(org.jf.dexlib2.immutable.ImmutableAnnotation) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationTag(soot.tagkit.AnnotationTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) ImmutableNullEncodedValue(org.jf.dexlib2.immutable.value.ImmutableNullEncodedValue) ImmutableFieldEncodedValue(org.jf.dexlib2.immutable.value.ImmutableFieldEncodedValue) ImmutableByteEncodedValue(org.jf.dexlib2.immutable.value.ImmutableByteEncodedValue) ImmutableStringEncodedValue(org.jf.dexlib2.immutable.value.ImmutableStringEncodedValue) ImmutableEncodedValue(org.jf.dexlib2.immutable.value.ImmutableEncodedValue) ImmutableAnnotationEncodedValue(org.jf.dexlib2.immutable.value.ImmutableAnnotationEncodedValue) ImmutableCharEncodedValue(org.jf.dexlib2.immutable.value.ImmutableCharEncodedValue) EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) ImmutableFloatEncodedValue(org.jf.dexlib2.immutable.value.ImmutableFloatEncodedValue) ImmutableLongEncodedValue(org.jf.dexlib2.immutable.value.ImmutableLongEncodedValue) ImmutableShortEncodedValue(org.jf.dexlib2.immutable.value.ImmutableShortEncodedValue) ImmutableTypeEncodedValue(org.jf.dexlib2.immutable.value.ImmutableTypeEncodedValue) ImmutableDoubleEncodedValue(org.jf.dexlib2.immutable.value.ImmutableDoubleEncodedValue) ImmutableMethodEncodedValue(org.jf.dexlib2.immutable.value.ImmutableMethodEncodedValue) ImmutableArrayEncodedValue(org.jf.dexlib2.immutable.value.ImmutableArrayEncodedValue) ImmutableEnumEncodedValue(org.jf.dexlib2.immutable.value.ImmutableEnumEncodedValue) ImmutableIntEncodedValue(org.jf.dexlib2.immutable.value.ImmutableIntEncodedValue) ImmutableBooleanEncodedValue(org.jf.dexlib2.immutable.value.ImmutableBooleanEncodedValue) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) ArrayList(java.util.ArrayList) List(java.util.List) AnnotationElem(soot.tagkit.AnnotationElem) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem)

Example 33 with AnnotationTag

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

the class AbstractJasminClass method getVisibilityAnnotationAttr.

private String getVisibilityAnnotationAttr(VisibilityAnnotationTag tag) {
    StringBuffer sb = new StringBuffer();
    if (tag == null) {
        return "";
    } else if (tag.getVisibility() == AnnotationConstants.RUNTIME_VISIBLE) {
        sb.append(".runtime_visible_annotation\n");
    } else if (tag.getVisibility() == AnnotationConstants.RUNTIME_INVISIBLE) {
        sb.append(".runtime_invisible_annotation\n");
    } else {
        // source level annotation
        return "";
    }
    if (tag.hasAnnotations()) {
        Iterator<AnnotationTag> it = tag.getAnnotations().iterator();
        while (it.hasNext()) {
            AnnotationTag annot = it.next();
            sb.append(".annotation ");
            sb.append(soot.util.StringTools.getQuotedStringOf(annot.getType()) + "\n");
            for (AnnotationElem ae : annot.getElems()) {
                sb.append(getElemAttr(ae));
            }
            sb.append(".end .annotation\n");
        }
    }
    sb.append(".end .annotation_attr\n");
    return sb.toString();
}
Also used : VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationTag(soot.tagkit.AnnotationTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem) AnnotationElem(soot.tagkit.AnnotationElem)

Example 34 with AnnotationTag

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

the class TagBuilder method visitAnnotation.

/**
 * @see FieldVisitor#visitAnnotation(String, boolean)
 * @see MethodVisitor#visitAnnotation(String, boolean)
 * @see ClassVisitor#visitAnnotation(String, boolean)
 */
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
    VisibilityAnnotationTag tag;
    if (visible) {
        tag = visibleTag;
        if (tag == null) {
            visibleTag = tag = new VisibilityAnnotationTag(AnnotationConstants.RUNTIME_VISIBLE);
            host.addTag(tag);
        }
    } else {
        tag = invisibleTag;
        if (tag == null) {
            invisibleTag = tag = new VisibilityAnnotationTag(AnnotationConstants.RUNTIME_INVISIBLE);
            host.addTag(tag);
        }
    }
    scb.addDep(AsmUtil.toQualifiedName(desc.substring(1, desc.length() - 1)));
    final VisibilityAnnotationTag _tag = tag;
    return new AnnotationElemBuilder() {

        @Override
        public void visitEnd() {
            AnnotationTag annotTag = new AnnotationTag(desc, elems);
            _tag.addAnnotation(annotTag);
        }
    };
}
Also used : AnnotationTag(soot.tagkit.AnnotationTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag)

Example 35 with AnnotationTag

use of soot.tagkit.AnnotationTag 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)

Aggregations

AnnotationTag (soot.tagkit.AnnotationTag)37 VisibilityAnnotationTag (soot.tagkit.VisibilityAnnotationTag)22 VisibilityParameterAnnotationTag (soot.tagkit.VisibilityParameterAnnotationTag)20 ArrayList (java.util.ArrayList)15 AnnotationAnnotationElem (soot.tagkit.AnnotationAnnotationElem)11 AnnotationElem (soot.tagkit.AnnotationElem)11 Tag (soot.tagkit.Tag)10 AnnotationClassElem (soot.tagkit.AnnotationClassElem)7 AnnotationDefaultTag (soot.tagkit.AnnotationDefaultTag)7 AnnotationStringElem (soot.tagkit.AnnotationStringElem)7 EnclosingMethodTag (soot.tagkit.EnclosingMethodTag)7 InnerClassTag (soot.tagkit.InnerClassTag)7 SignatureTag (soot.tagkit.SignatureTag)7 AnnotationArrayElem (soot.tagkit.AnnotationArrayElem)6 SootMethod (soot.SootMethod)4 HashSet (java.util.HashSet)3 List (java.util.List)3 AnnotationElement (org.jf.dexlib2.iface.AnnotationElement)3 EncodedValue (org.jf.dexlib2.iface.value.EncodedValue)3 ImmutableAnnotation (org.jf.dexlib2.immutable.ImmutableAnnotation)3