English 中文(简体)
是否有这样的意思,比如说 In中的继承?
原标题:Is there something like Annotation Inheritance in java?

我探讨一下说明,并提到一些说明似乎在其中具有等级。

Im利用说明在卡片背景中形成代码。 不同的卡片类型(不同的代码和说明)各不相同,但有些内容是共同的,如名称。

@Target(value = {ElementType.TYPE})
public @interface Move extends Page{
 String method1();
 String method2();
}

这也是共同的含义:

@Target(value = {ElementType.TYPE})
public @interface Page{
 String method3();
}

在上述例子中,我预计Move将继承方法3,但我收到警告说,扩展并不具有附加说明的效力。 我试图有一份说明,它提供了一个共同的基础,但那是没有工作的。 这是否甚至可能,或者只是一个设计问题?

最佳回答

可惜的是,这显然与读上课说明的方案有关,而没有以任何方式负担。 见。 为什么无法在 Java提出说明?

但是,如果这些说明 rel=“noreferer”>@Inherited,则这些类别确实继承了它们的超级类别说明。

此外,除非你需要这些互动方法,否则你只能打上关于你的班子的说明:

@Move
@Page
public class myAwesomeClass {}

是否有某种理由不为你工作?

问题回答

您可以提出基本说明,而不是继承。 http://static.childrensource.org/children/docs/3.2.x/children-framework-vis/html/beans.html#beans-annotation-config" rel=“noreferer” 。

举一个例子

@Target(value = {ElementType.ANNOTATION_TYPE})
public @interface Vehicle {
}

@Target(value = {ElementType.TYPE})
@Vehicle
public @interface Car {
}

@Car
class Foo {
}

然后,请你通过

Vehicle vehicleAnnotation = AnnotationUtils.findAnnotation (Foo.class, Vehicle.class);
boolean isAnnotated = vehicleAnnotation != null;

This method is implemented as:

public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType) {
    return findAnnotation(clazz, annotationType, new HashSet<Annotation>());
}

@SuppressWarnings("unchecked")
private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType, Set<Annotation> visited) {
    try {
        Annotation[] anns = clazz.getDeclaredAnnotations();
        for (Annotation ann : anns) {
            if (ann.annotationType() == annotationType) {
                return (A) ann;
            }
        }
        for (Annotation ann : anns) {
            if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) {
                A annotation = findAnnotation(ann.annotationType(), annotationType, visited);
                if (annotation != null) {
                    return annotation;
                }
            }
        }
    }
    catch (Exception ex) {
        handleIntrospectionFailure(clazz, ex);
        return null;
    }

    for (Class<?> ifc : clazz.getInterfaces()) {
        A annotation = findAnnotation(ifc, annotationType, visited);
        if (annotation != null) {
            return annotation;
        }
    }

    Class<?> superclass = clazz.getSuperclass();
    if (superclass == null || Object.class == superclass) {
        return null;
    }
    return findAnnotation(superclass, annotationType, visited);
}

AnnotationUtils also contains additional methods for searching for annotations on methods and other annotated elements. The Spring class is also powerful enough to search through bridged methods, proxies, and other corner-cases, particularly those encountered in Spring.

除Grygoriys外,还有说明性说明的答复。

You can check e.g. methods for containing a @Qualifier annotation (or an annotation annotated with @Qualifier) by this loop:

for (Annotation a : method.getAnnotations()) {
    if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
        System.out.println("found @Qualifier annotation");//found annotation having Qualifier annotation itself
    }
}

What you re basically doing, is to get all annotations present on the method and of those annotations you get their types and check those types if they re annotated with @Qualifier. Your annotation needs to be Target.Annotation_type enabled as well to get this working.

你可以这样说:

@Inherited
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Page{
    String method3();
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签