Use Skipping Analyzer always Signed-off-by: jansupol <jan.supol@oracle.com>
diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/util/collection/Views.java b/core-common/src/main/java/org/glassfish/jersey/internal/util/collection/Views.java index 32e2640..4765056 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/util/collection/Views.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/util/collection/Views.java
@@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2024 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 @@ -24,13 +24,12 @@ import java.util.List; import java.util.ListIterator; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; -import static org.glassfish.jersey.internal.guava.Preconditions.checkNotNull; - /** * Collections utils, which provide transforming views for {@link List} and {@link Map}. * @@ -197,8 +196,8 @@ * @return union view of given sets. */ public static <E> Set<E> setUnionView(final Set<? extends E> set1, final Set<? extends E> set2) { - checkNotNull(set1, "set1"); - checkNotNull(set2, "set2"); + Objects.requireNonNull(set1, "set1"); + Objects.requireNonNull(set2, "set2"); return new AbstractSet<E>() { @Override @@ -220,18 +219,19 @@ } /** - * Create a view of a difference of provided sets. + * Create a view of a difference of provided sets, i.e. the diff filters out from the first set the items included + * in the second set. * <p> * View is updated whenever any of the provided set changes. * * @param set1 first set. * @param set2 second set. * @param <E> set item type. - * @return union view of given sets. + * @return view that is a difference of given sets. */ public static <E> Set<E> setDiffView(final Set<? extends E> set1, final Set<? extends E> set2) { - checkNotNull(set1, "set1"); - checkNotNull(set2, "set2"); + Objects.requireNonNull(set1, "set1"); + Objects.requireNonNull(set2, "set2"); return new AbstractSet<E>() { @Override
diff --git a/ext/cdi/jersey-cdi1x/src/main/java/org/glassfish/jersey/ext/cdi1x/internal/CdiComponentProvider.java b/ext/cdi/jersey-cdi1x/src/main/java/org/glassfish/jersey/ext/cdi1x/internal/CdiComponentProvider.java index 5595b8a..c9b333b 100644 --- a/ext/cdi/jersey-cdi1x/src/main/java/org/glassfish/jersey/ext/cdi1x/internal/CdiComponentProvider.java +++ b/ext/cdi/jersey-cdi1x/src/main/java/org/glassfish/jersey/ext/cdi1x/internal/CdiComponentProvider.java
@@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2018, 2022 Payara Foundation and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the @@ -59,8 +59,6 @@ import javax.enterprise.inject.spi.AnnotatedParameter; import javax.enterprise.inject.spi.AnnotatedType; import javax.enterprise.inject.spi.Bean; -import javax.enterprise.inject.spi.BeanManager; -import javax.enterprise.inject.spi.BeforeBeanDiscovery; import javax.enterprise.inject.spi.BeforeShutdown; import javax.enterprise.inject.spi.Extension; import javax.enterprise.inject.spi.InjectionPoint; @@ -144,6 +142,8 @@ private volatile Map<Class<?>, Set<Method>> methodsToSkip = new HashMap<>(); private volatile Map<Class<?>, Set<Field>> fieldsToSkip = new HashMap<>(); + private boolean initialized = false; + public CdiComponentProvider() { customHk2TypesProvider = CdiUtil.lookupService(Hk2CustomBoundTypesProvider.class); injectionManagerStore = CdiUtil.createHk2InjectionManagerStore(); @@ -155,7 +155,7 @@ this.injectionManager = injectionManager; this.beanManager = CdiUtil.getBeanManager(); - if (beanManager != null) { + if (beanManager != null && !injectionManager.getClass().getSimpleName().equals("NonInjectionManager")) { // Try to get CdiComponentProvider created by CDI. final CdiComponentProvider extension = beanManager.getExtension(CdiComponentProvider.class); @@ -168,18 +168,19 @@ bindHk2ClassAnalyzer(); LOGGER.config(LocalizationMessages.CDI_PROVIDER_INITIALIZED()); + initialized = true; } } } @Override public boolean bind(final Class<?> clazz, final Set<Class<?>> providerContracts) { - return bind(clazz, providerContracts, ContractProvider.NO_PRIORITY); + return initialized && bind(clazz, providerContracts, ContractProvider.NO_PRIORITY); } @Override public boolean bind(Class<?> component, ContractProvider contractProvider) { - return contractProvider != null + return initialized && contractProvider != null ? bind(component, contractProvider.getContracts(), contractProvider.getPriority(component)) : bind(component, Collections.EMPTY_SET); } @@ -629,11 +630,8 @@ ClassAnalyzer defaultClassAnalyzer = injectionManager.getInstance(ClassAnalyzer.class, ClassAnalyzer.DEFAULT_IMPLEMENTATION_NAME); - int skippedElements = methodsToSkip.size() + fieldsToSkip.size(); - - ClassAnalyzer customizedClassAnalyzer = skippedElements > 0 - ? new InjecteeSkippingAnalyzer(defaultClassAnalyzer, methodsToSkip, fieldsToSkip, beanManager) - : defaultClassAnalyzer; + ClassAnalyzer customizedClassAnalyzer = + new InjecteeSkippingAnalyzer(defaultClassAnalyzer, methodsToSkip, fieldsToSkip, beanManager); Binder binder = new AbstractBinder() { @Override
diff --git a/tests/integration/cdi-integration/cdi-skipping-analyzer/pom.xml b/tests/integration/cdi-integration/cdi-skipping-analyzer/pom.xml new file mode 100644 index 0000000..5782ec8 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/pom.xml
@@ -0,0 +1,71 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Copyright (c) 2024 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.46-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>cdi-skipping-analyzer</artifactId> + + <dependencies> + <dependency> + <groupId>jakarta.ws.rs</groupId> + <artifactId>jakarta.ws.rs-api</artifactId> + </dependency> + <dependency> + <groupId>jakarta.annotation</groupId> + <artifactId>jakarta.annotation-api</artifactId> + </dependency> + <dependency> + <groupId>jakarta.enterprise</groupId> + <artifactId>jakarta.enterprise.cdi-api</artifactId> + </dependency> + <dependency> + <groupId>org.glassfish.jersey.ext.cdi</groupId> + <artifactId>jersey-cdi1x</artifactId> + </dependency> + <dependency> + <groupId>org.jboss.weld.se</groupId> + <artifactId>weld-se-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.glassfish.jersey.test-framework</groupId> + <artifactId>jersey-test-framework-util</artifactId> + <scope>test</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.incubator</groupId> + <artifactId>jersey-injectless-client</artifactId> + <version>${jersey.version}</version> + </dependency> + </dependencies> +</project>
diff --git a/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/CdiService.java b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/CdiService.java new file mode 100644 index 0000000..44d56d2 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/CdiService.java
@@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024 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.skippinganalyzer; + +public interface CdiService<T> { + void doService(T t); +}
diff --git a/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/CdiServiceExtension.java b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/CdiServiceExtension.java new file mode 100644 index 0000000..638ed85 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/CdiServiceExtension.java
@@ -0,0 +1,34 @@ +/* + * Copyright (c) 2024 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.skippinganalyzer; + +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()); + } + +}
diff --git a/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/CdiServiceImpl.java b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/CdiServiceImpl.java new file mode 100644 index 0000000..e9e9777 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/CdiServiceImpl.java
@@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 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.skippinganalyzer; + +import javax.enterprise.inject.spi.BeanManager; +import javax.inject.Inject; + +public class CdiServiceImpl implements CdiService<StringBuilder> { + + @Inject + BeanManager beanManager; + + @Override + public void doService(StringBuilder sb) { + sb.append(getClass().getSimpleName()); + } +}
diff --git a/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/WeldDiscoveredBean.java b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/WeldDiscoveredBean.java new file mode 100644 index 0000000..c103954 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/WeldDiscoveredBean.java
@@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 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.skippinganalyzer; + +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class WeldDiscoveredBean { + +}
diff --git a/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/resources/META-INF/beans.xml b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000..56ad04f --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/resources/META-INF/beans.xml
@@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Copyright (c) 2024 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://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd" + bean-discovery-mode="annotated" version="2.0"> +</beans> \ No newline at end of file
diff --git a/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension new file mode 100644 index 0000000..7bfc71c --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
@@ -0,0 +1 @@ +org.glassfish.jersey.tests.cdi.skippinganalyzer.CdiServiceExtension \ No newline at end of file
diff --git a/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/webapp/WEB-INF/web.xml b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..e038a3a --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Copyright (c) 2024 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>
diff --git a/tests/integration/cdi-integration/cdi-skipping-analyzer/src/test/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/SkippingAnalyzerTest.java b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/test/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/SkippingAnalyzerTest.java new file mode 100644 index 0000000..8f0d3d8 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/test/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/SkippingAnalyzerTest.java
@@ -0,0 +1,79 @@ +/* + * Copyright (c) 2024 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.skippinganalyzer; + +import org.glassfish.hk2.api.ClassAnalyzer; +import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; +import org.glassfish.jersey.ext.cdi1x.internal.InjecteeSkippingAnalyzer; +import org.glassfish.jersey.inject.hk2.Hk2InjectionManagerFactory; +import org.glassfish.jersey.internal.inject.InjectionManager; +import org.glassfish.jersey.internal.inject.Injections; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.jboss.weld.environment.se.Weld; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.CDI; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Map; +import java.util.Set; + +public class SkippingAnalyzerTest { + private Weld weld; + + @BeforeEach + public void setup() { + Assumptions.assumeTrue(Hk2InjectionManagerFactory.isImmediateStrategy()); + } + + @BeforeEach + public void setUp() throws Exception { + if (Hk2InjectionManagerFactory.isImmediateStrategy()) { + weld = new Weld(); + weld.initialize(); + } + } + + @AfterEach + public void tearDown() throws Exception { + weld.shutdown(); + } + + @Test + public void testInjecteeSkippingAnalyzerWithZeroFieldsToSkip() throws Exception { + BeanManager beanManager = CDI.current().getBeanManager(); + CdiComponentProvider provider = beanManager.getExtension(CdiComponentProvider.class); + Method method = provider.getClass().getDeclaredMethod("getFieldsToSkip"); + method.setAccessible(true); + Map fieldMap = (Map) method.invoke(provider); + MatcherAssert.assertThat(0, Matchers.is(fieldMap.size())); + + InjectionManager injectionManager = Injections.createInjectionManager(); + provider.initialize(injectionManager); + injectionManager.completeRegistration(); + ClassAnalyzer analyzer = injectionManager.getInstance(ClassAnalyzer.class, CdiComponentProvider.CDI_CLASS_ANALYZER); + MatcherAssert.assertThat(InjecteeSkippingAnalyzer.class, Matchers.is(analyzer.getClass())); + + Set<Field> fieldSet = analyzer.getFields(CdiServiceImpl.class); + MatcherAssert.assertThat(0, Matchers.is(fieldSet.size())); + } +}
diff --git a/tests/integration/cdi-integration/pom.xml b/tests/integration/cdi-integration/pom.xml index 7bfa48c..5b647ca 100644 --- a/tests/integration/cdi-integration/pom.xml +++ b/tests/integration/cdi-integration/pom.xml
@@ -43,6 +43,7 @@ <module>cdi-multipart-webapp</module> <module>cdi-resource-with-at-context</module> <module>cdi-singleton</module> + <module>cdi-skipping-analyzer</module> <module>cdi-test-webapp</module> <module>cdi-with-jersey-injection-custom-cfg-webapp</module> <module>cdi-with-jersey-injection-custom-hk2-banned-webapp</module>