View RSS Feed

Annotations

  1. Override annotation

    by , 11-01-2011 at 06:02 PM (My Java Tips)
    Tiger provides many built-in annotation types and Override is one of them. Override should be used only on methods to indicate that the annotated method is overriding a method in a superclass. An example is presented below:





    Java Code:
    package com.domian.a.test;
    
    public class OverrideTester {
    
      public OverrideTester() { }
    
      @Override
      public String toString() {
        return super.toString() + " [Override Tester
    ...
    Categories
    Annotations
  2. Defining own annotation type

    by , 10-31-2011 at 05:49 PM (My Java Tips)
    The Java language supports defining new annotation types. An annotation type looks similar to an ordinary class, but it has some unique properties. One can use it with the at sign in the classes to annotate your other Java code.


    Its right to say that defining a new annotation type is similar to creating an interface. We have to precede the interface keyword with the @ sign.

    The following example shows the simplest possible annotation type:

    Java Code:
    package com.domain.tiger.a;
    ...
    Categories
    Annotations
  3. Custom annotations - Adding a member

    by , 10-31-2011 at 05:45 PM (My Java Tips)
    Annotation types can have member variables. You will understand its importance when you use annotations for metadata, not just raw documentation. These member variables provides very useful information to code-analysis tools.




    Data members in annotation types are set up to work using limited information. The way to add member is to define a single method that should be named after the member which you want to allow for. The data type should be the return value of the ...
    Categories
    Annotations
  4. Custom annotations - default values

    by , 10-31-2011 at 05:40 PM (My Java Tips)
    I'll focus on setting some default values for the annotation.


    Yes, you want the users to specify values for the annotations but only if their values differ some default values. Default values should be provided to make things easier.

    The following example shows how one can associate default values with custom annotations.

    Java Code:
    package com.domain.tiger.a;
    
    // Annotation type with default values
    public @interface GroupTODO {
    ...
    Categories
    Annotations
  5. Annotating an annotation

    by , 10-31-2011 at 05:32 PM (My Java Tips)
    I will briefly write about annotating annotations. You should have good idea about the set of predefined annotation types we have seen in the last few posts.


    When you start writing your own annotation types, the main purpose of your annotation types is to provide basic documentation. Also you write annotation types that are specific to a certain member type,
    or perhaps a certain set of member types. This requires you to supply some sort of metadata on your annotation type, ...
    Categories
    Annotations
  6. Adding public documentation

    by , 10-31-2011 at 05:24 PM (My Java Tips)
    Documented is a meta-annotation and its a marker annotation. A marker annotations have no member variables.


    Documented annotation is used to indicate that an annotation should appear in the Javadoc for a class. Remember that the default behavior is that annotations are not included in Javadocs. The presented example shows what the Documented meta-annotation looks like in use:

    Java Code:
    package com.domain.tiger.a;
    
    import java.lang.annotation.Documented;
    ...
    Categories
    Annotations
  7. Setting up inheritance

    by , 10-31-2011 at 05:19 PM (My Java Tips)
    Inherited is a meta-annotation that is complicated as compared to other annotations. It is least-often used.


    Let me present a scenarios for better understanding: Suppose that you use your custom annotation called InProgress to mark a class as being in progress.
    If the Documented meta-annotation is applied correctly, then this will show up in the Javadoc. All is well till here.

    Suppose you write a new class and extend the in-progress class. Now remember one this: ...
    Categories
    Annotations
  8. Java Persistence API - annotations

    by , 10-31-2011 at 05:15 PM (My Java Tips)
    The Java Persistence API heavily depends on the metadata annotations. The API consists of:

    Java Persistence API
    The Java Persistence query language
    Metadata annotations


    An entity is a persistence object. It is coded as a POJO, and marked as an entity with the @Entity (javax.persistence.Entity) annotation. By default, all properties/fields are persisted into the datastore, except those marked with the @Transient annotation. Following example shows few annotations ...
    Categories
    Annotations
  9. Categories of annotations

    by , 10-31-2011 at 04:57 PM (My Java Tips)
    There are three categories of annotations namely Marker, Single-Value and Full annotation.


    Let me present these one by one:

    Marker annotations

    There annotations have no variables. These are identified by name, with no additional data supplied. For example:
    Code:


    @SingleValueAnnotation

    Single-value annotations

    These are similar to markers, but also provide a single piece of data. You can only ...
    Categories
    Annotations