Skip to content

Commit 8f53b6f

Browse files
committed
WW-4744 WW-4694 Removes annotation search to commons lang 3.6
2 parents fa20b71 + 33e1eeb commit 8f53b6f

File tree

12 files changed

+27
-213
lines changed

12 files changed

+27
-213
lines changed

core/src/main/java/com/opensymphony/xwork2/interceptor/DefaultWorkflowInterceptor.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import org.apache.commons.lang3.reflect.MethodUtils;
2323
import org.apache.logging.log4j.LogManager;
2424
import org.apache.logging.log4j.Logger;
25-
import com.opensymphony.xwork2.util.AnnotationUtils;
26-
import java.lang.reflect.Method;
2725

2826
/**
2927
* <!-- START SNIPPET: description -->
@@ -208,7 +206,8 @@ private String processValidationWorkflowAware(final Object action, final String
208206
*/
209207
protected String processInputConfig(final Object action, final String method, final String currentResultName) throws Exception {
210208
String resultName = currentResultName;
211-
InputConfig annotation = AnnotationUtils.findAnnotation(action.getClass().getMethod(method, EMPTY_CLASS_ARRAY), InputConfig.class);
209+
InputConfig annotation = MethodUtils.getAnnotation(action.getClass().getMethod(method, EMPTY_CLASS_ARRAY),
210+
InputConfig.class ,true,true);
212211
if (annotation != null) {
213212
if (StringUtils.isNotEmpty(annotation.methodName())) {
214213
resultName = (String) MethodUtils.invokeMethod(action, true, annotation.methodName());

core/src/main/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationWorkflowInterceptor.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.opensymphony.xwork2.XWorkException;
2020
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
2121
import com.opensymphony.xwork2.interceptor.PreResultListener;
22-
import com.opensymphony.xwork2.util.AnnotationUtils;
2322
import org.apache.commons.lang3.reflect.MethodUtils;
2423

2524
import java.lang.reflect.Method;
@@ -113,13 +112,15 @@ public class AnnotationWorkflowInterceptor extends AbstractInterceptor implement
113112
public String intercept(ActionInvocation invocation) throws Exception {
114113
final Object action = invocation.getAction();
115114
invocation.addPreResultListener(this);
116-
List<Method> methods = new ArrayList<>(AnnotationUtils.getAnnotatedMethods(action.getClass(), Before.class));
115+
List<Method> methods = new ArrayList<>(MethodUtils.getMethodsListWithAnnotation(action.getClass(), Before.class,
116+
true, true));
117117
if (methods.size() > 0) {
118118
// methods are only sorted by priority
119119
Collections.sort(methods, new Comparator<Method>() {
120120
public int compare(Method method1, Method method2) {
121-
return comparePriorities(AnnotationUtils.findAnnotation(method1, Before.class).priority(),
122-
AnnotationUtils.findAnnotation(method2, Before.class).priority());
121+
return comparePriorities(MethodUtils.getAnnotation(method1, Before.class, true,
122+
true).priority(), MethodUtils.getAnnotation(method2, Before.class, true,
123+
true).priority());
123124
}
124125
});
125126
for (Method m : methods) {
@@ -134,14 +135,16 @@ public int compare(Method method1, Method method2) {
134135
String invocationResult = invocation.invoke();
135136

136137
// invoke any @After methods
137-
methods = new ArrayList<Method>(AnnotationUtils.getAnnotatedMethods(action.getClass(), After.class));
138+
methods = new ArrayList<Method>(MethodUtils.getMethodsListWithAnnotation(action.getClass(), After.class,
139+
true, true));
138140

139141
if (methods.size() > 0) {
140142
// methods are only sorted by priority
141143
Collections.sort(methods, new Comparator<Method>() {
142144
public int compare(Method method1, Method method2) {
143-
return comparePriorities(AnnotationUtils.findAnnotation(method1, After.class).priority(),
144-
AnnotationUtils.findAnnotation(method2, After.class).priority());
145+
return comparePriorities(MethodUtils.getAnnotation(method1, After.class, true,
146+
true).priority(), MethodUtils.getAnnotation(method2, After.class, true,
147+
true).priority());
145148
}
146149
});
147150
for (Method m : methods) {
@@ -169,14 +172,16 @@ protected static int comparePriorities(int val1, int val2) {
169172
*/
170173
public void beforeResult(ActionInvocation invocation, String resultCode) {
171174
Object action = invocation.getAction();
172-
List<Method> methods = new ArrayList<Method>(AnnotationUtils.getAnnotatedMethods(action.getClass(), BeforeResult.class));
175+
List<Method> methods = new ArrayList<Method>(MethodUtils.getMethodsListWithAnnotation(action.getClass(),
176+
BeforeResult.class, true, true));
173177

174178
if (methods.size() > 0) {
175179
// methods are only sorted by priority
176180
Collections.sort(methods, new Comparator<Method>() {
177181
public int compare(Method method1, Method method2) {
178-
return comparePriorities(AnnotationUtils.findAnnotation(method1, BeforeResult.class).priority(),
179-
AnnotationUtils.findAnnotation(method2, BeforeResult.class).priority());
182+
return comparePriorities(MethodUtils.getAnnotation(method1, BeforeResult.class, true,
183+
true).priority(), MethodUtils.getAnnotation(method2, BeforeResult.class,
184+
true, true).priority());
180185
}
181186
});
182187
for (Method m : methods) {

core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java

-113
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,10 @@
1515
*/
1616
package com.opensymphony.xwork2.util;
1717

18-
import org.apache.commons.lang3.ArrayUtils;
19-
import org.apache.commons.lang3.ClassUtils;
20-
2118
import java.lang.annotation.Annotation;
22-
import java.lang.reflect.AnnotatedElement;
2319
import java.lang.reflect.Field;
2420
import java.lang.reflect.Method;
25-
import java.util.ArrayList;
2621
import java.util.Arrays;
27-
import java.util.Collection;
2822
import java.util.List;
2923
import java.util.regex.Matcher;
3024
import java.util.regex.Pattern;
@@ -107,113 +101,6 @@ public static void addAllInterfaces(Class clazz, List<Class> allInterfaces) {
107101
addAllInterfaces(clazz.getSuperclass(), allInterfaces);
108102
}
109103

110-
/**
111-
* For the given <code>Class</code> get a collection of the the {@link AnnotatedElement}s
112-
* that match the given <code>annotation</code>s or if no <code>annotation</code>s are
113-
* specified then return all of the annotated elements of the given <code>Class</code>.
114-
* Includes only the method level annotations.
115-
*
116-
* @param clazz The {@link Class} to inspect
117-
* @param annotation the {@link Annotation}s to find
118-
* @return A {@link Collection}&lt;{@link AnnotatedElement}&gt; containing all of the
119-
* method {@link AnnotatedElement}s matching the specified {@link Annotation}s
120-
* @deprecated Will be removed after release of <a href="https://github.com/apache/commons-lang/pull/261">LANG-1317</a>
121-
*/
122-
@Deprecated
123-
public static Collection<Method> getAnnotatedMethods(Class clazz, Class<? extends Annotation>... annotation) {
124-
List<Class<?>> allSuperclasses = ClassUtils.getAllSuperclasses(clazz);
125-
allSuperclasses.add(0, clazz);
126-
int sci = 0;
127-
List<Class<?>> allInterfaces = ClassUtils.getAllInterfaces(clazz);
128-
int ifi = 0;
129-
final List<Method> annotatedMethods = new ArrayList<>();
130-
while (ifi < allInterfaces.size() ||
131-
sci < allSuperclasses.size()) {
132-
Class<?> acls;
133-
if (ifi >= allInterfaces.size()) {
134-
acls = allSuperclasses.get(sci++);
135-
}
136-
else if (sci >= allSuperclasses.size()) {
137-
acls = allInterfaces.get(ifi++);
138-
}
139-
else if (sci <= ifi) {
140-
acls = allSuperclasses.get(sci++);
141-
}
142-
else {
143-
acls = allInterfaces.get(ifi++);
144-
}
145-
final Method[] allMethods = acls.getDeclaredMethods();
146-
for (final Method method : allMethods) {
147-
if (ArrayUtils.isEmpty(annotation) && ArrayUtils.isNotEmpty(method.getAnnotations())) {
148-
annotatedMethods.add(method);
149-
continue;
150-
}
151-
for (Class<? extends Annotation> c : annotation) {
152-
if (method.getAnnotation(c) != null) {
153-
annotatedMethods.add(method);
154-
}
155-
}
156-
}
157-
}
158-
159-
return annotatedMethods;
160-
}
161-
162-
/**
163-
* <p>BFS to find the annotation object that is present on the given method or any equivalent method in
164-
* super classes and interfaces, with the given annotation type. Returns null if the annotation type was not present
165-
* on any of them.</p>
166-
* @param <A>
167-
* the annotation type
168-
* @param method
169-
* the {@link Method} to query
170-
* @param annotationCls
171-
* the {@link Annotation} to check if is present on the method
172-
* @return an Annotation (possibly null).
173-
* @deprecated Will be removed after release of <a href="https://github.com/apache/commons-lang/pull/261">LANG-1317</a>
174-
*/
175-
@Deprecated
176-
public static <A extends Annotation> A findAnnotation(final Method method, final Class<A> annotationCls) {
177-
A annotation = method.getAnnotation(annotationCls);
178-
179-
if(annotation == null) {
180-
Class<?> mcls = method.getDeclaringClass();
181-
List<Class<?>> allSuperclasses = ClassUtils.getAllSuperclasses(mcls);
182-
int sci = 0;
183-
List<Class<?>> allInterfaces = ClassUtils.getAllInterfaces(mcls);
184-
int ifi = 0;
185-
while (ifi < allInterfaces.size() ||
186-
sci < allSuperclasses.size()) {
187-
Class<?> acls;
188-
if(ifi >= allInterfaces.size()) {
189-
acls = allSuperclasses.get(sci++);
190-
}
191-
else if(sci >= allSuperclasses.size()) {
192-
acls = allInterfaces.get(ifi++);
193-
}
194-
else if(ifi <= sci) {
195-
acls = allInterfaces.get(ifi++);
196-
}
197-
else {
198-
acls = allSuperclasses.get(sci++);
199-
}
200-
Method equivalentMethod = null;
201-
try {
202-
equivalentMethod = acls.getDeclaredMethod(method.getName(), method.getParameterTypes());
203-
} catch (NoSuchMethodException e) {
204-
// If not found, just keep on breadth first search
205-
}
206-
if(equivalentMethod != null) {
207-
annotation = equivalentMethod.getAnnotation(annotationCls);
208-
if(annotation != null) {
209-
break;
210-
}
211-
}
212-
}
213-
}
214-
return annotation;
215-
}
216-
217104
/**
218105
* Returns the property name for a method.
219106
* This method is independent from property fields.

core/src/main/java/org/apache/struts2/components/Component.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
package org.apache.struts2.components;
2323

2424
import com.opensymphony.xwork2.inject.Inject;
25-
import com.opensymphony.xwork2.util.AnnotationUtils;
2625
import com.opensymphony.xwork2.util.TextParseUtil;
2726
import com.opensymphony.xwork2.util.ValueStack;
2827
import org.apache.commons.lang3.BooleanUtils;
28+
import org.apache.commons.lang3.reflect.MethodUtils;
2929
import org.apache.commons.lang3.StringUtils;
3030
import org.apache.logging.log4j.LogManager;
3131
import org.apache.logging.log4j.Logger;
@@ -543,7 +543,8 @@ protected Collection<String> getStandardAttributes() {
543543
Class clz = getClass();
544544
Collection<String> standardAttributes = standardAttributesMap.get(clz);
545545
if (standardAttributes == null) {
546-
Collection<Method> methods = AnnotationUtils.getAnnotatedMethods(clz, StrutsTagAttribute.class);
546+
Collection<Method> methods = MethodUtils.getMethodsListWithAnnotation(clz, StrutsTagAttribute.class,
547+
true, true);
547548
standardAttributes = new HashSet<>(methods.size());
548549
for(Method m : methods) {
549550
standardAttributes.add(StringUtils.uncapitalize(m.getName().substring(3)));

core/src/main/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
import com.opensymphony.xwork2.ActionInvocation;
2525
import com.opensymphony.xwork2.config.ConfigurationException;
26-
import com.opensymphony.xwork2.util.AnnotationUtils;
2726
import com.opensymphony.xwork2.validator.ValidationInterceptor;
27+
import org.apache.commons.lang3.reflect.MethodUtils;
2828
import org.apache.logging.log4j.LogManager;
2929
import org.apache.logging.log4j.Logger;
3030

@@ -44,7 +44,7 @@ protected String doIntercept(ActionInvocation invocation) throws Exception {
4444
if (action != null) {
4545
Method method = getActionMethod(action.getClass(), invocation.getProxy().getMethod());
4646

47-
if (null != AnnotationUtils.findAnnotation(method, SkipValidation.class)) {
47+
if (null != MethodUtils.getAnnotation(method, SkipValidation.class, true, true)) {
4848
return invocation.invoke();
4949
}
5050
}

core/src/test/java/com/opensymphony/xwork2/util/AnnotationUtilsTest.java

-53
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,15 @@
22

33
import com.opensymphony.xwork2.util.annotation.Dummy2Class;
44
import com.opensymphony.xwork2.util.annotation.DummyClass;
5-
import com.opensymphony.xwork2.util.annotation.DummyClassExt;
6-
import com.opensymphony.xwork2.util.annotation.DummyInterface;
75
import com.opensymphony.xwork2.util.annotation.MyAnnotation;
8-
import com.opensymphony.xwork2.util.annotation.MyAnnotation2;
9-
import com.opensymphony.xwork2.util.annotation.MyAnnotationI;
106

117
import junit.framework.TestCase;
128

13-
import java.lang.reflect.AnnotatedElement;
14-
import java.util.Collection;
15-
169
/**
1710
* @author Dan Oxlade, dan d0t oxlade at gmail d0t c0m
1811
*/
1912
public class AnnotationUtilsTest extends TestCase {
2013

21-
public void testFindAnnotationFromSuperclass() throws Exception {
22-
assertNotNull(AnnotationUtils.findAnnotation(DummyClassExt.class.getMethod("methodWithAnnotation"), MyAnnotation.class));
23-
}
24-
25-
public void testFindAnnotationFromInterface() throws Exception {
26-
assertNotNull(AnnotationUtils.findAnnotation(DummyClass.class.getMethod("interfaceMethodWithAnnotation"), MyAnnotationI.class));
27-
}
28-
29-
public void testFindAnnotation() throws Exception {
30-
assertNotNull(AnnotationUtils.findAnnotation(DummyClassExt.class.getMethod("anotherAnnotatedMethod"), MyAnnotation2.class));
31-
}
32-
33-
@SuppressWarnings("unchecked")
34-
public void testGetAnnotatedMethodsIncludingSuperclassAndInterface() throws Exception {
35-
36-
Collection<? extends AnnotatedElement> ans = AnnotationUtils.getAnnotatedMethods(DummyClassExt.class, Deprecated.class, MyAnnotation.class, MyAnnotation2.class, MyAnnotationI.class);
37-
assertEquals(4, ans.size());
38-
}
39-
40-
@SuppressWarnings("unchecked")
41-
public void testGetAnnotatedMethodsWithoutAnnotationArgs() throws Exception {
42-
Collection<? extends AnnotatedElement> ans = AnnotationUtils.getAnnotatedMethods(DummyClass.class);
43-
assertEquals(3, ans.size());
44-
assertTrue(ans.contains(DummyClass.class.getMethod("methodWithAnnotation")));
45-
assertTrue(ans.contains(DummyClass.class.getDeclaredMethod("privateMethodWithAnnotation")));
46-
assertTrue(ans.contains(DummyInterface.class.getDeclaredMethod("interfaceMethodWithAnnotation")));
47-
}
48-
49-
@SuppressWarnings("unchecked")
50-
public void testGetAnnotatedMethodsWithAnnotationArgs() throws Exception {
51-
Collection<? extends AnnotatedElement> ans = AnnotationUtils.getAnnotatedMethods(DummyClass.class, Deprecated.class);
52-
assertTrue(ans.isEmpty());
53-
54-
ans = AnnotationUtils.getAnnotatedMethods(DummyClass.class, Deprecated.class, MyAnnotation.class);
55-
assertEquals(1, ans.size());
56-
57-
ans = AnnotationUtils.getAnnotatedMethods(DummyClass.class, MyAnnotation.class);
58-
assertEquals(1, ans.size());
59-
60-
ans = AnnotationUtils.getAnnotatedMethods(DummyClass.class, MyAnnotation.class, MyAnnotation2.class);
61-
assertEquals(2, ans.size());
62-
63-
ans = AnnotationUtils.getAnnotatedMethods(DummyClassExt.class, MyAnnotation.class, MyAnnotation2.class);
64-
assertEquals(3, ans.size());
65-
}
66-
6714
public void testFindAnnotationOnClass() {
6815
MyAnnotation a1 = AnnotationUtils.findAnnotation(DummyClass.class, MyAnnotation.class);
6916
assertNotNull(a1);
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
package com.opensymphony.xwork2.util.annotation;
22

33
@MyAnnotation("class-test")
4-
public class DummyClass implements DummyInterface {
4+
public class DummyClass {
55

66
public DummyClass() {
77
}
88

99
@MyAnnotation("method-test")
1010
public void methodWithAnnotation() {
11-
}
12-
13-
@Override
14-
public void interfaceMethodWithAnnotation() {
15-
}
16-
17-
@MyAnnotation2
18-
private void privateMethodWithAnnotation() {
1911
}
2012
}

core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyClassExt.java

-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,4 @@ public final class DummyClassExt extends DummyClass {
55
@MyAnnotation2
66
public void anotherAnnotatedMethod() {
77
}
8-
9-
@Override
10-
public void methodWithAnnotation() {
11-
}
128
}

core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyInterface.java

-6
This file was deleted.

core/src/test/java/com/opensymphony/xwork2/util/annotation/MyAnnotationI.java

-8
This file was deleted.

plugins/bean-validation/src/main/java/org/apache/struts/beanvalidation/validation/interceptor/BeanValidationInterceptor.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
import com.opensymphony.xwork2.TextProviderFactory;
2727
import com.opensymphony.xwork2.inject.Inject;
2828
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
29-
import com.opensymphony.xwork2.util.AnnotationUtils;
3029
import com.opensymphony.xwork2.validator.DelegatingValidatorContext;
3130
import com.opensymphony.xwork2.validator.ValidatorContext;
3231
import org.apache.commons.lang3.BooleanUtils;
32+
import org.apache.commons.lang3.reflect.MethodUtils;
3333
import org.apache.commons.lang3.StringUtils;
3434
import org.apache.logging.log4j.LogManager;
3535
import org.apache.logging.log4j.Logger;
@@ -98,7 +98,8 @@ protected String doIntercept(ActionInvocation invocation) throws Exception {
9898
LOG.debug("Validating [{}/{}] with method [{}]", invocation.getProxy().getNamespace(), invocation.getProxy().getActionName(), methodName);
9999
}
100100

101-
if (null == AnnotationUtils.findAnnotation(getActionMethod(action.getClass(), methodName), SkipValidation.class)) {
101+
if (null == MethodUtils.getAnnotation(getActionMethod(action.getClass(), methodName), SkipValidation.class,
102+
true, true)) {
102103
// performing bean validation on action
103104
performBeanValidation(action, validator);
104105
}

0 commit comments

Comments
 (0)