Skip to content

fix: handle nested java types like Flow.Publisher in Util.shortName #826

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
merged 1 commit into from
Jun 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions inject-generator/src/main/java/io/avaje/inject/generator/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,26 @@ static String shortName(String fullType) {
final int p = fullType.lastIndexOf('.');
if (p == -1) {
return fullType;
} else if (fullType.startsWith("java")) {
return fullType.substring(p + 1);
} else {
var result = "";
var foundClass = false;
for (final String part : fullType.split("\\.")) {
char firstChar = part.charAt(0);
if (foundClass
|| Character.isUpperCase(firstChar)
|| !Character.isAlphabetic(firstChar) && Character.isJavaIdentifierStart(firstChar)) {
foundClass = true;
result += (result.isEmpty() ? "" : ".") + part;
}
}

String[] parts = fullType.split("\\.");
StringBuilder result = new StringBuilder();
boolean foundClass = false;

for (String part : parts) {
char firstChar = part.charAt(0);
if (!foundClass && Character.isUpperCase(firstChar)) {
foundClass = true;
}
// when in doubt, do the basic thing
if (result.isBlank()) {
return fullType.substring(p + 1);
if (foundClass) {
if (result.length() > 0) {
result.append(".");
}
result.append(part);
}
return result;
}

return result.length() > 0 ? result.toString() : fullType.substring(p + 1);
}

static String shortName(UType uType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,12 @@ void sanitizeImports() {
assertEquals("my.Foo", Util.sanitizeImports("@org.bar.annotationMcgee my.Foo"));
assertEquals("java.util.String", Util.sanitizeImports("java.util.String>"));
}

@Test
void testShortName_nestedTypes() {
assertEquals("Flow.Publisher", Util.shortName("java.util.concurrent.Flow.Publisher"));
assertEquals("Outer.Inner", Util.shortName("com.foo.Outer.Inner"));
assertEquals("Only", Util.shortName("a.b.c.Only"));
assertEquals("simple", Util.shortName("simple"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.avaje.inject.generator.models.valid.generic;

import jakarta.inject.Singleton;
import java.util.concurrent.Flow;
import java.util.concurrent.SubmissionPublisher;

@Singleton
public class GenericImpl implements GenericInterfaceObject<Flow.Publisher<Object>> {
public Flow.Publisher<Object> get() {
return new SubmissionPublisher();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.avaje.inject.generator.models.valid.generic;

public interface GenericInterfaceObject<T> {
T get();
}