Commit 6e1afc3413e6cf50212a7ffcbfec987160387f6d
1 parent
ab7d964f
Exists in
master
Tornando a implementação do ProxyBean mais gernérica. Poderemos mudar
até o nome desta classe para algo mais genérico também.
Showing
1 changed file
with
55 additions
and
15 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ProxyBean.java
| ... | ... | @@ -38,20 +38,24 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; |
| 38 | 38 | |
| 39 | 39 | import java.lang.annotation.Annotation; |
| 40 | 40 | import java.lang.reflect.Type; |
| 41 | -import java.util.Collections; | |
| 42 | 41 | import java.util.HashSet; |
| 43 | 42 | import java.util.Set; |
| 44 | 43 | |
| 45 | 44 | import javax.enterprise.context.Dependent; |
| 45 | +import javax.enterprise.context.NormalScope; | |
| 46 | 46 | import javax.enterprise.context.spi.CreationalContext; |
| 47 | +import javax.enterprise.inject.Alternative; | |
| 47 | 48 | import javax.enterprise.inject.Any; |
| 48 | 49 | import javax.enterprise.inject.Default; |
| 50 | +import javax.enterprise.inject.Stereotype; | |
| 49 | 51 | import javax.enterprise.inject.spi.AnnotatedType; |
| 50 | 52 | import javax.enterprise.inject.spi.Bean; |
| 51 | 53 | import javax.enterprise.inject.spi.BeanManager; |
| 52 | 54 | import javax.enterprise.inject.spi.InjectionPoint; |
| 53 | 55 | import javax.enterprise.inject.spi.InjectionTarget; |
| 54 | 56 | import javax.enterprise.util.AnnotationLiteral; |
| 57 | +import javax.inject.Named; | |
| 58 | +import javax.inject.Qualifier; | |
| 55 | 59 | import javax.inject.Scope; |
| 56 | 60 | |
| 57 | 61 | /** |
| ... | ... | @@ -59,21 +63,22 @@ import javax.inject.Scope; |
| 59 | 63 | */ |
| 60 | 64 | public class ProxyBean implements Bean<Object> { |
| 61 | 65 | |
| 62 | - private Class<Object> proxy; | |
| 66 | + private Class<Object> beanClass; | |
| 63 | 67 | |
| 64 | 68 | private InjectionTarget<Object> injectionTarget; |
| 65 | 69 | |
| 66 | - public ProxyBean(Class<Object> proxy, BeanManager beanManager) { | |
| 67 | - AnnotatedType<Object> annotatedType = beanManager.createAnnotatedType(proxy); | |
| 70 | + public ProxyBean(Class<Object> beanClass, BeanManager beanManager) { | |
| 71 | + AnnotatedType<Object> annotatedType = beanManager.createAnnotatedType(beanClass); | |
| 68 | 72 | |
| 69 | 73 | this.injectionTarget = beanManager.createInjectionTarget(annotatedType); |
| 70 | - this.proxy = proxy; | |
| 74 | + this.beanClass = beanClass; | |
| 71 | 75 | } |
| 72 | 76 | |
| 73 | 77 | public Object create(CreationalContext<Object> creationalContext) { |
| 74 | 78 | Object instance = injectionTarget.produce(creationalContext); |
| 75 | 79 | injectionTarget.inject(instance, creationalContext); |
| 76 | 80 | injectionTarget.postConstruct(instance); |
| 81 | + | |
| 77 | 82 | return instance; |
| 78 | 83 | } |
| 79 | 84 | |
| ... | ... | @@ -85,40 +90,75 @@ public class ProxyBean implements Bean<Object> { |
| 85 | 90 | |
| 86 | 91 | public Set<Type> getTypes() { |
| 87 | 92 | Set<Type> types = new HashSet<Type>(); |
| 88 | - types.add(proxy.getSuperclass()); | |
| 93 | + types.add(beanClass.getSuperclass()); | |
| 89 | 94 | types.add(Object.class); |
| 95 | + | |
| 90 | 96 | return types; |
| 91 | 97 | } |
| 92 | 98 | |
| 93 | 99 | @SuppressWarnings("serial") |
| 94 | 100 | public Set<Annotation> getQualifiers() { |
| 95 | - Set<Annotation> qualifiers = new HashSet<Annotation>(); | |
| 96 | - qualifiers.add(new AnnotationLiteral<Default>() { | |
| 101 | + Set<Annotation> result = new HashSet<Annotation>(); | |
| 102 | + | |
| 103 | + result.add(new AnnotationLiteral<Default>() { | |
| 97 | 104 | }); |
| 98 | - qualifiers.add(new AnnotationLiteral<Any>() { | |
| 105 | + result.add(new AnnotationLiteral<Any>() { | |
| 99 | 106 | }); |
| 100 | 107 | |
| 101 | - return qualifiers; | |
| 108 | + for (Annotation annotation : beanClass.getAnnotations()) { | |
| 109 | + if (annotation.getClass().isAnnotationPresent(Qualifier.class)) { | |
| 110 | + result.add(annotation); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 114 | + return result; | |
| 102 | 115 | } |
| 103 | 116 | |
| 104 | 117 | public Class<? extends Annotation> getScope() { |
| 105 | - return Dependent.class; | |
| 118 | + Class<? extends Annotation> result = Dependent.class; | |
| 119 | + | |
| 120 | + Class<? extends Annotation> annotationClass; | |
| 121 | + for (Annotation annotation : beanClass.getAnnotations()) { | |
| 122 | + annotationClass = annotation.getClass(); | |
| 123 | + | |
| 124 | + if (annotationClass.isAnnotationPresent(Scope.class) | |
| 125 | + || annotationClass.isAnnotationPresent(NormalScope.class)) { | |
| 126 | + result = annotationClass; | |
| 127 | + break; | |
| 128 | + } | |
| 129 | + } | |
| 130 | + | |
| 131 | + return result; | |
| 106 | 132 | } |
| 107 | 133 | |
| 108 | 134 | public String getName() { |
| 109 | - return null; | |
| 135 | + String result = null; | |
| 136 | + | |
| 137 | + if (beanClass.isAnnotationPresent(Named.class)) { | |
| 138 | + result = beanClass.getAnnotation(Named.class).value(); | |
| 139 | + } | |
| 140 | + | |
| 141 | + return result; | |
| 110 | 142 | } |
| 111 | 143 | |
| 112 | 144 | public Set<Class<? extends Annotation>> getStereotypes() { |
| 113 | - return Collections.emptySet(); | |
| 145 | + Set<Class<? extends Annotation>> result = new HashSet<Class<? extends Annotation>>(); | |
| 146 | + | |
| 147 | + for (Annotation annotation : beanClass.getAnnotations()) { | |
| 148 | + if (annotation.getClass().isAnnotationPresent(Stereotype.class)) { | |
| 149 | + result.add(annotation.getClass()); | |
| 150 | + } | |
| 151 | + } | |
| 152 | + | |
| 153 | + return result; | |
| 114 | 154 | } |
| 115 | 155 | |
| 116 | 156 | public Class<Object> getBeanClass() { |
| 117 | - return proxy; | |
| 157 | + return beanClass; | |
| 118 | 158 | } |
| 119 | 159 | |
| 120 | 160 | public boolean isAlternative() { |
| 121 | - return false; | |
| 161 | + return beanClass.isAnnotationPresent(Alternative.class); | |
| 122 | 162 | } |
| 123 | 163 | |
| 124 | 164 | public boolean isNullable() { | ... | ... |