17

i want to ask why are the java annotations used so much... I know that they replaced xml configuration in for example jpa, but why is this kind configuration used at all? Consider this piece of code:

@Entity 
class Ent{
   // some fields
}
//... somewhere in the other file far far away
class NonEnt{
   // whatever here
}
Now, when I try to put this in persistence context, with EntityManager's persist method, I get runtime error(better would be to get compile error) with trying to persist NonEnt instance. There is obvious solution for me, force the entities to implement some no-method interface instead of using @Annotations. But this isn't popular among framework designer, what is the drawback of this solution?
Thanks for answering...
Neeme Praks
  • 8,956
  • 5
  • 47
  • 47
ryskajakub
  • 6,351
  • 8
  • 45
  • 75
  • Its a design question for the developers of this library. You are right that compile time errors are better than runtime errors. Perhaps the java compiler should have a way to specify the annotations of an argument? Until it does, a marker interface would be better. – Peter Lawrey Nov 26 '10 at 13:23
  • 1
    There's too many request to close questions nowadays...why don't people justify reasons to close a question? – Buhake Sindi Nov 26 '10 at 13:33
  • 1
    Backward compatibility is the cause of so many of these deisgn decisions. When they had XML files only, they may have wanted to not need to modify the java clases. However, now they cannot mandate an interface without breaking older versions. With the annotation, you can use this OR an XML file. To have an interface, you would have to change all code to have it, even the users of XML. – Peter Lawrey Nov 26 '10 at 13:35
  • 1
    Off topic: the mention of Java and XML reminded me of this little gem: http://www.reddit.com/r/programming/comments/eaqgk/java_is_a_dsl_for_taking_large_xml_files_and/ – Sean Patrick Floyd Nov 26 '10 at 14:20

5 Answers5

11

When compared to marker interfaces, annotations have some advantages:

  • they can be parameterized
  • they are more fine grained - you can attach them not only to classes but also to other class elements (fields, methods, method arguments, etc)

Annotations are also supposedly less intrusive, but this point is matter of taste and debatable.

See also:

Community
  • 1
  • 1
Neeme Praks
  • 8,956
  • 5
  • 47
  • 47
5

The use of annotations is a lot less invasive than forcing the client to implement a interface or extend a class.

willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
3

There is obvious solution for me,

What you describe is called a "marker interface" and it's an abuse of the interface concept. I suspect the only reason why you consider it obvious is because of Serializable - which only exists because there were no annotations at that time.

force the entities to implement some no-method interface instead of using @Annotations. But this isn't popular among framework designer, what is the drawback of this solution?

What are its advantages? Annotations have the huge advantage that they can have parameters, and they are much more fine-grained. Marker interfaces only work at the class level.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • @coubeatczech true, but that's a huge price for a small gain. If you want compile errors with annotations, use AspectJ. It lets you define custom compiler errors, based on many different pointcuts, including annotations (or a lack thereof) – Sean Patrick Floyd Nov 26 '10 at 14:22
  • 1
    you can also achieve the same with annotation processors (apt) – Sean Patrick Floyd Nov 26 '10 at 17:01
3

Citing the java tutorial:

Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.

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.
  • Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
  • Runtime processing — Some annotations are available to be examined at runtime.

As you can see, annotations are a way of specifying meta-data about your types in java, including interfaces, they are in no way a replacement for them.

Jose Diaz
  • 5,353
  • 1
  • 31
  • 29
0

Java annotation are really helpful when you want to add some additional information to your class, method or instance variable. There are a lot of libraries which use these annotations heavily. These annotations keep the code simple and readable with the power of making changes to the code at runtime.

For example if you have used lombok library, which creates setter, getter and constructor at compile time and saves you lines of code and time.

When compiler executes the code, lomok searches for all the fields marked with @Setter or @Getter annotation and add setter and getter for that field in the class.

One other example is Junit test runner. How junit differentiates between normal helper method in test class and a test. To differentiate between the two it uses @Test annotation.

This tutorial explains how you can use java annotations to create you own test runner.

Ajit Singh
  • 2,436
  • 23
  • 27