定制Eclipse上的警告注解 [英] Custom Eclipse warning on an annotation

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

问题描述

比方说,我有一个内部的方法,它应该只在某些情况下使用。

Let's say I have an internal method, which should be used only in certain situations.

是否有可能在Eclipse中,将其标记为内部和用于$ P $时显示警告pvent我和谁将会使用我的API错误地使用它,不知道他们在做什么的人。我无法改变它的知名度,因为它可能会在其他软件包中使用/非扩展类了。

Is there any possibility, in Eclipse, to mark it as internal and show a warning when used to prevent me or people who will use my API to use it by mistake not knowing what they're doing. I can't change its visibility as it might be used in other packages/non-extending classes too.

事情是这样的:

@Internal
public void doSomeInternalStuff()
{
    // ...
}

然后,在Eclipse中警告:

And then, a warning in Eclipse:

您明白了吧。

什么希望?

推荐答案

JSR269 可插拔注解处理器API 的),您可以编写自定义的注释处理器,可以处理自定义的注释,使您可以使用 javax.annotation.processing中打印错误或警告。梅萨
下面是一个例子。

JSR269 (Pluggable Annotation Processor API) allows you to write custom annotation processors that can handle custom annotations and enable you to print errors or warning using a javax.annotation.processing.Messager. Below is an example.

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

@SupportedAnnotationTypes("fully.qualified.name.of.InternalAnnotationType")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class CustomAnnotationProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        for (Element element : roundEnv.getElementsAnnotatedWith(InternalAnnotationType.class)) {
            InternalAnnotationType internalAnnotation = element.getAnnotation(InternalAnnotationType.class);
            String message = "The method " + element.getSimpleName()
                       + " is marked internal and its use is discouraged";
            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, message);
        }
        return true;
    }
}

在Eclipse中,您可以在Java编译器通过右键点击你的项目注册的注解处理器,然后选择的属性 - > Java编译器 - > 注释加工 - > 工厂路径并添加包含您的自定义注解处理器的罐子。 这里是一个有趣的帖子,解释的细节。

In Eclipse, you can register annotation processors in your Java Compiler by right-clicking on your project, then selecting Properties -> Java Compiler -> Annotation Processing -> Factory Path and adding the Jar that contains your custom annotation processor. Here is an interesting post that explains the details.

或者的,你可以把你所有的内部方法API的专用类中,并添加访问规则作为该类在Eclipse构建路径,这样在项目中的任何code取决于该类会显示警告。

Alternatively, you can put all of your "internal" method API inside a dedicated class, and add an access rule for that class in your Eclipse build path, so that any code in your project that depends on this class would display a warning.

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

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