Skip to content

Commit 4bdf0a6

Browse files
committed
Merge branch 'SentryMan-shortType'
2 parents b244ac8 + a3a4007 commit 4bdf0a6

File tree

15 files changed

+164
-46
lines changed

15 files changed

+164
-46
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ private Set<String> importTypes() {
276276
if (!genericTypes.isEmpty()) {
277277
importTypes.add(Constants.TYPE);
278278
importTypes.add(Constants.GENERICTYPE);
279-
// TYPE_ generic types are fully qualified
279+
genericTypes.forEach(t->t.addImports(importTypes));
280280
}
281281
}
282282
checkImports();
@@ -371,7 +371,7 @@ void writeRequestCreate(Append writer) {
371371
for (FieldReader field : injectFields) {
372372
field.writeRequestInject(writer);
373373
}
374-
for (MethodReader method : injectMethods) {
374+
for (final MethodReader method : injectMethods) {
375375
writer.append(" bean.%s(", method.name());
376376
method.writeRequestConstructor(writer);
377377
writer.append(");").eol();
@@ -411,11 +411,7 @@ boolean hasConditions() {
411411
}
412412

413413
String shortName() {
414-
if (beanType.getNestingKind().isNested()) {
415-
return Util.nestedShortName(beanQualifiedName());
416-
} else {
417-
return Util.shortName(beanQualifiedName());
418-
}
414+
return Util.shortName(beanQualifiedName());
419415
}
420416

421417
String packageName() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ void addImports(ImportTypeMap importTypes) {
4747
}
4848

4949
String builderGetDependency(String builder) {
50-
StringBuilder sb = new StringBuilder();
50+
final var sb = new StringBuilder();
5151
sb.append(builder).append(".").append(utype.getMethod(nullable, isBeanMap));
5252
if (isGenericParam()) {
53-
sb.append("TYPE_").append(type.shortName());
53+
sb.append("TYPE_").append(type.shortName().replace(".", "_"));
5454
} else {
5555
sb.append(Util.shortName(fieldType)).append(".class");
5656
}

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.avaje.inject.generator;
22

3+
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
4+
35
import java.util.HashMap;
46
import java.util.Map;
57
import java.util.Set;
@@ -26,16 +28,22 @@ boolean containsShortName(String suffix) {
2628
return mapByShortName.containsKey(suffix);
2729
}
2830

29-
/**
30-
* Register the full type checking for unique short name and returning the short name to use.
31-
*/
31+
/** Register the full type checking for unique short name and returning the short name to use. */
3232
String add(String fullType) {
33-
String shortName = Util.shortName(fullType);
34-
String existingFull = mapByShortName.get(shortName);
33+
final String shortName = Util.shortName(fullType);
34+
String fullTypeActual;
35+
final var index = shortName.lastIndexOf('.');
36+
if (index != -1) {
37+
fullTypeActual = fullType.replace(shortName, shortName.substring(0, index));
38+
39+
} else {
40+
fullTypeActual = fullType;
41+
}
42+
final String existingFull = mapByShortName.get(shortName);
3543
if (existingFull == null) {
36-
mapByShortName.put(shortName, fullType);
44+
mapByShortName.put(shortName, fullTypeActual);
3745
return shortName;
38-
} else if (existingFull.equals(fullType)) {
46+
} else if (existingFull.equals(fullTypeActual)) {
3947
// already existing
4048
return shortName;
4149
} else {

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ 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-
1917
private final ExecutableElement element;
2018
private final String factoryType;
2119
private final String methodName;
@@ -398,11 +396,11 @@ void addDependsOnGeneric(Set<GenericType> set) {
398396
void builderGetDependency(Append writer, String builderName, boolean forFactory) {
399397
writer.append(builderName).append(".").append(utilType.getMethod(nullable, isBeanMap));
400398
if (!genericType.isGenericType()) {
401-
writer.append(safeShortName(genericType.topType())).append(".class");
399+
writer.append(Util.shortName(genericType.topType())).append(".class");
402400
} else if (isProvider()) {
403401
writer.append(providerParam()).append(".class");
404402
} else {
405-
writer.append("TYPE_").append(genericType.shortName());
403+
writer.append("TYPE_").append(genericType.shortName().replace(".", "_"));
406404
}
407405
if (named != null && !named.isEmpty()) {
408406
writer.append(",\"").append(named).append("\"");
@@ -420,14 +418,6 @@ void builderGetDependency(Append writer, String builderName, boolean forFactory)
420418
writer.append(")");
421419
}
422420

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-
431421
private String providerParam() {
432422
return Util.shortName(Util.unwrapProvider(paramType));
433423
}

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
import java.io.IOException;
77
import java.io.Writer;
8+
import java.util.HashMap;
89
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Objects;
912
import java.util.Set;
1013

1114
import javax.lang.model.element.TypeElement;
@@ -67,16 +70,52 @@ private void writeGenericTypeFields() {
6770
// collect all types to prevent duplicates
6871
Set<GenericType> genericTypes = beanReader.allGenericTypes();
6972
if (!genericTypes.isEmpty()) {
70-
for (GenericType type : genericTypes) {
71-
writer.append(" public static final Type TYPE_%s = new GenericType<", type.shortName());
73+
74+
final Map<String, String> seenShortNames= new HashMap<>();
75+
76+
for (final GenericType type : genericTypes) {
77+
writer
78+
.append(" public static final Type TYPE_%s =", type.shortName().replace(".", "_"))
79+
.eol()
80+
.append(" new GenericType<");
81+
82+
writeGenericType(type,seenShortNames, writer);
7283
// use fully qualified types here rather than use type.writeShort(writer)
73-
writer.append(type.toString());
84+
7485
writer.append(">(){}.type();").eol();
7586
}
7687
writer.eol();
7788
}
7889
}
7990

91+
private void writeGenericType(GenericType type, Map<String, String> seenShortNames, Append writer) {
92+
final var typeShortName = Util.shortName(type.topType());
93+
final var topType = seenShortNames.computeIfAbsent(typeShortName, k -> type.topType());
94+
if (type.isGenericType()) {
95+
final var shortName =
96+
Objects.equals(type.topType(), topType) ? typeShortName : type.topType();
97+
98+
writer.append(shortName);
99+
writer.append("<");
100+
boolean first = true;
101+
for (final var param : type.params()) {
102+
if (first) {
103+
first = false;
104+
writeGenericType(param, seenShortNames, writer);
105+
continue;
106+
}
107+
writer.append(", ");
108+
writeGenericType(param, seenShortNames, writer);
109+
}
110+
writer.append(">");
111+
} else {
112+
final var shortName =
113+
Objects.equals(type.topType(), topType) ? typeShortName : type.topType();
114+
115+
writer.append(shortName);
116+
}
117+
}
118+
80119
private void writeRequestCreate() {
81120
beanReader.writeRequestCreate(writer);
82121
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void addSimpleType(String classType) {
4343

4444
private void addGenericType(GenericType genericType) {
4545
genericTypes.add(genericType);
46-
types.add("TYPE_" + genericType.shortName());
46+
types.add("TYPE_" + genericType.shortName().replace(".", "_"));
4747
}
4848

4949
Set<GenericType> genericTypes() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void extraImports(ImportTypeMap importTypes) {
116116
if (!genericTypes.isEmpty()) {
117117
importTypes.add(Constants.TYPE);
118118
importTypes.add(Constants.GENERICTYPE);
119-
// TYPE_ generic types are fully qualified
119+
genericTypes.forEach(t->t.addImports(importTypes));
120120
}
121121
}
122122
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,22 @@ static String initLower(String name) {
129129
return sb.toString();
130130
}
131131

132-
static String nestedShortName(String fullType) {
133-
int pos = fullType.lastIndexOf('.');
134-
if (pos < 0) {
135-
return fullType;
136-
} else {
137-
pos = fullType.lastIndexOf('.', pos - 1);
138-
return pos < 0 ? fullType : fullType.substring(pos + 1);
139-
}
140-
}
141-
142132
static String shortName(String fullType) {
143133
final int p = fullType.lastIndexOf('.');
144134
if (p == -1) {
145135
return fullType;
146-
} else {
136+
} else if (fullType.startsWith("java")) {
147137
return fullType.substring(p + 1);
138+
} else {
139+
var result = "";
140+
var foundClass = false;
141+
for (final String part : fullType.split("\\.")) {
142+
if (foundClass || Character.isUpperCase(part.charAt(0))) {
143+
foundClass = true;
144+
result += (result.isEmpty() ? "" : ".") + part;
145+
}
146+
}
147+
return result;
148148
}
149149
}
150150

inject-generator/src/test/java/io/avaje/inject/generator/UtilTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ class UtilTest {
1212

1313
@Test
1414
void nestedShortName() {
15-
assertEquals(Util.nestedShortName("com.example.Foo.Bar"), "Foo.Bar");
16-
assertEquals(Util.nestedShortName("com.example.foo.Bar"), "foo.Bar");
15+
assertEquals(Util.shortName("com.example.Foo.Bar"), "Foo.Bar");
1716
}
1817

1918
@Test
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.avaje.inject.generator.models.valid;
2+
3+
public interface A0 {
4+
/** Builder name clash in generated code, use the full type for this in generated code */
5+
interface Builder {}
6+
}

0 commit comments

Comments
 (0)