You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by tv...@apache.org on 2016/11/17 19:47:28 UTC

svn commit: r1770272 - in /turbine/core/trunk/src/java/org/apache/turbine: annotation/AnnotationProcessor.java modules/ActionEvent.java

Author: tv
Date: Thu Nov 17 19:47:28 2016
New Revision: 1770272

URL: http://svn.apache.org/viewvc?rev=1770272&view=rev
Log:
Generalize annotation caching

Modified:
    turbine/core/trunk/src/java/org/apache/turbine/annotation/AnnotationProcessor.java
    turbine/core/trunk/src/java/org/apache/turbine/modules/ActionEvent.java

Modified: turbine/core/trunk/src/java/org/apache/turbine/annotation/AnnotationProcessor.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/annotation/AnnotationProcessor.java?rev=1770272&r1=1770271&r2=1770272&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/annotation/AnnotationProcessor.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/annotation/AnnotationProcessor.java Thu Nov 17 19:47:28 2016
@@ -22,6 +22,7 @@ package org.apache.turbine.annotation;
 
 
 import java.lang.annotation.Annotation;
+import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.Field;
 import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
@@ -54,6 +55,29 @@ public class AnnotationProcessor
     private static ConcurrentMap<String, Annotation[]> annotationCache = new ConcurrentHashMap<String, Annotation[]>();
 
     /**
+     * Get cached annotations for field, class or method
+     *
+     * @param object a field, class or method
+     *
+     * @return the declared annotations for the object
+     */
+    public static Annotation[] getAnnotations(AccessibleObject object)
+    {
+        String key = object.getClass() + object.toString();
+        Annotation[] annotations = annotationCache.get(key);
+        if (annotations == null)
+        {
+            Annotation[] newAnnotations = object.getDeclaredAnnotations();
+            annotations = annotationCache.putIfAbsent(key, newAnnotations);
+            if (annotations == null)
+            {
+                annotations = newAnnotations;
+            }
+        }
+        return annotations;
+    }
+
+    /**
      * Search for annotated fields of the object and provide them with the
      * appropriate TurbineService
      *
@@ -73,17 +97,7 @@ public class AnnotationProcessor
 
             for (Field field : fields)
             {
-                String key = field.toString();
-                Annotation[] annotations = annotationCache.get(key);
-                if (annotations == null)
-                {
-                    Annotation[] newAnnotations = field.getDeclaredAnnotations();
-                    annotations = annotationCache.putIfAbsent(key, newAnnotations);
-                    if (annotations == null)
-                    {
-                        annotations = newAnnotations;
-                    }
-                }
+                Annotation[] annotations = getAnnotations(field);
 
                 for (Annotation a : annotations)
                 {

Modified: turbine/core/trunk/src/java/org/apache/turbine/modules/ActionEvent.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/modules/ActionEvent.java?rev=1770272&r1=1770271&r2=1770272&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/modules/ActionEvent.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/modules/ActionEvent.java Thu Nov 17 19:47:28 2016
@@ -1,5 +1,7 @@
 package org.apache.turbine.modules;
 
+import java.lang.annotation.Annotation;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -31,6 +33,7 @@ import org.apache.fulcrum.parser.Paramet
 import org.apache.fulcrum.parser.ValueParser.URLCaseFolding;
 import org.apache.turbine.Turbine;
 import org.apache.turbine.TurbineConstants;
+import org.apache.turbine.annotation.AnnotationProcessor;
 import org.apache.turbine.annotation.TurbineActionEvent;
 import org.apache.turbine.annotation.TurbineConfiguration;
 import org.apache.turbine.pipeline.PipelineData;
@@ -136,17 +139,23 @@ public abstract class ActionEvent extend
 	    {
 	        // Try annotations of public methods
 	        Method[] methods = getClass().getMethods();
+
+        methodLoop:
 	        for (Method m : methods)
 	        {
-	            if (m.isAnnotationPresent(TurbineActionEvent.class))
+	            Annotation[] annotations = AnnotationProcessor.getAnnotations(m);
+	            for (Annotation a : annotations)
 	            {
-	                TurbineActionEvent tae = m.getAnnotation(TurbineActionEvent.class);
-	                if (name.equals(pp.convert(tae.value()))
-                        && Arrays.equals(signature, m.getParameterTypes()))
-	                {
-	                    method = m;
-	                    break;
-	                }
+    	            if (a instanceof TurbineActionEvent)
+    	            {
+    	                TurbineActionEvent tae = (TurbineActionEvent) a;
+    	                if (name.equals(pp.convert(tae.value()))
+                            && Arrays.equals(signature, m.getParameterTypes()))
+    	                {
+    	                    method = m;
+    	                    break methodLoop;
+    	                }
+    	            }
 	            }
 	        }