use of com.bluelinelabs.logansquare.annotation.JsonObject.FieldDetectionPolicy 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);
}
}
Aggregations