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 plain stupid.

They are good for binding, however I think that I will use them to bind text data with methods (part of @Menu ("File"), since it is more explicit and flexible than reflecting the method name. In addition, I learned a bit from this I will send the code here in a few days as an answer.

- original post -

I have not used these newfangled annotations, but they look surprisingly interesting. It's hard for me to understand the syntax though (or, in a more appropriate way, the best way to use it).

When writing code in response to this question, it occurred to me that my methods were pretty outdated.

I used to parse strings to determine my method structure and then used reflection to pass it to classes, but I think annotations can significantly improve the menu structure.

I would like to replace my test class in a file with something like the following:

@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 to a method that produces and returns the JMenuBar associated with the class instance without further input.

The first problem is that I cannot figure out how to pass the "default" strings, they all want to have (attribute = "value") instead of just ("value"), can this be done? I can live without him, but it's a bit verbose. It would be even better if I could get rid of the partners and / or quotes, but I do not hold my breath (I think that for this I would have to define an individual interface for each menu item, which is unacceptable).

Secondly, he doesn’t like several @Menu tags for one class. I could get around this by parsing one line, but I was wondering if there was another way.

Most importantly, is there a library that does this already? (If no one comes up, I will post the code in this thread when I earn it, if someone likes it.)

+4
source share
3 answers

The way I saw a few attached annotations is to use the container annotation and then specify the elements 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"); } } 
+2
source

I know that I will climb for this, but I really think that people are starting to overload the annotation mechanism in Java.

Everything that was developed for him was supposed to be a mechanism for providing meta-information about classes and methods for the compiler or programming support tools (for example, for testing the infrastructure, checking the model, code generators, etc.)

It was not intended for real production-oriented code, macro-metaprogramming and all that. This is as complex as using preprocessor macros in C instead of actual functions.

If menus are first-class objects in your program, I really don't feel like you should use the annotation mechanism for them.

For your specific questions, you can easily determine the default value. However, you cannot start doing things like nested annotations to overcome the menu problem. It really was not meant to be.

+4
source
  • You can define a default value for annotation - here's an example String str() default "text";
  • You cannot easily overcome this. You can define a Menus annotation that accepts string arrays
0
source

All Articles