Search in sources :

Example 1 with AnnotationBooleanElem

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

the class AbstractASMBackend method generateAnnotationElems.

/**
 * Emits the bytecode for the values of an annotation
 * @param av The AnnotationVisitor to emit the bytecode to
 * @param elements A collection of AnnatiotionElem that are the values of the annotation
 * @param addName True, if the name of the annotation has to be added, false otherwise (should be false only in recursive calls!)
 */
protected void generateAnnotationElems(AnnotationVisitor av, Collection<AnnotationElem> elements, boolean addName) {
    if (av != null) {
        Iterator<AnnotationElem> it = elements.iterator();
        while (it.hasNext()) {
            AnnotationElem elem = it.next();
            if (elem instanceof AnnotationEnumElem) {
                AnnotationEnumElem enumElem = (AnnotationEnumElem) elem;
                av.visitEnum(enumElem.getName(), enumElem.getTypeName(), enumElem.getConstantName());
            } else if (elem instanceof AnnotationArrayElem) {
                AnnotationArrayElem arrayElem = (AnnotationArrayElem) elem;
                AnnotationVisitor arrayVisitor = av.visitArray(arrayElem.getName());
                generateAnnotationElems(arrayVisitor, arrayElem.getValues(), false);
            } else if (elem instanceof AnnotationAnnotationElem) {
                AnnotationAnnotationElem aElem = (AnnotationAnnotationElem) elem;
                AnnotationVisitor aVisitor = av.visitAnnotation(aElem.getName(), aElem.getValue().getType());
                generateAnnotationElems(aVisitor, aElem.getValue().getElems(), true);
            } else {
                Object val = null;
                if (elem instanceof AnnotationIntElem) {
                    AnnotationIntElem intElem = (AnnotationIntElem) elem;
                    int value = intElem.getValue();
                    switch(intElem.getKind()) {
                        case 'B':
                            val = (byte) value;
                            break;
                        case 'Z':
                            val = (value == 1);
                            break;
                        case 'I':
                            val = value;
                            break;
                        case 'S':
                            val = (short) value;
                            break;
                    }
                } else if (elem instanceof AnnotationBooleanElem) {
                    AnnotationBooleanElem booleanElem = (AnnotationBooleanElem) elem;
                    val = booleanElem.getValue();
                } else if (elem instanceof AnnotationFloatElem) {
                    AnnotationFloatElem floatElem = (AnnotationFloatElem) elem;
                    val = floatElem.getValue();
                } else if (elem instanceof AnnotationLongElem) {
                    AnnotationLongElem longElem = (AnnotationLongElem) elem;
                    val = longElem.getValue();
                } else if (elem instanceof AnnotationDoubleElem) {
                    AnnotationDoubleElem doubleElem = (AnnotationDoubleElem) elem;
                    val = doubleElem.getValue();
                } else if (elem instanceof AnnotationStringElem) {
                    AnnotationStringElem stringElem = (AnnotationStringElem) elem;
                    val = stringElem.getValue();
                } else if (elem instanceof AnnotationClassElem) {
                    AnnotationClassElem classElem = (AnnotationClassElem) elem;
                    val = org.objectweb.asm.Type.getType(classElem.getDesc());
                }
                if (addName) {
                    av.visit(elem.getName(), val);
                } else {
                    av.visit(null, val);
                }
            }
        }
        av.visitEnd();
    }
}
Also used : AnnotationStringElem(soot.tagkit.AnnotationStringElem) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem) AnnotationBooleanElem(soot.tagkit.AnnotationBooleanElem) AnnotationArrayElem(soot.tagkit.AnnotationArrayElem) AnnotationLongElem(soot.tagkit.AnnotationLongElem) AnnotationEnumElem(soot.tagkit.AnnotationEnumElem) AnnotationDoubleElem(soot.tagkit.AnnotationDoubleElem) AnnotationIntElem(soot.tagkit.AnnotationIntElem) AnnotationFloatElem(soot.tagkit.AnnotationFloatElem) AnnotationClassElem(soot.tagkit.AnnotationClassElem) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) AnnotationElem(soot.tagkit.AnnotationElem) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem)

Example 2 with AnnotationBooleanElem

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

AnnotationAnnotationElem (soot.tagkit.AnnotationAnnotationElem)2 AnnotationArrayElem (soot.tagkit.AnnotationArrayElem)2 AnnotationBooleanElem (soot.tagkit.AnnotationBooleanElem)2 AnnotationClassElem (soot.tagkit.AnnotationClassElem)2 AnnotationDoubleElem (soot.tagkit.AnnotationDoubleElem)2 AnnotationElem (soot.tagkit.AnnotationElem)2 AnnotationEnumElem (soot.tagkit.AnnotationEnumElem)2 AnnotationFloatElem (soot.tagkit.AnnotationFloatElem)2 AnnotationIntElem (soot.tagkit.AnnotationIntElem)2 AnnotationLongElem (soot.tagkit.AnnotationLongElem)2 AnnotationStringElem (soot.tagkit.AnnotationStringElem)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AnnotationElement (org.jf.dexlib2.iface.AnnotationElement)1 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)1 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)1 AnnotationEncodedValue (org.jf.dexlib2.iface.value.AnnotationEncodedValue)1 ArrayEncodedValue (org.jf.dexlib2.iface.value.ArrayEncodedValue)1 BooleanEncodedValue (org.jf.dexlib2.iface.value.BooleanEncodedValue)1 ByteEncodedValue (org.jf.dexlib2.iface.value.ByteEncodedValue)1