Search in sources :

Example 1 with IFieldRef

use of jadx.api.plugins.input.data.IFieldRef 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 2 with IFieldRef

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

the class PrepareForCodeGen method checkEncodedValue.

@SuppressWarnings("unchecked")
private void checkEncodedValue(MethodNode mth, EncodedValue encodedValue) {
    switch(encodedValue.getType()) {
        case ENCODED_FIELD:
            Object fieldData = encodedValue.getValue();
            FieldInfo fieldInfo;
            if (fieldData instanceof IFieldRef) {
                fieldInfo = FieldInfo.fromRef(mth.root(), (IFieldRef) fieldData);
            } else {
                fieldInfo = (FieldInfo) fieldData;
            }
            FieldNode fieldNode = mth.root().resolveField(fieldInfo);
            if (fieldNode != null) {
                fieldNode.addUseIn(mth);
            }
            break;
        case ENCODED_ANNOTATION:
            IAnnotation annotation = (IAnnotation) encodedValue.getValue();
            annotation.getValues().forEach((k, v) -> checkEncodedValue(mth, v));
            break;
        case ENCODED_ARRAY:
            List<EncodedValue> valueList = (List<EncodedValue>) encodedValue.getValue();
            valueList.forEach(v -> checkEncodedValue(mth, v));
            break;
    }
}
Also used : IAnnotation(jadx.api.plugins.input.data.annotations.IAnnotation) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) FieldNode(jadx.core.dex.nodes.FieldNode) InsnList(jadx.core.utils.InsnList) List(java.util.List) IFieldRef(jadx.api.plugins.input.data.IFieldRef) FieldInfo(jadx.core.dex.info.FieldInfo)

Aggregations

IFieldRef (jadx.api.plugins.input.data.IFieldRef)2 EncodedValue (jadx.api.plugins.input.data.annotations.EncodedValue)2 FieldInfo (jadx.core.dex.info.FieldInfo)2 IAnnotation (jadx.api.plugins.input.data.annotations.IAnnotation)1 FieldNode (jadx.core.dex.nodes.FieldNode)1 InsnList (jadx.core.utils.InsnList)1 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)1 List (java.util.List)1