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
375
votes
8 answers

Why does JPA have a @Transient annotation?

Java has the transientkeyword. Why does JPA have @Transient instead of simply using the already existing java keyword?
deamon
  • 89,107
  • 111
  • 320
  • 448
304
votes
23 answers

Injecting Mockito mocks into a Spring bean

I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the @Autowired annotation on private member fields. I have considered using…
teabot
  • 15,358
  • 11
  • 64
  • 79
300
votes
4 answers

Which types can be used for Java annotation members?

Today I wanted to create my first annotation interface following this documentation and I got this compiler error Invalid type for annotation member": public @interface MyAnnotation { Object myParameter; ^^^^^^ } Obviously Object cannot…
Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
296
votes
20 answers

Setting default values for columns in JPA

Is it possible to set a default value for columns in JPA, and if, how is it done using annotations?
homaxto
  • 5,428
  • 8
  • 37
  • 53
294
votes
13 answers

Scanning Java annotations at runtime

How do I search the whole classpath for an annotated class? I'm doing a library and I want to allow the users to annotate their classes, so when the Web application starts I need to scan the whole classpath for certain annotation. I'm thinking about…
Alotor
  • 7,407
  • 12
  • 38
  • 36
264
votes
2 answers

Python void return type annotation

In python 3.x, it is common to use return type annotation of a function, such as: def foo() -> str: return "bar" What is the correct annotation for the "void" type? I'm considering 3 options: def foo() -> None: not logical IMO, because None…
Tregoreg
  • 18,872
  • 15
  • 48
  • 69
261
votes
8 answers

Does Spring @Transactional attribute work on a private method?

If I have a @Transactional -annotation on a private method in a Spring bean, does the annotation have any effect? If the @Transactional annotation is on a public method, it works and open a transaction. public class Bean { public void doStuff() { …
Juha Syrjälä
  • 33,425
  • 31
  • 131
  • 183
260
votes
50 answers

intellij incorrectly saying no beans of type found for autowired repository

I have created a simple unit test but IntelliJ is incorrectly highlighting it red. marking it as an error No beans? As you can see below it passes the test? So it must be Autowired?
Robbo_UK
  • 11,351
  • 25
  • 81
  • 117
246
votes
8 answers

Why is it not possible to extend annotations in Java?

I don't understand why there is no inheritance in Java annotations, just as Java classes. I think it would be very useful. For example: I want to know if a given annotation is a validator. With inheritance, I could reflexively navigate through…
sinuhepop
  • 20,010
  • 17
  • 72
  • 107
232
votes
14 answers

How and where are Annotations used in Java?

What are the major areas that we can use Annotations? Is the feature a replacement for XML based configuration?
Biju CD
  • 4,999
  • 11
  • 34
  • 55
214
votes
5 answers

codestyle; put javadoc before or after annotation?

I know that it isn't the most vital of issues, but I just realised that I can put the javadoc comment block before or after the annotation. What would we want to adopt as a coding standard? /** * This is a javadoc comment before the annotation …
Paul McKenzie
  • 19,646
  • 25
  • 76
  • 120
206
votes
5 answers

How do different retention policies affect my annotations?

Can anyone explain in a clear way the practical differences between the java.lang.annotation.RetentionPolicy constants SOURCE, CLASS, and RUNTIME? I'm also not exactly sure what the phrase "retaining annotation" means.
xdevel2000
  • 20,780
  • 41
  • 129
  • 196
189
votes
4 answers

How does lombok work?

I met lombok today. I'm very anxious to know how it works. A Java Geek Article gives some clues but it's not perfectly clear to me: Java 6 removes apt and make javac able to manage annotations, streamlining the process to obtain a simpler …
uuidcode
  • 3,790
  • 3
  • 25
  • 30
189
votes
6 answers

How to supply value to an annotation from a Constant java

I am thinking this may not be possible in Java because annotation and its parameters are resolved at compile time. I have an interface as follows, public interface FieldValues { String[] FIELD1 = new String[]{"value1", "value2"}; } and another…
Kannan Ekanath
  • 16,759
  • 22
  • 75
  • 101
178
votes
11 answers

How to use @Nullable and @Nonnull annotations more effectively?

I can see that @Nullable and @Nonnull annotations could be helpful in preventing NullPointerExceptions but they do not propagate very far. The effectiveness of these annotations drop off completely after one level of indirection, so if you only add…
Rylander
  • 19,449
  • 25
  • 93
  • 144