mirror of
https://github.com/apache/struts.git
synced 2026-08-06 15:17:00 +00:00
WW-4744 WW-4694 Removes annotation search to commons lang 3.6
This commit is contained in:
+2
-3
@@ -22,8 +22,6 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.reflect.MethodUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import com.opensymphony.xwork2.util.AnnotationUtils;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* <!-- START SNIPPET: description -->
|
||||
@@ -208,7 +206,8 @@ public class DefaultWorkflowInterceptor extends MethodFilterInterceptor {
|
||||
*/
|
||||
protected String processInputConfig(final Object action, final String method, final String currentResultName) throws Exception {
|
||||
String resultName = currentResultName;
|
||||
InputConfig annotation = AnnotationUtils.findAnnotation(action.getClass().getMethod(method, EMPTY_CLASS_ARRAY), InputConfig.class);
|
||||
InputConfig annotation = MethodUtils.getAnnotation(action.getClass().getMethod(method, EMPTY_CLASS_ARRAY),
|
||||
InputConfig.class ,true,true);
|
||||
if (annotation != null) {
|
||||
if (StringUtils.isNotEmpty(annotation.methodName())) {
|
||||
resultName = (String) MethodUtils.invokeMethod(action, true, annotation.methodName());
|
||||
|
||||
+15
-10
@@ -19,7 +19,6 @@ import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.XWorkException;
|
||||
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
|
||||
import com.opensymphony.xwork2.interceptor.PreResultListener;
|
||||
import com.opensymphony.xwork2.util.AnnotationUtils;
|
||||
import org.apache.commons.lang3.reflect.MethodUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
@@ -113,13 +112,15 @@ public class AnnotationWorkflowInterceptor extends AbstractInterceptor implement
|
||||
public String intercept(ActionInvocation invocation) throws Exception {
|
||||
final Object action = invocation.getAction();
|
||||
invocation.addPreResultListener(this);
|
||||
List<Method> methods = new ArrayList<>(AnnotationUtils.getAnnotatedMethods(action.getClass(), Before.class));
|
||||
List<Method> methods = new ArrayList<>(MethodUtils.getMethodsListWithAnnotation(action.getClass(), Before.class,
|
||||
true, true));
|
||||
if (methods.size() > 0) {
|
||||
// methods are only sorted by priority
|
||||
Collections.sort(methods, new Comparator<Method>() {
|
||||
public int compare(Method method1, Method method2) {
|
||||
return comparePriorities(AnnotationUtils.findAnnotation(method1, Before.class).priority(),
|
||||
AnnotationUtils.findAnnotation(method2, Before.class).priority());
|
||||
return comparePriorities(MethodUtils.getAnnotation(method1, Before.class, true,
|
||||
true).priority(), MethodUtils.getAnnotation(method2, Before.class, true,
|
||||
true).priority());
|
||||
}
|
||||
});
|
||||
for (Method m : methods) {
|
||||
@@ -134,14 +135,16 @@ public class AnnotationWorkflowInterceptor extends AbstractInterceptor implement
|
||||
String invocationResult = invocation.invoke();
|
||||
|
||||
// invoke any @After methods
|
||||
methods = new ArrayList<Method>(AnnotationUtils.getAnnotatedMethods(action.getClass(), After.class));
|
||||
methods = new ArrayList<Method>(MethodUtils.getMethodsListWithAnnotation(action.getClass(), After.class,
|
||||
true, true));
|
||||
|
||||
if (methods.size() > 0) {
|
||||
// methods are only sorted by priority
|
||||
Collections.sort(methods, new Comparator<Method>() {
|
||||
public int compare(Method method1, Method method2) {
|
||||
return comparePriorities(AnnotationUtils.findAnnotation(method1, After.class).priority(),
|
||||
AnnotationUtils.findAnnotation(method2, After.class).priority());
|
||||
return comparePriorities(MethodUtils.getAnnotation(method1, After.class, true,
|
||||
true).priority(), MethodUtils.getAnnotation(method2, After.class, true,
|
||||
true).priority());
|
||||
}
|
||||
});
|
||||
for (Method m : methods) {
|
||||
@@ -169,14 +172,16 @@ public class AnnotationWorkflowInterceptor extends AbstractInterceptor implement
|
||||
*/
|
||||
public void beforeResult(ActionInvocation invocation, String resultCode) {
|
||||
Object action = invocation.getAction();
|
||||
List<Method> methods = new ArrayList<Method>(AnnotationUtils.getAnnotatedMethods(action.getClass(), BeforeResult.class));
|
||||
List<Method> methods = new ArrayList<Method>(MethodUtils.getMethodsListWithAnnotation(action.getClass(),
|
||||
BeforeResult.class, true, true));
|
||||
|
||||
if (methods.size() > 0) {
|
||||
// methods are only sorted by priority
|
||||
Collections.sort(methods, new Comparator<Method>() {
|
||||
public int compare(Method method1, Method method2) {
|
||||
return comparePriorities(AnnotationUtils.findAnnotation(method1, BeforeResult.class).priority(),
|
||||
AnnotationUtils.findAnnotation(method2, BeforeResult.class).priority());
|
||||
return comparePriorities(MethodUtils.getAnnotation(method1, BeforeResult.class, true,
|
||||
true).priority(), MethodUtils.getAnnotation(method2, BeforeResult.class,
|
||||
true, true).priority());
|
||||
}
|
||||
});
|
||||
for (Method m : methods) {
|
||||
|
||||
@@ -15,16 +15,10 @@
|
||||
*/
|
||||
package com.opensymphony.xwork2.util;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.ClassUtils;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -107,113 +101,6 @@ public class AnnotationUtils {
|
||||
addAllInterfaces(clazz.getSuperclass(), allInterfaces);
|
||||
}
|
||||
|
||||
/**
|
||||
* For the given <code>Class</code> get a collection of the the {@link AnnotatedElement}s
|
||||
* that match the given <code>annotation</code>s or if no <code>annotation</code>s are
|
||||
* specified then return all of the annotated elements of the given <code>Class</code>.
|
||||
* Includes only the method level annotations.
|
||||
*
|
||||
* @param clazz The {@link Class} to inspect
|
||||
* @param annotation the {@link Annotation}s to find
|
||||
* @return A {@link Collection}<{@link AnnotatedElement}> containing all of the
|
||||
* method {@link AnnotatedElement}s matching the specified {@link Annotation}s
|
||||
* @deprecated Will be removed after release of <a href="https://github.com/apache/commons-lang/pull/261">LANG-1317</a>
|
||||
*/
|
||||
@Deprecated
|
||||
public static Collection<Method> getAnnotatedMethods(Class clazz, Class<? extends Annotation>... annotation) {
|
||||
List<Class<?>> allSuperclasses = ClassUtils.getAllSuperclasses(clazz);
|
||||
allSuperclasses.add(0, clazz);
|
||||
int sci = 0;
|
||||
List<Class<?>> allInterfaces = ClassUtils.getAllInterfaces(clazz);
|
||||
int ifi = 0;
|
||||
final List<Method> annotatedMethods = new ArrayList<>();
|
||||
while (ifi < allInterfaces.size() ||
|
||||
sci < allSuperclasses.size()) {
|
||||
Class<?> acls;
|
||||
if (ifi >= allInterfaces.size()) {
|
||||
acls = allSuperclasses.get(sci++);
|
||||
}
|
||||
else if (sci >= allSuperclasses.size()) {
|
||||
acls = allInterfaces.get(ifi++);
|
||||
}
|
||||
else if (sci <= ifi) {
|
||||
acls = allSuperclasses.get(sci++);
|
||||
}
|
||||
else {
|
||||
acls = allInterfaces.get(ifi++);
|
||||
}
|
||||
final Method[] allMethods = acls.getDeclaredMethods();
|
||||
for (final Method method : allMethods) {
|
||||
if (ArrayUtils.isEmpty(annotation) && ArrayUtils.isNotEmpty(method.getAnnotations())) {
|
||||
annotatedMethods.add(method);
|
||||
continue;
|
||||
}
|
||||
for (Class<? extends Annotation> c : annotation) {
|
||||
if (method.getAnnotation(c) != null) {
|
||||
annotatedMethods.add(method);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return annotatedMethods;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>BFS to find the annotation object that is present on the given method or any equivalent method in
|
||||
* super classes and interfaces, with the given annotation type. Returns null if the annotation type was not present
|
||||
* on any of them.</p>
|
||||
* @param <A>
|
||||
* the annotation type
|
||||
* @param method
|
||||
* the {@link Method} to query
|
||||
* @param annotationCls
|
||||
* the {@link Annotation} to check if is present on the method
|
||||
* @return an Annotation (possibly null).
|
||||
* @deprecated Will be removed after release of <a href="https://github.com/apache/commons-lang/pull/261">LANG-1317</a>
|
||||
*/
|
||||
@Deprecated
|
||||
public static <A extends Annotation> A findAnnotation(final Method method, final Class<A> annotationCls) {
|
||||
A annotation = method.getAnnotation(annotationCls);
|
||||
|
||||
if(annotation == null) {
|
||||
Class<?> mcls = method.getDeclaringClass();
|
||||
List<Class<?>> allSuperclasses = ClassUtils.getAllSuperclasses(mcls);
|
||||
int sci = 0;
|
||||
List<Class<?>> allInterfaces = ClassUtils.getAllInterfaces(mcls);
|
||||
int ifi = 0;
|
||||
while (ifi < allInterfaces.size() ||
|
||||
sci < allSuperclasses.size()) {
|
||||
Class<?> acls;
|
||||
if(ifi >= allInterfaces.size()) {
|
||||
acls = allSuperclasses.get(sci++);
|
||||
}
|
||||
else if(sci >= allSuperclasses.size()) {
|
||||
acls = allInterfaces.get(ifi++);
|
||||
}
|
||||
else if(ifi <= sci) {
|
||||
acls = allInterfaces.get(ifi++);
|
||||
}
|
||||
else {
|
||||
acls = allSuperclasses.get(sci++);
|
||||
}
|
||||
Method equivalentMethod = null;
|
||||
try {
|
||||
equivalentMethod = acls.getDeclaredMethod(method.getName(), method.getParameterTypes());
|
||||
} catch (NoSuchMethodException e) {
|
||||
// If not found, just keep on breadth first search
|
||||
}
|
||||
if(equivalentMethod != null) {
|
||||
annotation = equivalentMethod.getAnnotation(annotationCls);
|
||||
if(annotation != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return annotation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the property name for a method.
|
||||
* This method is independent from property fields.
|
||||
|
||||
@@ -22,10 +22,10 @@
|
||||
package org.apache.struts2.components;
|
||||
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.util.AnnotationUtils;
|
||||
import com.opensymphony.xwork2.util.TextParseUtil;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.apache.commons.lang3.reflect.MethodUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -543,7 +543,8 @@ public class Component {
|
||||
Class clz = getClass();
|
||||
Collection<String> standardAttributes = standardAttributesMap.get(clz);
|
||||
if (standardAttributes == null) {
|
||||
Collection<Method> methods = AnnotationUtils.getAnnotatedMethods(clz, StrutsTagAttribute.class);
|
||||
Collection<Method> methods = MethodUtils.getMethodsListWithAnnotation(clz, StrutsTagAttribute.class,
|
||||
true, true);
|
||||
standardAttributes = new HashSet<>(methods.size());
|
||||
for(Method m : methods) {
|
||||
standardAttributes.add(StringUtils.uncapitalize(m.getName().substring(3)));
|
||||
|
||||
+2
-2
@@ -23,8 +23,8 @@ package org.apache.struts2.interceptor.validation;
|
||||
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.config.ConfigurationException;
|
||||
import com.opensymphony.xwork2.util.AnnotationUtils;
|
||||
import com.opensymphony.xwork2.validator.ValidationInterceptor;
|
||||
import org.apache.commons.lang3.reflect.MethodUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@@ -44,7 +44,7 @@ public class AnnotationValidationInterceptor extends ValidationInterceptor {
|
||||
if (action != null) {
|
||||
Method method = getActionMethod(action.getClass(), invocation.getProxy().getMethod());
|
||||
|
||||
if (null != AnnotationUtils.findAnnotation(method, SkipValidation.class)) {
|
||||
if (null != MethodUtils.getAnnotation(method, SkipValidation.class, true, true)) {
|
||||
return invocation.invoke();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,68 +2,15 @@ package com.opensymphony.xwork2.util;
|
||||
|
||||
import com.opensymphony.xwork2.util.annotation.Dummy2Class;
|
||||
import com.opensymphony.xwork2.util.annotation.DummyClass;
|
||||
import com.opensymphony.xwork2.util.annotation.DummyClassExt;
|
||||
import com.opensymphony.xwork2.util.annotation.DummyInterface;
|
||||
import com.opensymphony.xwork2.util.annotation.MyAnnotation;
|
||||
import com.opensymphony.xwork2.util.annotation.MyAnnotation2;
|
||||
import com.opensymphony.xwork2.util.annotation.MyAnnotationI;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author Dan Oxlade, dan d0t oxlade at gmail d0t c0m
|
||||
*/
|
||||
public class AnnotationUtilsTest extends TestCase {
|
||||
|
||||
public void testFindAnnotationFromSuperclass() throws Exception {
|
||||
assertNotNull(AnnotationUtils.findAnnotation(DummyClassExt.class.getMethod("methodWithAnnotation"), MyAnnotation.class));
|
||||
}
|
||||
|
||||
public void testFindAnnotationFromInterface() throws Exception {
|
||||
assertNotNull(AnnotationUtils.findAnnotation(DummyClass.class.getMethod("interfaceMethodWithAnnotation"), MyAnnotationI.class));
|
||||
}
|
||||
|
||||
public void testFindAnnotation() throws Exception {
|
||||
assertNotNull(AnnotationUtils.findAnnotation(DummyClassExt.class.getMethod("anotherAnnotatedMethod"), MyAnnotation2.class));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testGetAnnotatedMethodsIncludingSuperclassAndInterface() throws Exception {
|
||||
|
||||
Collection<? extends AnnotatedElement> ans = AnnotationUtils.getAnnotatedMethods(DummyClassExt.class, Deprecated.class, MyAnnotation.class, MyAnnotation2.class, MyAnnotationI.class);
|
||||
assertEquals(4, ans.size());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testGetAnnotatedMethodsWithoutAnnotationArgs() throws Exception {
|
||||
Collection<? extends AnnotatedElement> ans = AnnotationUtils.getAnnotatedMethods(DummyClass.class);
|
||||
assertEquals(3, ans.size());
|
||||
assertTrue(ans.contains(DummyClass.class.getMethod("methodWithAnnotation")));
|
||||
assertTrue(ans.contains(DummyClass.class.getDeclaredMethod("privateMethodWithAnnotation")));
|
||||
assertTrue(ans.contains(DummyInterface.class.getDeclaredMethod("interfaceMethodWithAnnotation")));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testGetAnnotatedMethodsWithAnnotationArgs() throws Exception {
|
||||
Collection<? extends AnnotatedElement> ans = AnnotationUtils.getAnnotatedMethods(DummyClass.class, Deprecated.class);
|
||||
assertTrue(ans.isEmpty());
|
||||
|
||||
ans = AnnotationUtils.getAnnotatedMethods(DummyClass.class, Deprecated.class, MyAnnotation.class);
|
||||
assertEquals(1, ans.size());
|
||||
|
||||
ans = AnnotationUtils.getAnnotatedMethods(DummyClass.class, MyAnnotation.class);
|
||||
assertEquals(1, ans.size());
|
||||
|
||||
ans = AnnotationUtils.getAnnotatedMethods(DummyClass.class, MyAnnotation.class, MyAnnotation2.class);
|
||||
assertEquals(2, ans.size());
|
||||
|
||||
ans = AnnotationUtils.getAnnotatedMethods(DummyClassExt.class, MyAnnotation.class, MyAnnotation2.class);
|
||||
assertEquals(3, ans.size());
|
||||
}
|
||||
|
||||
public void testFindAnnotationOnClass() {
|
||||
MyAnnotation a1 = AnnotationUtils.findAnnotation(DummyClass.class, MyAnnotation.class);
|
||||
assertNotNull(a1);
|
||||
|
||||
@@ -1,20 +1,12 @@
|
||||
package com.opensymphony.xwork2.util.annotation;
|
||||
|
||||
@MyAnnotation("class-test")
|
||||
public class DummyClass implements DummyInterface {
|
||||
public class DummyClass {
|
||||
|
||||
public DummyClass() {
|
||||
}
|
||||
|
||||
@MyAnnotation("method-test")
|
||||
public void methodWithAnnotation() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void interfaceMethodWithAnnotation() {
|
||||
}
|
||||
|
||||
@MyAnnotation2
|
||||
private void privateMethodWithAnnotation() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,4 @@ public final class DummyClassExt extends DummyClass {
|
||||
@MyAnnotation2
|
||||
public void anotherAnnotatedMethod() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void methodWithAnnotation() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.opensymphony.xwork2.util.annotation;
|
||||
|
||||
public interface DummyInterface {
|
||||
@MyAnnotationI
|
||||
void interfaceMethodWithAnnotation();
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.opensymphony.xwork2.util.annotation;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MyAnnotationI {
|
||||
}
|
||||
+3
-2
@@ -26,10 +26,10 @@ import com.opensymphony.xwork2.ModelDriven;
|
||||
import com.opensymphony.xwork2.TextProviderFactory;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
|
||||
import com.opensymphony.xwork2.util.AnnotationUtils;
|
||||
import com.opensymphony.xwork2.validator.DelegatingValidatorContext;
|
||||
import com.opensymphony.xwork2.validator.ValidatorContext;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.apache.commons.lang3.reflect.MethodUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
@@ -98,7 +98,8 @@ public class BeanValidationInterceptor extends MethodFilterInterceptor {
|
||||
LOG.debug("Validating [{}/{}] with method [{}]", invocation.getProxy().getNamespace(), invocation.getProxy().getActionName(), methodName);
|
||||
}
|
||||
|
||||
if (null == AnnotationUtils.findAnnotation(getActionMethod(action.getClass(), methodName), SkipValidation.class)) {
|
||||
if (null == MethodUtils.getAnnotation(getActionMethod(action.getClass(), methodName), SkipValidation.class,
|
||||
true, true)) {
|
||||
// performing bean validation on action
|
||||
performBeanValidation(action, validator);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user