Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.x: Supplier in builder #7402

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ static TypeHandler create(String name, String getterName, String setterName, Typ
if (TypeNames.OPTIONAL.equals(returnType)) {
return new TypeHandlerOptional(name, getterName, setterName, returnType);
}
if (TypeNames.SUPPLIER.equals(returnType)) {
return new TypeHandlerSupplier(name, getterName, setterName, returnType);
}
if (TypeNames.SET.equals(returnType)) {
return new TypeHandlerSet(name, getterName, setterName, returnType);
}
Expand Down Expand Up @@ -202,7 +205,7 @@ String configGet(PrototypeProperty.ConfiguredOption configured) {

String generateFromConfig(FactoryMethods factoryMethods) {
if (actualType().fqName().equals("char[]")) {
return ".asString().map(String::toCharArray)";
return ".asString().as(String::toCharArray)";
}

TypeName boxed = actualType().boxed();
Expand All @@ -214,7 +217,7 @@ String generateFromConfig(FactoryMethods factoryMethods) {

void generateFromConfig(Method.Builder method, FactoryMethods factoryMethods) {
if (actualType().fqName().equals("char[]")) {
method.add(".asString().map(").typeName(String.class).add("::toCharArray)");
method.add(".asString().as(").typeName(String.class).add("::toCharArray)");
return;
}

Expand Down Expand Up @@ -296,10 +299,10 @@ boolean builderGetterOptional(boolean required, boolean hasDefault) {

}

private void declaredSetter(InnerClass.Builder classBuilder,
PrototypeProperty.ConfiguredOption configured,
TypeName returnType,
Javadoc blueprintJavadoc) {
protected void declaredSetter(InnerClass.Builder classBuilder,
PrototypeProperty.ConfiguredOption configured,
TypeName returnType,
Javadoc blueprintJavadoc) {
Method.Builder builder = Method.builder()
.name(setterName())
.returnType(returnType, "updated builder instance")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ private void declaredSetterAdd(InnerClass.Builder classBuilder, PrototypePropert
.addLine("return self();"));
}

private void declaredSetter(InnerClass.Builder classBuilder,
@Override
protected void declaredSetter(InnerClass.Builder classBuilder,
PrototypeProperty.ConfiguredOption configured,
TypeName returnType,
Javadoc blueprintJavadoc) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.builder.processor;

import java.util.Objects;
import java.util.function.Consumer;

import io.helidon.common.processor.classmodel.Field;
import io.helidon.common.processor.classmodel.InnerClass;
import io.helidon.common.processor.classmodel.Javadoc;
import io.helidon.common.processor.classmodel.Method;
import io.helidon.common.types.TypeName;

import static io.helidon.builder.processor.Types.CHAR_ARRAY_TYPE;
import static io.helidon.builder.processor.Types.STRING_TYPE;
import static io.helidon.common.types.TypeNames.SUPPLIER;

class TypeHandlerSupplier extends TypeHandler.OneTypeHandler {

TypeHandlerSupplier(String name, String getterName, String setterName, TypeName declaredType) {
super(name, getterName, setterName, declaredType);
}

@Override
Field.Builder fieldDeclaration(PrototypeProperty.ConfiguredOption configured, boolean isBuilder, boolean alwaysFinal) {
Field.Builder builder = Field.builder()
.type(declaredType())
.name(name())
.isFinal(alwaysFinal || !isBuilder);

if (isBuilder && configured.hasDefault()) {
builder.defaultValue("() -> " + configured.defaultValue());
}

return builder;
}

@Override
TypeName argumentTypeName() {
return TypeName.builder(SUPPLIER)
.addTypeArgument(toWildcard(actualType()))
.build();
}

@Override
void generateFromConfig(Method.Builder method, PrototypeProperty.ConfiguredOption configured, FactoryMethods factoryMethods) {
if (configured.provider()) {
return;
}
if (factoryMethods.createFromConfig().isPresent()) {
method.addLine(configGet(configured)
+ generateFromConfig(factoryMethods)
+ ".ifPresent(this::" + setterName() + ");");
} else if (actualType().isOptional()) {
method.add(setterName() + "(");
method.add(configGet(configured));
method.add(generateFromConfigOptional(factoryMethods));
method.addLine(".optionalSupplier());");
} else {
method.add(setterName() + "(");
method.add(configGet(configured));
method.add(generateFromConfig(factoryMethods));
method.addLine(".supplier());");
}
}

String generateFromConfigOptional(FactoryMethods factoryMethods) {
TypeName optionalType = actualType().typeArguments().get(0);
if (optionalType.fqName().equals("char[]")) {
return ".asString().as(String::toCharArray)";
}

TypeName boxed = optionalType.boxed();
return factoryMethods.createFromConfig()
.map(it -> ".map(" + it.typeWithFactoryMethod().genericTypeName().fqName() + "::" + it.createMethodName() + ")")
.orElseGet(() -> ".as(" + boxed.fqName() + ".class)");

}

@Override
void setters(InnerClass.Builder classBuilder,
PrototypeProperty.ConfiguredOption configured,
PrototypeProperty.Singular singular,
FactoryMethods factoryMethod,
TypeName returnType,
Javadoc blueprintJavadoc) {

declaredSetter(classBuilder, configured, returnType, blueprintJavadoc);

// and add the setter with the actual type
Method.Builder method = Method.builder()
.name(setterName())
.description(blueprintJavadoc.content())
.returnType(returnType, "updated builder instance")
.addParameter(param -> param.name(name())
.type(actualType())
.description(blueprintJavadoc.returnDescription()))
.addJavadocTag("see", "#" + getterName() + "()")
.typeName(Objects.class)
.addLine(".requireNonNull(" + name() + ");")
.addLine("this." + name() + " = () -> " + name() + ";")
.addLine("return self();");
classBuilder.addMethod(method);

if (actualType().equals(CHAR_ARRAY_TYPE)) {
classBuilder.addMethod(builder -> builder.name(setterName())
.returnType(returnType, "updated builder instance")
.description(blueprintJavadoc.content())
.addJavadocTag("see", "#" + getterName() + "()")
.addParameter(param -> param.name(name())
.type(STRING_TYPE)
.description(blueprintJavadoc.returnDescription()))
.accessModifier(setterAccessModifier(configured))
.typeName(Objects.class).addLine(".requireNonNull(" + name() + ");")
.addLine("this." + name() + " = () -> " + name() + ".toCharArray();")
.addLine("return self();"));
}

if (factoryMethod.createTargetType().isPresent()) {
// if there is a factory method for the return type, we also have setters for the type (probably config object)
FactoryMethods.FactoryMethod fm = factoryMethod.createTargetType().get();
String argumentName = name() + "Config";

classBuilder.addMethod(builder -> builder.name(setterName())
.accessModifier(setterAccessModifier(configured))
.description(blueprintJavadoc.content())
.returnType(returnType, "updated builder instance")
.addParameter(param -> param.name(argumentName)
.type(fm.argumentType())
.description(blueprintJavadoc.returnDescription()))
.addJavadocTag("see", "#" + getterName() + "()")
.typeName(Objects.class)
.addLine(".requireNonNull(" + argumentName + ");")
.add("this." + name() + " = ")
.typeName(fm.typeWithFactoryMethod().genericTypeName())
.addLine("." + fm.createMethodName() + "(" + argumentName + ");")
.addLine("return self();"));
}

if (factoryMethod.builder().isPresent()) {
// if there is a factory method for the return type, we also have setters for the type (probably config object)
FactoryMethods.FactoryMethod fm = factoryMethod.builder().get();

TypeName builderType;
String className = fm.factoryMethodReturnType().className();
if (className.equals("Builder") || className.endsWith(".Builder")) {
builderType = fm.factoryMethodReturnType();
} else {
builderType = TypeName.create(fm.factoryMethodReturnType().fqName() + ".Builder");
}
String argumentName = "consumer";
TypeName argumentType = TypeName.builder()
.type(Consumer.class)
.addTypeArgument(builderType)
.build();

classBuilder.addMethod(builder -> builder.name(setterName())
.accessModifier(setterAccessModifier(configured))
.description(blueprintJavadoc.content())
.returnType(returnType, "updated builder instance")
.addParameter(param -> param.name(argumentName)
.type(argumentType)
.description(blueprintJavadoc.returnDescription()))
.addJavadocTag("see", "#" + getterName() + "()")
.typeName(Objects.class)
.addLine(".requireNonNull(" + argumentName + ");")
.add("var builder = ")
.typeName(fm.typeWithFactoryMethod().genericTypeName())
.addLine("." + fm.createMethodName() + "();")
.addLine("consumer.accept(builder);")
.addLine("this." + name() + "(builder.build());")
.addLine("return self();"));
}
}

protected void declaredSetter(InnerClass.Builder classBuilder,
PrototypeProperty.ConfiguredOption configured,
TypeName returnType,
Javadoc blueprintJavadoc) {
classBuilder.addMethod(method -> method.name(setterName())
.returnType(returnType, "updated builder instance")
.description(blueprintJavadoc.content())
.addJavadocTag("see", "#" + getterName() + "()")
.addParameter(param -> param.name(name())
.type(argumentTypeName())
.description(blueprintJavadoc.returnDescription()))
.accessModifier(setterAccessModifier(configured))
.typeName(Objects.class).addLine(".requireNonNull(" + name() + ");")
.addLine("this." + name() + " = " + name() + "::get;")
.addLine("return self();"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.builder.test.testsubjects;

import java.util.Optional;
import java.util.function.Supplier;

import io.helidon.builder.api.Prototype;
import io.helidon.config.metadata.Configured;
import io.helidon.config.metadata.ConfiguredOption;

/**
* Blueprint with a supplier from configuration.
*/
@Prototype.Blueprint
@Configured
interface SupplierBeanBlueprint {
/**
* This value is either explicitly configured, or uses config to get the supplier.
* If config source with change support is changed, the supplier should provide the latest value from configuration.
*
* @return supplier with latest value
*/
@ConfiguredOption
Supplier<String> stringSupplier();

@ConfiguredOption(key = "string-supplier")
Supplier<char[]> charSupplier();

@ConfiguredOption
Supplier<Optional<String>> optionalSupplier();

@ConfiguredOption(key = "optional-supplier")
Supplier<Optional<char[]>> optionalCharSupplier();
}
Loading
Loading