英文:
Create a Java annotation for warnings - @NonOptimal
问题
@Deprecated
注解有什么特殊之处,我无法复现吗?
我需要创建一个类似于@Deprecated
的注解,以便在Eclipse中产生警告,并且在构建时也能产生警告。当我将一个方法标记为@Deprecated
时,我会得到很好的警告。例如,如果我有一个旧的方法(出于兼容性的原因可能仍然保留):
@Deprecated
public List<Account> getClientAccounts(final int clientId) {
// 实现搜索...
}
- 然后,在Eclipse中尝试使用它时,我可以看到它被划掉了,左侧栏中有一个黄色图标:
- 还有,在构建时我可以看到:
> [WARNING] app1/src/test/java/com/app1/MyApp.java: app1/src/test/java/com/app1/MyApp.java 使用或覆盖了已弃用的 API。
现在,根据我无法控制的外部因素(例如缺少数据库索引),有些方法不够优化,我想明确地标记它们... 使用我的全新@NonOptimal
注解。我需要突出显示这个问题。到目前为止,我有:
@Retention(RUNTIME)
@Target(METHOD)
// 在这里加入什么?
public @interface NonOptimal {
}
我应该如何创建这个注解?
英文:
Is there something special to the @Deprecated
annotation that I cannot reproduce?
I need to create an annotation similar to @Deprecated
to produce warnings in Eclipse and also at build time. When I mark a method as @Deprecated
I get nice warnings. For example, if I have an old method (that I may still keep for compatibility reasons):
@Deprecated
public List<Account> getClientAccounts(final int clientId) {
// Implement search...
}
-
Then, if I try to use it in Eclipse I can see it strikethrough, and a yellow icon in the left bar:
-
Also when building I can see the:
> [WARNING] app1/src/test/java/com/app1/MyApp.java: app1/src/test/java/com/app1/MyApp.java uses or overrides a deprecated API.
Now, depending on external factors I cannot control (e.g. absence of database indexes) some methods are not optimal, and I would like to clearly mark them as such... with my brand new @NonOptimal
annotation. I need to add visibility to the problem. So far I have:
@Retention(RUNTIME)
@Target(METHOD)
// What else here?
public @interface NonOptimal {
}
How can I create this annotation?
答案1
得分: 3
@TheImpaler 这实际上并不是你问题的真正答案,但是前段时间我在使用Zalando的Problem API时偶然发现了Google Annotations Library(也称为gag)。
这个库提供了许多自定义注解,有些情况下可以通过使用ASM和自定义Java代理来对你的实际代码进行插桩。
也许它能够为你解决当前问题提供一些思路。
该项目已不再维护,但在GitHub上有一个分支。
英文:
@TheImpaler This is actually not a true answer for your problem, but some time ago I came across the Google Annotations Library (a.k.a. gag) while using Zalando's Problem API.
This library provides a great number of custom annotations that, in some cases, can be used to instrument your actual code by using a ASM and a custom java agent.
Maybe it can give you some ideas regarding your actual issue.
The project is no longer maintained but there is a fork in Github.
答案2
得分: 2
我希望我能够扩展Deprecated
,但无法实现。
在阅读了相当多的相关内容之后,我最终采取了一个丑陋的解决方法。虽然它能够工作,但我并不喜欢它。
我决定用@Deprecated
和 @NonOptimal
两个注解来标记不好的方法。这在概念上是错误的(实际上这些方法并没有被弃用),但它能够很好地直接使用,而不需要开发一个过于复杂的 Eclipse 插件:
-
@Deprecated
注解在各个地方(在 Eclipse 中和构建时)都会提醒开发人员,这是一件好事。 -
@NonOptimal
注解会提供详细信息,解释为什么这是一个不好的方法。
虽然丑陋,但能够起作用。就目前而言,Eclipse 没有提供更好的选择。
注意:更糟糕的是,在使用工具链(toolchains)时,NonOptimal
注解在 Maven 中效果不佳:警告会变得无声,消失不见……因此,最终来看,注解处理器变得有点无用。
英文:
I wish I could extend Deprecated, but no can do.
After reading about this quite a bit I ended up with an ugly workaround. It works, though I don't like it.
I decided to mark the bad methods with both the @Deprecated
and @NonOptimal
annotations. It's conceptually wrong (the methods are not actually deprecated) but it works well out of the box. No need to develop an overkill Eclipse plugin:
-
The
@Deprecated
annnotation bugs developers all around the place (in Eclipse and when building), and that's a good thing. -
The
@NonOptimal
annotation provides details on why this is a bad method to use.
Ugly but works. As of now Eclipse does not provide any better option.
Note: to make things worse, the NonOptimal annotation does not work well in Maven when using toolchains: warnings go silent, disappear, nada... Therefore, AnnotationProcessors are kind of useless in the end.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论