Skip to content
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 @@ -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.
*
* <p>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.
*
* <p>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
Expand Down Expand Up @@ -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<Field> 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 <property>} when the prefix
* is empty, otherwise {@code <prefix><Property>} (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.<fieldFunction>(value.<accessor>)}.
*
* @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) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -367,7 +368,7 @@ prefix, collectPrefilledFields(properties, fieldFunctions, prefix)))
return Optional.empty();
}
return Optional.of(
new TypeInstantiation.PrefillCall(
new PrefillCall(
builderTypeName,
referencedTypeName,
funcForEmptyBuilder,
Expand All @@ -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<TypeInstantiation.PrefillCall.Field> prefilledFields) {}
private record ConventionCoverage(String prefix, List<PrefillCall.Field> prefilledFields) {}

/**
* Candidate field-function prefixes: {@code ""} (fluent {@code name(v)}) and {@code set}
Expand Down Expand Up @@ -422,7 +422,7 @@ private List<String> candidatePrefixes(
* @param prefix the naming convention to try
* @return the prefilled fields in property order
*/
private List<TypeInstantiation.PrefillCall.Field> collectPrefilledFields(
private List<PrefillCall.Field> collectPrefilledFields(
List<JavaLangAnalyser.ReadableProperty> properties,
List<ExecutableElement> fieldFunctions,
String prefix) {
Expand All @@ -439,9 +439,8 @@ private List<TypeInstantiation.PrefillCall.Field> 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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Builder>(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<Builder>(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 {

Expand Down Expand Up @@ -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<TypeInstantiation.PrefillCall> fieldPrefill(
private static Optional<PrefillCall> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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());
Expand All @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Field> 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 <property>} when the prefix
* is empty, otherwise {@code <prefix><Property>} (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.<fieldFunction>(value.<accessor>)}.
*
* @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) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
Loading