The Source for Java Technology Collaboration
User: Password:



Eitan Suez

Eitan Suez's Blog

Proposed Annotations Enhancements

Posted by eitan on February 15, 2006 at 03:27 PM | Comments (2)

I think these should make for a nice extension to the existing Java 5 Annotation API:

  1. From an Annotation, one should be able to navigate back to the programming element that it annotates.

    For example, a method annotation clearly marked with a Target of ElementType.METHOD cannot say getAnnotatedElement().getName(),

    There are two problems here. The first is the lack of the getAnnotatedElement() method The second is the fact that an AnnotatedElement is too primitive of an interface. You cannot actually invoke getName() on an annotated element. I feel that there should exist a basic ProgrammingElement implemented by or extended by all annotated elements.

  2. The default value for an annotated element should not be restricted to a constant expression. Instead, the value should be a code block.

    For example, let's assume that I want to annotate methods to give them a caption. By default, the caption should be some kind of derivation from the method name it is bound to.

    That is,

    class MyClass
    {
      @MyMethodAt(caption="The X Method")
      public void method x()
      {
        // ..
      }
    }
    
    
    and
    
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface MyMethodAt
    {
      public String caption() 
         default
         {
           return getAnnotatedElement().name();  // see [1]
         }
    }
    

    As far as I know, this is currently not possible.

* Entry cross-posted on my personal weblog

Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • If it has behaviour it is not metadata anymore and actually belong to the concrete class. Also note that annotations are actually interfaces and more over, reflection API for annotations is implemented using dynamic proxies, so your proposal very unlikely going to be considered.


    Though it would be nice to be able to annotate code blocks inside of the class methods and have this information available in the runtime. I've suggested that some time ago but haven't got much of the positive feedback.

    Posted by: euxx on February 15, 2006 at 11:25 PM

  • I have a better proposal: ditch annotations. NEVER mix code with metadata, it only ever leads to disaster.

    Posted by: jwenting on February 16, 2006 at 12:54 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds