Skip to content

Commit f16bfae

Browse files
fix: handle nested java types like Flow.Publisher in Util.shortName
1 parent f98847c commit f16bfae

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,26 +141,26 @@ static String shortName(String fullType) {
141141
final int p = fullType.lastIndexOf('.');
142142
if (p == -1) {
143143
return fullType;
144-
} else if (fullType.startsWith("java")) {
145-
return fullType.substring(p + 1);
146-
} else {
147-
var result = "";
148-
var foundClass = false;
149-
for (final String part : fullType.split("\\.")) {
150-
char firstChar = part.charAt(0);
151-
if (foundClass
152-
|| Character.isUpperCase(firstChar)
153-
|| !Character.isAlphabetic(firstChar) && Character.isJavaIdentifierStart(firstChar)) {
154-
foundClass = true;
155-
result += (result.isEmpty() ? "" : ".") + part;
156-
}
144+
}
145+
146+
String[] parts = fullType.split("\\.");
147+
StringBuilder result = new StringBuilder();
148+
boolean foundClass = false;
149+
150+
for (String part : parts) {
151+
char firstChar = part.charAt(0);
152+
if (!foundClass && Character.isUpperCase(firstChar)) {
153+
foundClass = true;
157154
}
158-
// when in doubt, do the basic thing
159-
if (result.isBlank()) {
160-
return fullType.substring(p + 1);
155+
if (foundClass) {
156+
if (result.length() > 0) {
157+
result.append(".");
158+
}
159+
result.append(part);
161160
}
162-
return result;
163161
}
162+
163+
return result.length() > 0 ? result.toString() : fullType.substring(p + 1);
164164
}
165165

166166
static String shortName(UType uType) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,12 @@ void sanitizeImports() {
144144
assertEquals("my.Foo", Util.sanitizeImports("@org.bar.annotationMcgee my.Foo"));
145145
assertEquals("java.util.String", Util.sanitizeImports("java.util.String>"));
146146
}
147+
148+
@Test
149+
void testShortName_nestedTypes() {
150+
assertEquals("Flow.Publisher", Util.shortName("java.util.concurrent.Flow.Publisher"));
151+
assertEquals("Outer.Inner", Util.shortName("com.foo.Outer.Inner"));
152+
assertEquals("Only", Util.shortName("a.b.c.Only"));
153+
assertEquals("simple", Util.shortName("simple"));
154+
}
147155
}
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 jakarta.inject.Singleton;
4+
import java.util.concurrent.Flow;
5+
import java.util.concurrent.SubmissionPublisher;
6+
7+
@Singleton
8+
public class GenericImpl implements GenericInterfaceObject<Flow.Publisher<Object>> {
9+
public Flow.Publisher<Object> get() {
10+
return new SubmissionPublisher();
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.avaje.inject.generator.models.valid.generic;
2+
3+
public interface GenericInterfaceObject<T> {
4+
T get();
5+
}

0 commit comments

Comments
 (0)