Search in sources :

Example 1 with SettableByName

use of com.datastax.oss.driver.api.core.data.SettableByName in project java-driver by datastax.

the class EntityHelperSetMethodGenerator method generate.

@Override
public Optional<MethodSpec> generate() {
    // The method's type variable: <SettableT extends SettableByName<SettableT>>
    TypeVariableName settableT = TypeVariableName.get("SettableT");
    settableT = settableT.withBounds(ParameterizedTypeName.get(ClassName.get(SettableByName.class), settableT));
    MethodSpec.Builder injectBuilder = MethodSpec.methodBuilder("set").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addTypeVariable(settableT).addParameter(ParameterSpec.builder(entityDefinition.getClassName(), "entity").build()).addParameter(ParameterSpec.builder(settableT, "target").build()).addParameter(ParameterSpec.builder(NullSavingStrategy.class, "nullSavingStrategy").build()).addParameter(ParameterSpec.builder(TypeName.BOOLEAN, "lenient").build()).returns(settableT);
    CodeBlock.Builder injectBodyBuilder = CodeBlock.builder();
    for (PropertyDefinition property : entityDefinition.getAllColumns()) {
        injectBodyBuilder.beginControlFlow("if (!lenient || hasProperty(target, $L))", property.getCqlName());
        GeneratedCodePatterns.setValue(property.getCqlName(), property.getType(), CodeBlock.of("entity.$L()", property.getGetterName()), "target", injectBodyBuilder, enclosingClass, true, true);
        injectBodyBuilder.endControlFlow();
    }
    injectBodyBuilder.addStatement("return target");
    return Optional.of(injectBuilder.addCode(injectBodyBuilder.build()).build());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) CodeBlock(com.squareup.javapoet.CodeBlock) TypeVariableName(com.squareup.javapoet.TypeVariableName) SettableByName(com.datastax.oss.driver.api.core.data.SettableByName)

Example 2 with SettableByName

use of com.datastax.oss.driver.api.core.data.SettableByName in project java-driver by datastax.

the class DaoSetEntityMethodGenerator method generate.

@Override
public Optional<MethodSpec> generate() {
    String entityParameterName = null;
    TypeElement entityElement = null;
    String targetParameterName = null;
    // SettableByName.
    if (methodElement.getParameters().size() != 2) {
        context.getMessager().error(methodElement, "Wrong number of parameters: %s methods must have two", SetEntity.class.getSimpleName());
        return Optional.empty();
    }
    TypeMirror targetParameterType = null;
    for (VariableElement parameterElement : methodElement.getParameters()) {
        TypeMirror parameterType = parameterElement.asType();
        if (context.getClassUtils().implementsSettableByName(parameterType)) {
            targetParameterName = parameterElement.getSimpleName().toString();
            targetParameterType = parameterElement.asType();
        } else if (parameterType.getKind() == TypeKind.DECLARED || parameterType.getKind() == TypeKind.TYPEVAR) {
            TypeElement parameterTypeElement = EntityUtils.asEntityElement(parameterType, typeParameters);
            if (parameterTypeElement != null) {
                entityParameterName = parameterElement.getSimpleName().toString();
                entityElement = parameterTypeElement;
            }
        }
    }
    if (entityParameterName == null || targetParameterName == null) {
        context.getMessager().error(methodElement, "Wrong parameter types: %s methods must take a %s " + "and an annotated entity (in any order)", SetEntity.class.getSimpleName(), SettableByName.class.getSimpleName());
        return Optional.empty();
    }
    // Validate the return type: either void or the same SettableByName as the parameter
    TypeMirror returnType = methodElement.getReturnType();
    boolean isVoid = returnType.getKind() == TypeKind.VOID;
    if (isVoid) {
        if (context.getClassUtils().isSame(targetParameterType, BoundStatement.class)) {
            context.getMessager().warn(methodElement, "BoundStatement is immutable, " + "this method will not modify '%s' in place. " + "It should probably return BoundStatement rather than void", targetParameterName);
        }
    } else if (!context.getTypeUtils().isSameType(returnType, targetParameterType)) {
        context.getMessager().error(methodElement, "Invalid return type: %s methods must either be void, or return the same " + "type as their settable parameter (in this case, %s to match '%s')", SetEntity.class.getSimpleName(), targetParameterType, targetParameterName);
        return Optional.empty();
    }
    // Generate the method:
    String helperFieldName = enclosingClass.addEntityHelperField(ClassName.get(entityElement));
    NullSavingStrategy nullSavingStrategy = nullSavingStrategyValidation.getNullSavingStrategy(SetEntity.class, SetEntity::nullSavingStrategy, methodElement, enclosingClass);
    // Forward to the base injector in the helper:
    return Optional.of(GeneratedCodePatterns.override(methodElement, typeParameters).addStatement("$1L$2L.set($3L, $4L, $5T.$6L, $7L)", isVoid ? "" : "return ", helperFieldName, entityParameterName, targetParameterName, NullSavingStrategy.class, nullSavingStrategy, lenient).build());
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) SetEntity(com.datastax.oss.driver.api.mapper.annotations.SetEntity) NullSavingStrategy(com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy) VariableElement(javax.lang.model.element.VariableElement) SettableByName(com.datastax.oss.driver.api.core.data.SettableByName)

Aggregations

SettableByName (com.datastax.oss.driver.api.core.data.SettableByName)2 SetEntity (com.datastax.oss.driver.api.mapper.annotations.SetEntity)1 NullSavingStrategy (com.datastax.oss.driver.api.mapper.entity.saving.NullSavingStrategy)1 CodeBlock (com.squareup.javapoet.CodeBlock)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 TypeVariableName (com.squareup.javapoet.TypeVariableName)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1