Skip to content

Inherited package private types not found for injection #903

@benalexau

Description

@benalexau

Package-level access visibility is frequently used to improve encapsulation and guide users toward a public API. It is still useful to have Avaje construct these package-protected types and inject them into other types in the same package where requested. At present, Avaje raises an error if this pattern is attempted. Consider:

package com.foo;

interface Adder {
  int add(int a, int b);
}
package com.foo;

import jakarta.inject.Singleton;

@Singleton
class AdderImpl implements Adder {

  @Override
  public int add(int a, int b) {
    return a + b;
  }
}
package com.foo;

import jakarta.inject.Singleton;

@Singleton
public class Calculator {
  private final Adder adder;

  public Calculator(Adder adder) {
    this.adder = adder;
  }

  public int sum(int a, int b) {
    return adder.add(a, b);
  }
}
package com.foo;

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

import io.avaje.inject.test.InjectTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

@InjectTest
final class CalculatorTest {

  @Inject Calculator calculator;

  @Test
  void verify() {
    assertThat(calculator.sum(40, 2)).isEqualTo(42);
  }
}

This reports a compilation failure:

/project-path/src/main/java/com/foo/Calculator.java:[6,8] No dependency provided for com.foo.Adder on com.foo.Calculator
[ERROR] Dependencies [com.foo.Adder] are not provided - there are no @Singleton, @Component, @Factory/@Bean that currently provide this type. If this is an external dependency consider specifying via @External

It would be good if Avaje allowed package-protected types to be eligible for injection.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions