views:

612

answers:

9

Possible Duplicates:
How and where are Annotations used in Java?
Java beans, annotations: What do they do? How do they help me?

Over and over, I read about Java 5's annotations being an 'advanced feature' of the language. Until recently, I haven't much used annotations (other than the usual @Override, &c), but work on a number of webservice-related projects has forced my hand. Since I learned Java pre-5, I never really took the time to sit down and grok the annotation system.

My question- do you guys actually use annotations? How helpful are they to you, day-to-day? How many StackOverflow-ers have had to write a custom annotation?

+3  A: 

Perhaps the most useful and used case of Java Annotations is to use POJO + Annotation instead of xml configuration files

I use it a lot since (as you already stated) if you use a web framework (like spring or seam) they usually have plenty of annotations to help you.

I have recently wrote some annotations to build a custom statemachine, validations purpose and annotations of annotations (using the metadata aspect of it). And IMO they help a lot making the code cleaner, easier to understand and manage.

Diego Dias
+1  A: 

Current project (200KLOC), annotations I use all the time are:

@NotNull / @Nullabe
@Override
@Test
@Ignore
@ThreadSafe
@Immutable

But I haven't written yet my own annotation... Yet!

Webinator
+1  A: 

Annotations are just for frameworks and they do work great in hibernate/jpa. until you write a framework that needs some extra information from passed to it objects you wont write your own annotations.

however there is new and cool junit feature that let you write your own annotations in tests - http://blog.mycila.com/2009/11/writing-your-own-junit-extensions-using.html

01
That is, until you realize all the useful stuff you can do with them if you write them yourself!
Matthew Flynn
A: 

I use annotations daily and they are wonderful. I use them with jsf and jpa and find them much easier to manage and work with than the alternative XML configurations.

kgrad
+1  A: 

I have used annotations for:

  • Hibernate, so I don't need to keep those huge XML files;
  • XML Serialization, so I describe how the object should be rendered in the object itself;
  • Warning removal for warnings that I don't want to disable (and for which the particular case cannot be properly solved).

I have created annotations for:

  • Describe the state required in order for my method to be executed (for example, that a user must be logged in);
  • Mark my method as executable from a specific platform with additional properties for that platform;
  • And probably some other similar operations.

The annotations that I have created are read with Reflection when I need to get more information about the object I am working with. It works and it works great.

Ravi Wallau
A: 

I use annotations for describing in my state synchronisation system what classes are specialisations of the annotated classes, and the environment in which they should be used (when an object is created, it will work out for its entity lists which are the best entity classes to create for the nodes on the network; i.e., a Player entity for a server node is instead a ServerPlayer entity). Additionally, the attributes inside the classes are described and how they should be synchronised across machines.

Chris Dennett
A: 

We have a report builder as part of our webapp. A user can add a large number of widgets that are all small variations on the same set of themes (graphs, tables, etc).

The UI builds itself based on custom annotations in the widget classes. (e.g. an annotation might contain default value and valid values that would render as a dropdown. Or a flag indicating if the field is mandatory).

It has turned out be be a good way to allow junior devs crank out widgets without having to touch the UI.

z5h
A: 

We just used annotations to create a simple way to validate our POJO's:

@NotEmpty
@Pattern(regex = "I")
private String value;

Then we run this through the Hibernate validator which will do all our validation for us:

import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;

public void validate(T validateMe) {
    ClassValidator<T> validator = new ClassValidator<T>((Class<T>) validateMe.getClass());
    InvalidValue[] errors = validator.getInvalidValues(validateMe);
}

Works great. Nice clean code.

Marcus
A: 

We use custom annotations as a part of our integration testing system:

@Artifact: Associates an integration test with an issue ID. Trace matrices are then automatically generated for our testing and regulatory departments.

@Exclude: Ignores an integration test based on the browser platform / version. Keeps the IE 6 bugs from clogging up our nightly test runs :)

@SeleniumSession: Defines test specific selenium settings for each integration test.

They are a very powerful tool, but you gotta use them carefully. Just have a look at those early .NET Enterprise class files to see what a nightmare mandatory annotations can be :)

Andrew Cowan