Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect work of @Named annotation #66

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.bootique.di.Injector;
import io.bootique.di.Key;
import io.bootique.di.TypeLiteral;
import jakarta.inject.Named;
import org.glassfish.hk2.api.Injectee;

import javax.inject.Provider;
Expand Down Expand Up @@ -35,15 +36,22 @@ protected Provider<?> resolveBqProvider(Injectee injectee) {
Annotation bindingAnnotation = injectee.getRequiredQualifiers().isEmpty()
? null
: injectee.getRequiredQualifiers().iterator().next();
Key<?> key = bindingAnnotation == null
? Key.get(typeLiteral)
: Key.get(typeLiteral, bindingAnnotation);

Key<?> key = getKey(typeLiteral, bindingAnnotation);
if(!injector.hasProvider(key) && !allowDynamicInjectionForKey(injectee, key)) {
return null;
}

return injector.getProvider(key);
return injector. getProvider(key);
}

private Key<?> getKey(TypeLiteral<?> typeLiteral, Annotation bindingAnnotation) {
if (bindingAnnotation instanceof Named) {
return Key.get(typeLiteral, ((Named) bindingAnnotation).value());
} else {
return bindingAnnotation == null
? Key.get(typeLiteral)
: Key.get(typeLiteral, bindingAnnotation);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public BqBindingActiveDescriptor(Provider<T> provider, Type implType, Set<Annota
super(
Collections.singleton(implType),
jakarta.inject.Singleton.class,
null,
name(qualifiers),
qualifiers,
DescriptorType.CLASS,
DescriptorVisibility.NORMAL,
Expand All @@ -112,6 +112,18 @@ public BqBindingActiveDescriptor(Provider<T> provider, Type implType, Set<Annota
setImplementation(implClass.getName());
}

// special case for @Named annotation
private static String name (Set<Annotation> qualifiers){
for (Annotation qualifier : qualifiers) {
if(qualifier instanceof jakarta.inject.Named) {
return ((jakarta.inject.Named) qualifier).value();
} else if(qualifier instanceof javax.inject.Named) {
return ((javax.inject.Named) qualifier).value();
}
}
return null;
}

public BqBindingActiveDescriptor() {
this.provider = null;
this.implClass = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*
* Licensed to ObjectStyle LLC under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ObjectStyle LLC licenses
* this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.bootique.jersey;

import io.bootique.BQRuntime;
import io.bootique.Bootique;
import io.bootique.di.BQInject;
import io.bootique.jetty.junit5.JettyTester;
import io.bootique.junit5.BQApp;
import io.bootique.junit5.BQTest;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Configuration;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.concurrent.atomic.AtomicInteger;

import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.junit.jupiter.api.Assertions.assertEquals;

@Disabled("Demonstrates several issues with @Inject and @Named annotations")
@BQTest
public class IssuesNamedAnnotationInjectionIT {

private static final String TEST_PROPERTY = "bq.test.label";
private static final InjectedServiceInterface serviceA = new InjectedServiceImplA();
private static final InjectedServiceInterface serviceB = new InjectedServiceImplB();
private static final InjectedServiceInterface serviceC = new InjectedServiceImplC();

static final JettyTester jetty = JettyTester.create();

@BQApp
static final BQRuntime app = Bootique.app("-s")
.autoLoadModules()
.module(b -> b.bind(InjectedServiceInterface.class, "A").toInstance(serviceA))
.module(b -> b.bind(InjectedServiceInterface.class, "B").toInstance(serviceB))
.module(b -> b.bind(InjectedServiceInterface.class, CustomQualifierC.class).toInstance(serviceC))
.module(b -> JerseyModule.extend(b)
.addFeature(ctx -> {
ctx.property(TEST_PROPERTY, "x");
return false;
})
.addResource(NamedFieldInjectedResourceJavaXAnnotations.class)
.addResource(NamedFieldInjectedResourceBQAnnotations.class)
.addResource(NamedFieldInjectedResourceCustomJavaXAnnotations.class))
.module(jetty.moduleReplacingConnectors())
.createRuntime();

@BeforeEach
public void before() {
serviceA.reset();
serviceB.reset();
serviceC.reset();
}


@Test
public void testNamedFieldInjectedJakartaAnnotations() {

Response r1 = jetty.getTarget().path("nfJakartaInject").request().get();
assertEquals(Response.Status.OK.getStatusCode(), r1.getStatus());
assertEquals("nf_1x", r1.readEntity(String.class));
r1.close();

Response r2 = jetty.getTarget().path("nfJakartaInject").request().get();
assertEquals(Response.Status.OK.getStatusCode(), r2.getStatus());
assertEquals("nf_2x", r2.readEntity(String.class));
r2.close();
}

@Test
public void testNamedFieldInjectedBQAnnotations() {

Response r1 = jetty.getTarget().path("nfBQInject").request().get();
assertEquals(Response.Status.OK.getStatusCode(), r1.getStatus());
assertEquals("nf_1x", r1.readEntity(String.class));
r1.close();

Response r2 = jetty.getTarget().path("nfBQInject").request().get();
assertEquals(Response.Status.OK.getStatusCode(), r2.getStatus());
assertEquals("nf_2x", r2.readEntity(String.class));
r2.close();
}

@Test
public void testNamedFieldInjectedCustomJavaXAnnotations() {

Response r1 = jetty.getTarget().path("nfCustomInjectJavaX").request().get();
assertEquals(Response.Status.OK.getStatusCode(), r1.getStatus());
assertEquals("nfC_1x", r1.readEntity(String.class));
r1.close();

Response r2 = jetty.getTarget().path("nfCustomInjectJavaX").request().get();
assertEquals(Response.Status.OK.getStatusCode(), r2.getStatus());
assertEquals("nfC_2x", r2.readEntity(String.class));
r2.close();
}

@Test
public void testNamedFieldInjectedCustomJakartaAnnotations() {

Response r1 = jetty.getTarget().path("nfCustomInjectJakarta").request().get();
assertEquals(Response.Status.OK.getStatusCode(), r1.getStatus());
assertEquals("nfD_1x", r1.readEntity(String.class));
r1.close();

Response r2 = jetty.getTarget().path("nfCustomInjectJakarta").request().get();
assertEquals(Response.Status.OK.getStatusCode(), r2.getStatus());
assertEquals("nfD_2x", r2.readEntity(String.class));
r2.close();
}


@Path("/nfJavaXInject")
@Produces(MediaType.TEXT_PLAIN)
public static class NamedFieldInjectedResourceJavaXAnnotations {

@Inject
@Named("A")
private InjectedServiceInterface serviceA;

@Context
private Configuration config;

@GET
public String get() {
return "nf_" + serviceA.getNext() + config.getProperty(TEST_PROPERTY);
}
}

@Path("/nfBQInject")
@Produces(MediaType.TEXT_PLAIN)
public static class NamedFieldInjectedResourceBQAnnotations {

@BQInject
@Named("B")
private InjectedServiceInterface serviceB;

@Context
private Configuration config;

@GET
public String get() {
return "nf_" + serviceB.getNext() + config.getProperty(TEST_PROPERTY);
}
}

@Path("/nfCustomInjectJavaX")
@Produces(MediaType.TEXT_PLAIN)
public static class NamedFieldInjectedResourceCustomJavaXAnnotations {

@Inject
@CustomQualifierC
private InjectedServiceInterface serviceC;

@Context
private Configuration config;

@GET
public String get() {
return "nfC_" + serviceC.getNext() + config.getProperty(TEST_PROPERTY);
}
}


public static interface InjectedServiceInterface {
AtomicInteger atomicInt = new AtomicInteger();
default void reset() {
atomicInt.set(0);
}
int getNext();
}

public static class InjectedServiceImplA implements InjectedServiceInterface {
public int getNext() {
return atomicInt.incrementAndGet();
}
}

public static class InjectedServiceImplB implements InjectedServiceInterface {
public int getNext() {
return atomicInt.incrementAndGet();
}
}

public static class InjectedServiceImplC implements InjectedServiceInterface {
public int getNext() {
return atomicInt.incrementAndGet();
}
}

@java.lang.annotation.Documented
@java.lang.annotation.Retention(RUNTIME)
@javax.inject.Qualifier
public @interface CustomQualifierC {
}

}
Loading