diff --git a/codegen-core/src/main/java/org/javahelpers/codegen/model/type/TypeInstantiation.java b/codegen-core/src/main/java/org/javahelpers/codegen/model/type/TypeInstantiation.java index 88878ccd..b443149a 100644 --- a/codegen-core/src/main/java/org/javahelpers/codegen/model/type/TypeInstantiation.java +++ b/codegen-core/src/main/java/org/javahelpers/codegen/model/type/TypeInstantiation.java @@ -24,17 +24,17 @@ package org.javahelpers.codegen.model.type; -import java.util.List; -import org.apache.commons.lang3.StringUtils; - /** * The instantiation path generated code uses to obtain an instance of a resolved generated type. * *

The resolver records the concrete decision made while checking the type's contract, so * generation sites emit exactly the path that was resolved - constructor call or static factory * call - instead of silently falling back between the two. + * + *

The nested records are the domain-neutral paths; a domain adds its own paths by implementing + * this interface. */ -public sealed interface TypeInstantiation { +public interface TypeInstantiation { /** * Renders the code obtaining an instance of {@code typeExpression} invoked with {@code @@ -123,61 +123,4 @@ public String methodReference(String typeExpression) { return anchor.getFullQualifiedName() + "::" + methodName; } } - - /** - * Instantiation by prefilling the generated type through its field functions: a generated static - * {@code methodName} on the target class obtains an empty instance via {@code - * funcForEmptyInstance} and calls one field function per prefilled property of the source value. - * {@link #instantiationCode} renders the call to that generated method. - * - * @param generatedType the resolved generated type (return type of the prefilling method) - * @param sourceType the referenced type the instance is prefilled from - * @param funcForEmptyInstance how the prefilling method obtains the empty instance - * @param methodPrefix the field-function prefix recognized on the generated type ({@code ""} for - * {@code name(v)}, {@code set} for {@code setName(v)}, or a detected convention like {@code - * with}) - * @param prefilledFields the properties to prefill, in the order of the source's properties - * @param methodName the name of the prefilling method generated on the target type - */ - record PrefillCall( - TypeName generatedType, - TypeName sourceType, - TypeInstantiation funcForEmptyInstance, - String methodPrefix, - List prefilledFields, - String methodName) - implements TypeInstantiation { - - @Override - public String instantiationCode(String typeExpression, String arguments) { - return methodName() + "(" + arguments + ")"; - } - - @Override - public String methodReference(String typeExpression) { - return "this::" + methodName(); - } - - /** - * The generated type's field function for a prefilled field: {@code } when the prefix - * is empty, otherwise {@code } (e.g. {@code setName} or {@code withName}). - * - * @param field the prefilled field - * @return the field function's name on the generated type - */ - public String fieldMethodFor(Field field) { - return methodPrefix.isEmpty() - ? field.property() - : methodPrefix + StringUtils.capitalize(field.property()); - } - - /** - * One property prefilled inside the prefilling method: {@code - * target.(value.)}. - * - * @param property the property name (e.g. {@code name}) - * @param accessor how the property is read from the value (e.g. {@code name()} or {@code name}) - */ - public record Field(String property, String accessor) {} - } } diff --git a/processor/src/main/java/org/javahelpers/simple/builders/processor/analysis/BuilderScopeResolver.java b/processor/src/main/java/org/javahelpers/simple/builders/processor/analysis/BuilderScopeResolver.java index 1a92ff04..f0780bf7 100644 --- a/processor/src/main/java/org/javahelpers/simple/builders/processor/analysis/BuilderScopeResolver.java +++ b/processor/src/main/java/org/javahelpers/simple/builders/processor/analysis/BuilderScopeResolver.java @@ -45,6 +45,7 @@ import org.javahelpers.codegen.model.type.TypeName; import org.javahelpers.simple.builders.core.annotations.Ignore4BuilderGeneration; import org.javahelpers.simple.builders.processor.model.core.BuilderConfiguration; +import org.javahelpers.simple.builders.processor.model.type.PrefillCall; import org.javahelpers.simple.builders.processor.model.type.ResolvedBuilder; import org.javahelpers.simple.builders.processor.processing.BuilderProcessingContext; @@ -367,7 +368,7 @@ prefix, collectPrefilledFields(properties, fieldFunctions, prefix))) return Optional.empty(); } return Optional.of( - new TypeInstantiation.PrefillCall( + new PrefillCall( builderTypeName, referencedTypeName, funcForEmptyBuilder, @@ -380,8 +381,7 @@ prefix, collectPrefilledFields(properties, fieldFunctions, prefix))) * A naming convention paired with the prefilled fields the builder covers under it: the * convention the most properties follow wins. */ - private record ConventionCoverage( - String prefix, List prefilledFields) {} + private record ConventionCoverage(String prefix, List prefilledFields) {} /** * Candidate field-function prefixes: {@code ""} (fluent {@code name(v)}) and {@code set} @@ -422,7 +422,7 @@ private List candidatePrefixes( * @param prefix the naming convention to try * @return the prefilled fields in property order */ - private List collectPrefilledFields( + private List collectPrefilledFields( List properties, List fieldFunctions, String prefix) { @@ -439,9 +439,8 @@ private List collectPrefilledFields( * @param property the readable property to prefill * @return the prefilled field to emit */ - private static TypeInstantiation.PrefillCall.Field prefilledField( - JavaLangAnalyser.ReadableProperty property) { - return new TypeInstantiation.PrefillCall.Field(property.name(), property.accessor()); + private static PrefillCall.Field prefilledField(JavaLangAnalyser.ReadableProperty property) { + return new PrefillCall.Field(property.name(), property.accessor()); } /** diff --git a/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/builder/FieldPrefillEnhancer.java b/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/builder/FieldPrefillEnhancer.java index 1fb7ec4f..ae743679 100644 --- a/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/builder/FieldPrefillEnhancer.java +++ b/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/builder/FieldPrefillEnhancer.java @@ -24,20 +24,20 @@ package org.javahelpers.simple.builders.processor.generators.builder; import java.util.Optional; -import org.javahelpers.codegen.model.type.TypeInstantiation; import org.javahelpers.codegen.model.type.TypeName; import org.javahelpers.simple.builders.processor.generators.BuilderEnhancer; import org.javahelpers.simple.builders.processor.generators.util.MethodGeneratorUtil; import org.javahelpers.simple.builders.processor.model.core.BuilderDefinitionDto; import org.javahelpers.simple.builders.processor.model.core.BuilderFieldDto; +import org.javahelpers.simple.builders.processor.model.type.PrefillCall; import org.javahelpers.simple.builders.processor.model.type.ResolvedBuilder; import org.javahelpers.simple.builders.processor.processing.BuilderProcessingContext; /** - * Enhancer that adds the private prefilling methods backing {@link TypeInstantiation.PrefillCall} - * resolutions: one {@code prefill(T value)} method per referenced builder type, obtaining - * an empty builder and calling one field function per readable property. Several fields referencing - * the same type share one prefilling method. + * Enhancer that adds the private prefilling methods backing {@link PrefillCall} resolutions: one + * {@code prefill(T value)} method per referenced builder type, obtaining an empty builder + * and calling one field function per readable property. Several fields referencing the same type + * share one prefilling method. */ public class FieldPrefillEnhancer implements BuilderEnhancer { @@ -72,12 +72,12 @@ public void enhanceBuilder(BuilderDefinitionDto builderDto, BuilderProcessingCon * @param field the field to inspect * @return the field-prefilling call, or empty */ - private static Optional fieldPrefill( + private static Optional fieldPrefill( BuilderFieldDto field, BuilderProcessingContext context) { return context .resolveBuilderType(field.getFieldType()) .flatMap(ResolvedBuilder::funcForPrefilledBuilder) - .filter(TypeInstantiation.PrefillCall.class::isInstance) - .map(TypeInstantiation.PrefillCall.class::cast); + .filter(PrefillCall.class::isInstance) + .map(PrefillCall.class::cast); } } diff --git a/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/util/MethodGeneratorUtil.java b/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/util/MethodGeneratorUtil.java index 7d9493fd..fb6f1087 100644 --- a/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/util/MethodGeneratorUtil.java +++ b/processor/src/main/java/org/javahelpers/simple/builders/processor/generators/util/MethodGeneratorUtil.java @@ -35,7 +35,6 @@ import org.javahelpers.codegen.model.javadoc.JavadocDto; import org.javahelpers.codegen.model.method.MethodParameterDto; import org.javahelpers.codegen.model.type.GenericParameterDto; -import org.javahelpers.codegen.model.type.TypeInstantiation; import org.javahelpers.codegen.model.type.TypeName; import org.javahelpers.codegen.model.type.TypeNameGeneric; import org.javahelpers.codegen.model.type.TypeNameList; @@ -44,6 +43,7 @@ import org.javahelpers.codegen.model.type.TypeNameVariable; import org.javahelpers.simple.builders.processor.model.core.BuilderFieldDto; import org.javahelpers.simple.builders.processor.model.method.BuilderMethodDto; +import org.javahelpers.simple.builders.processor.model.type.PrefillCall; import org.javahelpers.simple.builders.processor.model.type.ResolvedBuilder; import org.javahelpers.simple.builders.processor.processing.BuilderProcessingContext; @@ -289,14 +289,13 @@ public static BuilderMethodDto createFieldConsumerWithBuilder( } /** - * Creates the private prefilling method backing a {@link TypeInstantiation.PrefillCall}: obtains - * an empty builder and calls one field function per prefilled property of the value. + * Creates the private prefilling method backing a {@link PrefillCall}: obtains an empty builder + * and calls one field function per prefilled property of the value. * * @param prefillCall the resolved field-prefilling instantiation * @return the method DTO for the prefilling method */ - public static BuilderMethodDto createFieldPrefillMethod( - TypeInstantiation.PrefillCall prefillCall) { + public static BuilderMethodDto createFieldPrefillMethod(PrefillCall prefillCall) { MethodParameterDto parameter = new MethodParameterDto(); parameter.setParameterName("value"); parameter.setParameterTypeName(prefillCall.sourceType()); @@ -311,7 +310,7 @@ public static BuilderMethodDto createFieldPrefillMethod( code.append("$builderType:T builder = ") .append(prefillCall.funcForEmptyInstance().instantiationCode("$builderType:T", "")) .append(";\n"); - for (TypeInstantiation.PrefillCall.Field prefilledField : prefillCall.prefilledFields()) { + for (PrefillCall.Field prefilledField : prefillCall.prefilledFields()) { code.append("builder.") .append(prefillCall.fieldMethodFor(prefilledField)) .append("(value.") diff --git a/processor/src/main/java/org/javahelpers/simple/builders/processor/model/type/PrefillCall.java b/processor/src/main/java/org/javahelpers/simple/builders/processor/model/type/PrefillCall.java new file mode 100644 index 00000000..8787d2cf --- /dev/null +++ b/processor/src/main/java/org/javahelpers/simple/builders/processor/model/type/PrefillCall.java @@ -0,0 +1,87 @@ +/* + * MIT License + * + * Copyright (c) 2025-2026 Andreas Igel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.javahelpers.simple.builders.processor.model.type; + +import java.util.List; +import org.apache.commons.lang3.StringUtils; +import org.javahelpers.codegen.model.type.TypeInstantiation; +import org.javahelpers.codegen.model.type.TypeName; + +/** + * Instantiation by prefilling the generated type through its field functions: a generated static + * {@code methodName} on the target class obtains an empty instance via {@code funcForEmptyInstance} + * and calls one field function per prefilled property of the source value. {@link + * #instantiationCode} renders the call to that generated method. + * + * @param generatedType the resolved generated type (return type of the prefilling method) + * @param sourceType the referenced type the instance is prefilled from + * @param funcForEmptyInstance how the prefilling method obtains the empty instance + * @param methodPrefix the field-function prefix recognized on the generated type ({@code ""} for + * {@code name(v)}, {@code set} for {@code setName(v)}, or a detected convention like {@code + * with}) + * @param prefilledFields the properties to prefill, in the order of the source's properties + * @param methodName the name of the prefilling method generated on the target type + */ +public record PrefillCall( + TypeName generatedType, + TypeName sourceType, + TypeInstantiation funcForEmptyInstance, + String methodPrefix, + List prefilledFields, + String methodName) + implements TypeInstantiation { + + @Override + public String instantiationCode(String typeExpression, String arguments) { + return methodName() + "(" + arguments + ")"; + } + + @Override + public String methodReference(String typeExpression) { + return "this::" + methodName(); + } + + /** + * The generated type's field function for a prefilled field: {@code } when the prefix + * is empty, otherwise {@code } (e.g. {@code setName} or {@code withName}). + * + * @param field the prefilled field + * @return the field function's name on the generated type + */ + public String fieldMethodFor(Field field) { + return methodPrefix.isEmpty() + ? field.property() + : methodPrefix + StringUtils.capitalize(field.property()); + } + + /** + * One property prefilled inside the prefilling method: {@code + * target.(value.)}. + * + * @param property the property name (e.g. {@code name}) + * @param accessor how the property is read from the value (e.g. {@code name()} or {@code name}) + */ + public record Field(String property, String accessor) {} +} diff --git a/processor/src/test/java/org/javahelpers/simple/builders/processor/BuilderScopeResolverTest.java b/processor/src/test/java/org/javahelpers/simple/builders/processor/BuilderScopeResolverTest.java index 92748201..bd93f7ad 100644 --- a/processor/src/test/java/org/javahelpers/simple/builders/processor/BuilderScopeResolverTest.java +++ b/processor/src/test/java/org/javahelpers/simple/builders/processor/BuilderScopeResolverTest.java @@ -48,6 +48,7 @@ import org.javahelpers.codegen.processing.logging.MessagerProcessingLogger; import org.javahelpers.simple.builders.processor.analysis.BuilderScopeResolver; import org.javahelpers.simple.builders.processor.model.core.BuilderConfiguration; +import org.javahelpers.simple.builders.processor.model.type.PrefillCall; import org.javahelpers.simple.builders.processor.model.type.ResolvedBuilder; import org.javahelpers.simple.builders.processor.processing.BuilderProcessingContext; import org.javahelpers.simple.builders.processor.testing.ProcessorTestUtils; @@ -519,9 +520,8 @@ public LibHelper build() { ResolvedBuilder resolved = ResolverProbeProcessor.usageWithoutAnnotation.get(); assertEquals("lib.LibHelper.Builder", resolved.typeName().getFullQualifiedName()); assertInstanceOf(TypeInstantiation.ConstructorCall.class, resolved.funcForEmptyBuilder()); - TypeInstantiation.PrefillCall prefilled = - assertInstanceOf( - TypeInstantiation.PrefillCall.class, resolved.funcForPrefilledBuilder().get()); + PrefillCall prefilled = + assertInstanceOf(PrefillCall.class, resolved.funcForPrefilledBuilder().get()); assertEquals("", prefilled.methodPrefix()); assertEquals("name", prefilled.prefilledFields().get(0).property()); assertEquals("getName()", prefilled.prefilledFields().get(0).accessor()); @@ -605,9 +605,8 @@ public static class Builder { ResolvedBuilder resolved = ResolverProbeProcessor.usageWithoutAnnotation.get(); assertEquals("lib.AutoValue_LibHelper.Builder", resolved.typeName().getFullQualifiedName()); assertInstanceOf(TypeInstantiation.NestedFactoryCall.class, resolved.funcForEmptyBuilder()); - TypeInstantiation.PrefillCall prefilled = - assertInstanceOf( - TypeInstantiation.PrefillCall.class, resolved.funcForPrefilledBuilder().get()); + PrefillCall prefilled = + assertInstanceOf(PrefillCall.class, resolved.funcForPrefilledBuilder().get()); assertEquals("set", prefilled.methodPrefix()); assertEquals("name", prefilled.prefilledFields().get(0).property()); } @@ -652,9 +651,8 @@ public LibHelperBuilder withActive(boolean active) { // Builder-framework style: several field functions share the camel-case prefix "with", // which is detected as the builder's convention ResolvedBuilder resolved = ResolverProbeProcessor.usageWithoutAnnotation.get(); - TypeInstantiation.PrefillCall prefilled = - assertInstanceOf( - TypeInstantiation.PrefillCall.class, resolved.funcForPrefilledBuilder().get()); + PrefillCall prefilled = + assertInstanceOf(PrefillCall.class, resolved.funcForPrefilledBuilder().get()); assertEquals("with", prefilled.methodPrefix()); assertEquals(2, prefilled.prefilledFields().size()); } @@ -688,9 +686,8 @@ public LibHelperBuilder() {} assertThat(compilation).succeeded(); // For a single-property type, one prefixed method is enough evidence for the convention ResolvedBuilder resolved = ResolverProbeProcessor.usageWithoutAnnotation.get(); - TypeInstantiation.PrefillCall prefilled = - assertInstanceOf( - TypeInstantiation.PrefillCall.class, resolved.funcForPrefilledBuilder().get()); + PrefillCall prefilled = + assertInstanceOf(PrefillCall.class, resolved.funcForPrefilledBuilder().get()); assertEquals("put", prefilled.methodPrefix()); } @@ -719,9 +716,8 @@ public LibHelperBuilder() {} assertThat(compilation).succeeded(); // Record components count as readable properties via the name() accessor ResolvedBuilder resolved = ResolverProbeProcessor.usageWithoutAnnotation.get(); - TypeInstantiation.PrefillCall prefilled = - assertInstanceOf( - TypeInstantiation.PrefillCall.class, resolved.funcForPrefilledBuilder().get()); + PrefillCall prefilled = + assertInstanceOf(PrefillCall.class, resolved.funcForPrefilledBuilder().get()); assertEquals("", prefilled.methodPrefix()); assertEquals("name", prefilled.prefilledFields().get(0).property()); assertEquals("name()", prefilled.prefilledFields().get(0).accessor()); @@ -758,9 +754,8 @@ public LibHelper build() { assertThat(compilation).succeeded(); // Accessible fields count as readable properties via direct name access ResolvedBuilder resolved = ResolverProbeProcessor.usageWithoutAnnotation.get(); - TypeInstantiation.PrefillCall prefilled = - assertInstanceOf( - TypeInstantiation.PrefillCall.class, resolved.funcForPrefilledBuilder().get()); + PrefillCall prefilled = + assertInstanceOf(PrefillCall.class, resolved.funcForPrefilledBuilder().get()); assertEquals("", prefilled.methodPrefix()); assertEquals("name", prefilled.prefilledFields().get(0).property()); assertEquals("name", prefilled.prefilledFields().get(0).accessor()); @@ -840,9 +835,8 @@ public LibHelperBuilder active(boolean active) { // age has no field function but two of three properties are covered: the convention is // trusted and the covered fields are prefilled ResolvedBuilder resolved = ResolverProbeProcessor.usageWithoutAnnotation.get(); - TypeInstantiation.PrefillCall prefilled = - assertInstanceOf( - TypeInstantiation.PrefillCall.class, resolved.funcForPrefilledBuilder().get()); + PrefillCall prefilled = + assertInstanceOf(PrefillCall.class, resolved.funcForPrefilledBuilder().get()); assertEquals(2, prefilled.prefilledFields().size()); assertEquals("name", prefilled.prefilledFields().get(0).property()); assertEquals("active", prefilled.prefilledFields().get(1).property());