Repository navigation
refactor: centralize erasure-aware Java signature calculation #486
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
Merged
AndreasIgel
merged 3 commits into
java-helpers:main
from
igel-devin-ai:java-signature-erasure
Dec 4, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
a504d80
refactor: centralize erasure-aware Java signature calculation
devin-ai-integration[bot] 593eaf2
fix: builder-specific signature helper and generation-target overload
devin-ai-integration[bot] e8b1988
docs: explain type erasure in JavaSignatures javadoc
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
codegen-core/src/main/java/org/javahelpers/codegen/util/JavaSignatures.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
| * | ||
| * <p>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 <T extends Number>} 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<String, TypeName> 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<MethodParameterDto> parameters, | ||
| List<GenericParameterDto> genericParameters, | ||
| Map<String, TypeName> ownerTypeVariableUpperBounds) { | ||
| Map<String, TypeName> 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<MethodParameterDto> parameters, Map<String, TypeName> 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<String, TypeName> 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<String, TypeName> typeVariableUpperBounds(List<GenericParameterDto> generics) { | ||
| Map<String, TypeName> 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<GenericParameterDto> generics, Map<String, TypeName> typeVariableUpperBounds) { | ||
| for (GenericParameterDto generic : generics) { | ||
| typeVariableUpperBounds.put( | ||
| generic.getName(), | ||
| generic.getUpperBounds().stream().findFirst().orElse(TypeName.of(Object.class))); | ||
| } | ||
| } | ||
|
|
||
| private static String parametersSignature( | ||
| List<MethodParameterDto> parameters, Map<String, TypeName> 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<String, TypeName> 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(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.