Skip to content

Commit a3a4007

Browse files
committed
use short types in DI classes
1 parent 0f155c3 commit a3a4007

File tree

8 files changed

+79
-15
lines changed

8 files changed

+79
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
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();

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,12 @@ boolean containsShortName(String suffix) {
3232
String add(String fullType) {
3333
final String shortName = Util.shortName(fullType);
3434
String fullTypeActual;
35-
String shortNameActual;
3635
final var index = shortName.lastIndexOf('.');
3736
if (index != -1) {
38-
shortNameActual = shortName.substring(0, index);
39-
fullTypeActual = fullType.replace(shortName, shortNameActual);
37+
fullTypeActual = fullType.replace(shortName, shortName.substring(0, index));
4038

4139
} else {
4240
fullTypeActual = fullType;
43-
shortNameActual = shortName;
4441
}
4542
final String existingFull = mapByShortName.get(shortName);
4643
if (existingFull == null) {
@@ -51,7 +48,7 @@ String add(String fullType) {
5148
return shortName;
5249
} else {
5350
// must use fully qualified type
54-
return fullTypeActual;
51+
return fullType;
5552
}
5653
}
5754

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().replace(".", "_"));
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/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
}
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.avaje.inject.generator.models.valid.generic;
2+
3+
import java.util.function.BiConsumer;
4+
import java.util.function.Function;
5+
6+
import io.avaje.inject.generator.models.valid.A0;
7+
8+
public class MyConsumer implements BiConsumer<A0, io.avaje.inject.generator.models.valid.nested.A0> {
9+
10+
@Override
11+
public void accept(A0 t, io.avaje.inject.generator.models.valid.nested.A0 u) {}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.avaje.inject.generator.models.valid.generic;
2+
3+
import io.avaje.inject.Bean;
4+
import io.avaje.inject.Factory;
5+
6+
@Factory
7+
public class MyConsumerFactory {
8+
9+
@Bean
10+
MyConsumer createTest() {
11+
return new MyConsumer();
12+
}
13+
}

inject-generator/src/test/java/io/avaje/inject/generator/models/valid/nested/AFactory.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ void ad(A0.Builder b1, A1.Builder b2) {
2727
b2.hashCode();
2828
}
2929

30-
static class I0 implements A0.Builder {
30+
static class I0 implements A0.Builder {}
3131

32-
}
33-
static class I1 implements A1.Builder {
34-
35-
}
32+
static class I1 implements A1.Builder {}
3633
}

0 commit comments

Comments
 (0)