Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What function do these Java annotations serve?

I'm still not clear on the purpose of annotations in Java. Initially I thought they just served as documentation. But looking at this documentation from Google App Engine Datastore, I'm not so sure. @PersistenceCapable(identityType = IdentityType.APPLICATION) looks more like a method signature.

What's the purpose of this type of annotation? What does it do?

import java.util.Date;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;

    @Persistent
    private String firstName;

    @Persistent
    private String lastName;

    @Persistent
    private Date hireDate;

    public Employee(String firstName, String lastName, Date hireDate) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.hireDate = hireDate;
    }

    // Accessors for the fields.  JDO doesn't use these, but your application does.

    public Long getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    } 

    // ... other accessors...
}
like image 705
Bijou Avatar asked Apr 30 '09 19:04

Bijou


People also ask

What are annotations in Java used for?

Annotations have a number of uses, among them: Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings. Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.

What is annotation in Java and how it works?

Annotations are used to provide supplemental information about a program. Annotations start with '@'. Annotations do not change the action of a compiled program. Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.

What is annotation process in Java?

"Annotation Processing" is a hook into the compile process of the java compiler, to analyse the source code for user defined annotations and handle then (by producing compiler errors, compiler warning, emitting source code, byte code ...). API reference: javax. annotation.


2 Answers

They're source-level metadata. They're a way of adding information to the code that's not code, and that is easily machine-processed.

In your example, they're used to configure object-relational mapping for that entity type. It's saying that for example the id field should be the primary key for that object, and that firstName, lastName, and hireDate should be stored in the database. (To tell those fields apart from some transient object state.)

The GAE support for JDO needs to know what objects you'll try to store in the database. It does this by looking through the classes in your code, looking for the ones that are annotated with @PersistenceCapable.

Commonly, they're used to replace where you'd use external configuration files before; the Java standard library has tools to read the annotations in your code, which makes them much easier to process than rolling your own configuration file plumbing, and gets you IDE support for free.

like image 114
millimoose Avatar answered Oct 14 '22 16:10

millimoose


Annotations can be processed with the Annotation Processing Tool APIs to auto-generate boilerplate code.

like image 26
Michael Myers Avatar answered Oct 14 '22 17:10

Michael Myers