Skip to content

Commit f36387e

Browse files
authored
support inherited pkg private when in the same package (#904)
1 parent dfe4078 commit f36387e

File tree

14 files changed

+161
-9
lines changed

14 files changed

+161
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.example.myapp.pkg_private;
2+
3+
interface Adder {
4+
int add(int a, int b);
5+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.example.myapp.pkg_private;
2+
3+
import jakarta.inject.Singleton;
4+
5+
@Singleton
6+
class AdderImpl implements Adder {
7+
8+
@Override
9+
public int add(int a, int b) {
10+
return a + b;
11+
}
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.example.myapp.pkg_private;
2+
3+
import jakarta.inject.Singleton;
4+
5+
@Singleton
6+
public class Calculator {
7+
private final Adder adder;
8+
9+
public Calculator(Adder adder) {
10+
this.adder = adder;
11+
}
12+
13+
public int sum(int a, int b) {
14+
return adder.add(a, b);
15+
}
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.example.myapp.pkg_private;
2+
3+
import io.avaje.inject.BeanScope;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
class PackagePrivateTest {
9+
10+
@Test
11+
void package_private_wires() {
12+
try (BeanScope beanScope = BeanScope.builder().build()) {
13+
var calculator = beanScope.get(Calculator.class);
14+
var adder = beanScope.get(Adder.class);
15+
16+
assertThat(calculator).isNotNull();
17+
assertThat(adder).isNotNull();
18+
19+
int result = calculator.sum(3, 7);
20+
assertThat(result).isEqualTo(10);
21+
}
22+
}
23+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ final class MethodReader {
142142
var beanTypes = BeanTypesPrism.getOptionalOn(element).map(BeanTypesPrism::value);
143143
beanTypes.ifPresent(t -> Util.validateBeanTypes(element, t));
144144
this.typeReader =
145-
new TypeReader(beanTypes.orElse(List.of()), genericType, returnElement, importTypes);
145+
new TypeReader(
146+
beanTypes.orElse(List.of()), genericType, returnElement, importTypes, element);
146147
typeReader.process();
147148
MethodLifecycleReader lifecycleReader = new MethodLifecycleReader(returnElement, initMethod, destroyMethod);
148149
this.initMethod = lifecycleReader.initMethod();

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ final class TypeExtendsReader {
4141
* The implied qualifier name based on naming convention.
4242
*/
4343
private String qualifierName;
44-
45-
TypeExtendsReader(UType baseUType, TypeElement baseType, boolean factory, ImportTypeMap importTypes, boolean proxyBean) {
44+
private Element source;
45+
46+
TypeExtendsReader(
47+
UType baseUType,
48+
TypeElement baseType,
49+
boolean factory,
50+
ImportTypeMap importTypes,
51+
boolean proxyBean,
52+
Element source) {
4653
this.baseUType = baseUType;
4754
this.baseType = baseType;
4855
this.extendsInjection = new TypeExtendsInjection(baseType, factory, importTypes);
@@ -54,6 +61,7 @@ final class TypeExtendsReader {
5461
this.controller = ControllerPrism.isPresent(baseType);
5562
this.closeable = closeableClient(baseType);
5663
this.autoProvide = autoProvide();
64+
this.source = source;
5765
}
5866

5967
/**
@@ -255,7 +263,11 @@ private void readInterfacesOf(TypeMirror anInterface) {
255263
}
256264

257265
private boolean isPublic(Element element) {
258-
return element != null && element.getModifiers().contains(Modifier.PUBLIC);
266+
return element != null && element.getModifiers().contains(Modifier.PUBLIC)
267+
|| source != null
268+
&& APContext.elements()
269+
.getPackageOf(element)
270+
.equals(APContext.elements().getPackageOf(source));
259271
}
260272

261273
void validate() {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Set;
88

99
import javax.lang.model.element.Element;
10+
import javax.lang.model.element.ExecutableElement;
1011
import javax.lang.model.element.TypeElement;
1112
import javax.lang.model.type.TypeKind;
1213
import javax.lang.model.type.TypeMirror;
@@ -28,15 +29,16 @@ final class TypeReader {
2829
TypeElement beanType,
2930
ImportTypeMap importTypes,
3031
boolean factory) {
31-
this(injectsTypes, genericType, true, beanType, importTypes, factory);
32+
this(injectsTypes, genericType, true, beanType, importTypes, factory, beanType);
3233
}
3334

3435
TypeReader(
3536
List<TypeMirror> injectsTypes,
3637
UType genericType,
3738
TypeElement returnElement,
38-
ImportTypeMap importTypes) {
39-
this(injectsTypes, genericType, false, returnElement, importTypes, false);
39+
ImportTypeMap importTypes,
40+
ExecutableElement source) {
41+
this(injectsTypes, genericType, false, returnElement, importTypes, false, source);
4042
}
4143

4244
private TypeReader(
@@ -45,13 +47,15 @@ private TypeReader(
4547
boolean forBean,
4648
TypeElement beanType,
4749
ImportTypeMap importTypes,
48-
boolean factory) {
50+
boolean factory,
51+
Element source) {
4952
this.injectsTypes = injectsTypes.stream().map(UType::parse).collect(toList());
5053
this.forBean = forBean;
5154
this.beanType = beanType;
5255
this.importTypes = importTypes;
5356
final boolean proxyBean = forBean && ProxyPrism.isPresent(beanType);
54-
this.extendsReader = new TypeExtendsReader(genericType, beanType, factory, importTypes, proxyBean);
57+
this.extendsReader =
58+
new TypeExtendsReader(genericType, beanType, factory, importTypes, proxyBean, source);
5559
this.annotationReader = new TypeAnnotationReader(beanType);
5660
}
5761

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.avaje.inject.generator.models.valid;
2+
3+
import io.avaje.inject.Bean;
4+
import io.avaje.inject.Factory;
5+
import io.avaje.inject.generator.models.valid.pkg_private.PubExposed;
6+
7+
@Factory
8+
public class InhPub {
9+
10+
@Bean
11+
PubExposed exposed() {
12+
return new PubExposed();
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.avaje.inject.generator.models.valid.pkg_private;
2+
3+
interface Adder {
4+
int add(int a, int b);
5+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.avaje.inject.generator.models.valid.pkg_private;
2+
3+
import jakarta.inject.Singleton;
4+
5+
@Singleton
6+
class AdderImpl implements Adder {
7+
8+
@Override
9+
public int add(int a, int b) {
10+
return a + b;
11+
}
12+
}

0 commit comments

Comments
 (0)