Search in sources :

Example 1 with Property

use of io.requery.proxy.Property in project requery by requery.

the class EntityMetaGenerator method generateProperties.

private void generateProperties(AttributeDescriptor attribute, AttributeDescriptor parent, TypeMirror typeMirror, TypeName targetName, TypeName attributeName, CodeBlock.Builder block) {
    String prefix = "";
    if (parent != null) {
        prefix = parent.getterName() + "().";
    }
    // boxed get/set using Objects
    Class propertyClass = propertyClassFor(typeMirror);
    ParameterizedTypeName propertyType = propertyName(propertyClass, targetName, attributeName);
    TypeSpec.Builder builder = TypeSpec.anonymousClassBuilder("").addSuperinterface(propertyType);
    boolean isNullable = typeMirror.getKind().isPrimitive() && attribute.isNullable();
    boolean useGetter = entity.isUnimplementable() || entity.isImmutable();
    boolean useSetter = entity.isUnimplementable();
    String getName = prefix + (useGetter ? attribute.getterName() : attribute.fieldName());
    String setName = prefix + (useSetter ? attribute.setterName() : attribute.fieldName());
    new GeneratedProperty(getName, setName, targetName, attributeName).setNullable(isNullable).setReadOnly(entity.isImmutable()).setUseMethod(useGetter).build(builder);
    // additional primitive get/set if the type is primitive
    if (propertyClass != Property.class) {
        TypeName primitiveType = TypeName.get(attribute.typeMirror());
        String name = Names.upperCaseFirst(attribute.typeMirror().toString());
        new GeneratedProperty(getName, setName, targetName, primitiveType).setMethodSuffix(name).setReadOnly(entity.isImmutable()).setUseMethod(useGetter).build(builder);
    }
    block.add(".setProperty($L)\n", builder.build());
    block.add(".setPropertyName($S)\n", attribute.element().getSimpleName());
    // property state get/set
    if (!entity.isStateless()) {
        ClassName stateClass = ClassName.get(PropertyState.class);
        TypeSpec.Builder stateType = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Property.class, targetName, stateClass));
        String fieldName = prefix + propertyStateFieldName(attribute);
        new GeneratedProperty(fieldName, targetName, stateClass).build(stateType);
        block.add(".setPropertyState($L)\n", stateType.build());
    }
    if (!entity.isImmutable()) {
        return;
    }
    // if immutable add setter for the builder
    String propertyName = attribute.fieldName();
    TypeName builderName = typeName;
    useSetter = false;
    String parameterSuffix = null;
    Optional<TypeMirror> builderType = entity.builderType();
    if (builderType.isPresent()) {
        parameterSuffix = ".builder";
        if (parent != null) {
            parameterSuffix = "." + parent.fieldName() + "Builder";
        }
        propertyName = attribute.setterName();
        useSetter = true;
        TypeElement element = elements.getTypeElement(builderType.get().toString());
        if (element != null) {
            for (ExecutableElement method : ElementFilter.methodsIn(element.getEnclosedElements())) {
                List<? extends VariableElement> parameters = method.getParameters();
                String name = Names.removeMethodPrefixes(method.getSimpleName());
                // (some builders have with<Property> setters so strip that
                if ((Names.matchesSetter("with", name)) || name.equalsIgnoreCase(attribute.fieldName()) && parameters.size() == 1) {
                    propertyName = method.getSimpleName().toString();
                    break;
                } else if (Names.matchesSetter("set", name) || name.equalsIgnoreCase(attribute.fieldName()) && parameters.size() == 1) {
                    propertyName = method.getSimpleName().toString();
                    break;
                }
            }
        } else {
            // prefixed, prefix boolean types with is
            if (ImmutableAnnotationKind.IMMUTABLE.isPresent(entity.element())) {
                propertyName = attribute.fieldName();
                String getterName = attribute.getterName().replaceFirst("get", "");
                if (attribute.typeMirror().getKind() == TypeKind.BOOLEAN) {
                    propertyName = "is" + Names.upperCaseFirst(propertyName);
                } else if (Names.isAllUpper(getterName)) {
                    propertyName = Names.lowerCaseFirst(getterName);
                }
            }
        }
    }
    propertyType = propertyName(propertyClass, builderName, attributeName);
    TypeSpec.Builder builderProperty = TypeSpec.anonymousClassBuilder("").addSuperinterface(propertyType);
    new GeneratedProperty(propertyName, builderName, attributeName).setWriteOnly(true).setUseMethod(useSetter).setAccessSuffix(parameterSuffix).build(builderProperty);
    if (propertyClass != Property.class) {
        TypeName primitiveType = TypeName.get(attribute.typeMirror());
        String name = Names.upperCaseFirst(attribute.typeMirror().toString());
        new GeneratedProperty(propertyName, builderName, primitiveType).setMethodSuffix(name).setAccessSuffix(parameterSuffix).setUseMethod(useSetter).setWriteOnly(true).build(builderProperty);
    }
    block.add(".setBuilderProperty($L)\n", builderProperty.build());
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeMirror(javax.lang.model.type.TypeMirror) ClassName(com.squareup.javapoet.ClassName) BooleanProperty(io.requery.proxy.BooleanProperty) LongProperty(io.requery.proxy.LongProperty) ByteProperty(io.requery.proxy.ByteProperty) FloatProperty(io.requery.proxy.FloatProperty) Property(io.requery.proxy.Property) IntProperty(io.requery.proxy.IntProperty) DoubleProperty(io.requery.proxy.DoubleProperty) ShortProperty(io.requery.proxy.ShortProperty) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ClassName (com.squareup.javapoet.ClassName)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 BooleanProperty (io.requery.proxy.BooleanProperty)1 ByteProperty (io.requery.proxy.ByteProperty)1 DoubleProperty (io.requery.proxy.DoubleProperty)1 FloatProperty (io.requery.proxy.FloatProperty)1 IntProperty (io.requery.proxy.IntProperty)1 LongProperty (io.requery.proxy.LongProperty)1 Property (io.requery.proxy.Property)1 ShortProperty (io.requery.proxy.ShortProperty)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1