Package net.java.sezpoz
Support for declaring, creating, and inspecting indices of annotated Java elements.
For example, to permit registration of simple menu items, while making it possible to prepare a menu without loading any of them until they are actually selected:
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.SOURCE) @Indexable(type=ActionListener.class) public @interface MenuItem { String menuName(); String itemName(); String iconPath() default ""; }A concrete registration might look like:
@MenuItem(menuName="File", itemName="Print", iconPath=".../print.png") public class PrintAction extends AbstractAction { public void actionPerformed(ActionEvent e) {...} }Alternatively:
public class Actions { @MenuItem(menuName="File", itemName="Print") public static Action print() {...} }or even:
public class Actions { @MenuItem(menuName="File", itemName="Print") public static final Action PRINT = ...; }To create the index on JDK 6, just compile your sources normally with javac. If using JDK 5, simply run apt instead of/in addition to javac. (The processor is in the same JAR as this API and should be autodetected.)
Usage is then simple:
for (final IndexItem<MenuItem,ActionListener> item : Index.load(MenuItem.class, ActionListener.class)) { JMenu menu = new JMenu(item.annotation().menuName()); JMenuItem menuitem = new JMenuItem(item.annotation().itemName()); String icon = item.annotation().iconPath(); if (!icon.equals("")) { menuitem.setIcon(new ImageIcon(icon)); } menuitem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { item.instance().actionPerformed(e); } catch (InstantiationException x) { x.printStackTrace(); } } }); }If you want to see what is getting loaded and when, configure
java.util.logging
so that the logger
named net.java.sezpoz
is visible at
Level.FINER
or above.-
Class Summary Class Description Index<A extends java.lang.annotation.Annotation,I> Represents an index of a single annotation.IndexItem<A extends java.lang.annotation.Annotation,I> One index item. -
Error Summary Error Description IndexError Error thrown when loading all or part of an index fails. -
Annotation Types Summary Annotation Type Description Indexable Marker for an annotation type which can be indexed.