mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
add support finding annotations on interfaces and their packages
See also WW-4912
This commit is contained in:
@@ -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 <T extends Annotation> List<T> findAnnotations(Class<?> clazz, Class<T> annotationClass) {
|
||||
List<T> anns = new ArrayList<>();
|
||||
|
||||
T ann = clazz.getAnnotation(annotationClass);
|
||||
if (ann != null) {
|
||||
anns.add(ann);
|
||||
}
|
||||
List<Class<?>> 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;
|
||||
|
||||
@@ -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<MyAnnotation> annotations = AnnotationUtils.findAnnotations(DummyClass.class, MyAnnotation.class);
|
||||
List<MyAnnotation> annotations = AnnotationUtils.findAnnotations(DummyClassExt.class, MyAnnotation.class);
|
||||
|
||||
assertThat(annotations)
|
||||
.isNotNull()
|
||||
.isNotEmpty()
|
||||
.hasSize(2);
|
||||
.hasSize(4);
|
||||
|
||||
Set<String> 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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
}
|
||||
|
||||
+25
@@ -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 {
|
||||
}
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user