简体   繁体   中英

Help with annotations

Edit--@Uri correctly pointed out that this was an abuse of annotations; 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. 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.

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? 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).

Secondly it doesn't like the multiple @Menu tags on a single class. 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.

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.

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";
  • You can't overcome this easily. You can define annotation Menus which accepts arrays of string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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