Search in sources :

Example 11 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class AnnotationGen method encodeValue.

// TODO: refactor this boilerplate code
public void encodeValue(RootNode root, ICodeWriter code, EncodedValue encodedValue) {
    if (encodedValue == null) {
        code.add("null");
        return;
    }
    Object value = encodedValue.getValue();
    switch(encodedValue.getType()) {
        case ENCODED_NULL:
            code.add("null");
            break;
        case ENCODED_BOOLEAN:
            code.add(Boolean.TRUE.equals(value) ? "true" : "false");
            break;
        case ENCODED_BYTE:
            code.add(TypeGen.formatByte((Byte) value, false));
            break;
        case ENCODED_SHORT:
            code.add(TypeGen.formatShort((Short) value, false));
            break;
        case ENCODED_CHAR:
            code.add(getStringUtils().unescapeChar((Character) value));
            break;
        case ENCODED_INT:
            code.add(TypeGen.formatInteger((Integer) value, false));
            break;
        case ENCODED_LONG:
            code.add(TypeGen.formatLong((Long) value, false));
            break;
        case ENCODED_FLOAT:
            code.add(TypeGen.formatFloat((Float) value));
            break;
        case ENCODED_DOUBLE:
            code.add(TypeGen.formatDouble((Double) value));
            break;
        case ENCODED_STRING:
            code.add(getStringUtils().unescapeString((String) value));
            break;
        case ENCODED_TYPE:
            classGen.useType(code, ArgType.parse((String) value));
            code.add(".class");
            break;
        case ENCODED_ENUM:
        case ENCODED_FIELD:
            // must be a static field
            if (value instanceof IFieldRef) {
                FieldInfo fieldInfo = FieldInfo.fromRef(root, (IFieldRef) value);
                InsnGen.makeStaticFieldAccess(code, fieldInfo, classGen);
            } else if (value instanceof FieldInfo) {
                InsnGen.makeStaticFieldAccess(code, (FieldInfo) value, classGen);
            } else {
                throw new JadxRuntimeException("Unexpected field type class: " + value.getClass());
            }
            break;
        case ENCODED_METHOD:
            // TODO
            break;
        case ENCODED_ARRAY:
            code.add('{');
            Iterator<?> it = ((Iterable<?>) value).iterator();
            while (it.hasNext()) {
                EncodedValue v = (EncodedValue) it.next();
                encodeValue(cls.root(), code, v);
                if (it.hasNext()) {
                    code.add(", ");
                }
            }
            code.add('}');
            break;
        case ENCODED_ANNOTATION:
            formatAnnotation(code, (IAnnotation) value);
            break;
        default:
            throw new JadxRuntimeException("Can't decode value: " + encodedValue.getType() + " (" + encodedValue + ')');
    }
}
Also used : EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IFieldRef(jadx.api.plugins.input.data.IFieldRef) FieldInfo(jadx.core.dex.info.FieldInfo)

Example 12 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class ClassGen method addField.

public void addField(ICodeWriter code, FieldNode f) {
    if (f.contains(AFlag.DONT_GENERATE)) {
        return;
    }
    if (Consts.DEBUG_USAGE) {
        addFieldUsageInfo(code, f);
    }
    CodeGenUtils.addComments(code, f);
    annotationGen.addForField(code, f);
    boolean addInfoComments = f.checkCommentsLevel(CommentsLevel.INFO);
    if (f.getFieldInfo().isRenamed() && addInfoComments) {
        code.newLine();
        CodeGenUtils.addRenamedComment(code, f, f.getName());
    }
    code.startLine(f.getAccessFlags().makeString(addInfoComments));
    useType(code, f.getType());
    code.add(' ');
    code.attachDefinition(f);
    code.add(f.getAlias());
    FieldInitInsnAttr initInsnAttr = f.get(AType.FIELD_INIT_INSN);
    if (initInsnAttr != null) {
        InsnGen insnGen = makeInsnGen(initInsnAttr.getInsnMth());
        code.add(" = ");
        addInsnBody(insnGen, code, initInsnAttr.getInsn());
    } else {
        EncodedValue constVal = f.get(JadxAttrType.CONSTANT_VALUE);
        if (constVal != null) {
            code.add(" = ");
            if (constVal.getType() == EncodedType.ENCODED_NULL) {
                code.add(TypeGen.literalToString(0, f.getType(), cls, fallback));
            } else {
                Object val = EncodedValueUtils.convertToConstValue(constVal);
                if (val instanceof LiteralArg) {
                    long lit = ((LiteralArg) val).getLiteral();
                    if (!AndroidResourcesUtils.handleResourceFieldValue(cls, code, lit, f.getType())) {
                        // force literal type to be same as field (java bytecode can use different type)
                        code.add(TypeGen.literalToString(lit, f.getType(), cls, fallback));
                    }
                } else {
                    annotationGen.encodeValue(cls.root(), code, constVal);
                }
            }
        }
    }
    code.add(';');
}
Also used : EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) FieldInitInsnAttr(jadx.core.dex.attributes.FieldInitInsnAttr) LiteralArg(jadx.core.dex.instructions.args.LiteralArg)

Example 13 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class AnnotationGen method formatAnnotation.

private void formatAnnotation(ICodeWriter code, IAnnotation a) {
    code.add('@');
    ClassNode annCls = cls.root().resolveClass(a.getAnnotationClass());
    if (annCls != null) {
        classGen.useClass(code, annCls);
    } else {
        classGen.useClass(code, a.getAnnotationClass());
    }
    Map<String, EncodedValue> vl = a.getValues();
    if (!vl.isEmpty()) {
        code.add('(');
        for (Iterator<Entry<String, EncodedValue>> it = vl.entrySet().iterator(); it.hasNext(); ) {
            Entry<String, EncodedValue> e = it.next();
            String paramName = getParamName(annCls, e.getKey());
            if (paramName.equals("value") && vl.size() == 1) {
            // don't add "value = " if no other parameters
            } else {
                code.add(paramName);
                code.add(" = ");
            }
            encodeValue(cls.root(), code, e.getValue());
            if (it.hasNext()) {
                code.add(", ");
            }
        }
        code.add(')');
    }
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) Entry(java.util.Map.Entry)

Example 14 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class AnnotationsParser method readAnnotation.

public static IAnnotation readAnnotation(SectionReader in, SectionReader ext, boolean readVisibility) {
    AnnotationVisibility visibility = null;
    if (readVisibility) {
        int v = in.readUByte();
        visibility = getVisibilityValue(v);
    }
    int typeIndex = in.readUleb128();
    int size = in.readUleb128();
    Map<String, EncodedValue> values = new LinkedHashMap<>(size);
    for (int i = 0; i < size; i++) {
        String name = ext.getString(in.readUleb128());
        values.put(name, EncodedValueParser.parseValue(in, ext));
    }
    String type = ext.getType(typeIndex);
    return new JadxAnnotation(visibility, type, values);
}
Also used : AnnotationVisibility(jadx.api.plugins.input.data.annotations.AnnotationVisibility) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) JadxAnnotation(jadx.api.plugins.input.data.annotations.JadxAnnotation) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class Smali method writeFields.

private void writeFields(SmaliWriter smali, IClassData classData, List<RawField> fields, int[] colWidths) {
    int staticIdx = 0;
    smali.startLine().startLine("# fields");
    String whites = new String(new byte[Math.max(colWidths[0], colWidths[1])]).replace("\0", " ");
    for (RawField fld : fields) {
        smali.startLine();
        int pad = colWidths[0] - fld.accessFlag.length();
        if (pad > 0) {
            fld.accessFlag += whites.substring(0, pad);
        }
        smali.add(".field ").add(fld.accessFlag);
        pad = colWidths[1] - fld.name.length();
        if (pad > 0) {
            fld.name += whites.substring(0, pad);
        }
        smali.add(fld.name).add(" ");
        smali.add(": ").add(fld.type);
        if (fld.isStatic) {
            EncodedValue constVal = fld.attributes.get(JadxAttrType.CONSTANT_VALUE);
            if (constVal != null) {
                smali.add(" # init val = ");
                writeEncodedValue(smali, constVal, false);
            }
        }
        AnnotationsAttr annotationsAttr = fld.attributes.get(JadxAttrType.ANNOTATION_LIST);
        if (annotationsAttr != null) {
            smali.incIndent();
            writeAnnotations(smali, annotationsAttr.getList());
            smali.decIndent();
        }
    }
    smali.startLine();
}
Also used : AnnotationsAttr(jadx.api.plugins.input.data.attributes.types.AnnotationsAttr) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue)

Aggregations

EncodedValue (jadx.api.plugins.input.data.annotations.EncodedValue)17 FieldInfo (jadx.core.dex.info.FieldInfo)4 FieldNode (jadx.core.dex.nodes.FieldNode)4 ArrayList (java.util.ArrayList)4 IAnnotation (jadx.api.plugins.input.data.annotations.IAnnotation)3 ClassNode (jadx.core.dex.nodes.ClassNode)3 List (java.util.List)3 Nullable (org.jetbrains.annotations.Nullable)3 ICallSite (jadx.api.plugins.input.data.ICallSite)2 IFieldRef (jadx.api.plugins.input.data.IFieldRef)2 AnnotationVisibility (jadx.api.plugins.input.data.annotations.AnnotationVisibility)2 JadxAnnotation (jadx.api.plugins.input.data.annotations.JadxAnnotation)2 AnnotationsAttr (jadx.api.plugins.input.data.attributes.types.AnnotationsAttr)2 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)2 ConstPoolReader (jadx.plugins.input.java.data.ConstPoolReader)2 JavaClassParseException (jadx.plugins.input.java.utils.JavaClassParseException)2 LinkedHashMap (java.util.LinkedHashMap)2 EncodedType (jadx.api.plugins.input.data.annotations.EncodedType)1 IJadxAttribute (jadx.api.plugins.input.data.attributes.IJadxAttribute)1 AnnotationDefaultClassAttr (jadx.api.plugins.input.data.attributes.types.AnnotationDefaultClassAttr)1