[jboss-cvs] JBossAS SVN: r90056 - in projects/jboss-mdr/trunk/src: test/java/org/jboss/test/metadata/loader/reflection/test and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Jun 10 11:31:21 EDT 2009
Author: jaikiran
Date: 2009-06-10 11:31:21 -0400 (Wed, 10 Jun 2009)
New Revision: 90056
Added:
projects/jboss-mdr/trunk/src/test/java/org/jboss/test/metadata/loader/reflection/test/AnnotatedElementMetadataLoaderTestCase.java
Modified:
projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/loader/reflection/AnnotatedElementMetaDataLoader.java
projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/loader/reflection/SecurityActions.java
Log:
JBMDR-56 Improved the way DeclaredMethodSignature was handled - instead of getting all methods and then looking for the right one, just lookup the correct method with the information available in DeclaredMethodSignature
Modified: projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/loader/reflection/AnnotatedElementMetaDataLoader.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/loader/reflection/AnnotatedElementMetaDataLoader.java 2009-06-10 15:11:59 UTC (rev 90055)
+++ projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/loader/reflection/AnnotatedElementMetaDataLoader.java 2009-06-10 15:31:21 UTC (rev 90056)
@@ -162,14 +162,17 @@
clazz = getDeclaringClass(clazz, methodSignature.getDeclaringClass());
if (clazz == null)
return null;
- method = SecurityActions.findMethod(clazz, signature.getName(), signature.getParametersTypes(clazz));
+ try
+ {
+ method = SecurityActions.findDeclaredMethod(clazz, signature.getName(), signature.getParametersTypes(clazz));
+ }
+ catch (NoSuchMethodException nsme)
+ {
+ if (log.isTraceEnabled())
+ log.trace("Method with signature " + signature + " does not exist on class " + clazz.getName());
+ return null;
+ }
}
- if (method == null)
- {
- if (log.isTraceEnabled())
- log.trace("Method with signature " + signature + " does not exist on class " + clazz.getName());
- return null;
- }
return new AnnotatedElementMetaDataLoader(method);
}
else if (signature instanceof MethodParametersSignature)
Modified: projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/loader/reflection/SecurityActions.java
===================================================================
--- projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/loader/reflection/SecurityActions.java 2009-06-10 15:11:59 UTC (rev 90055)
+++ projects/jboss-mdr/trunk/src/main/java/org/jboss/metadata/plugins/loader/reflection/SecurityActions.java 2009-06-10 15:31:21 UTC (rev 90056)
@@ -26,6 +26,8 @@
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
import org.jboss.reflect.plugins.introspection.ReflectionUtils;
@@ -50,7 +52,7 @@
SecurityManager sm = System.getSecurityManager();
if (sm == null)
return ReflectionUtils.findMethod(clazz, name, parameterTypes);
-
+
return AccessController.doPrivileged(new PrivilegedAction<Method>()
{
public Method run()
@@ -60,7 +62,6 @@
});
}
-
/**
* Find the field by name.
*
@@ -73,7 +74,7 @@
SecurityManager sm = System.getSecurityManager();
if (sm == null)
return ReflectionUtils.findField(clazz, name);
-
+
return AccessController.doPrivileged(new PrivilegedAction<Field>()
{
public Field run()
@@ -95,7 +96,7 @@
SecurityManager sm = System.getSecurityManager();
if (sm == null)
return ReflectionUtils.findConstructor(clazz, parameterTypes);
-
+
return AccessController.doPrivileged(new PrivilegedAction<Constructor<?>>()
{
public Constructor<?> run()
@@ -104,4 +105,40 @@
}
});
}
+
+ /**
+ * Returns a method named <code>methodName</code> with params
+ * <code>parameterTypes</code> declared in the <code>declaringClass</code>.
+ * Methods with public, private, protected and package access are considered while
+ *
+ * @param declaringClass The Class declaring the method
+ * @param methodName The name of the method
+ * @param parameterTypes The parameters types accepted by the method
+ * @return
+ * @throws NoSuchMethodException If the method is not found in the <code>declaringClass</code>
+ */
+ static Method findDeclaredMethod(final Class<?> declaringClass, final String methodName,
+ final Class<?>... parameterTypes) throws NoSuchMethodException
+ {
+ SecurityManager sm = System.getSecurityManager();
+ if (sm == null)
+ return declaringClass.getDeclaredMethod(methodName, parameterTypes);
+
+ try
+ {
+ return AccessController.doPrivileged(new PrivilegedExceptionAction<Method>()
+ {
+ public Method run() throws NoSuchMethodException
+ {
+ return declaringClass.getDeclaredMethod(methodName, parameterTypes);
+ }
+
+ });
+ }
+ catch (PrivilegedActionException pae)
+ {
+ throw (NoSuchMethodException) pae.getException();
+ }
+
+ }
}
Added: projects/jboss-mdr/trunk/src/test/java/org/jboss/test/metadata/loader/reflection/test/AnnotatedElementMetadataLoaderTestCase.java
===================================================================
--- projects/jboss-mdr/trunk/src/test/java/org/jboss/test/metadata/loader/reflection/test/AnnotatedElementMetadataLoaderTestCase.java (rev 0)
+++ projects/jboss-mdr/trunk/src/test/java/org/jboss/test/metadata/loader/reflection/test/AnnotatedElementMetadataLoaderTestCase.java 2009-06-10 15:31:21 UTC (rev 90056)
@@ -0,0 +1,145 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.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.test.metadata.AbstractMetaDataTest;
+import org.jboss.test.metadata.loader.reflection.support.MethodBean;
+import org.jboss.test.metadata.loader.reflection.support.NoAnnotationBean;
+
+/**
+ * AnnotatedElementMetadataLoaderTestCase
+ *
+ * Tests that the {@link AnnotatedElementMetaDataLoader} correctly identifies
+ * the presence of annotations on beans
+ *
+ * @author Jaikiran Pai
+ * @version $Revision: $
+ */
+public class AnnotatedElementMetadataLoaderTestCase extends AbstractMetaDataTest
+{
+
+ /**
+ * Constructor
+ * @param name
+ */
+ public AnnotatedElementMetadataLoaderTestCase(String name)
+ {
+ super(name);
+
+ }
+
+ /**
+ * Test that a class having no annotations is successfully processed by the
+ * {@link AnnotatedElementMetaDataLoader}. The {@link AnnotatedElementMetaDataLoader} is
+ * expected to find *no* annotations on that class
+ *
+ * @throws Exception
+ */
+ public void testNoAnnotationPresent() throws Exception
+ {
+ // The NoAnnotationBean does not have any annotations
+ AnnotatedElementMetaDataLoader annotatedElementLoader = new AnnotatedElementMetaDataLoader(NoAnnotationBean.class);
+ MetaData metadata = new MetaDataRetrievalToMetaDataBridge(annotatedElementLoader);
+ // should be empty array since the bean has no annotations
+ Annotation[] annotations = metadata.getAnnotations();
+ assertEmpty(annotations);
+ }
+
+ /**
+ * Tests that the {@link AnnotatedElementMetaDataLoader} correctly identifies
+ * the presence of annotations on methods when a {@link DeclaredMethodSignature}
+ * is used to create the {@link AnnotatedElementMetaDataLoader}
+ *
+ * @throws Exception
+ */
+ public void testMethodLevelAnnotationsWithDeclaredMethodSignature() throws Exception
+ {
+ AnnotatedElementMetaDataLoader annotatedElementLoader = new AnnotatedElementMetaDataLoader(MethodBean.class);
+ // Create a DeclaredMethodSignature for the method with 1 annotation
+ Method methodWithOneAnnotation = MethodBean.class.getMethod("testAnnotation", new Class<?>[] {String.class});
+ DeclaredMethodSignature declaredMethodSignature = new DeclaredMethodSignature(methodWithOneAnnotation);
+ // create a retrieval out of it
+ MetaDataRetrieval retrieval = annotatedElementLoader.getComponentMetaDataRetrieval(declaredMethodSignature);
+ MetaData metadata = new MetaDataRetrievalToMetaDataBridge(retrieval);
+ // should be empty array since the bean has no annotations
+ Annotation[] annotations = metadata.getAnnotations();
+ assertTrue("Expected one annotation on " + methodWithOneAnnotation, annotations.length == 1);
+
+ // Now try the same on a method which has 2 annotations
+ Method methodWithTwoAnnotations = MethodBean.class.getMethod("testAnnotation12", new Class<?>[] {String.class, Class.class});
+ DeclaredMethodSignature anotherDeclaredMethodSignature = new DeclaredMethodSignature(methodWithTwoAnnotations);
+ // create a retrieval out of it
+ MetaDataRetrieval anotherRetrieval = annotatedElementLoader.getComponentMetaDataRetrieval(anotherDeclaredMethodSignature);
+ MetaData anotherMetadata = new MetaDataRetrievalToMetaDataBridge(anotherRetrieval);
+ // should be empty array since the bean has no annotations
+ Annotation[] annotationsOnMethodWithTwoAnnotations = anotherMetadata.getAnnotations();
+ assertTrue("Expected two annotations on " + anotherDeclaredMethodSignature, annotationsOnMethodWithTwoAnnotations.length == 2);
+
+
+ }
+
+ /**
+ * Tests that the {@link AnnotatedElementMetaDataLoader} correctly identifies
+ * the presence of annotations on methods when a {@link MethodSignature}
+ * is used to create the {@link AnnotatedElementMetaDataLoader}
+ *
+ * @throws Exception
+ */
+ public void testMethodLevelAnnotationsWithMethodSignature() throws Exception
+ {
+ AnnotatedElementMetaDataLoader annotatedElementLoader = new AnnotatedElementMetaDataLoader(MethodBean.class);
+ // Create a MethodSignature for the method with 1 annotation
+ MethodSignature methodSignature = new MethodSignature("testAnnotation", new Class<?>[] {String.class});
+ // create a retrieval out of it
+ MetaDataRetrieval retrieval = annotatedElementLoader.getComponentMetaDataRetrieval(methodSignature);
+ MetaData metadata = new MetaDataRetrievalToMetaDataBridge(retrieval);
+ // should be empty array since the bean has no annotations
+ Annotation[] annotations = metadata.getAnnotations();
+ assertTrue("Expected one annotation on testAnnotation method of " + MethodBean.class, annotations.length == 1);
+
+ // Now try the same on a method which has 2 annotations
+ MethodSignature anotherMethodSignature = new MethodSignature("testAnnotation12", new Class<?>[] {String.class, Class.class});
+ // create a retrieval out of it
+ MetaDataRetrieval anotherRetrieval = annotatedElementLoader.getComponentMetaDataRetrieval(anotherMethodSignature);
+ MetaData anotherMetadata = new MetaDataRetrievalToMetaDataBridge(anotherRetrieval);
+ // should be empty array since the bean has no annotations
+ Annotation[] annotationsOnMethodWithTwoAnnotations = anotherMetadata.getAnnotations();
+ assertTrue("Expected two annotations on testAnnotation12 method of " + MethodBean.class, annotationsOnMethodWithTwoAnnotations.length == 2);
+
+
+ }
+
+
+
+
+
+}
More information about the jboss-cvs-commits
mailing list