Skip to content

Commit

Permalink
Use Skipping Analyzer always
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol committed Sep 26, 2024
1 parent a7c88d3 commit 5a0f698
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}.
*
Expand Down Expand Up @@ -197,8 +196,8 @@ public int size() {
* @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
Expand All @@ -220,18 +219,19 @@ private Set<E> getUnion(Set<? extends E> set1, Set<? extends E> 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.
* <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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -144,6 +142,8 @@ public Boolean apply(final Class<?> clazz) {
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();
Expand All @@ -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);

Expand All @@ -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<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);
}
Expand Down Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions tests/integration/cdi-integration/cdi-skipping-analyzer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019, 2023 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>
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.skippinganalyzer;

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.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());
}

}
Original file line number Diff line number Diff line change
@@ -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<StringBuilder> {

@Inject
BeanManager beanManager;

@Override
public void doService(StringBuilder sb) {
sb.append(getClass().getSimpleName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.glassfish.jersey.tests.cdi.skippinganalyzer;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class WeldDiscoveredBean {

}
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.glassfish.jersey.tests.cdi.skippinganalyzer.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 5a0f698

Please sign in to comment.