Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 9637d54

Browse files
committed
Detect CDI produced beans, so that HK2 does not try to mock them.
Both field and method producer mechanisms should get covered. Change-Id: I5fbea011464566da0c21a342449d5ed19d68b70a Signed-off-by: Jakub Podlesak <[email protected]>
1 parent 54fce07 commit 9637d54

File tree

8 files changed

+434
-0
lines changed

8 files changed

+434
-0
lines changed

containers/glassfish/jersey-gf-cdi/src/main/java/org/glassfish/jersey/gf/cdi/internal/CdiComponentProvider.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
import javax.enterprise.inject.spi.InjectionTarget;
8686
import javax.enterprise.inject.spi.ProcessAnnotatedType;
8787
import javax.enterprise.inject.spi.ProcessInjectionTarget;
88+
import javax.enterprise.inject.spi.ProcessProducerField;
89+
import javax.enterprise.inject.spi.ProcessProducerMethod;
8890
import javax.enterprise.util.AnnotationLiteral;
8991
import javax.inject.Inject;
9092
import javax.inject.Qualifier;
@@ -414,6 +416,16 @@ private void processAnnotatedType(@Observes final ProcessAnnotatedType processAn
414416
}
415417
}
416418

419+
@SuppressWarnings("unused")
420+
private void processProducerMethod(@Observes final ProcessProducerMethod processProducerMethod) {
421+
typesSeenBeforeValidation.addAll(processProducerMethod.getAnnotatedProducerMethod().getTypeClosure());
422+
}
423+
424+
@SuppressWarnings("unused")
425+
private void processProducerField(@Observes final ProcessProducerField processProducerField) {
426+
typesSeenBeforeValidation.addAll(processProducerField.getAnnotatedProducerField().getTypeClosure());
427+
}
428+
417429
@SuppressWarnings("unused")
418430
private void afterTypeDiscovery(@Observes final AfterTypeDiscovery afterTypeDiscovery) {
419431
final List<Class<?>> interceptors = afterTypeDiscovery.getInterceptors();
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.jersey.tests.cdi.resources;
42+
43+
import javax.enterprise.inject.Produces;
44+
45+
/**
46+
* CDI producer to help us make sure HK2 do not mess up with
47+
* types backed by CDI producers.
48+
*
49+
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
50+
*/
51+
public class CustomCdiProducer {
52+
53+
/**
54+
* To cover field producer.
55+
*/
56+
@Produces
57+
public static FieldProducedBean<String> field = new FieldProducedBean<String>("field");
58+
59+
/**
60+
* To cover method producer.
61+
*
62+
* @return bean instance to inject
63+
*/
64+
@Produces
65+
public MethodProducedBean<String> produceBean() {
66+
return new MethodProducedBean<String>("method");
67+
}
68+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.jersey.tests.cdi.resources;
42+
43+
import javax.enterprise.inject.Vetoed;
44+
45+
/**
46+
* A bean that would be produced by a CDI producer field.
47+
* This is to make sure we do not mess up with CDI producers with HK2.
48+
*
49+
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
50+
*/
51+
@Vetoed
52+
public class FieldProducedBean<T> implements ValueHolder<T>{
53+
54+
private final T value;
55+
56+
/**
57+
* Make an instance with given value.
58+
*
59+
* @param value
60+
*/
61+
public FieldProducedBean(T value) {
62+
this.value = value;
63+
}
64+
65+
@Override
66+
public T getValue() {
67+
return value;
68+
}
69+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.jersey.tests.cdi.resources;
42+
43+
import javax.enterprise.inject.Vetoed;
44+
45+
/**
46+
* A bean that would be produced by a CDI producer method.
47+
* This is to make sure we do not mess up with CDI producers with HK2.
48+
*
49+
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
50+
*/
51+
@Vetoed
52+
public class MethodProducedBean<T> implements ValueHolder<T> {
53+
54+
final private T value;
55+
56+
/**
57+
* Make an instance with given value.
58+
*
59+
* @param value
60+
*/
61+
public MethodProducedBean(T value) {
62+
this.value = value;
63+
}
64+
65+
/**
66+
* Value getter.
67+
*
68+
* @return value.
69+
*/
70+
@Override
71+
public T getValue() {
72+
return value;
73+
}
74+
}

tests/integration/cdi-test-webapp/src/main/java/org/glassfish/jersey/tests/cdi/resources/MyApplication.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public Set<Class<?>> getClasses() {
7474
classes.add(StutteringEcho.class);
7575
classes.add(ReversingEchoResource.class);
7676
classes.add(CounterResource.class);
77+
classes.add(ProducerResource.class);
7778
return classes;
7879
}
7980

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.tests.cdi.resources;
41+
42+
import javax.enterprise.context.RequestScoped;
43+
import javax.inject.Inject;
44+
import javax.ws.rs.GET;
45+
import javax.ws.rs.Path;
46+
47+
/**
48+
* This one will get injected with a CDI producer.
49+
* HK2 should not mess up with this.
50+
*
51+
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
52+
*/
53+
@RequestScoped
54+
@Path("producer")
55+
public class ProducerResource {
56+
57+
@Inject
58+
MethodProducedBean<String> m;
59+
60+
@Inject
61+
FieldProducedBean<String> f;
62+
63+
/**
64+
* Return field produced bean value.
65+
*
66+
* @return value from field produced bean.
67+
*/
68+
@Path("f")
69+
@GET
70+
public String getFieldValue() {
71+
return f.getValue();
72+
}
73+
74+
/**
75+
* Return method produced bean value.
76+
*
77+
* @return value from method produced bean.
78+
*/
79+
@Path("m")
80+
@GET
81+
public String getMethodValue() {
82+
return m.getValue();
83+
}
84+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.jersey.tests.cdi.resources;
42+
43+
/**
44+
* Helper type to check CDI producer mechanism is not broken
45+
* by automatic HK2/CDI bindings.
46+
*
47+
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
48+
*/
49+
public interface ValueHolder<T> {
50+
51+
/**
52+
* Value getter.
53+
*
54+
* @return value.
55+
*/
56+
T getValue();
57+
}

0 commit comments

Comments
 (0)