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