Showing posts with label Annotation. Show all posts
Showing posts with label Annotation. Show all posts
Thursday, June 5, 2014
Today in this article I will show you how to create your own annotation. Here we will start with a very basic annotation and then slowly show you how to include arrays and also how to use default values. We will also show you how to yo use annotations. We have divided our tutorial on creating and using annotations in two parts. The first part meaning this one is a very basic and good one for beginners to start with.
In our first annotation we will just start with an empty annotation specifying only @target and @Retention.
import java.lang.annotation.*;

@Target(value={ElementType.FIELD})
@Retention(value=RetentionPolicy.RUNTIME)
public @interface Author {
}
In the next example you will see that when there is only one value for an annotation then we can omit the name as well as braces{} indicating arrays. The above sample is re-written as
import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Author {
}
In our next example you will see how to mention the target type when it is more than one say FIELD and METHOD.
import java.lang.annotation.*;

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Author {
}
In our next example we will deal with annotation which has annotation type elements. It looks almost like methods. We will also show you how to use this annotation in a class.
import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Author {
 String name();
}

class MyClass {
 @Author(name="Nirupam")
 int i = 10;
}
In our next and last example of today, we will show you how to mention default values of annotation type elements. It is done using keyword default . If a default value is mentioned, then you can omit adding your own value. But it does not mean that you cannot add your own value, of course you can do it.
import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Author {
 String name() default "Nirupam";
}

class MyClass {
 @Author
 int i = 10;
}
In our next and last part we will deal with creating and using advanced annotation. Till then keep coding and keep reading our articles.
Saturday, April 26, 2014
Today in this tutorial I am going to discuss with the annotations that are already defined in the Java language. Some of them are used by the Java compiler and some are used by other annotations.

Annotations used by compiler and declared in java.lang package
  • @Deprecated : This annotation is used to indicate that the element marked with it should not be used any longer. The compiler will generate warning if any program uses class, field or method marked with this annotation. It should also be documented with Javadoc annotation @deprecated. Many people make mistakes between the use of @ sign in annotation and javadoc comment. They are completely different and most importantly the annotation has upper-case while the Javadoc comment has a lower-case.
  • class DeprecationDemo{
       /**
       * @deprecated
       * defines how to use deprecated annotation
       */
       @Deprecated
       public void deprecatedMethod(){}
    }
  • @Override : This annotation is used to indicate that an element marked with it is meant to override an element of super-class. This is not necessary to override an element but helps to prevent runtime errors if Java fails to override during run-time. To know more why should you use this read our article here
  • @SupressWarnings : This annotation is used to supress some warnings that would be generated by the compiler if not used. In our example we will inherit a serializable class JPanel and not write the variable serialVersionUID which will generally result in warnings. But using this annotation will supress it.
  • @SupressWarnings("serial")
    class SupressWarningsDemo extends javax.swing.JPanel{
       
    }
    It can also be used to supress other warnings like deprecated using "deprecation" if using any deprecated elements. Also unchecked warnings using "unchecked" while interfacing with legacy codes before the inclusion of generics.
  • @SafeVarargs : This annotation when applied to a method confirms that there will be no unsafe operation with the variable argument parameter. When it is used unchecked warnings associated with varargs are supressed.
  • @FunctionalInterface : This  annotation is used to indicate that an interface is functional interface. this one is related to lambda expression and method references. This annotation is included in Java 8. It can only used with interfaces and if used with classes or enums it will create an error.
There are certain other annotations called meta-annotations that apply to other annotations. They are declared in java.lang.annotation package
  • @Retention : It is used to denote how the annotation should be stored. There are 3 ways
    • RetentionPolicy.SOURCE : Retained at source level and ignored by compiler
    • RetentionPolicy.CLASS : Retained by compiler at compile-time but ignored by JVM
    • RetentionPolicy.RUNTIME : Used by JVM during run-time.
  • @Documented : This annotation is used to indicate that whenever the particular annotation is used it should be documented using Javadoc tool. By default they are not documented
  • @Target : It is used to indicate which kind of Java elements it can be applied.
    • ElementType.ANNOTATION : Can be applied to annotation type
    • ElementType.CONSTRUCTOR : Can be applied to constructors
    • ElementType.FIELD : Can be applied to a field
    • ElementType.LOCAL_VARIABLE : Can be applied to a local variable
    • ElementType.METHOD : Can be applied to a method
    • ElementType.PACKAGE : Can be applied to a package declaration
    • ElementType.PARAMETER : Can be applied to a parameter of a method
    • ElementType.TYPE : Can be applied to any element of a class.
  • @Inherited : It indicates that the annotation type can be inherited from the super-class. When the user queries the annotation type and the class has no annotation for this type, the class' superclass is queried for the annotation type. This annotation applies only to class declarations.
  • @Repeatable : This annotation has been included in Java 8. It indicates that the annotation marked with this can be apllied more than once to the same declaration.
Friday, April 18, 2014
Today I am going to take you through the basics of annotation. This feature was introduce in JDK 5. Most of the time while writing programs you may have to provide some meta information along with your codes. In those cases annotations come into picture.
What is an annotation ?
Annotation is a metadata that provides data or information about a program which is not a part of the program itself with no direct effect on the code it annotates.

Uses of annotation

  • Information used by compiler : They can be used which carries special meaning to the compiler and helps in detecting any compile time errors.
  • Deployment-time processing : They can be used by softwares, servers to generate codes, files and also to deploy codes as in case of servlets.
  • Runtime processing : They can be used to carry information during runtime.
Annotations can be applied to declarations : declaration of classes, fields, methods etc. From Java 8, annotations can also be applied to use of types as below
  • Class intance types
  • Type-Cast
  • implements clause
  • throes exception declaration
Depending on the usage of annotation, the retention policy and target of an annotation declared is mentioned.Before we create our own annotaions, these two things must be kept in mind
  • Retention Policy - There is an enum to define named RetentionPolicy. The constants are used to define how long the annotations should be retained. It has three values : SOURCE, CLASS, RUNTIME .
  • Target - This is used to define which part of the program can be annotated with that particular annotation. There is an enum ElementType which has several constants PACKAGE, FIELD, METHOD, LOCAL_VARIABLE etc., to define which part should be targeted.
In our next tutorial we will deal with pre-defined annotations in java.

Total Pageviews

Followers


Labels

Popular Posts

free counters