Skip to content

Commit ee3f8ee

Browse files
committed
#197 - Bug isAddBeanFor() generated code includes package protected class and interface
1 parent 49efeaa commit ee3f8ee

File tree

6 files changed

+92
-16
lines changed

6 files changed

+92
-16
lines changed

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

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

33
import javax.lang.model.element.Element;
4+
import javax.lang.model.element.Modifier;
45
import javax.lang.model.element.TypeElement;
56
import javax.lang.model.type.TypeMirror;
67
import java.util.ArrayList;
@@ -117,8 +118,10 @@ private void addSuperType(TypeElement element) {
117118
if (!fullName.equals(JAVA_LANG_OBJECT)) {
118119
String type = Util.unwrapProvider(fullName);
119120
if (!GenericType.isGeneric(type)) {
120-
extendsTypes.add(type);
121-
extendsInjection.read(element);
121+
if (isPublic(element)) {
122+
extendsTypes.add(type);
123+
extendsInjection.read(element);
124+
}
122125
addSuperType(superOf(element));
123126
}
124127
}
@@ -130,26 +133,33 @@ private TypeElement superOf(TypeElement element) {
130133

131134
private void readInterfaces(TypeElement type) {
132135
for (TypeMirror anInterface : type.getInterfaces()) {
133-
String rawType = Util.unwrapProvider(anInterface.toString());
134-
if (rawType.indexOf('.') == -1) {
135-
context.logWarn("skip when no package on interface " + rawType);
136-
} else if (Constants.AUTO_CLOSEABLE.equals(rawType) || Constants.IO_CLOSEABLE.equals(rawType)) {
137-
closeable = true;
138-
} else {
139-
rawType = GenericType.removeParameter(rawType);
140-
if (qualifierName == null) {
141-
final String iShortName = Util.shortName(rawType);
142-
if (beanSimpleName.endsWith(iShortName)) {
143-
// derived qualifier name based on prefix to interface short name
144-
qualifierName = beanSimpleName.substring(0, beanSimpleName.length() - iShortName.length()).toLowerCase();
136+
Element element = context.asElement(anInterface);
137+
if (isPublic(element)) {
138+
String rawType = Util.unwrapProvider(anInterface.toString());
139+
if (rawType.indexOf('.') == -1) {
140+
context.logWarn("skip when no package on interface " + rawType);
141+
} else if (Constants.AUTO_CLOSEABLE.equals(rawType) || Constants.IO_CLOSEABLE.equals(rawType)) {
142+
closeable = true;
143+
} else {
144+
rawType = GenericType.removeParameter(rawType);
145+
if (qualifierName == null) {
146+
final String iShortName = Util.shortName(rawType);
147+
if (beanSimpleName.endsWith(iShortName)) {
148+
// derived qualifier name based on prefix to interface short name
149+
qualifierName = beanSimpleName.substring(0, beanSimpleName.length() - iShortName.length()).toLowerCase();
150+
}
145151
}
152+
interfaceTypes.add(rawType);
153+
readExtendedInterfaces(rawType);
146154
}
147-
interfaceTypes.add(rawType);
148-
readExtendedInterfaces(rawType);
149155
}
150156
}
151157
}
152158

159+
private boolean isPublic(Element element) {
160+
return element != null && element.getModifiers().contains(Modifier.PUBLIC);
161+
}
162+
153163
private void readExtendedInterfaces(String type) {
154164
GenericType genericType = GenericType.parse(type);
155165
final TypeElement element = context.element(genericType.topType());
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.example.inherit;
2+
3+
import io.avaje.inject.Bean;
4+
import io.avaje.inject.Factory;
5+
import org.example.inherit.notpublic.PubExposed;
6+
7+
@Factory
8+
public class InhPub {
9+
10+
@Bean
11+
PubExposed exposed() {
12+
return new PubExposed();
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.example.inherit;
2+
3+
import io.avaje.inject.xtra.ApplicationScope;
4+
import org.example.inherit.notpublic.PubExposed;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
class InhPubTest {
10+
11+
@Test
12+
void test() {
13+
final InhPub one = ApplicationScope.get(InhPub.class);
14+
PubExposed exposed = one.exposed();
15+
16+
assertThat(exposed).isNotNull();
17+
assertThat(exposed.hello()).isEqualTo("hello");
18+
assertThat(exposed.ifaceMethod()).isEqualTo("ifaceMethod");
19+
}
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.example.inherit.notpublic;
2+
3+
/**
4+
* Package protected (NOT public)
5+
*/
6+
class NonPubBase {
7+
8+
public String hello() {
9+
return "hello";
10+
}
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.example.inherit.notpublic;
2+
3+
interface NonPubIface {
4+
5+
String ifaceMethod();
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.example.inherit.notpublic;
2+
3+
/**
4+
* Extends a package protected class, implements package protected interface.
5+
* <p>
6+
* Neither should be including in the generated isAddBeanFor().
7+
*/
8+
public class PubExposed extends NonPubBase implements NonPubIface {
9+
10+
@Override
11+
public String ifaceMethod() {
12+
return "ifaceMethod";
13+
}
14+
15+
}

0 commit comments

Comments
 (0)