You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2014/04/19 19:21:10 UTC

git commit: DELTASPIKE-579 AnnotationUtils

Repository: deltaspike
Updated Branches:
  refs/heads/master cd518d880 -> 4f217a0bf


DELTASPIKE-579 AnnotationUtils


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/4f217a0b
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/4f217a0b
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/4f217a0b

Branch: refs/heads/master
Commit: 4f217a0bff420f119d8584efc50444445d1433a6
Parents: cd518d8
Author: gpetracek <gp...@apache.org>
Authored: Sat Apr 19 19:19:49 2014 +0200
Committer: gpetracek <gp...@apache.org>
Committed: Sat Apr 19 19:19:49 2014 +0200

----------------------------------------------------------------------
 .../deltaspike/core/util/AnnotationUtils.java   | 78 ++++++++++++++++++++
 .../transaction/TransactionStrategyHelper.java  | 41 +---------
 2 files changed, 81 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4f217a0b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AnnotationUtils.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AnnotationUtils.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AnnotationUtils.java
new file mode 100644
index 0000000..49267a7
--- /dev/null
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AnnotationUtils.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.deltaspike.core.util;
+
+import javax.enterprise.inject.Typed;
+import javax.enterprise.inject.spi.BeanManager;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+
+@Typed()
+public abstract class AnnotationUtils
+{
+    private AnnotationUtils()
+    {
+        // prevent instantiation
+    }
+
+    public static <T extends Annotation> T extractAnnotationFromMethodOrClass(
+        BeanManager beanManager, Method targetMethod, Class<T> targetAnnotationType)
+    {
+        T result = extractAnnotationFromMethod(beanManager, targetMethod, targetAnnotationType);
+
+        if (result == null)
+        {
+            //see DELTASPIKE-517
+            Class targetClass = ProxyUtils.getUnproxiedClass(targetMethod.getDeclaringClass());
+
+            // and if not found search on the class
+            result = findAnnotation(beanManager, targetClass.getAnnotations(), targetAnnotationType);
+        }
+        return result;
+    }
+
+    public static <T extends Annotation> T extractAnnotationFromMethod(
+        BeanManager beanManager, Method targetMethod, Class<T> targetAnnotationType)
+    {
+        return findAnnotation(beanManager, targetMethod.getAnnotations(), targetAnnotationType);
+    }
+
+    public static  <T extends Annotation> T findAnnotation(
+            BeanManager beanManager, Annotation[] annotations, Class<T> targetAnnotationType)
+    {
+        for (Annotation annotation : annotations)
+        {
+            if (targetAnnotationType.equals(annotation.annotationType()))
+            {
+                return (T) annotation;
+            }
+            if (beanManager.isStereotype(annotation.annotationType()))
+            {
+                T result = findAnnotation(
+                        beanManager, annotation.annotationType().getAnnotations(), targetAnnotationType);
+                if (result != null)
+                {
+                    return result;
+                }
+            }
+        }
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4f217a0b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/TransactionStrategyHelper.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/TransactionStrategyHelper.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/TransactionStrategyHelper.java
index 9025139..c6ec253 100644
--- a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/TransactionStrategyHelper.java
+++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/transaction/TransactionStrategyHelper.java
@@ -18,7 +18,7 @@
  */
 package org.apache.deltaspike.jpa.impl.transaction;
 
-import org.apache.deltaspike.core.util.ProxyUtils;
+import org.apache.deltaspike.core.util.AnnotationUtils;
 import org.apache.deltaspike.jpa.api.transaction.Transactional;
 
 import javax.enterprise.context.Dependent;
@@ -154,42 +154,7 @@ public class TransactionStrategyHelper implements Serializable
      */
     protected Transactional extractTransactionalAnnotation(InvocationContext context)
     {
-        // try to detect the interceptor on the method
-        Transactional transactionalAnnotation = extractTransactionalAnnotation(context.getMethod().getAnnotations());
-
-        if (transactionalAnnotation == null)
-        {
-            Class targetClass = ProxyUtils.getUnproxiedClass(context.getTarget().getClass()); //see DELTASPIKE-517
-
-            // and if not found search on the class
-            transactionalAnnotation = extractTransactionalAnnotation(targetClass.getAnnotations());
-        }
-        return transactionalAnnotation;
-    }
-
-    /**
-     * @return a &#064;Transactional annotation extracted from the list of given annotations
-     *         or <code>null</code> if none present.
-     */
-    private Transactional extractTransactionalAnnotation(Annotation[] annotations)
-    {
-        for (Annotation annotation : annotations)
-        {
-            if (Transactional.class.equals(annotation.annotationType()))
-            {
-                return (Transactional) annotation;
-            }
-            if (beanManager.isStereotype(annotation.annotationType()))
-            {
-                Transactional transactionalAnnotation =
-                        extractTransactionalAnnotation(annotation.annotationType().getAnnotations());
-                if (transactionalAnnotation != null)
-                {
-                    return transactionalAnnotation;
-                }
-            }
-        }
-
-        return null;
+        return AnnotationUtils
+            .extractAnnotationFromMethodOrClass(this.beanManager, context.getMethod(), Transactional.class);
     }
 }