views:

169

answers:

3

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

A: 
  • 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
Yoni Roit
+4  A: 

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 (e.g., 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.

Uri
Interesting observation. I started out thinking about using annotations to facilitate the binding process for my menus, having them define the menus themselves was just a second step. It's better than binding to method names, and better than repetitive "new Menu()" code. Other alternatives?
Bill K
Well, you could use the annotations as the input to a custom 'menu binding generator' which produces actual code. But I don't see how this is any different from other presentation/logic separation technology, such as XAML, and saves a lot of boiler plate code if you use it a lot.
Mike Houston
In situations like this, I'd go with XML if it was really necessary.Annotations made more sense than XML in things like Hibernate, where the code is already in place.However, here we're talking about generating UI elements.
Uri
Although I've used XML/string binding before (as in my example code in the link), It's irritating if it's in a different file. On the other hand, this approach would be virtually impossible to i18nalize. Hmm. Maybe a combination of XML and annotations for binding?
Bill K
I'm not sure I understand what you mean. Why would XML be more difficult to internationalize ? If anything, by putting UI labels as annotations you're binding yourself to specific version. Can't you write code that generates a menu class from XML?
Uri
Not downvoted, up voted. I agree completely, annotation are meta data. IMO, if you can't replace an annotation with a marker interface, they you've abused the feature (yes, Spring 2.5, I'm talking to you, you horrible "configuration all over the code" framework).
Ran Biron
Annotations can be used quite well for code generation. For example, see http://code.google.com/p/javadude/wiki/AnnotationsPropertyExample1Rather than writing code to say "I am a bean", you declare it, and have a superclass ("isA") generated for you to match the spec.
Scott Stanchfield
@Uri, I was agreeing with you saying "my annotation approach was more difficult to internationalize", I just didn't phrase it well. I would accept this as the correct answer (it is) but since it didn't actually answer the question I asked, I just upvoted it.
Bill K
+2  A: 

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");
    }
}
Mike Houston
awesome. I figured out the default thing by using "value()" which should make things a lot easier if I combine that with this. Now I have to figure out how to read them all... Thank you.
Bill K