Skip to content

Comments

DS: Add tests for inner-class constructor injection#7085

Merged
chrisrueger merged 1 commit intobndtools:masterfrom
chrisrueger:7078-more-testcases
Feb 4, 2026
Merged

DS: Add tests for inner-class constructor injection#7085
chrisrueger merged 1 commit intobndtools:masterfrom
chrisrueger:7078-more-testcases

Conversation

@chrisrueger
Copy link
Contributor

@chrisrueger chrisrueger commented Feb 4, 2026

Closes #7078 and successor of #7082

Add two test component classes and corresponding tests to verify DS constructor-injection behavior for inner classes. New classes: ConstructorInjectionErrorOnNonStaticInnerClassComponent (non-static inner should fail) and ConstructorInjectionStaticInnerClassComponent (static inner should succeed). Update DSAnnotationTest.java to import Pattern, add tests that build jars, assert expected failure for non-static inner class, assert success for static inner class, and validate generated OSGI-INF XML. Also include a minor whitespace fix and additional XML assertions for the existing default no-arg constructor test.

Background

After using bnd 7.3.0-SNAPSHOT of #7082 a project of mine suddenly showed errors. It turns out we have put a @Component on a non-static inner class.

At first I thought PR #7082 was wrong. But it turns out it is correct. Our code is wrong:


@Component(service = ConstructorInjectionErrorOnNonStaticInnerClassComponent.class)
public class ConstructorInjectionErrorOnNonStaticInnerClassComponent {

	@Component(service = ConstructorInjectionErrorOnNonStaticInnerClassComponentInner.class)
        @HttpWhiteboardContextSelect("(osgi.http.whiteboard.context.name=app-context)")
        @HttpWhiteboardResource(pattern = "/templates/*", prefix = "/src/main/resource/apps/templates")
	public class ConstructorInjectionErrorOnNonStaticInnerClassComponentInner {
	}
}

This is impossible for DS to instantiate , as it has no outer instance.
I guess we did not notice this until today, because the @Component is not really needed, and the only thing we need are the @HttpWhiteboard annotations. But since there was no error, we just didn't care. The app was working as expected.

Now the error shows and we can react:

  • either we remove the @Component
  • or make the inner class static

Add two test component classes and corresponding tests to verify DS constructor-injection behavior for inner classes. New classes: ConstructorInjectionErrorOnNonStaticInnerClassComponent (non-static inner should fail) and ConstructorInjectionStaticInnerClassComponent (static inner should succeed). Update DSAnnotationTest.java to import Pattern, add tests that build jars, assert expected failure for non-static inner class, assert success for static inner class, and validate generated OSGI-INF XML. Also include a minor whitespace fix and additional XML assertions for the existing default no-arg constructor test.

Signed-off-by: Christoph Rueger <chrisrueger@gmail.com>
@chrisrueger chrisrueger changed the title Add tests for inner-class constructor injection DS: Add tests for inner-class constructor injection Feb 4, 2026
@chrisrueger chrisrueger merged commit 962e350 into bndtools:master Feb 4, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bail out at compile time if no suitable constructor in DS component found

1 participant