Search in sources :

Example 1 with JsonObject

use of com.bluelinelabs.logansquare.annotation.JsonObject 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);
    }
}
Also used : TypeName(com.squareup.javapoet.TypeName) FieldDetectionPolicy(com.bluelinelabs.logansquare.annotation.JsonObject.FieldDetectionPolicy) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) JsonObject(com.bluelinelabs.logansquare.annotation.JsonObject) JsonObjectHolderBuilder(com.bluelinelabs.logansquare.processor.JsonObjectHolder.JsonObjectHolderBuilder) TypeMirror(javax.lang.model.type.TypeMirror) JsonObjectHolder(com.bluelinelabs.logansquare.processor.JsonObjectHolder)

Aggregations

JsonObject (com.bluelinelabs.logansquare.annotation.JsonObject)1 FieldDetectionPolicy (com.bluelinelabs.logansquare.annotation.JsonObject.FieldDetectionPolicy)1 JsonObjectHolder (com.bluelinelabs.logansquare.processor.JsonObjectHolder)1 JsonObjectHolderBuilder (com.bluelinelabs.logansquare.processor.JsonObjectHolder.JsonObjectHolderBuilder)1 TypeName (com.squareup.javapoet.TypeName)1 ArrayList (java.util.ArrayList)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1