Author: dkulp
Date: Fri Jun 13 08:51:46 2008
New Revision: 667575
URL: http://svn.apache.org/viewvc?rev=667575&view=rev
Log:
[CXF-1645] Simplify the target method aquisition for proxies
Also remove reflection from the AnnotationProcessor
Modified:
cxf/trunk/common/common/src/main/java/org/apache/cxf/common/annotation/AnnotationProcessor.java
cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java
cxf/trunk/common/common/src/test/java/org/apache/cxf/common/annotation/AnnotationProcessorTest.java
Modified:
cxf/trunk/common/common/src/main/java/org/apache/cxf/common/annotation/AnnotationProcessor.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/annotation/AnnotationProcessor.java?rev=667575&r1=667574&r2=667575&view=diff
==============================================================================
---
cxf/trunk/common/common/src/main/java/org/apache/cxf/common/annotation/AnnotationProcessor.java
(original)
+++
cxf/trunk/common/common/src/main/java/org/apache/cxf/common/annotation/AnnotationProcessor.java
Fri Jun 13 08:51:46 2008
@@ -20,12 +20,9 @@
package org.apache.cxf.common.annotation;
import java.lang.annotation.Annotation;
-import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
-import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cxf.common.i18n.Message;
@@ -42,23 +39,6 @@
private static final Logger LOG =
LogUtils.getL7dLogger(AnnotationProcessor.class);
- private static Method visitClassMethod;
- private static Method visitFieldMethod;
- private static Method visitMethodMethod;
-
- static {
- try {
- visitClassMethod = AnnotationVisitor.class.getMethod("visitClass",
Class.class, Annotation.class);
- visitFieldMethod = AnnotationVisitor.class.getMethod("visitField",
Field.class, Annotation.class);
- visitMethodMethod =
AnnotationVisitor.class.getMethod("visitMethod",
-
Method.class, Annotation.class);
-
- } catch (NoSuchMethodException e) {
- // ignore
- }
-
- }
-
private final Object target;
private List<Class<? extends Annotation>> annotationTypes;
@@ -100,14 +80,28 @@
if (targetClass.getSuperclass() != null) {
processMethods(visitor, targetClass.getSuperclass());
}
- visitAnnotatedElement(targetClass.getDeclaredMethods(), visitor,
visitMethodMethod);
+ for (Method element : targetClass.getDeclaredMethods()) {
+ for (Class<? extends Annotation> clz : annotationTypes) {
+ Annotation ann = element.getAnnotation(clz);
+ if (ann != null) {
+ visitor.visitMethod(element, ann);
+ }
+ }
+ }
}
private void processFields(AnnotationVisitor visitor, Class<? extends
Object> targetClass) {
if (targetClass.getSuperclass() != null) {
processFields(visitor, targetClass.getSuperclass());
}
- visitAnnotatedElement(targetClass.getDeclaredFields(), visitor,
visitFieldMethod);
+ for (Field element : targetClass.getDeclaredFields()) {
+ for (Class<? extends Annotation> clz : annotationTypes) {
+ Annotation ann = element.getAnnotation(clz);
+ if (ann != null) {
+ visitor.visitField(element, ann);
+ }
+ }
+ }
}
@@ -115,28 +109,11 @@
if (targetClass.getSuperclass() != null) {
processClass(visitor, targetClass.getSuperclass());
}
- Class<?>[] classes = {targetClass};
- visitAnnotatedElement(classes, visitor, visitClassMethod);
- }
-
- private <U extends AnnotatedElement> void visitAnnotatedElement(U[]
elements,
-
AnnotationVisitor visitor,
- Method
visitorMethod) {
-
- for (U element : elements) {
- for (Class<? extends Annotation> clz : annotationTypes) {
- Annotation ann = element.getAnnotation(clz);
- if (ann != null) {
- try {
- visitorMethod.invoke(visitor, element, ann);
- } catch (IllegalAccessException e) {
- // ignore, we're invoking methods of a public interface
- } catch (InvocationTargetException e) {
- Throwable cause = e.getCause() == null ? e :
e.getCause();
- LogUtils.log(LOG, Level.SEVERE,
"VISITOR_RAISED_EXCEPTION", cause, visitor, element);
- }
- }
+ for (Class<? extends Annotation> clz : annotationTypes) {
+ Annotation ann = targetClass.getAnnotation(clz);
+ if (ann != null) {
+ visitor.visitClass(targetClass, ann);
}
}
- }
+ }
}
Modified:
cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java?rev=667575&r1=667574&r2=667575&view=diff
==============================================================================
---
cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java
(original)
+++
cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java
Fri Jun 13 08:51:46 2008
@@ -244,10 +244,8 @@
if
(method.getDeclaringClass().isAssignableFrom(getTarget().getClass())) {
method.invoke(getTarget(), resource);
} else { // deal with the proxy setter method
- Method targetMethod = findMethod(getTarget().getClass(),
- method.getName(),
- resource);
-
+ Method targetMethod =
getTarget().getClass().getMethod(method.getName(),
+
method.getParameterTypes());
targetMethod.invoke(getTarget(), resource);
}
} catch (IllegalAccessException e) {
@@ -261,23 +259,6 @@
}
}
-
- private Method findMethod(Class<? extends Object> class1, String name,
Object resource)
- throws SecurityException, NoSuchMethodException {
- try {
- return class1.getMethod(name, new Class[]{resource.getClass()});
- } catch (NoSuchMethodException e) {
- for (Method m : class1.getMethods()) {
- if (m.getName().equals(name)
- && m.getParameterTypes().length == 1
- && m.getParameterTypes()[0].isInstance(resource)) {
- return m;
- }
- }
- throw e;
- }
- }
-
private String getResourceName(Resource res, Method method) {
assert method != null;
assert res != null;
Modified:
cxf/trunk/common/common/src/test/java/org/apache/cxf/common/annotation/AnnotationProcessorTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/annotation/AnnotationProcessorTest.java?rev=667575&r1=667574&r2=667575&view=diff
==============================================================================
---
cxf/trunk/common/common/src/test/java/org/apache/cxf/common/annotation/AnnotationProcessorTest.java
(original)
+++
cxf/trunk/common/common/src/test/java/org/apache/cxf/common/annotation/AnnotationProcessorTest.java
Fri Jun 13 08:51:46 2008
@@ -85,6 +85,7 @@
Method expectedMethod3 =
AnnotatedGreeterImpl.class.getDeclaredMethod("greetMe", String.class);
Method expectedMethod4 =
AnnotatedGreeterImpl.class.getDeclaredMethod("setContext",
WebServiceContext.class);
+ Method expectedMethod5 =
AnnotatedGreeterImpl.class.getDeclaredMethod("greetMeOneWay", String.class);
expectedAnnotations.add(WebMethod.class);
expectedAnnotations.add(Resource.class);
@@ -100,6 +101,8 @@
(Annotation)EasyMock.isA(WebMethod.class));
visitor.visitMethod(EasyMock.eq(expectedMethod4),
(Annotation)EasyMock.isA(Resource.class));
+ visitor.visitMethod(EasyMock.eq(expectedMethod5),
+ (Annotation)EasyMock.isA(WebMethod.class));
runProcessor(visitor);
}