diff --git a/codegen-core/src/main/java/org/javahelpers/codegen/model/method/MethodDto.java b/codegen-core/src/main/java/org/javahelpers/codegen/model/method/MethodDto.java index 59760158..04f5dd67 100644 --- a/codegen-core/src/main/java/org/javahelpers/codegen/model/method/MethodDto.java +++ b/codegen-core/src/main/java/org/javahelpers/codegen/model/method/MethodDto.java @@ -34,6 +34,7 @@ import org.javahelpers.codegen.model.javadoc.JavadocDto; import org.javahelpers.codegen.model.type.GenericParameterDto; import org.javahelpers.codegen.model.type.TypeName; +import org.javahelpers.codegen.util.JavaSignatures; /** * Rendering-side DTO containing all information to generate a method in a generated class. @@ -244,22 +245,15 @@ public void setReturnType(TypeName returnType) { } /** - * Returns a unique signature key for the method based on name and parameter types. Used for - * conflict resolution. The signature matches Java's method signature rules (name + parameter - * types, ignoring generics due to type erasure). + * Returns a signature key for the method based on name and erased parameter types. Used for + * conflict resolution. Type variables declared by this method are erased; type variables declared + * by the owning type require owner context and are resolved by validation/conflict-resolution + * callers through {@link JavaSignatures}. * * @return the signature key (e.g., "fieldName(java.lang.String,java.util.List)") */ public String getSignatureKey() { - StringBuilder sb = new StringBuilder(); - sb.append(methodName).append('('); - for (int i = 0; i < parameters.size(); i++) { - if (i > 0) sb.append(','); - TypeName tn = parameters.get(i).getParameterType(); - sb.append(tn.getRawQualifiedName()); - } - sb.append(')'); - return sb.toString(); + return JavaSignatures.methodSignature(this); } public JavadocDto getJavadoc() { diff --git a/codegen-core/src/main/java/org/javahelpers/codegen/util/JavaSignatures.java b/codegen-core/src/main/java/org/javahelpers/codegen/util/JavaSignatures.java new file mode 100644 index 00000000..84d46c66 --- /dev/null +++ b/codegen-core/src/main/java/org/javahelpers/codegen/util/JavaSignatures.java @@ -0,0 +1,175 @@ +/* + * 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.codegen.util; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.javahelpers.codegen.model.core.GenerationTargetClassDto; +import org.javahelpers.codegen.model.method.MethodDto; +import org.javahelpers.codegen.model.method.MethodParameterDto; +import org.javahelpers.codegen.model.type.GenericParameterDto; +import org.javahelpers.codegen.model.type.TypeName; +import org.javahelpers.codegen.model.type.TypeNameArray; +import org.javahelpers.codegen.model.type.TypeNameVariable; + +/** + * Calculates Java source-level constructor and method signatures from generation DTOs. + * + *

Signatures follow Java's type erasure rules (JLS §4.6): during compilation the compiler + * replaces every type variable with its first upper bound — {@code T extends Number} becomes {@code + * Number}, an unbounded {@code T} becomes {@code Object}. As a consequence two methods like {@code + * m(T)} and {@code m(Number)} in a {@code } class are the same method to the + * compiler. Because the generated source is compiled with exactly these rules, signatures here are + * calculated with the same substitution, so collisions are detected the way javac would see them. + */ +public final class JavaSignatures { + + private JavaSignatures() {} + + /** + * Returns the signature of a method using type erasure for its declared type variables. + * + * @param method method model + * @return method signature + */ + public static String methodSignature(MethodDto method) { + return methodSignature(method, Map.of()); + } + + /** + * Returns the signature of a method using type erasure for owner and method type variables. + * + * @param method method model + * @param ownerTypeVariableUpperBounds upper bounds of type variables declared by the owner + * @return method signature + */ + public static String methodSignature( + MethodDto method, Map ownerTypeVariableUpperBounds) { + return methodSignature( + method.getMethodName(), + method.getParameters(), + method.getGenericParameters(), + ownerTypeVariableUpperBounds); + } + + /** + * Returns the signature of a method using type erasure for owner and method type variables. + * + * @param methodName method name + * @param parameters method parameters + * @param genericParameters type variables declared by the method + * @param ownerTypeVariableUpperBounds upper bounds of type variables declared by the owner + * @return method signature + */ + public static String methodSignature( + String methodName, + List parameters, + List genericParameters, + Map ownerTypeVariableUpperBounds) { + Map signatureTypeVariableUpperBounds = + new HashMap<>(ownerTypeVariableUpperBounds); + addTypeVariableUpperBounds(genericParameters, signatureTypeVariableUpperBounds); + return methodName + parametersSignature(parameters, signatureTypeVariableUpperBounds); + } + + /** + * Returns the signature of constructor parameters using type erasure for owner type variables. + * + * @param parameters constructor parameters + * @param ownerTypeVariableUpperBounds upper bounds of type variables declared by the owner + * @return constructor parameter signature + */ + public static String constructorSignature( + List parameters, Map ownerTypeVariableUpperBounds) { + return parametersSignature(parameters, ownerTypeVariableUpperBounds); + } + + /** + * Creates a lookup of type-variable names to their first upper bound for a generation target. + * + * @param target generation target class + * @return upper bounds by type-variable name + */ + public static Map typeVariableUpperBounds(GenerationTargetClassDto target) { + return typeVariableUpperBounds(target.getGenerics()); + } + + /** + * Creates a lookup of type-variable names to their first upper bound. + * + * @param generics type variables + * @return upper bounds by type-variable name + */ + public static Map typeVariableUpperBounds(List generics) { + Map result = new HashMap<>(); + addTypeVariableUpperBounds(generics, result); + return result; + } + + /** + * Adds type-variable names and their first upper bounds to a lookup. + * + * @param generics type variables + * @param typeVariableUpperBounds target lookup + */ + public static void addTypeVariableUpperBounds( + List generics, Map typeVariableUpperBounds) { + for (GenericParameterDto generic : generics) { + typeVariableUpperBounds.put( + generic.getName(), + generic.getUpperBounds().stream().findFirst().orElse(TypeName.of(Object.class))); + } + } + + private static String parametersSignature( + List parameters, Map typeVariableUpperBounds) { + StringBuilder signature = new StringBuilder("("); + for (int i = 0; i < parameters.size(); i++) { + if (i > 0) { + signature.append(','); + } + signature.append(typeErasure(parameters.get(i).getParameterType(), typeVariableUpperBounds)); + } + return signature.append(')').toString(); + } + + /** + * Resolves a type to its erasure (JLS §4.6): type variables are replaced by their first upper + * bound, applied recursively through array types; all other types are already their own erasure. + */ + private static String typeErasure(TypeName type, Map typeVariableUpperBounds) { + if (type instanceof TypeNameVariable) { + TypeName bound = typeVariableUpperBounds.get(type.getClassName()); + return bound == null + ? type.getRawQualifiedName() + : typeErasure(bound, typeVariableUpperBounds); + } + if (type instanceof TypeNameArray arrayType) { + return typeErasure(arrayType.getTypeOfArray(), typeVariableUpperBounds) + "[]"; + } + return type.getRawQualifiedName(); + } +} diff --git a/codegen-roaster/src/main/java/org/javahelpers/codegen/roaster/RoasterCodeGenerator.java b/codegen-roaster/src/main/java/org/javahelpers/codegen/roaster/RoasterCodeGenerator.java index ac2c884f..e89f51d4 100644 --- a/codegen-roaster/src/main/java/org/javahelpers/codegen/roaster/RoasterCodeGenerator.java +++ b/codegen-roaster/src/main/java/org/javahelpers/codegen/roaster/RoasterCodeGenerator.java @@ -64,6 +64,7 @@ import org.javahelpers.codegen.model.type.TypeName; import org.javahelpers.codegen.model.type.TypeNameArray; import org.javahelpers.codegen.util.ImportCollector; +import org.javahelpers.codegen.util.JavaSignatures; import org.jboss.forge.roaster.Roaster; import org.jboss.forge.roaster.model.source.AnnotationSource; import org.jboss.forge.roaster.model.source.FieldSource; @@ -300,7 +301,8 @@ private void appendMethods( logger.debugStartOperation("Generating %d method candidates", classDef.getMethods().size()); // Resolve method conflicts by signature and priority - List resolvedMethods = resolveMethodConflicts(classDef.getMethods()); + Map classGenerics = JavaSignatures.typeVariableUpperBounds(classDef); + List resolvedMethods = resolveMethodConflicts(classDef.getMethods(), classGenerics); logger.debug("Resolved to %d methods after conflict resolution", resolvedMethods.size()); for (MethodDto methodDto : resolvedMethods) { @@ -324,16 +326,18 @@ private void appendMethods( * simply keeps the first occurrence for any remaining duplicate signatures and reports it as a * warning (an error in strict mode). */ - private List resolveMethodConflicts(List methods) { + private List resolveMethodConflicts( + List methods, Map typeVariableUpperBounds) { Set seenSignatures = new HashSet<>(); List deduplicated = new ArrayList<>(methods.size()); for (MethodDto method : methods) { - if (seenSignatures.add(method.getSignatureKey())) { + String signature = JavaSignatures.methodSignature(method, typeVariableUpperBounds); + if (seenSignatures.add(signature)) { deduplicated.add(method); } else { issueReporter.report( "Unexpected duplicate method signature: '%s' — keeping first occurrence (safety net)", - method.getSignatureKey()); + signature); } } return deduplicated; diff --git a/processor/src/main/java/org/javahelpers/simple/builders/processor/model/method/BuilderMethodDto.java b/processor/src/main/java/org/javahelpers/simple/builders/processor/model/method/BuilderMethodDto.java index 4a5dd1d1..d8b4048b 100644 --- a/processor/src/main/java/org/javahelpers/simple/builders/processor/model/method/BuilderMethodDto.java +++ b/processor/src/main/java/org/javahelpers/simple/builders/processor/model/method/BuilderMethodDto.java @@ -38,6 +38,7 @@ import org.javahelpers.codegen.model.method.MethodParameterDto; import org.javahelpers.codegen.model.type.GenericParameterDto; import org.javahelpers.codegen.model.type.TypeName; +import org.javahelpers.simple.builders.processor.util.BuilderSignatures; /** * BuilderMethodDto containing all information for generating a method in the builder class, @@ -370,22 +371,15 @@ public void setReturnType(TypeName returnType) { } /** - * Returns a unique signature key for the method based on name and parameter types. Used for - * conflict resolution. The signature matches Java's method signature rules (name + parameter - * types, ignoring generics due to type erasure). + * Returns a signature key for the method based on name and erased parameter types. Used for + * conflict resolution. Type variables declared by this method are erased; type variables declared + * by the owning type require owner context and are resolved by validation/conflict-resolution + * callers through {@link JavaSignatures}. * * @return the signature key (e.g., "fieldName(java.lang.String,java.util.List)") */ public String getSignatureKey() { - StringBuilder sb = new StringBuilder(); - sb.append(methodName).append('('); - for (int i = 0; i < parameters.size(); i++) { - if (i > 0) sb.append(','); - TypeName tn = parameters.get(i).getParameterType(); - sb.append(tn.getRawQualifiedName()); - } - sb.append(')'); - return sb.toString(); + return BuilderSignatures.methodSignature(this); } public JavadocDto getJavadoc() { diff --git a/processor/src/main/java/org/javahelpers/simple/builders/processor/processing/BuilderDefinitionCreator.java b/processor/src/main/java/org/javahelpers/simple/builders/processor/processing/BuilderDefinitionCreator.java index 6b322a5c..aaf95df5 100644 --- a/processor/src/main/java/org/javahelpers/simple/builders/processor/processing/BuilderDefinitionCreator.java +++ b/processor/src/main/java/org/javahelpers/simple/builders/processor/processing/BuilderDefinitionCreator.java @@ -61,6 +61,7 @@ import org.javahelpers.codegen.model.method.MethodParameterDto; import org.javahelpers.codegen.model.type.TypeName; import org.javahelpers.codegen.model.type.TypeNameGeneric; +import org.javahelpers.codegen.util.JavaSignatures; import org.javahelpers.simple.builders.core.annotations.IgnoreInBuilder; import org.javahelpers.simple.builders.core.annotations.SimpleBuilderConstructor; import org.javahelpers.simple.builders.core.util.TrackedValue; @@ -71,6 +72,7 @@ import org.javahelpers.simple.builders.processor.model.core.BuilderFieldDtoBuilder; import org.javahelpers.simple.builders.processor.model.core.GetterInfoDtoBuilder; import org.javahelpers.simple.builders.processor.model.method.BuilderMethodDto; +import org.javahelpers.simple.builders.processor.util.BuilderSignatures; /** Class for creating a specific BuilderDefinitionDto for an annotated DTO class. */ public class BuilderDefinitionCreator { @@ -384,24 +386,32 @@ private static void resolveMethodConflicts( private static Map> collectMethodsBySignature( BuilderDefinitionDto builderDto) { Map> methodsBySignature = new HashMap<>(); + Map typeVariableUpperBounds = + JavaSignatures.typeVariableUpperBounds(builderDto.getGenerics()); for (BuilderFieldDto field : builderDto.getConstructorFieldsForBuilder()) { for (BuilderMethodDto method : field.getMethods()) { methodsBySignature - .computeIfAbsent(method.getSignatureKey(), k -> new ArrayList<>()) + .computeIfAbsent( + BuilderSignatures.methodSignature(method, typeVariableUpperBounds), + k -> new ArrayList<>()) .add(method); } } for (BuilderFieldDto field : builderDto.getSetterFieldsForBuilder()) { for (BuilderMethodDto method : field.getMethods()) { methodsBySignature - .computeIfAbsent(method.getSignatureKey(), k -> new ArrayList<>()) + .computeIfAbsent( + BuilderSignatures.methodSignature(method, typeVariableUpperBounds), + k -> new ArrayList<>()) .add(method); } } for (BuilderMethodDto classMethod : builderDto.getMethods()) { methodsBySignature - .computeIfAbsent(classMethod.getSignatureKey(), k -> new ArrayList<>()) + .computeIfAbsent( + BuilderSignatures.methodSignature(classMethod, typeVariableUpperBounds), + k -> new ArrayList<>()) .add(classMethod); } diff --git a/processor/src/main/java/org/javahelpers/simple/builders/processor/util/BuilderSignatures.java b/processor/src/main/java/org/javahelpers/simple/builders/processor/util/BuilderSignatures.java new file mode 100644 index 00000000..c5c970f4 --- /dev/null +++ b/processor/src/main/java/org/javahelpers/simple/builders/processor/util/BuilderSignatures.java @@ -0,0 +1,63 @@ +/* + * 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.util; + +import java.util.Map; +import org.javahelpers.codegen.model.type.TypeName; +import org.javahelpers.codegen.util.JavaSignatures; +import org.javahelpers.simple.builders.processor.model.method.BuilderMethodDto; + +/** Calculates Java source-level method signatures for builder method DTOs. */ +public final class BuilderSignatures { + + private BuilderSignatures() {} + + /** + * Returns the signature of a builder method using type erasure for its declared type variables. + * + * @param method builder method model + * @return method signature + */ + public static String methodSignature(BuilderMethodDto method) { + return methodSignature(method, Map.of()); + } + + /** + * Returns the signature of a builder method using type erasure for owner and method type + * variables. + * + * @param method builder method model + * @param ownerTypeVariableUpperBounds upper bounds of type variables declared by the owner + * @return method signature + */ + public static String methodSignature( + BuilderMethodDto method, Map ownerTypeVariableUpperBounds) { + return JavaSignatures.methodSignature( + method.getMethodName(), + method.getParameters(), + method.getGenericParameters(), + ownerTypeVariableUpperBounds); + } +}