高效的Java注解 [英] Efficient Java Annotations

查看:173
本文介绍了高效的Java注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的注解一个巨大的风扇,但觉得这是一个痛苦的脖子上有包括谷歌的的思考 Scannotations我要让我自己的每一次

I'm a huge fan of Java's annotations, but find it a pain in the neck to have to include Google's Reflections or Scannotations every time I want to make my own.

我一直没能找到关于Java能够自动扫描注解和放大器的任何文件;适当地使用他们,没有一个容器或类似的帮助。

I haven't been able to find any documentation about Java being able to automatically scan for annotations & use them appropriately, without the help of a container or alike.

问:我错过了有关Java的一些基本的东西,或者是注释总是设计成手动扫描和放大器;检查是必需的?是否有处理注释一些内置的方式?

Question: Have I missed something fundamental about Java, or were annotations always designed such that manual scanning & checking is required? Is there some built-in way of handling annotations?

为了进一步澄清

我希望能够在Java中多一点编程接近注解。例如,假设你想建立 s的列表。要做到这一点,你标注与可以填充列表为你的类列表。例如:

I'd like to be able to approach annotations in Java a little more programatically. For instance, say you wanted to build a List of Cars. To do this, you annotate the list with a class that can populate the list for you. For instance:

@CarMaker
List<Car> cars = new List<Car>();

在这个例子中,汽车制造商标注是由Java的,谁罢工的协议,并要求他们想要多少汽车提供接洽。这是到汽车制造商注释/类,然后为他们提供一个清单,其中汽车包括。这可能是所有类与 @CarType 注释和接口。

In this example, the CarMaker annotation is approached by Java, who strikes a deal and asks them how many cars they want to provide. It's up to the CarMaker annotation/class to then provide them with a list of which cars to include. This could be all classes with @CarType annotations, and a Car interface.

看它的另一种方式是,如果你知道你想建立这样的事:列表&LT;汽车&GT;汽车,您可以用 @ ListMaker&LT其标注;汽车&GT; 。在 ListMaker 是内置到Java的东西。它看起来与 @CarType 注释的所有类,并相应地填充列表。

Another way of looking at it, is that if you know you want to build something like this: List<Car> cars, you could annotate it with @ListMaker<Car>. The ListMaker is something built into Java. It looks for all classes annotated with @CarType, and populates the list accordingly.

推荐答案

您可以创建自己的注解,并将其应用到自己的类。

You can create your own annotations and apply them to your own classes.

如果您指定的注释在运行时检测到的,你可以很容易地反射处理它。

If you specify that an annotation is detectable at runtime, you can process it easily with reflection.

例如,你可以使用这样的事情在已经打上了阶级打印每个字段的名称中的质朴的注释:

For example, you could use something like this to print the name of each field in a class that has been marked with the Funky annotation:

for (Field someField : AnnotatedClass.getClass().getDeclaredFields()) {
    if (someField.isAnnotationPresent(Funky.class)) {
        System.out.println("This field is funky: " + someField.getName());
    }
}

在code申报的质朴的注释会是这个样子:

package org.foo.annotations;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Funky { }

下面是一个使用注解的类:

Here's a class that uses the annotation:

package org.foo.examples;

import org.foo.annotations.Funky;

public class AnnotatedClass {
    @Funky
    private  String   funkyString;
    private  String   nonFunkyString;
    @Funky
    private  Integer  funkyInteger;
    private  Integer  nonFunkyInteger;
}

下面是上批注的一些多看书

Here's some more reading on Annotations.

下面是用于上述类的javadoc:

Here are the javadocs for the classes used above:

  • Retention annotation
  • RetentionPolicy enum
  • Target annotation
  • Field class
  • isAnnotationPresent() method
  • getDeclaredFields() method

我想了解你的车的例子,但我不知道我跟着你想要的东西。

I'm trying to understand your car example, but I'm not sure I follow what you want.

如果你有扩展汽车和标有各种汽车相关的注释对象(捷豹,保时捷,法拉利,起亚)的列表,你可以创建一个过滤器基于注释列表中的一个对象。

If you had a list of objects (Jaguar, Porche, Ferrari, Kia) that extend Car and are marked with various car-related annotations, you could create an object that filters the list based on annotations.

在code可能是这样的:

The code might look like this:

@WorldsFinestMotorCar
class Jaguar extends Car {
    // blah blah
}

@BoringCar
class Porche extends Car {
    // blah blah
} 

@BoringCar
class Ferrari extends Car {
    // blah blah
}

@IncredibleCar
class Kia extends Car {
    // blah blah
}

您可以实施的 AnnotationFilter 的类,它从没有一定的批注列表中删除的汽车。

You could implement an AnnotationFilter class that removes cars from the list that do not have a certain annotation.

这可能是这个样子:

List<Car> carList = getListOfRandomCars();
AnnotationFilter<Car> annoFilter = new AnnotationFilter<Car>(BoringCar.class);
List<Car> boringCars = annoFilter.filter(carList);

这是你想要做什么?

Is that what you want to do?

如果是这样,它绝对可以实现。

If so, it can definitely be done.

有关AnnotationFilter的实现可能是这个样子:

The implementation for AnnotationFilter might look something like this:

public class AnnotationFilter<T> {
    private Class filterAnno;

    public AnnotationFilter(Class a) {
        filterAnno = a;
    }

    public List<T> filter(List<T> inputList) {
        if (inputList == null || inputList.isEmpty()) {
            return inputList;
        }
        List<T> filteredList = new ArrayList<T>();
        for (T someT : inputList) {
            if (someT.getClass().isAnnotationPresent(filterAnno)) {
                filteredList.add(someT);
            }
        }
        return filteredList;
    }
}

如果这不是你以后,一个具体的例子将是有益的。

If that's not what you're after, a specific example would be helpful.

这篇关于高效的Java注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆