Commit 38700bb3aa484bfa2313c7e883238ee1b1ea19d6
1 parent
d7147791
Exists in
master
Correção do erro de InjectionPoint nulo ao utilizar o
Beans.getReference(...)
Showing
5 changed files
with
165 additions
and
19 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/Name.java
@@ -47,7 +47,9 @@ import java.lang.annotation.Retention; | @@ -47,7 +47,9 @@ import java.lang.annotation.Retention; | ||
47 | import java.lang.annotation.Target; | 47 | import java.lang.annotation.Target; |
48 | 48 | ||
49 | import javax.enterprise.util.Nonbinding; | 49 | import javax.enterprise.util.Nonbinding; |
50 | +import javax.inject.Qualifier; | ||
50 | 51 | ||
52 | +@Qualifier | ||
51 | @Inherited | 53 | @Inherited |
52 | @Retention(RUNTIME) | 54 | @Retention(RUNTIME) |
53 | @Target({ TYPE, FIELD, METHOD, PARAMETER }) | 55 | @Target({ TYPE, FIELD, METHOD, PARAMETER }) |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomInjectionPoint.java
0 → 100644
@@ -0,0 +1,136 @@ | @@ -0,0 +1,136 @@ | ||
1 | +/* | ||
2 | + * Demoiselle Framework | ||
3 | + * Copyright (C) 2010 SERPRO | ||
4 | + * ---------------------------------------------------------------------------- | ||
5 | + * This file is part of Demoiselle Framework. | ||
6 | + * | ||
7 | + * Demoiselle Framework is free software; you can redistribute it and/or | ||
8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | ||
9 | + * as published by the Free Software Foundation. | ||
10 | + * | ||
11 | + * This program is distributed in the hope that it will be useful, | ||
12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | + * GNU General Public License for more details. | ||
15 | + * | ||
16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | ||
17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | ||
18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | ||
20 | + * ---------------------------------------------------------------------------- | ||
21 | + * Este arquivo é parte do Framework Demoiselle. | ||
22 | + * | ||
23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
25 | + * do Software Livre (FSF). | ||
26 | + * | ||
27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
30 | + * para maiores detalhes. | ||
31 | + * | ||
32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
36 | + */ | ||
37 | +package br.gov.frameworkdemoiselle.internal.bootstrap; | ||
38 | + | ||
39 | +import java.lang.annotation.Annotation; | ||
40 | +import java.lang.reflect.Member; | ||
41 | +import java.lang.reflect.Type; | ||
42 | +import java.util.Arrays; | ||
43 | +import java.util.HashSet; | ||
44 | +import java.util.Set; | ||
45 | + | ||
46 | +import javax.enterprise.inject.spi.Annotated; | ||
47 | +import javax.enterprise.inject.spi.Bean; | ||
48 | +import javax.enterprise.inject.spi.InjectionPoint; | ||
49 | + | ||
50 | +public class CustomInjectionPoint implements InjectionPoint { | ||
51 | + | ||
52 | + private final Bean<?> bean; | ||
53 | + | ||
54 | + private final Type beanType; | ||
55 | + | ||
56 | + private final Set<Annotation> qualifiers; | ||
57 | + | ||
58 | + public CustomInjectionPoint(Bean<?> bean, Type beanType, Annotation... qualifiers) { | ||
59 | + this.bean = bean; | ||
60 | + this.beanType = beanType; | ||
61 | + this.qualifiers = new HashSet<Annotation>(Arrays.asList(qualifiers)); | ||
62 | + } | ||
63 | + | ||
64 | + @Override | ||
65 | + public Type getType() { | ||
66 | + return this.beanType; | ||
67 | + } | ||
68 | + | ||
69 | + @Override | ||
70 | + public Set<Annotation> getQualifiers() { | ||
71 | + return this.qualifiers; | ||
72 | + } | ||
73 | + | ||
74 | + @Override | ||
75 | + public Bean<?> getBean() { | ||
76 | + return this.bean; | ||
77 | + } | ||
78 | + | ||
79 | + @Override | ||
80 | + public Member getMember() { | ||
81 | + return null; | ||
82 | + } | ||
83 | + | ||
84 | + @Override | ||
85 | + public boolean isDelegate() { | ||
86 | + return false; | ||
87 | + } | ||
88 | + | ||
89 | + @Override | ||
90 | + public boolean isTransient() { | ||
91 | + return false; | ||
92 | + } | ||
93 | + | ||
94 | + @Override | ||
95 | + public Annotated getAnnotated() { | ||
96 | + return new Annotated() { | ||
97 | + | ||
98 | + @Override | ||
99 | + public Type getBaseType() { | ||
100 | + // TODO Auto-generated method stub | ||
101 | + return null; | ||
102 | + } | ||
103 | + | ||
104 | + @Override | ||
105 | + public Set<Type> getTypeClosure() { | ||
106 | + // TODO Auto-generated method stub | ||
107 | + return null; | ||
108 | + } | ||
109 | + | ||
110 | + @Override | ||
111 | + @SuppressWarnings("unchecked") | ||
112 | + public <T extends Annotation> T getAnnotation(Class<T> annotationType) { | ||
113 | + T result = null; | ||
114 | + | ||
115 | + for (Annotation annotation : getAnnotations()) { | ||
116 | + if (annotation.annotationType() == annotationType) { | ||
117 | + result = (T) annotation; | ||
118 | + break; | ||
119 | + } | ||
120 | + } | ||
121 | + | ||
122 | + return result; | ||
123 | + } | ||
124 | + | ||
125 | + @Override | ||
126 | + public Set<Annotation> getAnnotations() { | ||
127 | + return qualifiers; | ||
128 | + } | ||
129 | + | ||
130 | + @Override | ||
131 | + public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) { | ||
132 | + return qualifiers.contains(annotationType); | ||
133 | + } | ||
134 | + }; | ||
135 | + } | ||
136 | +} |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ResourceBundleProducer.java
@@ -73,26 +73,27 @@ public class ResourceBundleProducer implements Serializable { | @@ -73,26 +73,27 @@ public class ResourceBundleProducer implements Serializable { | ||
73 | * baseName | 73 | * baseName |
74 | */ | 74 | */ |
75 | public static ResourceBundle create(String baseName, Locale locale) { | 75 | public static ResourceBundle create(String baseName, Locale locale) { |
76 | - ResourceBundle bundle = null; | ||
77 | - bundle = new ResourceBundle(baseName, locale); | ||
78 | - return bundle; | 76 | + return new ResourceBundle(baseName, locale); |
79 | } | 77 | } |
80 | 78 | ||
81 | /** | 79 | /** |
82 | * This method is the factory default for ResourceBundle. It creates the ResourceBundle based on a file called | 80 | * This method is the factory default for ResourceBundle. It creates the ResourceBundle based on a file called |
83 | * messages.properties. | 81 | * messages.properties. |
84 | */ | 82 | */ |
85 | - @Produces | ||
86 | @Default | 83 | @Default |
87 | - public ResourceBundle create(InjectionPoint ip) { | ||
88 | - String baseName; | ||
89 | - | ||
90 | - if (ip != null && ip.getAnnotated().isAnnotationPresent(Name.class)) { | ||
91 | - baseName = ip.getAnnotated().getAnnotation(Name.class).value(); | ||
92 | - } else { | ||
93 | - baseName = "messages"; | ||
94 | - } | 84 | + @Produces |
85 | + public ResourceBundle createDefault(InjectionPoint ip) { | ||
86 | + return create("messages", Beans.getReference(Locale.class)); | ||
87 | + } | ||
95 | 88 | ||
89 | + /** | ||
90 | + * This method is the factory default for ResourceBundle. It creates the ResourceBundle based on a file called | ||
91 | + * messages.properties. | ||
92 | + */ | ||
93 | + @Name("") | ||
94 | + @Produces | ||
95 | + public ResourceBundle createNamed(InjectionPoint ip) { | ||
96 | + String baseName = ip.getAnnotated().getAnnotation(Name.class).value(); | ||
96 | return create(baseName, Beans.getReference(Locale.class)); | 97 | return create(baseName, Beans.getReference(Locale.class)); |
97 | } | 98 | } |
98 | } | 99 | } |
impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Beans.java
@@ -49,14 +49,18 @@ | @@ -49,14 +49,18 @@ | ||
49 | package br.gov.frameworkdemoiselle.util; | 49 | package br.gov.frameworkdemoiselle.util; |
50 | 50 | ||
51 | import java.lang.annotation.Annotation; | 51 | import java.lang.annotation.Annotation; |
52 | +import java.lang.reflect.Type; | ||
52 | import java.util.Locale; | 53 | import java.util.Locale; |
53 | import java.util.NoSuchElementException; | 54 | import java.util.NoSuchElementException; |
54 | import java.util.Set; | 55 | import java.util.Set; |
55 | 56 | ||
57 | +import javax.enterprise.context.spi.CreationalContext; | ||
56 | import javax.enterprise.inject.spi.Bean; | 58 | import javax.enterprise.inject.spi.Bean; |
57 | import javax.enterprise.inject.spi.BeanManager; | 59 | import javax.enterprise.inject.spi.BeanManager; |
60 | +import javax.enterprise.inject.spi.InjectionPoint; | ||
58 | 61 | ||
59 | import br.gov.frameworkdemoiselle.DemoiselleException; | 62 | import br.gov.frameworkdemoiselle.DemoiselleException; |
63 | +import br.gov.frameworkdemoiselle.internal.bootstrap.CustomInjectionPoint; | ||
60 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; | 64 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; |
61 | 65 | ||
62 | public final class Beans { | 66 | public final class Beans { |
@@ -78,7 +82,7 @@ public final class Beans { | @@ -78,7 +82,7 @@ public final class Beans { | ||
78 | T instance; | 82 | T instance; |
79 | 83 | ||
80 | try { | 84 | try { |
81 | - instance = (T) getReference(manager.getBeans(beanClass, qualifiers), beanClass); | 85 | + instance = (T) getReference(manager.getBeans(beanClass, qualifiers), beanClass, qualifiers); |
82 | 86 | ||
83 | } catch (NoSuchElementException cause) { | 87 | } catch (NoSuchElementException cause) { |
84 | StringBuffer buffer = new StringBuffer(); | 88 | StringBuffer buffer = new StringBuffer(); |
@@ -126,10 +130,13 @@ public final class Beans { | @@ -126,10 +130,13 @@ public final class Beans { | ||
126 | } | 130 | } |
127 | 131 | ||
128 | @SuppressWarnings("unchecked") | 132 | @SuppressWarnings("unchecked") |
129 | - private static <T> T getReference(Set<Bean<?>> beans, Class<T> beanClass) { | 133 | + private static <T> T getReference(Set<Bean<?>> beans, Class<T> beanClass, Annotation... qualifiers) { |
130 | Bean<?> bean = beans.iterator().next(); | 134 | Bean<?> bean = beans.iterator().next(); |
131 | - return (T) manager.getReference(bean, beanClass == null ? bean.getBeanClass() : beanClass, | ||
132 | - manager.createCreationalContext(bean)); | 135 | + CreationalContext<?> context = manager.createCreationalContext(bean); |
136 | + Type beanType = beanClass == null ? bean.getBeanClass() : beanClass; | ||
137 | + InjectionPoint injectionPoint = new CustomInjectionPoint(bean, beanType, qualifiers); | ||
138 | + | ||
139 | + return (T) manager.getInjectableReference(injectionPoint, context); | ||
133 | } | 140 | } |
134 | 141 | ||
135 | private static <T> T getReference(Set<Bean<?>> beans) { | 142 | private static <T> T getReference(Set<Bean<?>> beans) { |
@@ -139,4 +146,4 @@ public final class Beans { | @@ -139,4 +146,4 @@ public final class Beans { | ||
139 | private static ResourceBundle getBundle() { | 146 | private static ResourceBundle getBundle() { |
140 | return ResourceBundleProducer.create("demoiselle-core-bundle", Locale.getDefault()); | 147 | return ResourceBundleProducer.create("demoiselle-core-bundle", Locale.getDefault()); |
141 | } | 148 | } |
142 | -} | ||
143 | \ No newline at end of file | 149 | \ No newline at end of file |
150 | +} |
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/producer/ResourceBundleProducerTest.java
@@ -94,7 +94,7 @@ public class ResourceBundleProducerTest { | @@ -94,7 +94,7 @@ public class ResourceBundleProducerTest { | ||
94 | @Test | 94 | @Test |
95 | public void testCreateNullInjectionPoint() { | 95 | public void testCreateNullInjectionPoint() { |
96 | ResourceBundleProducer factory = new ResourceBundleProducer(); | 96 | ResourceBundleProducer factory = new ResourceBundleProducer(); |
97 | - ResourceBundle resourceBundle = factory.create((InjectionPoint) null); | 97 | + ResourceBundle resourceBundle = factory.createDefault((InjectionPoint) null); |
98 | Assert.assertNotNull(resourceBundle); | 98 | Assert.assertNotNull(resourceBundle); |
99 | } | 99 | } |
100 | 100 | ||
@@ -114,7 +114,7 @@ public class ResourceBundleProducerTest { | @@ -114,7 +114,7 @@ public class ResourceBundleProducerTest { | ||
114 | replay(ip); | 114 | replay(ip); |
115 | 115 | ||
116 | ResourceBundleProducer factory = new ResourceBundleProducer(); | 116 | ResourceBundleProducer factory = new ResourceBundleProducer(); |
117 | - Assert.assertNotNull(factory.create(ip)); | 117 | + Assert.assertNotNull(factory.createDefault(ip)); |
118 | } | 118 | } |
119 | 119 | ||
120 | // @Test | 120 | // @Test |