Questions tagged [annotations]

In programming, annotations are used to add information to a code element which cannot be expressed by the type system.

In programming, annotations are used to add information to a code element which cannot be expressed by the type system.

Java annotations

Up to annotations were only usable inside comments and were used for denoting special information like the author of a class or method or references to other entities.

In some cases they were also used for code generation, by facilitating the toolchain.

As these annotations were part of the code, it was not possible to use them in any way at run time since they were not part of the byte code.

With , annotations became a proper part of java syntax. Annotations can be defined using a syntax similar to the definition of interfaces. They can be used to annotate classes, methods, fields, parameters and packages.

Depending on the definition an annotation is available in source code, byte code or run time. Therefore they can be used for code generation, byte code manipulation at class loading time and via reflection at run time.

For info more see Wiki page and docs.oracle.com

C# Attributes

Attributes are a similar concept to Java annotations, they provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). Once associated with a program entity, the attribute can be queried at run time and used in any number of ways.

13026 questions
172
votes
12 answers

What are good uses for Python3's "Function Annotations"?

Function Annotations: PEP-3107 I ran across a snippet of code demonstrating Python3's function annotations. The concept is simple but I can't think of why these were implemented in Python3 or any good uses for them. Perhaps SO can enlighten…
agscala
  • 3,835
  • 5
  • 27
  • 25
166
votes
2 answers

Combining multiple @SuppressWarnings annotations - Eclipse Indigo

So the issue is being able to combine multple warning suppressions so that each item doesn't need its own @SuppressWarnings annotation. So for example: public class Example public Example() { GO go = new GO(); // unused .... …
knownasilya
  • 5,998
  • 4
  • 36
  • 59
163
votes
10 answers

Can't find @Nullable inside javax.annotation.*

I want use @Nullable annotation to eliminate NullPointerExceptions. I found some tutorials on the net, I noticed that this annotation comes from the package javax.annotation.Nullable; but when I import it a compilation error is generated: cannot…
user2354035
163
votes
5 answers

@RequestBody and @ResponseBody annotations in Spring

Can someone explain the @RequestBody and @ResponseBody annotations in Spring 3? What are they for? Any examples would be great.
leo
  • 3,045
  • 5
  • 24
  • 23
162
votes
4 answers

@Column(s) not allowed on a @ManyToOne property

I have a JPA entity with a property set as @ManyToOne @Column(name="LicenseeFK") private Licensee licensee; But when I deploy on JBoss 6 the application throws an error saying: org.hibernate.AnnotationException: @Column(s) not allowed on a…
newguy
  • 5,668
  • 12
  • 55
  • 95
161
votes
7 answers

Which annotation should I use: @IdClass or @EmbeddedId

The JPA (Java Persistence API) specification has 2 different ways to specify entity composite keys: @IdClass and @EmbeddedId. I'm using both annotations on my mapped entities, but it turns out to be a big mess to people who aren't very familiar…
sakana
  • 4,251
  • 5
  • 25
  • 20
159
votes
2 answers

putting current class as return type annotation

In python 3 I can make arguments and return type annotations. Example: class Graph: def __init__(self, V: int, E: int, edges: list): pass @classmethod def fromfile(cls, readobj: type(sys.stdin)): pass def V(self) ->…
sasha.sochka
  • 14,395
  • 10
  • 44
  • 68
155
votes
25 answers

Hibernate Annotations - Which is better, field or property access?

This question is somewhat related to Hibernate Annotation Placement Question. But I want to know which is better? Access via properties or access via fields? What are the advantages and disadvantages of each?
Martin OConnor
  • 3,583
  • 4
  • 25
  • 32
151
votes
5 answers

Is there something like Annotation Inheritance in java?

I'm exploring annotations and came to a point where some annotations seems to have a hierarchy among them. I'm using annotations to generate code in the background for Cards. There are different Card types (thus different code and annotations) but…
javydreamercsw
  • 5,363
  • 13
  • 61
  • 106
148
votes
5 answers

How to parameterize @Scheduled(fixedDelay) with Spring 3.0 expression language?

When using the Spring 3.0 capability to annotate a scheduled task, I would like to set the fixedDelay as parameter from my configuration file, instead of hard-wiring it into my task class, like currently... @Scheduled(fixedDelay = 5000) public void…
ngeek
  • 7,733
  • 11
  • 36
  • 42
144
votes
2 answers

When annotating a class with @Component, does this mean it is a Spring Bean and Singleton?

Being fairly new to Spring I have a question about annotating a class. When annotating a class with @Component does this mean this class will be a Spring Bean and by default a singleton?
Marco
  • 15,101
  • 33
  • 107
  • 174
139
votes
10 answers

How to annotate MYSQL autoincrement field with JPA annotations

Straight to the point, problem is saving the object Operator into MySQL DB. Prior to save, I try to select from this table and it works, so is connection to db. Here is my Operator object: @Entity public class Operator{ @Id @GeneratedValue …
trivunm
  • 1,643
  • 4
  • 19
  • 21
138
votes
10 answers

Why does Eclipse complain about @Override on interface methods?

I have an existing project that uses @Override on methods that override interface methods, rather than superclass methods. I cannot alter this in code, but I would like Eclpse to stop complaining about the annotation, as I can still build with…
Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
136
votes
16 answers

Xml configuration versus Annotation based configuration

In a few large projects i have been working on lately it seems to become increasingly important to choose one or the other (XML or Annotation). As projects grow, consistency is very important for maintainability. My questions are: what are the…
abarax
  • 6,229
  • 8
  • 28
  • 30
132
votes
10 answers

Is it possible to read the value of a annotation in java?

this is my code: @Column(columnName="firstname") private String firstName; @Column(columnName="lastname") private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { …
BeeS
  • 1,329
  • 2
  • 9
  • 3