From cf71e0f34f4a0d7da55d280592ac61e8a2d3482c Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Tue, 30 Jan 2018 11:35:16 +0330 Subject: [PATCH 1/2] add support finding annotations on interfaces and their packages See also WW-4912 --- .../xwork2/util/AnnotationUtils.java | 25 +++++++++++-------- .../xwork2/util/AnnotationUtilsTest.java | 7 +++--- .../xwork2/util/annotation/DummyClass.java | 4 ++- .../pkg2/Package2DummyInterface.java | 25 +++++++++++++++++++ .../util/annotation/pkg2/package-info.java | 23 +++++++++++++++++ 5 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 core/src/test/java/com/opensymphony/xwork2/util/annotation/pkg2/Package2DummyInterface.java create mode 100644 core/src/test/java/com/opensymphony/xwork2/util/annotation/pkg2/package-info.java 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 c08841c63..dd148550e 100644 --- a/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java +++ b/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java @@ -18,6 +18,8 @@ */ package com.opensymphony.xwork2.util; +import org.apache.commons.lang3.ClassUtils; + import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -168,18 +170,21 @@ public class AnnotationUtils { public static List findAnnotations(Class clazz, Class annotationClass) { List anns = new ArrayList<>(); - T ann = clazz.getAnnotation(annotationClass); - if (ann != null) { - anns.add(ann); - } + List> classes = new ArrayList<>(); + classes.add(clazz); - ann = clazz.getPackage().getAnnotation(annotationClass); - if (ann != null) { - anns.add(ann); - } + classes.addAll(ClassUtils.getAllSuperclasses(clazz)); + classes.addAll(ClassUtils.getAllInterfaces(clazz)); + for (Class aClass : classes) { + T ann = aClass.getAnnotation(annotationClass); + if (ann != null && !anns.contains(ann)) { + anns.add(ann); + } - if (clazz.getSuperclass() != Object.class) { - anns.addAll(findAnnotations(clazz.getSuperclass(), annotationClass)); + ann = aClass.getPackage().getAnnotation(annotationClass); + if (ann != null && !anns.contains(ann)) { + anns.add(ann); + } } return anns; 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 67f190126..12ce2936c 100644 --- a/core/src/test/java/com/opensymphony/xwork2/util/AnnotationUtilsTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/util/AnnotationUtilsTest.java @@ -21,6 +21,7 @@ package com.opensymphony.xwork2.util; import com.opensymphony.xwork2.util.annotation.Dummy2Class; import com.opensymphony.xwork2.util.annotation.Dummy3Class; import com.opensymphony.xwork2.util.annotation.DummyClass; +import com.opensymphony.xwork2.util.annotation.DummyClassExt; import com.opensymphony.xwork2.util.annotation.MyAnnotation; import com.opensymphony.xwork2.util.annotation.MyAnnotation2; import junit.framework.TestCase; @@ -52,18 +53,18 @@ public class AnnotationUtilsTest extends TestCase { } public void testFindAnnotationsOnAll() { - List annotations = AnnotationUtils.findAnnotations(DummyClass.class, MyAnnotation.class); + List annotations = AnnotationUtils.findAnnotations(DummyClassExt.class, MyAnnotation.class); assertThat(annotations) .isNotNull() .isNotEmpty() - .hasSize(2); + .hasSize(4); Set values = new HashSet<>(); for (MyAnnotation annotation : annotations) { values.add(annotation.value()); } - assertThat(values).contains("class-test", "package-test"); + assertThat(values).contains("class-test", "package-test", "interface-test", "package2-test"); } } 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 707b73592..bb65b5c6e 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 @@ -18,8 +18,10 @@ */ package com.opensymphony.xwork2.util.annotation; +import com.opensymphony.xwork2.util.annotation.pkg2.Package2DummyInterface; + @MyAnnotation("class-test") -public class DummyClass { +public class DummyClass implements Package2DummyInterface { public DummyClass() { } diff --git a/core/src/test/java/com/opensymphony/xwork2/util/annotation/pkg2/Package2DummyInterface.java b/core/src/test/java/com/opensymphony/xwork2/util/annotation/pkg2/Package2DummyInterface.java new file mode 100644 index 000000000..ea1aa1279 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/util/annotation/pkg2/Package2DummyInterface.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.opensymphony.xwork2.util.annotation.pkg2; + +import com.opensymphony.xwork2.util.annotation.MyAnnotation; + +@MyAnnotation("interface-test") +public interface Package2DummyInterface { +} diff --git a/core/src/test/java/com/opensymphony/xwork2/util/annotation/pkg2/package-info.java b/core/src/test/java/com/opensymphony/xwork2/util/annotation/pkg2/package-info.java new file mode 100644 index 000000000..2eee0a823 --- /dev/null +++ b/core/src/test/java/com/opensymphony/xwork2/util/annotation/pkg2/package-info.java @@ -0,0 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +@MyAnnotation("package2-test") +package com.opensymphony.xwork2.util.annotation.pkg2; + +import com.opensymphony.xwork2.util.annotation.MyAnnotation; \ No newline at end of file From 8d4194bbf7364625a5b4f407ef48208c4ba524fd Mon Sep 17 00:00:00 2001 From: Yasser Zamani Date: Tue, 30 Jan 2018 12:21:33 +0330 Subject: [PATCH 2/2] allow duplicate in AnnotationUtils.findAnnotations result See also WW-4912 --- .../java/com/opensymphony/xwork2/util/AnnotationUtils.java | 4 ++-- .../com/opensymphony/xwork2/util/AnnotationUtilsTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 dd148550e..cbebc8003 100644 --- a/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java +++ b/core/src/main/java/com/opensymphony/xwork2/util/AnnotationUtils.java @@ -177,12 +177,12 @@ public class AnnotationUtils { classes.addAll(ClassUtils.getAllInterfaces(clazz)); for (Class aClass : classes) { T ann = aClass.getAnnotation(annotationClass); - if (ann != null && !anns.contains(ann)) { + if (ann != null) { anns.add(ann); } ann = aClass.getPackage().getAnnotation(annotationClass); - if (ann != null && !anns.contains(ann)) { + if (ann != null) { anns.add(ann); } } 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 12ce2936c..16fbab4fe 100644 --- a/core/src/test/java/com/opensymphony/xwork2/util/AnnotationUtilsTest.java +++ b/core/src/test/java/com/opensymphony/xwork2/util/AnnotationUtilsTest.java @@ -58,7 +58,7 @@ public class AnnotationUtilsTest extends TestCase { assertThat(annotations) .isNotNull() .isNotEmpty() - .hasSize(4); + .hasSize(5); Set values = new HashSet<>(); for (MyAnnotation annotation : annotations) {