【发布时间】:2017-03-18 04:59:26
【问题描述】:
如果我们有一个注解用来设置某些特定于类的常量,这样声明:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Tooltip {
String value();
}
这样使用:
package applicationroot
@Tooltip("createCubeTool.tipText")
public class CreateCubeTool extends EditingTool
{
}
在超类型中使用 this:
public abstract class EditingTool
{
public String getToolTipText()
{
//Don't worry about this, other than that it requires a custom value Per Concrete class.
return null == tooltip ? null : Translate.text(tooltip.value());
}
}
注释应该在哪里声明?
撇开关于项目整体结构的问题不谈,我突然想到这个特定的注释只在ModelingTool 类型范围的子类中有用。将它声明在一个完全独立的包中是否有意义,package applicationroot.tool.annotations; 正如一位贡献者所建议的那样,还是最好将其声明为使用它的 ModelingTool 类型的成员?
到目前为止,我发现的所有参考资料都在讨论如何声明自定义注释,而不是它们适合项目整体结构的位置。
【问题讨论】:
标签: java annotations code-organization