Search in sources :

Example 1 with FromJson

use of com.instagram.common.json.annotation.FromJson in project ig-json-parser by Instagram.

the class JsonAnnotationProcessor method setJsonAdapterIfApplicable.

/**
 * Sets up JsonAdapter data for the annotation processor if applicable.
 *
 * @return true if we can skip enum validation of formatters, as we do not need them if we have a
 *     json adapter, false otherwise
 */
private boolean setJsonAdapterIfApplicable(TypeMirror type, JsonParserClassData injector, TypeData data, JsonField annotation) {
    // If there are custom formatters applied, it takes precedence over the json adapter of the
    // type.
    boolean eligibleToUseJsonAdapter = data.getParseType() == TypeUtils.ParseType.ENUM_OBJECT && annotation.valueExtractFormatter().isEmpty() && annotation.fieldAssignmentFormatter().isEmpty() && annotation.serializeCodeFormatter().isEmpty();
    boolean skipEnumValidationCheck = false;
    if (eligibleToUseJsonAdapter) {
        DeclaredType declaredType = (DeclaredType) type;
        Element typeElement = declaredType.asElement();
        JsonAdapter adapterAnnotation = typeElement.getAnnotation(JsonAdapter.class);
        if (adapterAnnotation != null) {
            TypeElement adapterTypeElement = AnnotationMirrorUtils.getAnnotationValueAsTypeElement(typeElement, mTypes, JsonAdapter.class, "adapterClass");
            ExecutableElement fromJson = null;
            ExecutableElement toJson = null;
            for (Element enclosedElement : adapterTypeElement.getEnclosedElements()) {
                if (enclosedElement.getKind() == METHOD) {
                    if (enclosedElement.getAnnotation(FromJson.class) != null) {
                        fromJson = (ExecutableElement) enclosedElement;
                    } else if (enclosedElement.getAnnotation(ToJson.class) != null) {
                        toJson = (ExecutableElement) enclosedElement;
                    }
                }
                if (fromJson != null && (!injector.generateSerializer() || toJson != null)) {
                    break;
                }
            }
            String qualifiedName = adapterTypeElement.getQualifiedName().toString();
            TypeMirror fromJsonParameterTypeMirror = null;
            // handle fromJson
            if (fromJson == null) {
                error("%s: method with @%s annotation must be present", type, FromJson.class.getSimpleName());
            } else if (!mTypes.isSameType(fromJson.getReturnType(), type)) {
                error(fromJson, "@%s must return the correct type, expected type: %s", FromJson.class.getSimpleName(), type);
            } else if (fromJson.getParameters().size() != 1) {
                error(fromJson, "%s: @%s must have exactly one parameter, the json type expected (String, Integer, etc.)", type, FromJson.class.getSimpleName());
            } else {
                fromJsonParameterTypeMirror = fromJson.getParameters().get(0).asType();
                data.setJsonAdapterFromJsonMethod(qualifiedName + "." + fromJson.getSimpleName().toString());
            }
            // handle toJson
            if (injector.generateSerializer() && fromJsonParameterTypeMirror != null) {
                if (toJson == null) {
                    error("%s: method with @%s annotation must be present", type, ToJson.class.getSimpleName());
                } else if (toJson.getParameters().size() != 1) {
                    error(toJson, "%s: @%s must have exactly one parameter, the type of the field.", type, ToJson.class.getSimpleName());
                } else if (!mTypes.isSameType(toJson.getParameters().get(0).asType(), type)) {
                    error(toJson, "@%s must take the correct type, expected type: %s", ToJson.class.getSimpleName(), type);
                } else if (!mTypes.isSameType(toJson.getReturnType(), fromJsonParameterTypeMirror)) {
                    error(fromJson, "@%s must return the correct type, expected type: %s", ToJson.class.getSimpleName(), fromJsonParameterTypeMirror);
                } else {
                    data.setJsonAdapterToJsonMethod(qualifiedName + "." + toJson.getSimpleName().toString());
                }
            }
            data.setJsonAdapterParseType(mTypeUtils.getParseType(fromJsonParameterTypeMirror, null));
            skipEnumValidationCheck = true;
        }
    }
    return skipEnumValidationCheck;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) ToJson(com.instagram.common.json.annotation.ToJson) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) FromJson(com.instagram.common.json.annotation.FromJson) JsonAdapter(com.instagram.common.json.annotation.JsonAdapter) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

FromJson (com.instagram.common.json.annotation.FromJson)1 JsonAdapter (com.instagram.common.json.annotation.JsonAdapter)1 ToJson (com.instagram.common.json.annotation.ToJson)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 DeclaredType (javax.lang.model.type.DeclaredType)1 TypeMirror (javax.lang.model.type.TypeMirror)1