简体   繁体   English

帮助注释

[英]Help with annotations

Edit--@Uri correctly pointed out that this was an abuse of annotations; 编辑 - @ Uri正确地指出这是滥用注释; trying to actually create the menu data itself in annotations is just silly. 试图在注释中实际创建菜单数据本身就是愚蠢的。

They are good for binding however, I think I'll stick with using them to link the text data to the methods (the @Menu ("File") portion) since it's more explicit and flexible than reflecting to a method name. 它们很适合绑定,但我认为我会坚持使用它们将文本数据链接到方法(@Menu(“文件”)部分),因为它比反映到方法名称更明确和灵活。 Also I learned quite a bit in messing with it. 我也搞砸了很多东西。 I'll post the code here in a few days as an answer. 我会在几天内将代码发布在这里作为答案。

--original post-- - 原始帖子 -

I haven't used these new-fangled annotations, but they look amazingly interesting. 我没有使用这些新奇的注释,但它们看起来非常有趣。 I'm having trouble figuring out the syntax though (or more appropriately, the best way to use it). 我无法弄清楚语法(或者更恰当地说,使用它的最佳方式)。

In writing some code in response to this question It occurred to me that my methods are quite outdated. 在编写一些代码以回应这个问题时我发现我的方法已经过时了。

I used to parse a string to define my method structure, then use reflection to pass it out to classes, but I think annotations could make a much better menu structure. 我曾经解析一个字符串来定义我的方法结构,然后使用反射将它传递给类,但我认为注释可以创建一个更好的菜单结构。

I'd like to replace my test class in the file with something like this: 我想用这样的东西替换文件中的测试类:

@TopMenu("File,Edit")
@Menu(name="File","Save,Load,Print,Preview,Quit")
@Menu(name="Print","Preview,Print")
@Menu(name="Edit","Copy,Paste")

public class TestMenu {
    @MenuItem ("Save")
    public void save() {
        System.out.println("saved");
    }
    @MenuItem ("Load")
    public void load() {
    System.out.println("loaded");
    }
...

and pass the entire class off to a method that manufactures and returns a JMenuBar bound to the class instance with no further input. 并将整个类传递给一个方法,该方法制造并返回绑定到类实例的JMenuBar而没有进一步的输入。

First problem is that I can't figure out how to pass a "Default" of a string, they all want to have (attribute="value") instead of just ("value"), can this be done? 第一个问题是我无法弄清楚如何传递一个字符串的“默认”,他们都想拥有(attribute =“value”)而不仅仅是(“value”),这可以做到吗? I can live without it, but it's a little verbose. 我可以没有它,但它有点冗长。 It'd be even better if I could get rid of the parens and/or quotes, but I'm not holding my breath (I think to do that I'd have to define an individual interface for each menu item, that's not acceptable). 如果我能摆脱parens和/或引号会更好,但我没有屏住呼吸(我想这样做我必须为每个菜单项定义一个单独的界面,这是不可接受的)。

Secondly it doesn't like the multiple @Menu tags on a single class. 其次,它不喜欢单个类上的多个@Menu标记。 I could get around this by parsing a single string, but I was wondering if there was another way. 我可以通过解析单个字符串来解决这个问题,但我想知道是否还有其他方法。

Most importantly, is there a library that does this already? 最重要的是,是否有一个图书馆已经这样做了? (If nobody comes up with one, I'll publish code to this thread when I get it working in case anyone else is interested.) (如果没有人提出一个,我会在我开始工作时将代码发布到这个帖子,以防其他人感兴趣。)

I know I'll get downvoted for this, but I really think people are starting to overabuse the annotation mechanism in Java. 我知道我会因此而失败,但我真的认为人们开始过度使用Java中的注释机制。

All it was designed for was to be a mechanism for providing metainformation about classes and methods for the purpose of the compiler or of programming-support tools (eg, testing infrastructure, model checkers, code generators, etc.) 所有它的设计目的是为了编译器或编程支持工具(例如,测试基础设施,模型检查器,代码生成器等)提供有关类和方法的元信息的机制。

It was not meant for actual production-oriented code, macro metaprogramming, and all that. 它不适用于实际的面向生产的代码,宏元编程以及所有这些。 This is just as inelegant as using preprocessor macros in C instead of actual functions. 这与在C中使用预处理器宏而不是实际函数一样不优雅。

If menus are first-class entities in your program, I really don't feel that you should be using the annotation mechanism for them. 如果菜单是程序中的第一类实体,我真的不觉得你应该为它们使用注释机制。

As for your specific questions, you can easily define a default value. 至于您的具体问题,您可以轻松定义默认值。 However, you can't start doing things like nesting annotations to overcome the menu problem. 但是,您无法开始执行嵌套注释以克服菜单问题。 It really wasn't designed for this. 它真的不是为此设计的。

The way I've seen multiple annotations attached is to use a container annotation, and then specify the items as an array. 我看到附加多个注释的方式是使用容器注释,然后将项指定为数组。

@Retention(RetentionPolicy.RUNTIME)
public @interface Menu {
    String name();
    String[] children();
}

@Retention(RetentionPolicy.RUNTIME)
public @interface MenuBar {
    Menu[] value();
}

@Retention(RetentionPolicy.RUNTIME)
public @interface MenuItem {
    String value();
}

@MenuBar(
    {
        @Menu(name="File", children= {"Save","Load","Print","Preview","Quit"}),
        @Menu(name="Print", children= {"Preview","Print"}),
        @Menu(name="Edit", children= {"Copy","Paste"})
    }
)
public class TestMenu {
    @MenuItem ("Save")
    public void save() {
        System.out.println("saved");
    }

    @MenuItem ("Load")
    public void load() {
        System.out.println("loaded");
    }
}
  • You can define default value for annotation - here's example String str() default "text"; 您可以为注释定义默认值 - 这里的示例String str() default "text";
  • You can't overcome this easily. 你无法轻易克服这一点。 You can define annotation Menus which accepts arrays of string 您可以定义接受字符串数组的注释Menus

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM