use of com.bluelinelabs.logansquare.processor.JsonObjectHolder in project LoganSquare by bluelinelabs.
the class JsonObjectProcessor method processJsonObjectAnnotation.
private void processJsonObjectAnnotation(Element element, Map<String, JsonObjectHolder> jsonObjectMap, Elements elements, Types types) {
TypeElement typeElement = (TypeElement) element;
if (element.getModifiers().contains(PRIVATE)) {
error(element, "%s: %s annotation can't be used on private classes.", typeElement.getQualifiedName(), JsonObject.class.getSimpleName());
}
JsonObjectHolder holder = jsonObjectMap.get(TypeUtils.getInjectedFQCN(typeElement, elements));
if (holder == null) {
String packageName = elements.getPackageOf(typeElement).getQualifiedName().toString();
String objectClassName = TypeUtils.getSimpleClassName(typeElement, packageName);
String injectedSimpleClassName = objectClassName + Constants.MAPPER_CLASS_SUFFIX;
boolean abstractClass = element.getModifiers().contains(ABSTRACT);
List<? extends TypeParameterElement> parentTypeParameters = new ArrayList<>();
List<String> parentUsedTypeParameters = new ArrayList<>();
TypeName parentClassName = null;
TypeMirror superclass = typeElement.getSuperclass();
if (superclass.getKind() != TypeKind.NONE) {
TypeElement superclassElement = (TypeElement) types.asElement(superclass);
if (superclassElement.getAnnotation(JsonObject.class) != null) {
if (superclassElement.getTypeParameters() != null) {
parentTypeParameters = superclassElement.getTypeParameters();
}
String superclassName = superclass.toString();
int indexOfTypeParamStart = superclassName.indexOf("<");
if (indexOfTypeParamStart > 0) {
String typeParams = superclassName.substring(indexOfTypeParamStart + 1, superclassName.length() - 1);
parentUsedTypeParameters = Arrays.asList(typeParams.split("\\s*,\\s*"));
}
}
}
while (superclass.getKind() != TypeKind.NONE) {
TypeElement superclassElement = (TypeElement) types.asElement(superclass);
if (superclassElement.getAnnotation(JsonObject.class) != null) {
String superclassPackageName = elements.getPackageOf(superclassElement).getQualifiedName().toString();
parentClassName = ClassName.get(superclassPackageName, TypeUtils.getSimpleClassName(superclassElement, superclassPackageName));
break;
}
superclass = superclassElement.getSuperclass();
}
JsonObject annotation = element.getAnnotation(JsonObject.class);
holder = new JsonObjectHolderBuilder().setPackageName(packageName).setInjectedClassName(injectedSimpleClassName).setObjectTypeName(TypeName.get(typeElement.asType())).setIsAbstractClass(abstractClass).setParentTypeName(parentClassName).setParentTypeParameters(parentTypeParameters).setParentUsedTypeParameters(parentUsedTypeParameters).setFieldDetectionPolicy(annotation.fieldDetectionPolicy()).setFieldNamingPolicy(annotation.fieldNamingPolicy()).setSerializeNullObjects(annotation.serializeNullObjects()).setSerializeNullCollectionElements(annotation.serializeNullCollectionElements()).setTypeParameters(typeElement.getTypeParameters()).build();
FieldDetectionPolicy fieldDetectionPolicy = annotation.fieldDetectionPolicy();
if (fieldDetectionPolicy == FieldDetectionPolicy.NONPRIVATE_FIELDS || fieldDetectionPolicy == FieldDetectionPolicy.NONPRIVATE_FIELDS_AND_ACCESSORS) {
addAllNonPrivateFields(element, elements, types, holder);
}
if (fieldDetectionPolicy == FieldDetectionPolicy.NONPRIVATE_FIELDS_AND_ACCESSORS) {
addAllNonPrivateAccessors(element, elements, types, holder);
}
jsonObjectMap.put(TypeUtils.getInjectedFQCN(typeElement, elements), holder);
}
}
use of com.bluelinelabs.logansquare.processor.JsonObjectHolder in project LoganSquare by bluelinelabs.
the class OnJsonParseCompleteProcessor method processOnCompleteMethodAnnotation.
private void processOnCompleteMethodAnnotation(Element element, Map<String, JsonObjectHolder> jsonObjectMap, Elements elements) throws Exception {
if (!isCallbackMethodAnnotationValid(element, OnJsonParseComplete.class.getSimpleName())) {
return;
}
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
ExecutableElement executableElement = (ExecutableElement) element;
JsonObjectHolder objectHolder = jsonObjectMap.get(TypeUtils.getInjectedFQCN(enclosingElement, elements));
objectHolder.onCompleteCallback = executableElement.getSimpleName().toString();
}
use of com.bluelinelabs.logansquare.processor.JsonObjectHolder in project LoganSquare by bluelinelabs.
the class OnPreSerializeProcessor method processOnPreJsonSerializeMethodAnnotation.
private void processOnPreJsonSerializeMethodAnnotation(Element element, Map<String, JsonObjectHolder> jsonObjectMap, Elements elements) throws Exception {
if (!isCallbackMethodAnnotationValid(element, OnPreJsonSerialize.class.getSimpleName())) {
return;
}
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
ExecutableElement executableElement = (ExecutableElement) element;
JsonObjectHolder objectHolder = jsonObjectMap.get(TypeUtils.getInjectedFQCN(enclosingElement, elements));
objectHolder.preSerializeCallback = executableElement.getSimpleName().toString();
}
use of com.bluelinelabs.logansquare.processor.JsonObjectHolder in project LoganSquare by bluelinelabs.
the class JsonFieldProcessor method processJsonFieldAnnotation.
private void processJsonFieldAnnotation(Element element, Map<String, JsonObjectHolder> jsonObjectMap, Elements elements, Types types) {
if (!isJsonFieldFieldAnnotationValid(element, elements)) {
return;
}
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
JsonObjectHolder objectHolder = jsonObjectMap.get(TypeUtils.getInjectedFQCN(enclosingElement, elements));
JsonFieldHolder fieldHolder = objectHolder.fieldMap.get(element.getSimpleName().toString());
if (fieldHolder == null) {
fieldHolder = new JsonFieldHolder();
objectHolder.fieldMap.put(element.getSimpleName().toString(), fieldHolder);
}
JsonField annotation = element.getAnnotation(JsonField.class);
TypeMirror typeConverterType;
try {
typeConverterType = mProcessingEnv.getElementUtils().getTypeElement(annotation.typeConverter().getCanonicalName()).asType();
} catch (MirroredTypeException mte) {
typeConverterType = mte.getTypeMirror();
}
String[] fieldName = annotation.name();
JsonIgnore ignoreAnnotation = element.getAnnotation(JsonIgnore.class);
boolean shouldParse = ignoreAnnotation == null || ignoreAnnotation.ignorePolicy() == IgnorePolicy.SERIALIZE_ONLY;
boolean shouldSerialize = ignoreAnnotation == null || ignoreAnnotation.ignorePolicy() == IgnorePolicy.PARSE_ONLY;
String error = fieldHolder.fill(element, elements, types, fieldName, typeConverterType, objectHolder, shouldParse, shouldSerialize);
if (!TextUtils.isEmpty(error)) {
error(element, error);
}
ensureTypeConverterClassValid(typeConverterType, elements, types);
}
Aggregations