Skip to content

Commit 3961d59

Browse files
committed
Change autoProvides to include top level concrete and abstract classes
Prior to this autoProvides only included interfaces
1 parent b411c05 commit 3961d59

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

blackbox-test-inject/src/main/java/org/example/myapp/config/AppConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ MyInterface myInterface2() {
7373
return new MyInterface() {};
7474
}
7575

76+
@Bean
77+
MyAbstract myAbstract() {
78+
return new MyAbstract() {};
79+
}
80+
7681
public static class Builder {
7782
}
7883

@@ -104,6 +109,10 @@ public MyPrim(String val) {
104109
}
105110
}
106111

112+
public abstract class MyAbstract {
113+
114+
}
115+
107116
@Component
108117
public static class BuilderUser {
109118

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package io.avaje.inject.generator;
22

3+
import io.avaje.inject.Factory;
4+
import io.avaje.inject.spi.Generated;
5+
import io.avaje.inject.spi.Proxy;
6+
37
import javax.lang.model.element.Element;
48
import javax.lang.model.element.ElementKind;
59
import javax.lang.model.element.Modifier;
610
import javax.lang.model.element.TypeElement;
711
import javax.lang.model.type.TypeMirror;
12+
import java.lang.annotation.Annotation;
813
import java.util.ArrayList;
914
import java.util.List;
1015

@@ -25,6 +30,8 @@ final class TypeExtendsReader {
2530
private final String beanSimpleName;
2631
private final String baseTypeRaw;
2732
private final boolean baseTypeIsInterface;
33+
private final boolean publicAccess;
34+
private final boolean autoProvide;
2835
private boolean closeable;
2936
/**
3037
* The implied qualifier name based on naming convention.
@@ -40,6 +47,24 @@ final class TypeExtendsReader {
4047
this.beanSimpleName = baseType.getSimpleName().toString();
4148
this.baseTypeRaw = Util.unwrapProvider(baseGenericType.toString());
4249
this.baseTypeIsInterface = baseType.getKind() == ElementKind.INTERFACE;
50+
this.publicAccess = baseType.getModifiers().contains(Modifier.PUBLIC);
51+
this.autoProvide = autoProvide();
52+
}
53+
54+
private boolean autoProvide() {
55+
return publicAccess
56+
&& baseType.getAnnotation(Factory.class) == null
57+
&& baseType.getAnnotation(Proxy.class) == null
58+
&& baseType.getAnnotation(Generated.class) == null
59+
&& !isController();
60+
}
61+
62+
private boolean isController() {
63+
try {
64+
return baseType.getAnnotation((Class<Annotation>) Class.forName(Constants.CONTROLLER)) != null;
65+
} catch (final ClassNotFoundException e) {
66+
return false;
67+
}
4368
}
4469

4570
GenericType baseType() {
@@ -83,16 +108,13 @@ String providesAspect() {
83108
}
84109

85110
String autoProvides() {
86-
if (!providesAspect.isEmpty()) {
111+
if (!autoProvide || !providesAspect.isEmpty()) {
87112
return null;
88113
}
89-
if (baseTypeIsInterface) {
114+
if (baseTypeIsInterface || interfaceTypes.isEmpty()) {
90115
return baseTypeRaw;
91116
}
92-
if (!interfaceTypes.isEmpty()) {
93-
return interfaceTypes.get(0);
94-
}
95-
return null;
117+
return interfaceTypes.get(0);
96118
}
97119

98120
List<String> provides() {

0 commit comments

Comments
 (0)