Skip to content

Fix duplicate short names in Module Generation #795

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 5 commits into from
Apr 7, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.example.myapp.duplicate;

import jakarta.inject.Singleton;

@Singleton
public class SameType {
@Singleton
public static class Inner {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.example.myapp.duplicate.two;

import jakarta.inject.Singleton;

@Singleton
public class SameType {
@Singleton
public static class Inner {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.example.myapp.duplicate;

import io.avaje.inject.BeanScope;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;


class SameTypeTest {

@Test
void testDuplicateShortName() {
try (BeanScope testScope = BeanScope.builder().build()) {

var sameType = testScope.get(SameType.class);
var sameTypeInner = testScope.get(SameType.Inner.class);
var sameType2 = testScope.get(org.example.myapp.duplicate.two.SameType.class);
var sameType2Inner = testScope.get(org.example.myapp.duplicate.two.SameType.Inner.class);

assertThat(sameType).isNotNull();
assertThat(sameTypeInner).isNotNull();
assertThat(sameType2).isNotNull();
assertThat(sameType2Inner).isNotNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void addImportTypes(Set<String> importTypes) {
}
}

void buildMethod(Append append) {
void buildMethod(Append append, boolean fullyQualify) {
if (generateProxy) {
return;
}
Expand Down Expand Up @@ -273,7 +273,7 @@ void buildMethod(Append append) {
if (hasMethod()) {
append.append(" ").append(Util.shortMethod(method)).append("(builder");
} else {
append.append(" ").append(shortType).append(Constants.DI).append(".build(builder");
append.append(" ").append(fullyQualify ? type : shortType).append(Constants.DI).append(".build(builder");
}
append.append(");").append(NEWLINE);
append.append(" }").append(NEWLINE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import static io.avaje.inject.generator.APContext.logError;
import static io.avaje.inject.generator.APContext.typeElement;
import static io.avaje.inject.generator.ProcessingContext.createMetaInfWriterFor;
import static java.util.stream.Collectors.toSet;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;

import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -52,6 +55,7 @@ final class SimpleModuleWriter {
private final String fullName;
private final ScopeInfo scopeInfo;
private final MetaDataOrdering ordering;
private final Set<String> duplicateTypes;

private Append writer;

Expand All @@ -61,6 +65,13 @@ final class SimpleModuleWriter {
this.modulePackage = scopeInfo.modulePackage();
this.shortName = scopeInfo.moduleShortName();
this.fullName = scopeInfo.moduleFullName();
final Set<String> seen = new HashSet<>();
this.duplicateTypes =
ordering.ordered().stream()
.map(MetaData::type)
.filter(t -> !seen.add(ProcessorUtils.shortType(t)))
.flatMap(t -> Stream.of(t, t + "$DI"))
.collect(toSet());
}

void write(ScopeInfo.Type scopeType) throws IOException {
Expand Down Expand Up @@ -183,7 +194,7 @@ private void writeBuildMethod() {

private void writeBuildMethods() {
for (MetaData metaData : ordering.ordered()) {
metaData.buildMethod(writer);
metaData.buildMethod(writer, duplicateTypes.contains(metaData.type()));
}
}

Expand All @@ -192,8 +203,9 @@ private void writePackage() {
for (String type : factoryImportTypes()) {
writer.append("import %s;", type).eol();
}

for (String type : scopeInfo.initModuleDependencies(ordering.importTypes())) {
if (Util.validImportType(type, modulePackage)) {
if (!duplicateTypes.contains(type) && Util.validImportType(type, modulePackage)) {
writer.append("import %s;", type).eol();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.avaje.inject.generator.models.valid.duplicate;

import jakarta.inject.Singleton;

@Singleton
public class SameType {
@Singleton
public static class Inner {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.avaje.inject.generator.models.valid.duplicate.two;

import jakarta.inject.Singleton;

@Singleton
public class SameType {
@Singleton
public static class Inner {}
}