使用 IDE 上下文行为创建 Java 注释 [英] Create a Java Annotation with IDE contextual behaviour

查看:26
本文介绍了使用 IDE 上下文行为创建 Java 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个注释

/**
 * Highlights this method is declared in XML
 */
public @interface FromXML {
}

我在看起来像这样的方法上使用它:

I'm using this on methods that look like this:

@FromXML
public void onSomethingClick(View v){

}

Android 反射系统需要 v 变量来调用此方法.但是输入 var v 未使用,因此我的 IDE 会警告我这一点.我喜欢这个警告,并希望在我的其余代码中使用它.

The v variable is needed by the Android reflection system to call this method. However the input var v is unused, so my IDE warns me of this. I like this warning and want it on for the rest of my code.

为了隐藏我可以做的警告

To hide the warning I could do

@SuppressWarnings("unused")
@FromXML
public void onSomethingClick(View v){

}

或者我可以添加一个参数标签

or I could add a param tag

但我宁愿 IDE (eclipse) 读取我的 @FromXML 注释并且不给我警告.

But I would rather that the IDE (eclipse) reads my @FromXML annotation and doesn't give me the warning.

我知道您不能扩展注释,但这基本上就是我想要做的.

I know you can't extend an Annotation, but thats basically what I want to do.

有没有办法添加我的注释以被识别为 '禁止警告'注释.

Is there a way I can add my annotation to be recognised as a 'suppress warnings' annotation.

有没有办法对我的注释进行编码以表现得像"抑制警告?

Is there a way to code my annotation to 'act like' suppress warnings?

推荐答案

好吧,我不能以任何简单的方式或形式做到这一点.

Ok I can't do this in any easy way or form.

看注解包http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/annotation/package-summary.html

我无法扩展注释:为什么不能在 Java 中扩展注解?

我无法实现另一个注释 java.lang.Override

I can't implement another annotation java.lang.Override

我不能模仿/模仿/假装是@Override

I can't mimic / mock / pretend to be @Override

如果我将 @Override 添加到我的 @FromXML 中,它不会沿链向下继承.

If I add @Override to my @FromXML it is NOT inherited down the chain.

唯一的方法是创建一个 Eclipse 插件来识别我的注释并停止警告.惭愧,因为我找不到一种简单的方法来做到这一点.

The only way would be to create an Eclipse plugin that recognises my annotation and stops the warning. Shame because I can't find an easy way to do this.

我还沿着为我的 @FromXML 入口点创建接口的路线走下去,这非常好,并且传达了我的 Activity 是一种类型,因此我没有不再需要注释,也许这种设计更改就是答案.

I also went down the route of creating an interface for my @FromXML entry points, this was very nice and communicated my Activity was of a type and therefore I didn't need the annotation anymore, perhaps this design change is the answer.

这篇关于使用 IDE 上下文行为创建 Java 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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