Search in sources :

Example 1 with PropertyNameStyle

use of io.requery.PropertyNameStyle in project requery by requery.

the class EntityGenerator method generateProxyMethods.

private void generateProxyMethods(TypeSpec.Builder builder) {
    // add proxy field
    TypeName entityType = entity.isEmbedded() ? nameResolver.typeNameOf(parent) : typeName;
    TypeName proxyName = parameterizedTypeName(EntityProxy.class, entityType);
    FieldSpec.Builder proxyField = FieldSpec.builder(proxyName, PROXY_NAME, Modifier.PRIVATE, Modifier.FINAL, Modifier.TRANSIENT);
    if (!entity.isEmbedded()) {
        proxyField.initializer("new $T(this, $L)", proxyName, TYPE_NAME);
    }
    builder.addField(proxyField.build());
    for (AttributeDescriptor attribute : entity.attributes().values()) {
        boolean useField = attribute.isTransient() || attribute.isEmbedded();
        TypeMirror typeMirror = attribute.typeMirror();
        TypeName unboxedTypeName;
        if (attribute.isIterable()) {
            unboxedTypeName = parameterizedCollectionName(typeMirror);
        } else if (attribute.isOptional()) {
            unboxedTypeName = TypeName.get(tryFirstTypeArgument(attribute.typeMirror()));
        } else if (attribute.isEmbedded()) {
            EntityDescriptor embedded = graph.embeddedDescriptorOf(attribute).orElseThrow(IllegalStateException::new);
            unboxedTypeName = nameResolver.embeddedTypeNameOf(embedded, entity);
        } else {
            unboxedTypeName = nameResolver.tryGeneratedTypeName(typeMirror);
        }
        String attributeName = attribute.fieldName();
        String getterName = attribute.getterName();
        String fieldName = Names.upperCaseUnderscore(Names.removeMemberPrefixes(attributeName));
        if (entity.isEmbedded()) {
            fieldName = attributeFieldName(attribute);
        }
        // getter
        MethodSpec.Builder getter = MethodSpec.methodBuilder(getterName).addModifiers(Modifier.PUBLIC).returns(attribute.isOptional() ? TypeName.get(typeMirror) : unboxedTypeName);
        if (Mirrors.overridesMethod(types, typeElement, getterName)) {
            getter.addAnnotation(Override.class);
        }
        memberExtensions.forEach(extension -> extension.addToGetter(attribute, getter));
        if (useField) {
            if (attribute.isEmbedded()) {
                // have to cast to embedded type
                getter.addStatement("return ($T)this.$L", unboxedTypeName, attributeName);
            } else {
                getter.addStatement("return this.$L", attributeName);
            }
        } else if (attribute.isOptional()) {
            getter.addStatement("return $T.ofNullable($L.get($L))", Optional.class, PROXY_NAME, fieldName);
        } else {
            getter.addStatement("return $L.get($L)", PROXY_NAME, fieldName);
        }
        builder.addMethod(getter.build());
        // setter
        String setterName = attribute.setterName();
        // if read only don't generate a public setter
        boolean readOnly = entity.isReadOnly() || attribute.isReadOnly();
        // edge case check if it's interface and we need to implement the setter
        if (entity.element().getKind().isInterface() && ElementFilter.methodsIn(entity.element().getEnclosedElements()).stream().anyMatch(element -> element.getSimpleName().toString().equals(setterName))) {
            readOnly = false;
        }
        if (!readOnly) {
            TypeName setTypeName = unboxedTypeName;
            boolean castType = false;
            // use wildcard generic collection type if necessary
            if (setTypeName instanceof ParameterizedTypeName) {
                ParameterizedTypeName parameterizedName = (ParameterizedTypeName) setTypeName;
                List<TypeName> arguments = parameterizedName.typeArguments;
                List<TypeName> wildcards = new ArrayList<>();
                for (TypeName argument : arguments) {
                    if (!(argument instanceof WildcardTypeName)) {
                        Elements elements = processingEnv.getElementUtils();
                        TypeElement element = elements.getTypeElement(argument.toString());
                        if (element != null && element.getKind() == ElementKind.INTERFACE) {
                            wildcards.add(WildcardTypeName.subtypeOf(argument));
                        } else {
                            wildcards.add(argument);
                        }
                    } else {
                        wildcards.add(argument);
                    }
                }
                TypeName[] array = new TypeName[wildcards.size()];
                setTypeName = ParameterizedTypeName.get(parameterizedName.rawType, wildcards.toArray(array));
                castType = true;
            }
            String paramName = Names.lowerCaseFirst(Names.removeMemberPrefixes(attributeName));
            MethodSpec.Builder setter = MethodSpec.methodBuilder(setterName).addModifiers(Modifier.PUBLIC).addParameter(setTypeName, paramName);
            if (useField) {
                setter.addStatement("this.$L = $L", attributeName, paramName);
            } else {
                if (castType) {
                    setter.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").build());
                    setter.addStatement("$L.set($L, ($T)$L)", PROXY_NAME, fieldName, unboxedTypeName, paramName);
                } else {
                    setter.addStatement("$L.set($L, $L)", PROXY_NAME, fieldName, paramName);
                }
            }
            memberExtensions.forEach(extension -> extension.addToSetter(attribute, setter));
            PropertyNameStyle style = entity.propertyNameStyle();
            if (style == PropertyNameStyle.FLUENT || style == PropertyNameStyle.FLUENT_BEAN) {
                setter.addStatement("return this");
                setter.returns(typeName);
            }
            builder.addMethod(setter.build());
        }
    }
    MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC);
    if (entity.isEmbedded()) {
        constructor.addParameter(ParameterSpec.builder(proxyName, "proxy").build());
        constructor.addStatement("this.$L = proxy", PROXY_NAME);
        entity.attributes().values().stream().filter(attribute -> !attribute.isTransient()).forEach(attribute -> {
            ParameterizedTypeName attributeType = ParameterizedTypeName.get(ClassName.get(Attribute.class), nameResolver.typeNameOf(parent), resolveAttributeType(attribute));
            constructor.addParameter(ParameterSpec.builder(attributeType, attribute.name()).build());
            constructor.addStatement("this.$L = $L", attributeFieldName(attribute), attribute.name());
        });
    }
    generateListeners(constructor);
    // initialize the generated embedded entities
    entity.attributes().values().stream().filter(AttributeDescriptor::isEmbedded).forEach(attribute -> graph.embeddedDescriptorOf(attribute).ifPresent(embedded -> {
        ClassName embeddedName = nameResolver.embeddedTypeNameOf(embedded, entity);
        String format = embedded.attributes().values().stream().map(attr -> Names.upperCaseUnderscore(embeddedAttributeName(attribute, attr))).collect(Collectors.joining(", ", "$L = new $T($L, ", ")"));
        constructor.addStatement(format, attribute.fieldName(), embeddedName, PROXY_NAME);
    }));
    builder.addMethod(constructor.build());
}
Also used : EntityProxy(io.requery.proxy.EntityProxy) FieldSpec(com.squareup.javapoet.FieldSpec) ClassName(com.squareup.javapoet.ClassName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) PropertyNameStyle(io.requery.PropertyNameStyle) ElementFilter(javax.lang.model.util.ElementFilter) javax.lang.model.element(javax.lang.model.element) Persistable(io.requery.Persistable) ParameterSpec(com.squareup.javapoet.ParameterSpec) Entity(io.requery.Entity) MethodSpec(com.squareup.javapoet.MethodSpec) Set(java.util.Set) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) IOException(java.io.IOException) Attribute(io.requery.meta.Attribute) PropertyState(io.requery.proxy.PropertyState) TypeSpec(com.squareup.javapoet.TypeSpec) Collectors(java.util.stream.Collectors) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) PreInsertListener(io.requery.proxy.PreInsertListener) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) StringJoiner(java.util.StringJoiner) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Annotation(java.lang.annotation.Annotation) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) MethodSpec(com.squareup.javapoet.MethodSpec) Attribute(io.requery.meta.Attribute) ArrayList(java.util.ArrayList) Elements(javax.lang.model.util.Elements) FieldSpec(com.squareup.javapoet.FieldSpec) TypeMirror(javax.lang.model.type.TypeMirror) ClassName(com.squareup.javapoet.ClassName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) PropertyNameStyle(io.requery.PropertyNameStyle)

Aggregations

AnnotationSpec (com.squareup.javapoet.AnnotationSpec)1 ClassName (com.squareup.javapoet.ClassName)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 ParameterSpec (com.squareup.javapoet.ParameterSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)1 Entity (io.requery.Entity)1 Persistable (io.requery.Persistable)1 PropertyNameStyle (io.requery.PropertyNameStyle)1 Attribute (io.requery.meta.Attribute)1 EntityProxy (io.requery.proxy.EntityProxy)1 PreInsertListener (io.requery.proxy.PreInsertListener)1 PropertyState (io.requery.proxy.PropertyState)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1