Skip to content

Commit d5e657a

Browse files
committed
Fix for Factory with multiple "Builder" types - short names all exactly match Builder
When there are multiple Builders like A0.Builder and A1.Builder then we really need to use the full type name. This change is to use the full type name when the short name matches Builder or Generated.
1 parent 4c64490 commit d5e657a

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.example.myapp.config;
2+
3+
public interface A0 {
4+
5+
/** Builder name clash in generated code, use the full type for this in generated code */
6+
interface Builder {
7+
8+
}
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.example.myapp.config;
2+
3+
public interface A1 {
4+
5+
/** Builder name clash in generated code, use the full type for this in generated code */
6+
interface Builder {
7+
8+
}
9+
10+
interface DSS {
11+
12+
}
13+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.example.myapp.config;
2+
3+
import io.avaje.inject.Bean;
4+
import io.avaje.inject.Factory;
5+
6+
@Factory
7+
class AFactory {
8+
9+
@Bean
10+
A0.Builder build0() {
11+
return new I0();
12+
}
13+
14+
@Bean
15+
A1.Builder build1() {
16+
return new I1();
17+
}
18+
19+
@Bean
20+
void andUse(A1.Builder a1Build) {
21+
a1Build.hashCode();
22+
}
23+
24+
@Bean
25+
void ad(A0.Builder b1, A1.Builder b2) {
26+
b1.hashCode();
27+
b2.hashCode();
28+
}
29+
30+
static class I0 implements A0.Builder {
31+
32+
}
33+
static class I1 implements A1.Builder {
34+
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.example.myapp.config;
2+
3+
import io.avaje.inject.Bean;
4+
import io.avaje.inject.Factory;
5+
6+
@Factory
7+
public class BuilderFactory {
8+
9+
@Bean
10+
void consume0(A0.Builder aBuilder) {
11+
aBuilder.hashCode();
12+
}
13+
14+
@Bean
15+
void consume1(A1.Builder aBuilder) {
16+
aBuilder.hashCode();
17+
}
18+
}

inject-generator/src/main/java/io/avaje/inject/generator/MethodReader.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ final class MethodReader {
1414

1515
private static final String CODE_COMMENT_BUILD_FACTORYBEAN = " /**\n * Create and register %s via factory bean method %s#%s().\n */";
1616

17+
private static final Set<String> UNSAFE_SHORT_NAME = Set.of("Builder", "Generated");
18+
1719
private final ExecutableElement element;
1820
private final String factoryType;
1921
private final String methodName;
@@ -396,7 +398,7 @@ void addDependsOnGeneric(Set<GenericType> set) {
396398
void builderGetDependency(Append writer, String builderName, boolean forFactory) {
397399
writer.append(builderName).append(".").append(utilType.getMethod(nullable, isBeanMap));
398400
if (!genericType.isGenericType()) {
399-
writer.append(Util.shortName(genericType.topType())).append(".class");
401+
writer.append(safeShortName(genericType.topType())).append(".class");
400402
} else if (isProvider()) {
401403
writer.append(providerParam()).append(".class");
402404
} else {
@@ -418,6 +420,14 @@ void builderGetDependency(Append writer, String builderName, boolean forFactory)
418420
writer.append(")");
419421
}
420422

423+
private String safeShortName(String fullType) {
424+
final String shortName = Util.shortName(fullType);
425+
if (UNSAFE_SHORT_NAME.contains(shortName)) {
426+
return fullType;
427+
}
428+
return shortName;
429+
}
430+
421431
private String providerParam() {
422432
return Util.shortName(Util.unwrapProvider(paramType));
423433
}

0 commit comments

Comments
 (0)