Source:
public Optional<MethodSpec> generate() { MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("validateEntityFields") .addAnnotation(Override.class) .addModifiers(Modifier.PUBLIC) .returns(TypeName.VOID); methodBuilder.addStatement( "$1T keyspaceId = context.getKeyspaceId() != null ? context.getKeyspaceId() : context.getSession().getKeyspace().orElse(null)", CqlIdentifier.class); methodBuilder.beginControlFlow( "if (!context.getSession().getMetadata().getKeyspace(keyspaceId).isPresent())"); methodBuilder.addStatement( "return"); methodBuilder.endControlFlow(); List<CodeBlock> expectedCqlNames = entityDefinition.getAllColumns().stream() .map(PropertyDefinition::getCqlName) .collect(Collectors.toList()); methodBuilder.addStatement( "$1T<$2T> expectedCqlNames = new $3T<>()", List.class, CqlIdentifier.class, ArrayList.class); for (CodeBlock expectedCqlName : expectedCqlNames) { methodBuilder.addStatement( "expectedCqlNames.add($1T.fromCql($2L))", CqlIdentifier.class, expectedCqlName); } methodBuilder.addStatement( "$1T<$2T> tableMetadata = context.getSession().getMetadata().getKeyspace(keyspaceId).flatMap(v -> v.getTable(tableId))", Optional.class, TableMetadata.class); methodBuilder.addStatement( "$1T<$2T> userDefinedType = context.getSession().getMetadata().getKeyspace(keyspaceId).flatMap(v -> v.getUserDefinedType(tableId))", Optional.class, UserDefinedType.class); methodBuilder.addStatement("String entityClassName = $S", entityDefinition.getClassName()); generateFindMissingChecks(methodBuilder); CodeBlock missingKeyspaceTableExceptionMessage = CodeBlock.of( "String.format(\"There is no ks.table: %s.%s for the entity class: %s\", keyspaceId, tableId, entityClassName)"); methodBuilder.beginControlFlow("else"); methodBuilder.addStatement( "throw new $1T($2L)", IllegalArgumentException.class, missingKeyspaceTableExceptionMessage); methodBuilder.endControlFlow(); return Optional.of(methodBuilder.build()); }

Target:
public Optional<MethodSpec> generate() { MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("validateEntityFields") .addAnnotation(Override.class) .addModifiers(Modifier.PUBLIC) .returns(TypeName.VOID); methodBuilder.addStatement( "$1T keyspaceId = this.keyspaceId != null ? this.keyspaceId : context.getSession().getKeyspace().orElse(null)", CqlIdentifier.class); methodBuilder.addStatement("String entityClassName = $S", entityDefinition.getClassName()); generateKeyspaceNull(methodBuilder); generateKeyspaceNameWrong(methodBuilder); methodBuilder.addStatement( "$1T<$2T> keyspace = context.getSession().getMetadata().getKeyspace(keyspaceId)", Optional.class, KeyspaceMetadata.class); List<CodeBlock> expectedCqlNames = entityDefinition.getAllColumns().stream() .map(PropertyDefinition::getCqlName) .collect(Collectors.toList()); methodBuilder.addStatement( "$1T<$2T> expectedCqlNames = new $3T<>()", List.class, CqlIdentifier.class, ArrayList.class); for (CodeBlock expectedCqlName : expectedCqlNames) { methodBuilder.addStatement( "expectedCqlNames.add($1T.fromCql($2L))", CqlIdentifier.class, expectedCqlName); } methodBuilder.addStatement( "$1T<$2T> tableMetadata = keyspace.flatMap(v -> v.getTable(tableId))", Optional.class, TableMetadata.class); methodBuilder.addStatement( "$1T<$2T> userDefinedType = keyspace.flatMap(v -> v.getUserDefinedType(tableId))", Optional.class, UserDefinedType.class); generateValidationChecks(methodBuilder); logMissingMetadata(methodBuilder); return Optional.of(methodBuilder.build()); }