Skip to content
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

Fix Factory Providers #517

Merged
merged 5 commits into from
Mar 11, 2024
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
Expand Up @@ -85,7 +85,7 @@ String buildName() {
if (Util.isVoid(type)) {
return "void_" + Util.trimMethod(method);
} else {
final String trimType = Util.trimMethod(type);
final String trimType = Util.trimMethod(Util.unwrapProvider(type));
if (name != null) {
return trimType + "_" + name.replaceAll("[^a-zA-Z0-9_$]+", "_");
} else {
Expand Down Expand Up @@ -243,7 +243,11 @@ private void appendProvides(Append sb, String attribute, List<String> types) {
if (size > 1) {
sb.eol().append(" ");
}
var seen = new HashSet<String>();
for (int i = 0; i < types.size(); i++) {
if (!seen.add(types.get(i))) {
continue;
}
if (i > 0) {
sb.append(",").eol().append(" ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,22 @@ void builderBuildBean(Append writer) {

void builderAddBeanProvider(Append writer) {
if (isVoid) {
writer.append("Error - void @Prototype method ?").eol();
APContext.logError("Error - void @Prototype method ?", element);
return;
}
if (optionalType) {
writer.append("Error - Optional type with @Prototype method is not supported").eol();
APContext.logError("Error - Optional type with @Prototype method is not supported", element);
return;
}
String indent = " ";
writer.indent(indent).append(" builder.");
if (prototype) {
writer.indent(indent).append(" builder.asPrototype().registerProvider(() -> {").eol();
} else {
writer.indent(indent).append(" builder.asSecondary().registerProvider(() -> {").eol();
writer.append("asPrototype()").eol();
} else if(secondary) {
writer.append("asSecondary()").eol();
}
writer.indent(".registerProvider(() -> {");

writer.indent(indent).append(" return ");
writer.append("factory.%s(", methodName);
for (int i = 0; i < params.size(); i++) {
Expand Down Expand Up @@ -224,7 +227,11 @@ void builderBuildAddBean(Append writer) {
} else if (secondary) {
writer.append(".asSecondary()");
}
writer.append(".register(bean);").eol();
if (Util.isProvider(returnTypeRaw)) {
writer.append(".registerProvider(bean);").eol();
} else {
writer.append(".register(bean);").eol();
}
if (notEmpty(initMethod)) {
writer.indent(indent).append("builder.addPostConstruct($bean::%s);", initMethod).eol();
}
Expand Down Expand Up @@ -340,7 +347,7 @@ boolean isProtoType() {
}

boolean isUseProviderForSecondary() {
return secondary && !optionalType;
return secondary && !optionalType && !Util.isProvider(returnTypeRaw);
}

boolean isPublic() {
Expand Down Expand Up @@ -424,8 +431,6 @@ void builderGetDependency(Append writer, String builderName) {
writer.append(builderName).append(".").append(utilType.getMethod(nullable, isBeanMap));
if (!genericType.isGeneric() || genericType.param0().kind() == TypeKind.WILDCARD) {
writer.append(Util.shortName(genericType.mainType())).append(".class");
} else if (isProvider()) {
writer.append(providerParam()).append(".class");
} else {
writer.append("TYPE_").append(Util.shortName(genericType).replace(".", "_"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.avaje.inject.generator.APContext.createSourceFile;
import static io.avaje.inject.generator.APContext.logError;
import static java.util.stream.Collectors.toSet;

import java.io.IOException;
import java.io.Writer;
Expand Down Expand Up @@ -69,13 +70,18 @@ void write() throws IOException {

private void writeGenericTypeFields() {
// collect all types to prevent duplicates
Set<UType> genericTypes = beanReader.allGenericTypes();
if (!genericTypes.isEmpty()) {
Set<UType> genericTypes =
beanReader.allGenericTypes().stream()
.map(Util::unwrapProvider)
.filter(UType::isGeneric)
.collect(toSet());

if (!genericTypes.isEmpty()) {
final Map<String, String> seenShortNames = new HashMap<>();
final Set<String> writtenFields = new HashSet<>();

for (final UType type : genericTypes) {
for (final UType utype : genericTypes) {
var type = Util.unwrapProvider(utype);
final var fieldName = Util.shortName(type).replace(".", "_");
final var components = type.componentTypes();
if (components.size() == 1 && components.get(0).kind() == TypeKind.WILDCARD
Expand All @@ -86,7 +92,6 @@ private void writeGenericTypeFields() {

writer.append(" public static final Type TYPE_%s =", fieldName).eol()
.append(" new GenericType<");

writeGenericType(type, seenShortNames, writer);
// use fully qualified types here rather than use type.writeShort(writer)
writer.append(">(){}.type();").eol();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.avaje.inject.generator;

import static java.lang.annotation.ElementType.LOCAL_VARIABLE;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -19,7 +21,8 @@ final class TypeAppender {
this.importTypes = importTypes;
}

void add(UType type) {
void add(UType utype) {
var type = Util.unwrapProvider(utype);
var components = type.componentTypes();
if (isAddGenericType(type, components)) {
addUType(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ List<UType> autoProvides() {
return List.of();
}
if (baseTypeIsInterface) {
return List.of(Util.unwrapProvider(baseType.asType()));
return List.of(Util.unwrapProvider(baseUType));
}
var autoProvides = new ArrayList<>(interfaceTypes);
autoProvides.addAll(extendsTypes);
autoProvides.add(Util.unwrapProvider(baseType.asType()));
autoProvides.add(Util.unwrapProvider(baseUType));
return autoProvides;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ static UType unwrapProvider(TypeMirror maybeProvider) {
}
}

static UType unwrapProvider(UType maybeProvider) {
if (isProvider(maybeProvider.mainType())) {
return maybeProvider.param0();
} else {
return maybeProvider;
}
}

static String initLower(String name) {
final StringBuilder sb = new StringBuilder(name.length());
boolean upper = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.avaje.inject.generator.models.valid.provider;
SentryMan marked this conversation as resolved.
Show resolved Hide resolved

import java.util.function.Supplier;

import io.avaje.inject.Bean;
import io.avaje.inject.Factory;
import io.avaje.inject.Secondary;
import jakarta.inject.Provider;

@Factory
public class FactoryProvider {

@Bean
@Secondary
Provider<Grinder> grinder() {
return Grinder::new;
}

@Bean
Provider<Supplier<String>> supply() {
return () -> () -> "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.avaje.inject.generator.models.valid.provider;

public class Grinder {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.avaje.inject.generator.models.valid.provider;

import java.util.function.Supplier;

import jakarta.inject.Provider;
import jakarta.inject.Singleton;

@Singleton
public class ProviderUser {

Provider<Grinder> grinder;
Provider<Supplier<String>> supplier;

public ProviderUser(Provider<Grinder> grinder, Provider<Supplier<String>> supplier) {
this.grinder = grinder;
this.supplier = supplier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.example.coffee.provider;

import java.util.function.Supplier;

import io.avaje.inject.Bean;
import io.avaje.inject.Factory;
import io.avaje.inject.Secondary;
import jakarta.inject.Named;
import jakarta.inject.Provider;

@Factory
public class FactoryProvider {

@Bean
@Secondary
@Named("second")
Provider<String> second() {
return () -> "Nah, I'd win";
}

@Bean
Provider<Supplier<String>> supply() {
return () -> () -> "Stand proud Provider, you were strong";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.example.coffee.provider;

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

import java.util.function.Supplier;

import org.junit.jupiter.api.Test;

import io.avaje.inject.BeanScope;
import io.avaje.inject.spi.GenericType;

class FactoryProviderTest {

@Test
void test() {
try (var scope = BeanScope.builder().build()) {
Supplier<String> prov = scope.get(new GenericType<Supplier<String>>() {});
assertThat(prov.get()).isEqualTo("Stand proud Provider, you were strong");
assertThat(scope.get(String.class, "second")).isEqualTo("Nah, I'd win");
}
}
}
Loading