From 5a0f6986720b10097dbd7b722214307f3fd27e31 Mon Sep 17 00:00:00 2001 From: jansupol Date: Thu, 26 Sep 2024 22:52:08 +0200 Subject: [PATCH] Use Skipping Analyzer always Signed-off-by: jansupol --- .../internal/util/collection/Views.java | 18 ++--- .../cdi1x/internal/CdiComponentProvider.java | 20 +++-- .../cdi-skipping-analyzer/pom.xml | 71 +++++++++++++++++ .../cdi/skippinganalyzer/CdiService.java | 21 +++++ .../skippinganalyzer/CdiServiceExtension.java | 34 ++++++++ .../cdi/skippinganalyzer/CdiServiceImpl.java | 31 ++++++++ .../skippinganalyzer/WeldDiscoveredBean.java | 8 ++ .../src/main/resources/META-INF/beans.xml | 24 ++++++ .../javax.enterprise.inject.spi.Extension | 1 + .../src/main/webapp/WEB-INF/web.xml | 24 ++++++ .../SkippingAnalyzerTest.java | 79 +++++++++++++++++++ tests/integration/cdi-integration/pom.xml | 1 + 12 files changed, 312 insertions(+), 20 deletions(-) create mode 100644 tests/integration/cdi-integration/cdi-skipping-analyzer/pom.xml create mode 100644 tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/CdiService.java create mode 100644 tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/CdiServiceExtension.java create mode 100644 tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/CdiServiceImpl.java create mode 100644 tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/WeldDiscoveredBean.java create mode 100644 tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/resources/META-INF/beans.xml create mode 100644 tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension create mode 100644 tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/webapp/WEB-INF/web.xml create mode 100644 tests/integration/cdi-integration/cdi-skipping-analyzer/src/test/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/SkippingAnalyzerTest.java 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 32e2640e9e..4765056bbf 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 @@ public int size() { * @return union view of given sets. */ public static Set setUnionView(final Set set1, final Set set2) { - checkNotNull(set1, "set1"); - checkNotNull(set2, "set2"); + Objects.requireNonNull(set1, "set1"); + Objects.requireNonNull(set2, "set2"); return new AbstractSet() { @Override @@ -220,18 +219,19 @@ private Set getUnion(Set set1, Set set2) { } /** - * 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. *

* View is updated whenever any of the provided set changes. * * @param set1 first set. * @param set2 second set. * @param set item type. - * @return union view of given sets. + * @return view that is a difference of given sets. */ public static Set setDiffView(final Set set1, final Set set2) { - checkNotNull(set1, "set1"); - checkNotNull(set2, "set2"); + Objects.requireNonNull(set1, "set1"); + Objects.requireNonNull(set2, "set2"); return new AbstractSet() { @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 5595b8a370..c9b333b720 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 @@ public Boolean apply(final Class clazz) { private volatile Map, Set> methodsToSkip = new HashMap<>(); private volatile Map, Set> fieldsToSkip = new HashMap<>(); + private boolean initialized = false; + public CdiComponentProvider() { customHk2TypesProvider = CdiUtil.lookupService(Hk2CustomBoundTypesProvider.class); injectionManagerStore = CdiUtil.createHk2InjectionManagerStore(); @@ -155,7 +155,7 @@ public void initialize(final InjectionManager injectionManager) { 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 @@ public void initialize(final InjectionManager injectionManager) { bindHk2ClassAnalyzer(); LOGGER.config(LocalizationMessages.CDI_PROVIDER_INITIALIZED()); + initialized = true; } } } @Override public boolean bind(final Class clazz, final Set> 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 @@ private void bindHk2ClassAnalyzer() { 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 0000000000..523fdb38c3 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/pom.xml @@ -0,0 +1,71 @@ + + + + + + cdi-integration-project + org.glassfish.jersey.tests.integration.cdi + 2.46-SNAPSHOT + + 4.0.0 + + cdi-skipping-analyzer + + + + jakarta.ws.rs + jakarta.ws.rs-api + + + jakarta.annotation + jakarta.annotation-api + + + jakarta.enterprise + jakarta.enterprise.cdi-api + + + org.glassfish.jersey.ext.cdi + jersey-cdi1x + + + org.jboss.weld.se + weld-se-core + test + + + org.glassfish.jersey.test-framework + jersey-test-framework-util + test + + + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-bundle + pom + test + + + org.glassfish.jersey.incubator + jersey-injectless-client + ${jersey.version} + + + 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 0000000000..546db99a93 --- /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) 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.skippinganalyzer; + +public interface CdiService { + 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 0000000000..75040129c3 --- /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) 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.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 0000000000..27d808de3d --- /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) 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.skippinganalyzer; + +import javax.enterprise.inject.spi.BeanManager; +import javax.inject.Inject; + +public class CdiServiceImpl implements CdiService { + + @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 0000000000..07f00aa9e3 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/java/org/glassfish/jersey/tests/cdi/skippinganalyzer/WeldDiscoveredBean.java @@ -0,0 +1,8 @@ +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 0000000000..56ad04f3c1 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/resources/META-INF/beans.xml @@ -0,0 +1,24 @@ + + + + + + \ 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 0000000000..7bfc71c13c --- /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 0000000000..daed0f5bb4 --- /dev/null +++ b/tests/integration/cdi-integration/cdi-skipping-analyzer/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,24 @@ + + + + + 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 0000000000..e85783bf7c --- /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) 2019, 2022 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 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 7bfa48c595..5b647cab88 100644 --- a/tests/integration/cdi-integration/pom.xml +++ b/tests/integration/cdi-integration/pom.xml @@ -43,6 +43,7 @@ cdi-multipart-webapp cdi-resource-with-at-context cdi-singleton + cdi-skipping-analyzer cdi-test-webapp cdi-with-jersey-injection-custom-cfg-webapp cdi-with-jersey-injection-custom-hk2-banned-webapp