private ArrayList<AnnotationElem> getElements(Set<? extends AnnotationElement> set) { ArrayList<AnnotationElem> aelemList = new ArrayList<AnnotationElem>(); for (AnnotationElement ae : set) { // Debug.printDbg("element: ", ae.getName() ," ", ae.getValue() ," type: ", ae.getClass()); // Debug.printDbg("value type: ", ae.getValue().getValueType() ," class: ", // ae.getValue().getClass()); Debug.printDbg(" element type: ", ae.getValue().getClass()); List<AnnotationElem> eList = handleAnnotationElement(ae, Collections.singletonList(ae.getValue())); if (eList != null) aelemList.addAll(eList); } return aelemList; }
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; Debug.printDbg("encoded value type: ", type); switch (type) { case 0x00: // BYTE { ByteEncodedValue v = (ByteEncodedValue) ev; elem = new AnnotationIntElem(v.getValue(), 'B', ae.getName()); break; } case 0x02: // SHORT { ShortEncodedValue v = (ShortEncodedValue) ev; elem = new AnnotationIntElem(v.getValue(), 'S', ae.getName()); break; } case 0x03: // CHAR { CharEncodedValue v = (CharEncodedValue) ev; elem = new AnnotationIntElem(v.getValue(), 'C', ae.getName()); break; } case 0x04: // INT { IntEncodedValue v = (IntEncodedValue) ev; elem = new AnnotationIntElem(v.getValue(), 'I', ae.getName()); break; } case 0x06: // LONG { LongEncodedValue v = (LongEncodedValue) ev; elem = new AnnotationLongElem(v.getValue(), 'J', ae.getName()); break; } case 0x10: // FLOAT { FloatEncodedValue v = (FloatEncodedValue) ev; elem = new AnnotationFloatElem(v.getValue(), 'F', ae.getName()); break; } case 0x11: // DOUBLE { DoubleEncodedValue v = (DoubleEncodedValue) ev; elem = new AnnotationDoubleElem(v.getValue(), 'D', ae.getName()); break; } case 0x17: // STRING { StringEncodedValue v = (StringEncodedValue) ev; elem = new AnnotationStringElem(v.getValue(), 's', ae.getName()); Debug.printDbg("value for string: ", v.getValue()); break; } case 0x18: // TYPE { TypeEncodedValue v = (TypeEncodedValue) ev; elem = new AnnotationClassElem(DexType.toSootAT(v.getValue()), 'c', ae.getName()); break; } case 0x19: // FIELD (Dalvik specific?) { FieldEncodedValue v = (FieldEncodedValue) ev; FieldReference fr = v.getValue(); String fieldSig = ""; fieldSig += DexType.toSootAT(fr.getDefiningClass()) + ": "; fieldSig += DexType.toSootAT(fr.getType()) + " "; fieldSig += fr.getName(); Debug.printDbg("FIELD: ", fieldSig); elem = new AnnotationStringElem(fieldSig, 'f', ae.getName()); break; } case 0x1a: // METHOD (Dalvik specific?) { 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 0x1b: // ENUM : Warning -> encoding Dalvik specific! { EnumEncodedValue v = (EnumEncodedValue) ev; FieldReference fr = v.getValue(); elem = new AnnotationEnumElem( DexType.toSootAT(fr.getType()).toString(), fr.getName(), 'e', ae.getName()); break; } case 0x1c: // ARRAY { ArrayEncodedValue v = (ArrayEncodedValue) ev; ArrayList<AnnotationElem> l = handleAnnotationElement(ae, v.getValue()); if (l != null) elem = new AnnotationArrayElem(l, '[', ae.getName()); break; } case 0x1d: // ANNOTATION { 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 0x1e: // NULL (Dalvik specific?) { elem = new AnnotationStringElem(null, 'N', ae.getName()); break; } case 0x1f: // BOOLEAN { BooleanEncodedValue v = (BooleanEncodedValue) ev; elem = new AnnotationBooleanElem(v.getValue(), 'Z', ae.getName()); break; } default: { throw new RuntimeException("Unknown annotation element 0x" + Integer.toHexString(type)); } } // switch (type) if (elem != null) aelemList.add(elem); } // for (EncodedValue) return aelemList; }
/** * Converts method and method parameters annotations from Dexlib to Jimple * * @param h * @param method */ void handleMethodAnnotation(Host h, Method method) { Set<? extends Annotation> aSet = method.getAnnotations(); if (!(aSet == null || aSet.isEmpty())) { List<Tag> tags = handleAnnotation(aSet, null); if (tags != null) for (Tag t : tags) if (t != null) { h.addTag(t); Debug.printDbg("add method annotation: ", t); } } ArrayList<String> parameterNames = new ArrayList<String>(); boolean addParameterNames = false; for (MethodParameter p : method.getParameters()) { String name = p.getName(); parameterNames.add(name); if (name != null) addParameterNames = true; } if (addParameterNames) { h.addTag(new ParamNamesTag(parameterNames)); } // Is there any parameter annotation? boolean doParam = false; List<? extends MethodParameter> parameters = method.getParameters(); for (MethodParameter p : parameters) { Debug.printDbg("parameter ", p, " annotations: ", p.getAnnotations()); if (p.getAnnotations().size() > 0) { doParam = true; break; } } if (doParam) { VisibilityParameterAnnotationTag tag = new VisibilityParameterAnnotationTag( parameters.size(), AnnotationConstants.RUNTIME_VISIBLE); for (MethodParameter p : parameters) { List<Tag> tags = handleAnnotation(p.getAnnotations(), null); // If we have no tag for this parameter, add a placeholder // so that we keep the order intact. if (tags == null) { tag.addVisibilityAnnotation(null); continue; } VisibilityAnnotationTag paramVat = new VisibilityAnnotationTag(AnnotationConstants.RUNTIME_VISIBLE); tag.addVisibilityAnnotation(paramVat); for (Tag t : tags) { if (t == null) continue; AnnotationTag vat = null; if (!(t instanceof VisibilityAnnotationTag)) { if (t instanceof DeprecatedTag) { vat = new AnnotationTag("Ljava/lang/Deprecated;"); } else if (t instanceof SignatureTag) { SignatureTag sig = (SignatureTag) t; ArrayList<AnnotationElem> sigElements = new ArrayList<AnnotationElem>(); for (String s : SootToDexUtils.splitSignature(sig.getSignature())) sigElements.add(new AnnotationStringElem(s, 's', "value")); AnnotationElem elem = new AnnotationArrayElem(sigElements, 's', "value"); vat = new AnnotationTag("Ldalvik/annotation/Signature;", Collections.singleton(elem)); } else { throw new RuntimeException( "error: unhandled tag for parameter annotation in method " + h + " (" + t + ")."); } } else { vat = ((VisibilityAnnotationTag) t).getAnnotations().get(0); } Debug.printDbg("add parameter annotation: ", t); paramVat.addAnnotation(vat); } } if (tag.getVisibilityAnnotations().size() > 0) h.addTag(tag); } }