[jboss-cvs] JBossAS SVN: r104150 - in projects/jboss-mdr/branches/Branch_2_0/src: test/java/org/jboss/test/metadata/loader/reflection/support and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Apr 22 09:36:49 EDT 2010


Author: kabir.khan at jboss.com
Date: 2010-04-22 09:36:47 -0400 (Thu, 22 Apr 2010)
New Revision: 104150

Added:
   projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/metadata/loader/reflection/support/BridgeInterface.java
   projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/metadata/loader/reflection/support/BridgeMethodBean.java
Modified:
   projects/jboss-mdr/branches/Branch_2_0/src/main/java/org/jboss/metadata/plugins/loader/reflection/AnnotatedElementMetaDataLoader.java
   projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/metadata/loader/reflection/test/AnnotatedElementMetadataLoaderTestCase.java
Log:
[JBMDR-69] Copy annotations for bridge methods from source method if it can be determined

Modified: projects/jboss-mdr/branches/Branch_2_0/src/main/java/org/jboss/metadata/plugins/loader/reflection/AnnotatedElementMetaDataLoader.java
===================================================================
--- projects/jboss-mdr/branches/Branch_2_0/src/main/java/org/jboss/metadata/plugins/loader/reflection/AnnotatedElementMetaDataLoader.java	2010-04-22 13:36:08 UTC (rev 104149)
+++ projects/jboss-mdr/branches/Branch_2_0/src/main/java/org/jboss/metadata/plugins/loader/reflection/AnnotatedElementMetaDataLoader.java	2010-04-22 13:36:47 UTC (rev 104150)
@@ -21,13 +21,6 @@
 */
 package org.jboss.metadata.plugins.loader.reflection;
 
-import java.lang.annotation.Annotation;
-import java.lang.reflect.AnnotatedElement;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.Field;
-import java.lang.reflect.Member;
-import java.lang.reflect.Method;
-
 import org.jboss.logging.Logger;
 import org.jboss.metadata.plugins.loader.BasicMetaDataLoader;
 import org.jboss.metadata.plugins.loader.SimpleMetaDataLoader;
@@ -39,16 +32,16 @@
 import org.jboss.metadata.spi.scope.CommonLevels;
 import org.jboss.metadata.spi.scope.Scope;
 import org.jboss.metadata.spi.scope.ScopeKey;
-import org.jboss.metadata.spi.signature.ConstructorParametersSignature;
-import org.jboss.metadata.spi.signature.ConstructorSignature;
-import org.jboss.metadata.spi.signature.DeclaredMethodSignature;
-import org.jboss.metadata.spi.signature.FieldSignature;
-import org.jboss.metadata.spi.signature.MethodParametersSignature;
-import org.jboss.metadata.spi.signature.MethodSignature;
-import org.jboss.metadata.spi.signature.Signature;
+import org.jboss.metadata.spi.signature.*;
 import org.jboss.util.JBossStringBuilder;
 import org.jboss.util.Strings;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.*;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
 /**
  * AnnotatedElementMetaDataLoader.
  * 
@@ -151,6 +144,15 @@
                   log.trace("Method with signature " + signature + " does not exist on class " + clazz.getName());
                return null;
             }
+            if (method.getAnnotations().length == 0)
+            {
+               if (method.isBridge())
+               {
+                  method = searchForRealBridgeMethodSignature(method);
+                  if (method == null)
+                     return null;
+               }
+            }
             return new AnnotatedElementMetaDataLoader(method);
          }
          else if (signature instanceof DeclaredMethodSignature)
@@ -173,6 +175,15 @@
                   return null;     
                }
             }
+            if (method.getAnnotations().length == 0)
+            {
+               if (method.isBridge())
+               {
+                  method = searchForRealBridgeMethodSignature(method);
+                  if (method == null)
+                     return null;
+               }
+            }
             return new AnnotatedElementMetaDataLoader(method);
          }
          else if (signature instanceof MethodParametersSignature)
@@ -280,4 +291,57 @@
       }
       return null;
    }
+   
+   private Method searchForRealBridgeMethodSignature(Method bridge)
+   {
+      List<Method> matching = new ArrayList<Method>();
+      Method[] all = bridge.getDeclaringClass().getDeclaredMethods();
+      for (int i = 0 ; i < all.length ; i++)
+      {
+         if (all[i].getName().equals(bridge.getName()) &&
+               all[i].getParameterTypes().length == bridge.getParameterTypes().length &&
+               !all[i].equals(bridge) &&
+               !all[i].isBridge())
+            matching.add(all[i]);
+      }
+      
+      if (matching.size() == 1)
+         return matching.get(0);
+      
+      //Should not happen
+      if (matching.size() == 0)
+         throw new IllegalStateException("No original methods found");
+      
+      for (Iterator<Method> it = matching.iterator() ; it.hasNext() ; )
+      {
+         Method cur = it.next();
+         if (!bridge.getReturnType().isAssignableFrom(cur.getReturnType()))
+         {
+            it.remove();
+            continue;
+         }
+         
+         
+         for (int i = 0 ; i < bridge.getParameterTypes().length ; i++)
+         {
+            if (!bridge.getParameterTypes()[i].isAssignableFrom(cur.getParameterTypes()[i]))
+            {
+               it.remove();
+               continue;
+            }
+         }
+      }
+
+      if (matching.size() == 1)
+         return matching.get(0);
+       
+      //Should not happen
+      if (matching.size() == 0)
+         throw new IllegalStateException("No original methods found");
+
+      if (log.isTraceEnabled())
+         log.trace("Could not determine original method for " + bridge + " found: " + matching);
+      
+      return null;
+   }
 }

Added: projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/metadata/loader/reflection/support/BridgeInterface.java
===================================================================
--- projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/metadata/loader/reflection/support/BridgeInterface.java	                        (rev 0)
+++ projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/metadata/loader/reflection/support/BridgeInterface.java	2010-04-22 13:36:47 UTC (rev 104150)
@@ -0,0 +1,32 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright (c) 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.metadata.loader.reflection.support;
+
+/**
+ * @author <a href="cdewolf at redhat.com">Carlo de Wolf</a>
+ */
+public interface BridgeInterface<T>
+{
+   T unambiguous(T t);
+   
+   T ambiguous(T t);
+}

Added: projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/metadata/loader/reflection/support/BridgeMethodBean.java
===================================================================
--- projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/metadata/loader/reflection/support/BridgeMethodBean.java	                        (rev 0)
+++ projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/metadata/loader/reflection/support/BridgeMethodBean.java	2010-04-22 13:36:47 UTC (rev 104150)
@@ -0,0 +1,48 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright (c) 2010, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.metadata.loader.reflection.support;
+
+import org.jboss.test.metadata.shared.support.TestAnnotation;
+
+/**
+ * @author <a href="cdewolf at redhat.com">Carlo de Wolf</a>
+ */
+public class BridgeMethodBean implements BridgeInterface<String>
+{
+   @TestAnnotation
+   public String unambiguous(String t)
+   {
+      throw new RuntimeException("NYI: org.jboss.test.metadata.loader.reflection.support.BridgedMethodBean.unambiguous");
+   }
+   
+   @TestAnnotation
+   public String ambiguous(String t)
+   {
+      throw new RuntimeException("NYI: org.jboss.test.metadata.loader.reflection.support.BridgedMethodBean.ambiguous");
+   }
+   
+   @TestAnnotation
+   public Long ambiguous(Long t)
+   {
+      throw new RuntimeException("NYI: org.jboss.test.metadata.loader.reflection.support.BridgedMethodBean.ambiguous");
+   }
+}

Modified: projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/metadata/loader/reflection/test/AnnotatedElementMetadataLoaderTestCase.java
===================================================================
--- projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/metadata/loader/reflection/test/AnnotatedElementMetadataLoaderTestCase.java	2010-04-22 13:36:08 UTC (rev 104149)
+++ projects/jboss-mdr/branches/Branch_2_0/src/test/java/org/jboss/test/metadata/loader/reflection/test/AnnotatedElementMetadataLoaderTestCase.java	2010-04-22 13:36:47 UTC (rev 104150)
@@ -22,19 +22,21 @@
 
 package org.jboss.test.metadata.loader.reflection.test;
 
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
-
 import org.jboss.metadata.plugins.loader.reflection.AnnotatedElementMetaDataLoader;
 import org.jboss.metadata.spi.MetaData;
 import org.jboss.metadata.spi.retrieval.MetaDataRetrieval;
 import org.jboss.metadata.spi.retrieval.MetaDataRetrievalToMetaDataBridge;
 import org.jboss.metadata.spi.signature.DeclaredMethodSignature;
 import org.jboss.metadata.spi.signature.MethodSignature;
+import org.jboss.metadata.spi.signature.Signature;
 import org.jboss.test.metadata.AbstractMetaDataTest;
+import org.jboss.test.metadata.loader.reflection.support.BridgeMethodBean;
 import org.jboss.test.metadata.loader.reflection.support.MethodBean;
 import org.jboss.test.metadata.loader.reflection.support.NoAnnotationBean;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+
 /**
  * AnnotatedElementMetadataLoaderTestCase
  *
@@ -134,4 +136,50 @@
       Annotation[] annotationsOnMethodWithTwoAnnotations = anotherMetadata.getAnnotations();
       assertTrue("Expected two annotations on testAnnotation12 method of " + MethodBean.class, annotationsOnMethodWithTwoAnnotations.length == 2);
    }
+
+   /**
+    * Tests that the {@link AnnotatedElementMetaDataLoader} correctly identifies
+    * bridge methods.
+    */
+   public void testMethodLevelAnnotationOnBridgeMethod() throws Exception
+   {
+      AnnotatedElementMetaDataLoader annotatedElementLoader = new AnnotatedElementMetaDataLoader(BridgeMethodBean.class);
+      
+      Method method = BridgeMethodBean.class.getMethod("unambiguous", Object.class);
+      assertTrue(method.isBridge());
+      MethodSignature methodSignature = new MethodSignature(method);
+      MetaDataRetrieval retrieval = annotatedElementLoader.getComponentMetaDataRetrieval(methodSignature);
+      assertNotNull("Expected a MetaDataRetrieval for method " + method, retrieval);
+      MetaData metadata = new MetaDataRetrievalToMetaDataBridge(retrieval);
+      Annotation[] annotations = metadata.getAnnotations();
+      assertTrue("Expected one annotation on unambiguous method of " + BridgeMethodBean.class, annotations.length == 1);
+      
+      method = BridgeMethodBean.class.getMethod("ambiguous", Object.class);
+      assertTrue(method.isBridge());
+      methodSignature = new MethodSignature(method);
+      assertNull(annotatedElementLoader.getComponentMetaDataRetrieval(methodSignature));
+   }
+
+   /**
+    * Tests that the {@link AnnotatedElementMetaDataLoader} correctly identifies
+    * bridge methods.
+    */
+   public void testMethodLevelAnnotationOnDeclaredBridgeMethod() throws Exception
+   {
+      AnnotatedElementMetaDataLoader annotatedElementLoader = new AnnotatedElementMetaDataLoader(BridgeMethodBean.class);
+
+      Method method = BridgeMethodBean.class.getMethod("unambiguous", Object.class);
+      assertTrue(method.isBridge());
+      Signature methodSignature = new DeclaredMethodSignature(method);
+      MetaDataRetrieval retrieval = annotatedElementLoader.getComponentMetaDataRetrieval(methodSignature);
+      assertNotNull("Expected a MetaDataRetrieval for method " + method, retrieval);
+      MetaData metadata = new MetaDataRetrievalToMetaDataBridge(retrieval);
+      Annotation[] annotations = metadata.getAnnotations();
+      assertTrue("Expected one annotation on unambiguous method of " + BridgeMethodBean.class, annotations.length == 1);
+
+      method = BridgeMethodBean.class.getMethod("ambiguous", Object.class);
+      assertTrue(method.isBridge());
+      methodSignature = new DeclaredMethodSignature(method);
+      assertNull(annotatedElementLoader.getComponentMetaDataRetrieval(methodSignature));
+   }
 }




More information about the jboss-cvs-commits mailing list