Search in sources :

Example 1 with Annotation

use of jadx.core.dex.attributes.annotations.Annotation in project jadx by skylot.

the class AnnotationsParser method readAnnotationSet.

private AnnotationsList readAnnotationSet(int offset) throws DecodeException {
    if (offset == 0) {
        return AnnotationsList.EMPTY;
    }
    Section section = dex.openSection(offset);
    int size = section.readInt();
    if (size == 0) {
        return AnnotationsList.EMPTY;
    }
    List<Annotation> list = new ArrayList<Annotation>(size);
    for (int i = 0; i < size; i++) {
        Section anSection = dex.openSection(section.readInt());
        Annotation a = readAnnotation(dex, anSection, true);
        list.add(a);
    }
    return new AnnotationsList(list);
}
Also used : ArrayList(java.util.ArrayList) AnnotationsList(jadx.core.dex.attributes.annotations.AnnotationsList) Section(com.android.dex.Dex.Section) Annotation(jadx.core.dex.attributes.annotations.Annotation)

Example 2 with Annotation

use of jadx.core.dex.attributes.annotations.Annotation in project jadx by skylot.

the class AnnotationGen method encodeValue.

// TODO: refactor this boilerplate code
public void encodeValue(CodeWriter code, Object val) {
    if (val == null) {
        code.add("null");
        return;
    }
    if (val instanceof String) {
        code.add(getStringUtils().unescapeString((String) val));
    } else if (val instanceof Integer) {
        code.add(TypeGen.formatInteger((Integer) val));
    } else if (val instanceof Character) {
        code.add(getStringUtils().unescapeChar((Character) val));
    } else if (val instanceof Boolean) {
        code.add(Boolean.TRUE.equals(val) ? "true" : "false");
    } else if (val instanceof Float) {
        code.add(TypeGen.formatFloat((Float) val));
    } else if (val instanceof Double) {
        code.add(TypeGen.formatDouble((Double) val));
    } else if (val instanceof Long) {
        code.add(TypeGen.formatLong((Long) val));
    } else if (val instanceof Short) {
        code.add(TypeGen.formatShort((Short) val));
    } else if (val instanceof Byte) {
        code.add(TypeGen.formatByte((Byte) val));
    } else if (val instanceof ArgType) {
        classGen.useType(code, (ArgType) val);
        code.add(".class");
    } else if (val instanceof FieldInfo) {
        // must be a static field
        FieldInfo field = (FieldInfo) val;
        InsnGen.makeStaticFieldAccess(code, field, classGen);
    } else if (val instanceof Iterable) {
        code.add('{');
        Iterator<?> it = ((Iterable) val).iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            encodeValue(code, obj);
            if (it.hasNext()) {
                code.add(", ");
            }
        }
        code.add('}');
    } else if (val instanceof Annotation) {
        formatAnnotation(code, (Annotation) val);
    } else {
        // TODO: also can be method values
        throw new JadxRuntimeException("Can't decode value: " + val + " (" + val.getClass() + ")");
    }
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) Annotation(jadx.core.dex.attributes.annotations.Annotation) Iterator(java.util.Iterator) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) FieldInfo(jadx.core.dex.info.FieldInfo)

Example 3 with Annotation

use of jadx.core.dex.attributes.annotations.Annotation in project jadx by skylot.

the class AnnotationGen method add.

private void add(IAttributeNode node, CodeWriter code) {
    AnnotationsList aList = node.get(AType.ANNOTATION_LIST);
    if (aList == null || aList.isEmpty()) {
        return;
    }
    for (Annotation a : aList.getAll()) {
        String aCls = a.getAnnotationClass();
        if (aCls.startsWith(Consts.DALVIK_ANNOTATION_PKG)) {
            // skip
            if (Consts.DEBUG) {
                code.startLine("// " + a);
            }
        } else {
            code.startLine();
            formatAnnotation(code, a);
        }
    }
}
Also used : AnnotationsList(jadx.core.dex.attributes.annotations.AnnotationsList) Annotation(jadx.core.dex.attributes.annotations.Annotation)

Example 4 with Annotation

use of jadx.core.dex.attributes.annotations.Annotation in project jadx by skylot.

the class AnnotationGen method addForParameter.

public void addForParameter(CodeWriter code, MethodParameters paramsAnnotations, int n) {
    List<AnnotationsList> paramList = paramsAnnotations.getParamList();
    if (n >= paramList.size()) {
        return;
    }
    AnnotationsList aList = paramList.get(n);
    if (aList == null || aList.isEmpty()) {
        return;
    }
    for (Annotation a : aList.getAll()) {
        formatAnnotation(code, a);
        code.add(' ');
    }
}
Also used : AnnotationsList(jadx.core.dex.attributes.annotations.AnnotationsList) Annotation(jadx.core.dex.attributes.annotations.Annotation)

Example 5 with Annotation

use of jadx.core.dex.attributes.annotations.Annotation in project jadx by skylot.

the class AnnotationGen method addThrows.

@SuppressWarnings("unchecked")
public void addThrows(MethodNode mth, CodeWriter code) {
    Annotation an = mth.getAnnotation(Consts.DALVIK_THROWS);
    if (an != null) {
        Object exs = an.getDefaultValue();
        code.add(" throws ");
        for (Iterator<ArgType> it = ((List<ArgType>) exs).iterator(); it.hasNext(); ) {
            ArgType ex = it.next();
            classGen.useType(code, ex);
            if (it.hasNext()) {
                code.add(", ");
            }
        }
    }
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) AnnotationsList(jadx.core.dex.attributes.annotations.AnnotationsList) List(java.util.List) Annotation(jadx.core.dex.attributes.annotations.Annotation)

Aggregations

Annotation (jadx.core.dex.attributes.annotations.Annotation)6 AnnotationsList (jadx.core.dex.attributes.annotations.AnnotationsList)4 ArgType (jadx.core.dex.instructions.args.ArgType)3 Section (com.android.dex.Dex.Section)1 Visibility (jadx.core.dex.attributes.annotations.Annotation.Visibility)1 FieldInfo (jadx.core.dex.info.FieldInfo)1 DecodeException (jadx.core.utils.exceptions.DecodeException)1 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1