From 33e1eeb386c98beeaeff51504a2ff156098f2c01 Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Wed, 21 Jun 2017 13:28:45 +0430 Subject: [PATCH] WW-4744 WW-4694 Removes annotation search to commons lang 3.6 --- .../DefaultWorkflowInterceptor.java | 5 +- .../AnnotationWorkflowInterceptor.java | 25 ++-- .../xwork2/util/AnnotationUtils.java | 113 ------------------ .../apache/struts2/components/Component.java | 5 +- .../AnnotationValidationInterceptor.java | 4 +- .../xwork2/util/AnnotationUtilsTest.java | 53 -------- .../xwork2/util/annotation/DummyClass.java | 10 +- .../xwork2/util/annotation/DummyClassExt.java | 4 - .../util/annotation/DummyInterface.java | 6 - .../xwork2/util/annotation/MyAnnotationI.java | 8 -- .../BeanValidationInterceptor.java | 5 +- pom.xml | 2 +- 12 files changed, 27 insertions(+), 213 deletions(-) delete mode 100644 core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyInterface.java delete mode 100644 core/src/test/java/com/opensymphony/xwork2/util/annotation/MyAnnotationI.java diff --git a/core/src/main/java/com/opensymphony/xwork2/interceptor/DefaultWorkflowInterceptor.java b/core/src/main/java/com/opensymphony/xwork2/interceptor/DefaultWorkflowInterceptor.java index f9bcbd8f9e..d88d6f84ff 100644 --- a/core/src/main/java/com/opensymphony/xwork2/interceptor/DefaultWorkflowInterceptor.java +++ b/core/src/main/java/com/opensymphony/xwork2/interceptor/DefaultWorkflowInterceptor.java @@ -22,8 +22,6 @@ 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; /** * @@ -208,7 +206,8 @@ private String processValidationWorkflowAware(final Object action, final String */ 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()); diff --git a/core/src/main/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationWorkflowInterceptor.java b/core/src/main/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationWorkflowInterceptor.java index 38e3503389..36faa09088 100644 --- a/core/src/main/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationWorkflowInterceptor.java +++ b/core/src/main/java/com/opensymphony/xwork2/interceptor/annotations/AnnotationWorkflowInterceptor.java @@ -19,7 +19,6 @@ 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 methods = new ArrayList<>(AnnotationUtils.getAnnotatedMethods(action.getClass(), Before.class)); + List 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() { 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 int compare(Method method1, Method method2) { String invocationResult = invocation.invoke(); // invoke any @After methods - methods = new ArrayList(AnnotationUtils.getAnnotatedMethods(action.getClass(), After.class)); + methods = new ArrayList(MethodUtils.getMethodsListWithAnnotation(action.getClass(), After.class, + true, true)); if (methods.size() > 0) { // methods are only sorted by priority Collections.sort(methods, new Comparator() { 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 @@ protected static int comparePriorities(int val1, int val2) { */ public void beforeResult(ActionInvocation invocation, String resultCode) { Object action = invocation.getAction(); - List methods = new ArrayList(AnnotationUtils.getAnnotatedMethods(action.getClass(), BeforeResult.class)); + List methods = new ArrayList(MethodUtils.getMethodsListWithAnnotation(action.getClass(), + BeforeResult.class, true, true)); if (methods.size() > 0) { // methods are only sorted by priority Collections.sort(methods, new Comparator() { 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) { diff --git a/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java b/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java index e0af7a5d4f..eb8570fd79 100644 --- a/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java +++ b/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java @@ -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 static void addAllInterfaces(Class clazz, List allInterfaces) { addAllInterfaces(clazz.getSuperclass(), allInterfaces); } - /** - * For the given Class get a collection of the the {@link AnnotatedElement}s - * that match the given annotations or if no annotations are - * specified then return all of the annotated elements of the given Class. - * 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 LANG-1317 - */ - @Deprecated - public static Collection getAnnotatedMethods(Class clazz, Class... annotation) { - List> allSuperclasses = ClassUtils.getAllSuperclasses(clazz); - allSuperclasses.add(0, clazz); - int sci = 0; - List> allInterfaces = ClassUtils.getAllInterfaces(clazz); - int ifi = 0; - final List 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 c : annotation) { - if (method.getAnnotation(c) != null) { - annotatedMethods.add(method); - } - } - } - } - - return annotatedMethods; - } - - /** - *

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.

- * @param - * 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 LANG-1317 - */ - @Deprecated - public static A findAnnotation(final Method method, final Class annotationCls) { - A annotation = method.getAnnotation(annotationCls); - - if(annotation == null) { - Class mcls = method.getDeclaringClass(); - List> allSuperclasses = ClassUtils.getAllSuperclasses(mcls); - int sci = 0; - List> 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. diff --git a/core/src/main/java/org/apache/struts2/components/Component.java b/core/src/main/java/org/apache/struts2/components/Component.java index 2505fd2ddb..971cd53b8d 100644 --- a/core/src/main/java/org/apache/struts2/components/Component.java +++ b/core/src/main/java/org/apache/struts2/components/Component.java @@ -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 @@ protected Collection getStandardAttributes() { Class clz = getClass(); Collection standardAttributes = standardAttributesMap.get(clz); if (standardAttributes == null) { - Collection methods = AnnotationUtils.getAnnotatedMethods(clz, StrutsTagAttribute.class); + Collection 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))); diff --git a/core/src/main/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptor.java index b9ba26c354..d0b56aa3ea 100644 --- a/core/src/main/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptor.java +++ b/core/src/main/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptor.java @@ -23,8 +23,8 @@ 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 @@ protected String doIntercept(ActionInvocation invocation) throws Exception { 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(); } } diff --git a/core/src/test/java/com/opensymphony/xwork2/util/AnnotationUtilsTest.java b/core/src/test/java/com/opensymphony/xwork2/util/AnnotationUtilsTest.java index 1b6e0d5c7d..872f670311 100644 --- a/core/src/test/java/com/opensymphony/xwork2/util/AnnotationUtilsTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/util/AnnotationUtilsTest.java @@ -2,68 +2,15 @@ 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 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 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 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); diff --git a/core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyClass.java b/core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyClass.java index f6a4be11ab..8332a888b1 100644 --- a/core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyClass.java +++ b/core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyClass.java @@ -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() { } } diff --git a/core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyClassExt.java b/core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyClassExt.java index 7c024f69da..ec8ca443fe 100644 --- a/core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyClassExt.java +++ b/core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyClassExt.java @@ -5,8 +5,4 @@ public final class DummyClassExt extends DummyClass { @MyAnnotation2 public void anotherAnnotatedMethod() { } - - @Override - public void methodWithAnnotation() { - } } diff --git a/core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyInterface.java b/core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyInterface.java deleted file mode 100644 index 4d5f908df5..0000000000 --- a/core/src/test/java/com/opensymphony/xwork2/util/annotation/DummyInterface.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.opensymphony.xwork2.util.annotation; - -public interface DummyInterface { - @MyAnnotationI - void interfaceMethodWithAnnotation(); -} diff --git a/core/src/test/java/com/opensymphony/xwork2/util/annotation/MyAnnotationI.java b/core/src/test/java/com/opensymphony/xwork2/util/annotation/MyAnnotationI.java deleted file mode 100644 index ea866b90ed..0000000000 --- a/core/src/test/java/com/opensymphony/xwork2/util/annotation/MyAnnotationI.java +++ /dev/null @@ -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 { -} diff --git a/plugins/bean-validation/src/main/java/org/apache/struts/beanvalidation/validation/interceptor/BeanValidationInterceptor.java b/plugins/bean-validation/src/main/java/org/apache/struts/beanvalidation/validation/interceptor/BeanValidationInterceptor.java index a20d134fb8..be7a0c9e9c 100644 --- a/plugins/bean-validation/src/main/java/org/apache/struts/beanvalidation/validation/interceptor/BeanValidationInterceptor.java +++ b/plugins/bean-validation/src/main/java/org/apache/struts/beanvalidation/validation/interceptor/BeanValidationInterceptor.java @@ -26,10 +26,10 @@ 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 @@ protected String doIntercept(ActionInvocation invocation) throws Exception { 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); } diff --git a/pom.xml b/pom.xml index 720b9c2777..ce294786be 100644 --- a/pom.xml +++ b/pom.xml @@ -718,7 +718,7 @@ org.apache.commons commons-lang3 - 3.5 + 3.6 commons-digester