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;
}
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;
}
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();
}
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);
}
};
}
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;
}
Aggregations