注释上的自定义 Eclipse 警告 [英] Custom Eclipse warning on an annotation

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

问题描述

假设我有一个内部方法,应该只在某些情况下使用.

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

在 Eclipse 中,是否有可能将其标记为内部并在用于阻止我或将使用我的 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 (Pluggable Annotation Processor API) 允许您编写自定义注释处理器,它可以处理自定义注释,并使您能够使用 javax.annotation.processing.Messager.下面是一个例子.

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 编译器中注册注释处理器Processing -> Factory Path 并添加包含自定义注释处理器的 Jar.这里是一篇解释细节的有趣帖子.

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 构建路径中,以便您项目中依赖于此类的任何代码都会显示警告.

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天全站免登陆