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
61
votes
10 answers

What is the use of marker interfaces in Java?

When there is nothing to implement in the marker interfaces like Serializable What is the use of implementing it?
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
61
votes
4 answers

Should I add an @Override annotation when implementing abstract methods in Java?

When overriding a non-virtual method in Java, use of the @Override annotation is recommended, but what if I implement an abstract method? Should I use @Override then as well?
Eyvind
  • 5,221
  • 5
  • 40
  • 59
60
votes
5 answers

Check a variable against Union type at runtime in Python 3.6

I'm trying to write a function decorator that uses Python 3.6 type hints to check that a dictionary of arguments respects the type hints and if not raise an error with a clear description of the problem, to be used for HTTP APIs. The problem is that…
Jacopofar
  • 3,407
  • 2
  • 19
  • 29
60
votes
4 answers

Not annotated method overrides method annotated with @NotNull

I'm implementing a custom data structure that gives me some properties of sets and other properties of lists. For most of the implemented methods though, I get this weird warning in IntelliJ IDEA on Java 7: Not annotated method overrides method…
BrDaHa
  • 5,138
  • 5
  • 32
  • 47
60
votes
2 answers

Annotating the functional interface of a Lambda Expression

Java 8 introduces both Lambda Expressions and Type Annotations. With type annotations, it is possible to define Java annotations like the following: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE_USE) public @interface MyTypeAnnotation…
Balder
  • 8,623
  • 4
  • 39
  • 61
60
votes
12 answers

Arguments Against Annotations

My team is moving to Spring 3.0 and there are some people who want to start moving everything into Annotations. I just get a really bad feeling in my gut (code smell?) when I see a class that has methods like this: (just an example - not all real…
Gandalf
  • 9,648
  • 8
  • 53
  • 88
59
votes
8 answers

Spring annotation-based DI vs xml configuration?

Recently in our team we started discussing using spring annotations in code to define spring dependencies. Currently we are using context.xml to define our dependencies. Would you give me some clues for either approach, and when one is better to be…
Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
59
votes
2 answers

How to annotate a generator in python3?

Python 3.x supports (optional) function annotations: def add_ints(x:int, y:int) -> int : return x+y I sometimes encounter problems as to how to represent a given "type" can be represented, and this time, I have a function that returns a…
Yosh
  • 2,512
  • 3
  • 24
  • 30
59
votes
3 answers

javax.annotation: @Nullable vs @CheckForNull

What is the difference between the two? Both seem to mean that the value may be null and should be dealt with accordingly i.e. checked for null. Update: The two annotations above are part of…
vitaly
  • 2,755
  • 4
  • 23
  • 35
57
votes
3 answers

RetentionPolicy CLASS vs. RUNTIME

What is the practical difference between RetentionPolicy.CLASS and RetentionPolicy.RUNTIME? It looks like both are recorded into the bytecode and both may be accessed at the run-time anyway.
Dima
  • 1,326
  • 2
  • 13
  • 19
56
votes
5 answers

How to map JSON field names to different object field names?

What is the equiv way in Jackson json annotation for the following jax-b annotations? I need to produce json rather than xml and need to know the conventional jackson annotations that is equivalently denoted in jax-b. rename a field. use getters…
Blessed Geek
  • 21,058
  • 23
  • 106
  • 176
56
votes
3 answers

How to find annotated methods in a given package?

I have a simple marker annotation for methods (similar to the first example in Item 35 in Effective Java (2nd ed)): /** * Marker annotation for methods that are called from installer's * validation scripts etc. …
Jonik
  • 80,077
  • 70
  • 264
  • 372
56
votes
9 answers

Spring @ContextConfiguration how to put the right location for the xml

In our project we are writting a test to check if the controller returns the right modelview @Test public void controllerReturnsModelToOverzichtpage() { ModelAndView modelView = new ModelAndView(); KlasoverzichtController…
David
  • 4,185
  • 2
  • 27
  • 46
55
votes
2 answers

Spring - Annotation Based Controller - RequestMapping based on query string

In Spring annotation-based controller, is it possible to map different query strings using @RequestMapping to different methods? For example @RequestMapping("/test.html?day=monday") public void writeMonday()…
njdeveloper
  • 1,465
  • 3
  • 13
  • 16
55
votes
3 answers

Inherited abstract class with JPA (+Hibernate)

How would you configure annotations in the following example code? I'd like to stick with JPA annotations only and avoid Hibernate specific dependencies. Is the code below correct? @Entity public class RefExample extends RefData { } (There will be…
Manius
  • 3,594
  • 3
  • 34
  • 44