Skip to content

Commit

Permalink
#213 - BUG: reference to Builder is ambiguous ... when injecting a cl…
Browse files Browse the repository at this point in the history
…ass with simple name of Builder
  • Loading branch information
rbygrave committed May 19, 2022
1 parent faa5d35 commit ab899e4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,20 @@ public String helloData() {
return "AppHelloData";
}
}

@Bean
Builder newBuilder() {
return new Builder();
}

@Bean
Generated newGenerated() {
return new Generated();
}

public static class Builder {
}

public static class Generated {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.example.myapp.config;

import io.avaje.inject.Component;

public class Silly {

@Component
public static class Builder {
public Builder() {
}
}

@Component
public static class Generated {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class BeanReader {
private final boolean proxy;
private final BeanAspects aspects;
private boolean writtenToFile;
private boolean suppressBuilderImport;
private boolean suppressGeneratedImport;

BeanReader(TypeElement beanType, ProcessingContext context, boolean factory) {
this.beanType = beanType;
Expand Down Expand Up @@ -210,8 +212,6 @@ private void prototypeNotSupported(Append writer, String lifecycle) {
}

private Set<String> importTypes() {
importTypes.add(Constants.GENERATED);
importTypes.add(Constants.BUILDER);
if (Util.validImportType(type)) {
importTypes.add(type);
}
Expand All @@ -230,10 +230,34 @@ private Set<String> importTypes() {
}
}
}

checkImports();
if (!suppressGeneratedImport){
importTypes.add(Constants.GENERATED);
}
if (!suppressBuilderImport) {
importTypes.add(Constants.BUILDER);
}
return importTypes;
}

private void checkImports() {
for (String type : importTypes) {
if (type.endsWith(".Builder")) {
suppressBuilderImport = true;
} else if (type.endsWith(".Generated")) {
suppressGeneratedImport = true;
}
}
}

String builderType() {
return suppressBuilderImport ? Constants.BUILDER : "Builder";
}

String generatedType() {
return suppressGeneratedImport ? "@io.avaje.inject.spi.Generated" : "@Generated";
}

void writeImports(Append writer) {
for (String importType : importTypes()) {
if (Util.validImportType(importType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Constants {
static final String AT_SINGLETON = "@Singleton";
static final String AT_PROXY = "@Proxy";
static final String AT_GENERATED = "@Generated(\"io.avaje.inject.generator\")";
static final String AT_GENERATED_COMMENT = "(\"io.avaje.inject.generator\")";
static final String META_INF_MODULE = "META-INF/services/io.avaje.inject.spi.Module";
static final String META_INF_TESTMODULE = "META-INF/services/io.avaje.inject.test.TestModule";
static final String META_INF_CUSTOM = "META-INF/services/io.avaje.inject.spi.Module.Custom";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void writeStaticFactoryBeanMethods() {

private void writeFactoryBeanMethod(MethodReader method) {
method.commentBuildMethod(writer);
writer.append(" public static void build_%s(Builder builder) {", method.getName()).eol();
writer.append(" public static void build_%s(%s builder) {", method.getName(), beanReader.builderType()).eol();
method.buildAddFor(writer);
writer.append(method.builderGetFactory()).eol();
writer.append(method.builderBuildBean()).eol();
Expand Down Expand Up @@ -171,7 +171,7 @@ private void writeBuildMethodStart(MethodReader constructor) {
} else {
writer.append(CODE_COMMENT_BUILD, shortName).eol();
}
writer.append(" public static void build(Builder builder");
writer.append(" public static void build(%s builder", beanReader.builderType());
for (MethodReader.MethodParam param : constructor.getParams()) {
if (param.isGenericParam()) {
param.addProviderParam(writer, providerIndex++);
Expand Down Expand Up @@ -250,7 +250,7 @@ private void writeClassStart() {
} else {
writer.append(CODE_COMMENT, shortName).eol();
}
writer.append(Constants.AT_GENERATED).eol();
writer.append(beanReader.generatedType()).append(Constants.AT_GENERATED_COMMENT).eol();
if (beanReader.isRequestScopedController()) {
writer.append(Constants.AT_SINGLETON).eol();
}
Expand Down

0 comments on commit ab899e4

Please sign in to comment.