Skip to content

Commit

Permalink
HK2 to skip fields injected by CDI in non bean-defining-annotated bea…
Browse files Browse the repository at this point in the history
…ns (#4277)

* HK2 to skip fields injected by CDI in non bean-defining-annotated beans

Signed-off-by: Jan Supol <[email protected]>
  • Loading branch information
jansupol authored and senivam committed Oct 3, 2019
1 parent 959b4e7 commit 94945df
Show file tree
Hide file tree
Showing 14 changed files with 401 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ private void bindHk2ClassAnalyzer() {
int skippedElements = methodsToSkip.size() + fieldsToSkip.size();

ClassAnalyzer customizedClassAnalyzer = skippedElements > 0
? new InjecteeSkippingAnalyzer(defaultClassAnalyzer, methodsToSkip, fieldsToSkip)
? new InjecteeSkippingAnalyzer(defaultClassAnalyzer, methodsToSkip, fieldsToSkip, beanManager)
: defaultClassAnalyzer;

Binder binder = new AbstractBinder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import org.glassfish.hk2.api.ClassAnalyzer;
import org.glassfish.hk2.api.MultiException;

import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;

/**
* Class analyzer that ignores given injection points.
* Used for CDI integration, where we need to avoid HK2 replacing CDI injected entities.
Expand All @@ -41,13 +44,16 @@ public final class InjecteeSkippingAnalyzer implements ClassAnalyzer {
private final ClassAnalyzer defaultAnalyzer;
private final Map<Class<?>, Set<Method>> methodsToSkip;
private final Map<Class<?>, Set<Field>> fieldsToSkip;
private final BeanManager beanManager;

public InjecteeSkippingAnalyzer(ClassAnalyzer defaultAnalyzer,
Map<Class<?>, Set<Method>> methodsToSkip,
Map<Class<?>, Set<Field>> fieldsToSkip) {
Map<Class<?>, Set<Field>> fieldsToSkip,
BeanManager beanManager) {
this.defaultAnalyzer = defaultAnalyzer;
this.methodsToSkip = methodsToSkip;
this.fieldsToSkip = fieldsToSkip;
this.beanManager = beanManager;
}

@Override
Expand All @@ -66,6 +72,7 @@ public <T> Set<Method> getInitializerMethods(Class<T> type) throws MultiExceptio
public <T> Set<Field> getFields(Class<T> type) throws MultiException {
final Set<Field> originalFields = defaultAnalyzer.getFields(type);
final Set<Field> skippedFields = getMembersToSkip(type, fieldsToSkip);
addCdiInjectedFieldsToSkip(skippedFields, originalFields);
return Views.setDiffView(originalFields, skippedFields);
}

Expand Down Expand Up @@ -98,4 +105,12 @@ private <M extends Member> Set<M> getMembersToSkip(final Class<?> type, final Ma

return compositeResult;
}

private void addCdiInjectedFieldsToSkip(Set<Field> skippedFields, Set<Field> originalFields) {
for (Field field : originalFields) {
if (field.getAnnotation(Inject.class) != null) {
skippedFields.add(field);
}
}
}
}
82 changes: 82 additions & 0 deletions tests/integration/cdi-integration/cdi-manually-bound/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cdi-integration-project</artifactId>
<groupId>org.glassfish.jersey.tests.integration.cdi</groupId>
<version>2.30-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cdi-manually-bound</artifactId>

<dependencies>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>3.0.3.Final</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-util</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-bundle</artifactId>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
<exclusions>
<exclusion>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
</dependencies>



</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.cdi.manuallybound;

public interface CdiService<T> {
void doService(T t);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.cdi.manuallybound;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.Extension;
import java.io.IOException;

public class CdiServiceExtension implements Extension {
public void observe(@Observes AfterBeanDiscovery event) throws IOException, ClassNotFoundException {
event.addBean()
.addType(CdiService.class)
.beanClass(CdiService.class)
.scope(ApplicationScoped.class)
.createWith(context -> new CdiServiceImpl());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.cdi.manuallybound;

public class CdiServiceImpl implements CdiService<StringBuilder> {

@Override
public void doService(StringBuilder sb) {
sb.append(getClass().getSimpleName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.cdi.manuallybound;

import javax.inject.Inject;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import java.io.IOException;
import java.util.Collections;

public class NoBeanDefiningAnnotationContainerFilter implements ContainerResponseFilter {

@Inject
CdiService service;

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
if (service != null) {
StringBuilder sb = new StringBuilder();
service.doService(sb);
responseContext.getHeaders().put(
NoBeanDefiningAnnotationContainerFilter.class.getSimpleName(),
Collections.singletonList(sb.toString())
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.cdi.manuallybound;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/")
public class Resource {
@GET
public String get() {
return Resource.class.getSimpleName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->


<beans
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd" bean-discovery-mode="none">
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.glassfish.jersey.tests.cdi.manuallybound.CdiServiceExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
</web-app>
Loading

0 comments on commit 94945df

Please sign in to comment.