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
89
votes
1 answer

Android Deprecated Annotation is deprecated, what's the replacement?

According to Official Android Documentation Deprecated itself is deprecated in API level S. So what's the replacement for Deprecated which itself is deprecated? Edit: Added web archive link for historical reference.
YaMiN
  • 3,194
  • 2
  • 12
  • 36
88
votes
12 answers

Setting a JPA timestamp column to be generated by the database?

In my SQL Server 2000 database, I have a timestamp (in function not in data type) column of type DATETIME named lastTouched set to getdate() as its default value/binding. I am using the Netbeans 6.5 generated JPA entity classes, and have this in my…
James McMahon
  • 48,506
  • 64
  • 207
  • 283
88
votes
15 answers

Implement a simple factory pattern with Spring 3 annotations

I was wondering how I could implement the simple factory pattern with Spring 3 annotations. I saw in the documentation that you can create beans that call the factory class and run a factory method. I was wondering if this was possible using…
blong824
  • 3,920
  • 14
  • 54
  • 75
87
votes
4 answers

Android Retrofit Parameterized @Headers

I am using OAuth and I need to put the OAuth token in my header every time I make a request. I see the @Header annotation, but is there a way to make it parameterized so i can pass in at run time? Here is the concept @Header({Authorization:'OAuth…
jpotts18
  • 4,951
  • 5
  • 31
  • 31
87
votes
5 answers

ggplot2 - annotate outside of plot

I would like to associate sample size values with points on a plot. I can use geom_text to position the numbers near the points, but this is messy. It would be much cleaner to line them up along the outside edge of the plot. For instance, I…
jslefche
  • 4,379
  • 7
  • 39
  • 50
86
votes
8 answers

Using Hibernate UUIDGenerator via annotations

I'm using my uuid as following: @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid") @Column(name = "uuid", unique = true) private String uuid; but I'm getting a smart Hibernate warning: Using …
Martin
  • 1,460
  • 1
  • 13
  • 17
86
votes
3 answers

What is purpose of @ConditionalOnProperty annotation?

I just modified spring boot configuration, and encountered @ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views") from org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration @Bean(name = {…
user3409583
  • 887
  • 1
  • 7
  • 9
86
votes
10 answers

@Value annotation type casting to Integer from String

I'm trying to cast the output of a value to an integer: @Value("${api.orders.pingFrequency}") private Integer pingFrequency; The above throws the error org.springframework.beans.TypeMismatchException: Failed to convert value of type…
Ben
  • 60,438
  • 111
  • 314
  • 488
85
votes
5 answers

Should I use JavaDoc deprecation or the annotation in Java?

There are at the moment, two ways to mark code as depreacted in java. Via JavaDoc /** * @deprecated */ Or as an annotation: @Deprecated This is my problem - I find it a bit too much to declare both, when marking a method as deprecated when using…
corgrath
  • 11,673
  • 15
  • 68
  • 99
84
votes
3 answers

Difference between @size(max = value ) and @min(value) and @max(value)

I want to do some domain validation. In my object I have one integer. Now my question is: if I write @Min(SEQ_MIN_VALUE) @Max(SEQ_MAX_VALUE) private Integer sequence; and @Size(min = 1, max = NAME_MAX_LENGTH) private Integer sequence; If it's an…
JOHND
  • 2,597
  • 6
  • 27
  • 35
83
votes
4 answers

Adding Java Annotations at Runtime

Is it possible to add an annotation to an object (in my case in particular, a Method) at runtime? For a bit more explanation: I have two modules, moduleA and moduleB. moduleB depends upon moduleA, which doesn't depend upon anything. (modA is my…
Clayton
  • 1,967
  • 4
  • 18
  • 26
82
votes
22 answers

Annotations from javax.validation.constraints not working

What configuration is needed to use annotations from javax.validation.constraints like @Size, @NotNull, etc.? Here's my code: import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class Person { …
Darshan Patil
  • 1,049
  • 2
  • 13
  • 19
82
votes
2 answers

from __future__ import annotations

Python doc __future__ In python doc about __future__ there is a table below where it shows that annotations "optional in" 3.7.0b1 and "mandatory in" 4.0 but I am still able to use annotations in 3.8.2 without importing annotations then what is the…
80
votes
2 answers

Get list of fields with annotation, by using reflection

I create my annotation public @interface MyAnnotation { } I put it on fields in my test object public class TestObject { @MyAnnotation final private Outlook outlook; @MyAnnotation final private Temperature temperature; …
user902383
  • 8,420
  • 8
  • 43
  • 63
80
votes
5 answers

How @Target(ElementType.ANNOTATION_TYPE) works

Java annotations are marked with a @Target annotation to declare possible joinpoints which can be decorated by that annotation. Values TYPE, FIELD, METHOD, etc. of the ElementType enum are clear and simply understandable. Question WHY to use…
Gaim
  • 6,734
  • 4
  • 38
  • 58