diff --git a/src/main/java/info/novatec/beantest/extension/BeanTestExtension.java b/src/main/java/info/novatec/beantest/extension/BeanTestExtension.java index 74ba6fb..6b0c5f7 100644 --- a/src/main/java/info/novatec/beantest/extension/BeanTestExtension.java +++ b/src/main/java/info/novatec/beantest/extension/BeanTestExtension.java @@ -16,22 +16,16 @@ package info.novatec.beantest.extension; import info.novatec.beantest.transactions.Transactional; - -import javax.annotation.Resource; import javax.ejb.EJB; import javax.ejb.MessageDriven; import javax.ejb.Stateless; import javax.enterprise.context.RequestScoped; import javax.enterprise.event.Observes; -import javax.enterprise.inject.spi.AnnotatedField; -import javax.enterprise.inject.spi.AnnotatedMethod; import javax.enterprise.inject.spi.AnnotatedType; import javax.enterprise.inject.spi.Extension; import javax.enterprise.inject.spi.ProcessAnnotatedType; import javax.inject.Inject; import javax.interceptor.Interceptor; -import javax.persistence.PersistenceContext; - import org.apache.deltaspike.core.util.metadata.AnnotationInstanceProvider; import org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder; @@ -83,7 +77,7 @@ private void modifyAnnotatedTypeMetaData(ProcessAnnotatedType pat) { AnnotatedTypeBuilder builder = new AnnotatedTypeBuilder().readFromType(at); builder.addToClass(transactionalAnnotation).addToClass(requestScopedAnnotation); - addInjectAnnotation(at, builder); + InjectionHelper.addInjectAnnotation(at, builder); //Set the wrapper instead the actual annotated type pat.setAnnotatedType(builder.create()); @@ -99,78 +93,9 @@ private void modifyAnnotatedTypeMetaData(ProcessAnnotatedType pat) { */ private void processInterceptorDependencies(ProcessAnnotatedType pat) { AnnotatedTypeBuilder builder = new AnnotatedTypeBuilder().readFromType(pat.getAnnotatedType()); - addInjectAnnotation(pat.getAnnotatedType(), builder); + InjectionHelper.addInjectAnnotation(pat.getAnnotatedType(), builder); pat.setAnnotatedType(builder.create()); } - /** - * Returns true if the field is NOT annotated with {@link Inject} and is annotated with one of the following annotations: - *
    - *
  • {@link EJB} - *
  • {@link PersistenceContext} - *
  • {@link Resource} - *
- * Otherwise, it returns false. - * - * @param - * the type of the annotated field - * @param field - * the annotated field whose annotations should be verified - * @return true if the field is NOT annotated with {@link Inject} and is annotated with {@link EJB}, - * {@link PersistenceContext} or {@link Resource} - */ - private boolean shouldInjectionAnnotationBeAddedToField(AnnotatedField field) { - return !field.isAnnotationPresent(Inject.class) - && (field.isAnnotationPresent(Resource.class) || field.isAnnotationPresent(EJB.class) || field.isAnnotationPresent(PersistenceContext.class)); - } - - /** - * Returns true if the method is NOT annotated with {@link Inject} and is annotated with one of the following annotations: - *
    - *
  • {@link EJB} - *
  • {@link PersistenceContext} - *
  • {@link Resource} - *
- * Otherwise, it returns false. - * - * @param - * the type of the annotated method - * @param method - * the annotated method whose annotations should be verified - * @return true if the method is NOT annotated with {@link Inject} and is annotated with {@link EJB}, - * {@link PersistenceContext} or {@link Resource} - */ - private boolean shouldInjectionAnnotationBeAddedToMethod(AnnotatedMethod method) { - return !method.isAnnotationPresent(Inject.class) - && (method.isAnnotationPresent(Resource.class) || method.isAnnotationPresent(EJB.class) || method.isAnnotationPresent(PersistenceContext.class)); - } - - /** - * Adds the {@link Inject} annotation to the fields and setters of the annotated type if required. - * - * @param - * the type of the annotated type - * @param annotatedType - * the annotated type whose fields and setters the inject annotation should be added to - * @param builder - * the builder that should be used to add the annotation. - * @see #shouldInjectionAnnotationBeAddedToField(AnnotatedField) and #shouldInjectionAnnotationBeAddedToMethod(AnnotatedMethod) - */ - private void addInjectAnnotation(final AnnotatedType annotatedType, AnnotatedTypeBuilder builder) { - Inject injectAnnotation = AnnotationInstanceProvider.of(Inject.class); - - for (AnnotatedField field : annotatedType.getFields() ) { - if (shouldInjectionAnnotationBeAddedToField(field)) { - builder.addToField(field, injectAnnotation); - } - } - - for (AnnotatedMethod method : annotatedType.getMethods()) { - if (shouldInjectionAnnotationBeAddedToMethod(method)) { - builder.addToMethod(method, injectAnnotation); - } - } - - } } diff --git a/src/main/java/info/novatec/beantest/extension/InjectionHelper.java b/src/main/java/info/novatec/beantest/extension/InjectionHelper.java new file mode 100644 index 0000000..1b9fa26 --- /dev/null +++ b/src/main/java/info/novatec/beantest/extension/InjectionHelper.java @@ -0,0 +1,122 @@ +/* + * Bean Testing. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package info.novatec.beantest.extension; + +import java.lang.annotation.Annotation; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import javax.annotation.Resource; +import javax.ejb.EJB; +import javax.enterprise.inject.spi.AnnotatedField; +import javax.enterprise.inject.spi.AnnotatedMember; +import javax.enterprise.inject.spi.AnnotatedMethod; +import javax.enterprise.inject.spi.AnnotatedType; +import javax.inject.Inject; +import javax.persistence.PersistenceContext; +import org.apache.deltaspike.core.util.metadata.AnnotationInstanceProvider; +import org.apache.deltaspike.core.util.metadata.builder.AnnotatedTypeBuilder; + +/** + * This class provides general convenience methods for injection and validation. + * + * @author Carlos Barragan (carlos.barragan@novatec-gmbh.de) + */ +public final class InjectionHelper { + + private static final Inject INJECT_ANNOTATION = AnnotationInstanceProvider.of(Inject.class); + + private static final Set> JAVA_EE_ANNOTATIONS = createJavaEEAnnotationSet(); + + private static Set> createJavaEEAnnotationSet() { + Set> javaEEAnnotations = new HashSet>(); + javaEEAnnotations.add(Resource.class); + javaEEAnnotations.add(EJB.class); + javaEEAnnotations.add(PersistenceContext.class); + return Collections.unmodifiableSet(javaEEAnnotations); + } + + private InjectionHelper() { + // Empty on purpose. + } + + /** + * Returns true if the member is NOT annotated with {@link Inject} and is annotated with one of the following annotations: + *
    + *
  • {@link EJB} + *
  • {@link PersistenceContext} + *
  • {@link Resource} + *
+ * Otherwise, it returns false. + * + * @param + * the type of the annotated member + * @param member + * the annotated member whose annotations should be verified. + * @return true if the member is NOT annotated with {@link Inject} and is annotated with {@link EJB}, + * {@link PersistenceContext} or {@link Resource} + */ + public static boolean shouldInjectionAnnotationBeAddedToMember(AnnotatedMember member) { + return !member.isAnnotationPresent(Inject.class) && hasJavaEEAnnotations(member); + } + + /** + * Returns true if at least one of the following Java EE annotations is present in the given member: + *
    + *
  • {@link EJB} + *
  • {@link PersistenceContext} + *
  • {@link Resource} + *
+ * Otherwise, it returns false. + * @param the type of the annotated member. + * @param member the member whose annotations should be verified. + * @return true if the member is at least annotated with one of the following annotations: {@link EJB}, {@link PersistenceContext} or {@link Resource}. + */ + private static boolean hasJavaEEAnnotations(AnnotatedMember member) { + for(Class javaEEannotation : JAVA_EE_ANNOTATIONS) { + if (member.isAnnotationPresent(javaEEannotation)) { + return true; + } + } + return false; + } + + /** + * Adds the {@link Inject} annotation to the fields and setters of the annotated type if required. + * + * @param + * the type of the annotated type + * @param annotatedType + * the annotated type whose fields and setters the inject annotation should be added to + * @param builder + * the builder that should be used to add the annotation. + * @see #shouldInjectionAnnotationBeAddedToMember(AnnotatedMember) + */ + public static void addInjectAnnotation(final AnnotatedType annotatedType, AnnotatedTypeBuilder builder) { + for (AnnotatedField field : annotatedType.getFields()) { + if (shouldInjectionAnnotationBeAddedToMember(field)) { + builder.addToField(field, INJECT_ANNOTATION); + } + } + for (AnnotatedMethod method : annotatedType.getMethods()) { + if (shouldInjectionAnnotationBeAddedToMember(method)) { + builder.addToMethod(method, INJECT_ANNOTATION); + } + } + } + + +}