Search in sources :

Example 1 with JsonObjectHolder

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

Example 2 with JsonObjectHolder

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();
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) JsonObjectHolder(com.bluelinelabs.logansquare.processor.JsonObjectHolder)

Example 3 with JsonObjectHolder

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();
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) JsonObjectHolder(com.bluelinelabs.logansquare.processor.JsonObjectHolder)

Example 4 with JsonObjectHolder

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);
}
Also used : MirroredTypeException(javax.lang.model.type.MirroredTypeException) JsonField(com.bluelinelabs.logansquare.annotation.JsonField) JsonIgnore(com.bluelinelabs.logansquare.annotation.JsonIgnore) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) JsonFieldHolder(com.bluelinelabs.logansquare.processor.JsonFieldHolder) JsonObjectHolder(com.bluelinelabs.logansquare.processor.JsonObjectHolder)

Aggregations

JsonObjectHolder (com.bluelinelabs.logansquare.processor.JsonObjectHolder)4 TypeElement (javax.lang.model.element.TypeElement)4 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 JsonField (com.bluelinelabs.logansquare.annotation.JsonField)1 JsonIgnore (com.bluelinelabs.logansquare.annotation.JsonIgnore)1 JsonObject (com.bluelinelabs.logansquare.annotation.JsonObject)1 FieldDetectionPolicy (com.bluelinelabs.logansquare.annotation.JsonObject.FieldDetectionPolicy)1 JsonFieldHolder (com.bluelinelabs.logansquare.processor.JsonFieldHolder)1 JsonObjectHolderBuilder (com.bluelinelabs.logansquare.processor.JsonObjectHolder.JsonObjectHolderBuilder)1 TypeName (com.squareup.javapoet.TypeName)1 ArrayList (java.util.ArrayList)1 MirroredTypeException (javax.lang.model.type.MirroredTypeException)1