Thursday, February 16, 2012

Java Reflection: Annotations

What are Java Annotations?

Annotations is a new feature from Java 5. Annotations are a kind of comment or meta data you can insert in your Java code. These annotations can then be processed at compile time by pre-compiler tools, or at runtime via Java Reflection. The annotation can be attached to Classes, Methods,Parameters,Fields etc.

How to create custom Annotation?

To create an annotation we use the interface keyword and add an @ symbol infront of it. The @ symbol will tell the compiler that it doing some business with an annotation.

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value();
}
The @ in front of the interface marks it as an annotation. Once you have defined the annotation you can use it in your code.Here is an example of class annotation:
@MyAnnotation(value="Class Annotation")
public class MyClass {
}
The two directives in the annotation definition, @Retention(RetentionPolicy.RUNTIME) and @Target(ElementType.TYPE), specifies how the annotation is to be used. 
  @Retention(RetentionPolicy.RUNTIME) means that the annotation can be accessed via reflection at runtime. If you do not set this directive, the annotation will not be preserved at runtime, and thus not available via reflection.

@Target(ElementType.TYPE) means that the annotation can only be used ontop of types (classes and interfaces typically). You can also specify METHOD or FIELD, or you can leave the target out alltogether so the annotation can be used for both classes, methods and fields.
 

Class Annotations

You can access the annotations of a class, method or field at runtime. Here is an example that accesses the class annotations:

Class aClass = MyClass.class;
Annotation[] annotations = aClass.getAnnotations();

for(Annotation annotation : annotations){
    if(annotation instanceof MyAnnotation){
        MyAnnotation myAnnotation = (MyAnnotation) annotation;
        System.out.println("value: " + myAnnotation.value());
    }
}
You can also access a specific class annotation like this:

Class aClass = MyClass.class;
Annotation annotation = aClass.getAnnotation(MyAnnotation.class);

if(annotation instanceof MyAnnotation){
    MyAnnotation myAnnotation = (MyAnnotation) annotation;
    System.out.println("value: " + myAnnotation.value());
}

Method Annotations

Here is an example of a method with annotations:
public class MyClass {
  @MyAnnotation(value = "Method Annotation")
  public void doSomething(){}
}
 
You can access method annotations like this:

Method method = ... //obtain method object
Annotation[] annotations = method.getDeclaredAnnotations();

for(Annotation annotation : annotations){
    if(annotation instanceof MyAnnotation){
        MyAnnotation myAnnotation = (MyAnnotation) annotation;
        System.out.println("value: " + myAnnotation.value());
    }
}
 
You can also access a specific method annotation like this:

Method method = ... // obtain method object
Annotation annotation = method.getAnnotation(MyAnnotation.class);

if(annotation instanceof MyAnnotation){
    MyAnnotation myAnnotation = (MyAnnotation) annotation;
    System.out.println("value: " + myAnnotation.value());
}


Parameter Annotations

It is possible to add annotations to method parameter declarations too. Here is how that looks:


public class MyClass {
  public static void doSomethingElse(
        @MyAnnotation(value="Parameter Annotation") String parameter){
  }
}
 
You can access parameter annotations from the Method object like this:

Method method = ... //obtain method object
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
Class[] parameterTypes = method.getParameterTypes();

int i=0;
for(Annotation[] annotations : parameterAnnotations){
  Class parameterType = parameterTypes[i++];

  for(Annotation annotation : annotations){
    if(annotation instanceof MyAnnotation){
        MyAnnotation myAnnotation = (MyAnnotation) annotation;
        System.out.println("param: " + parameterType.getName());
        System.out.println("value: " + myAnnotation.value());
    }
  }
}
 
Notice how the Method.getParameterAnnotations() method returns a two-dimensional Annotation array, containing an array of annotations for each method parameter.

Field Annotations

Here is an example of a field with annotations:

public class MyClass {
  @MyAnnotation(value = "Field Annotation")
  public String myField = null;
}
 
You can access field annotations like this:

Field field = ... //obtain field object
Annotation[] annotations = field.getDeclaredAnnotations();

for(Annotation annotation : annotations){
    if(annotation instanceof MyAnnotation){
        MyAnnotation myAnnotation = (MyAnnotation) annotation;
        System.out.println("value: " + myAnnotation.value());
    }
} 
 
You can also access a specific field annotation like this:


Field field = ... // obtain method object
Annotation annotation = field.getAnnotation(MyAnnotation.class);

if(annotation instanceof MyAnnotation){
    MyAnnotation myAnnotation = (MyAnnotation) annotation;
    System.out.println("value: " + myAnnotation.value());
}



The whole tutorial is on the github:
https://github.com/abdulwaheed18/Annotation-Tutorial

Friday, January 27, 2012

How to install libflashplayer.so on linux

When we trying to install the flash player for your linux operating system, we download file libflashplayer.so but we do know where to put this file.

This article guild you step by step to install the flash player for linux.


Step 1: Go to Adobe website and download you suitable version of flash player libflashplayer.so 32-bit/64bit . Then extract it to libflashplayer.so File.

Step 2:
cd to the folder has file libflashplayer.so and install
  • If you are using FireFox:
  • sudo mv libflashplayer.so /usr/lib/mozilla/plugins/libflashplayer.so
     
  • If you are using Google Chrome + Firefox
sudo mv libflashplayer.so /usr/lib/mozilla/plugins/libflashplayer.so
    sudo mkdir /opt/google/chrome/plugins
    sudo cp /usr/lib/flashplugin-installer/libflashplayer.so 
    /opt/google/chrome/plugins
Thanks.

Wednesday, January 18, 2012

Basic Linux Commands



*) How to extract tr.gz. file
To extract one or more members from an archive :
             tar -zxvf {file.tar.gz}

For example, If your tar name is backup.tar.gz, enter the following at a shell prompt:
             tar -zxvf backup.tar.gz



*) How to run .bin file

Change the permission of the file you downloaded to be executable. Type the following command:
              $ chmod +x file.bin

Start the installation process or run .bin file.Type the following command:
             ./file.bin

For example if .bin file name is application.bin. Type:
        $ chmod +x application.bin
        $ ./application.bin



*) How to set a BASH variable equal to the output from a command?

Use $(), which I find easier to read, and allows for nesting.
        OUTPUT=$(ls -1) echo $OUTPUT


*) How to read IP Address In a Shell Script

Type the following command:

ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'


*) How to find out particular ProcessId
                  ps -ef | grep processname
                  eg: ps -ef | grep mysql


*) How to display Line number from a file
          sed -n "linenumber p" file.txt
          sed -n "3 p" file1.txt

*) How to check Hidden file
          ls -a

*) How to check port
         netstat -tulpn


*) How to find directory on linux?
        find / -name 'httpdocs' -type d

the first parameter "/" is where to look, in this case "/" it's the entire system.
-name could be -iname to ignore case also -type is not mandatory
use : man find for more option


*) How to check free RAM size ?
                  free -m


*) How to clear cache memory ? 
        echo 1 > /proc/sys/vm/drop_caches

*) How to check linux version
              cat /proc/version

*) How to check which all service is running ?
                   intictl list 
For a particular service
                   intictl list | grep nova




How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...