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