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
80
votes
4 answers

How do Java method annotations work in conjunction with method overriding?

I have a parent class Parent and a child class Child, defined thus: class Parent { @MyAnnotation("hello") void foo() { // implementation irrelevant } } class Child extends Parent { @Override foo() { //…
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
78
votes
1 answer

Which @NonNull Java annotation to use

What is the best 'NonNull' annotation? "Best" in the sense of Standard way e.g. future proofness (e.g. support by standard jdk etc.) Support of IDE's (shows up in java doc to indicate the usage for developers) Support by static analysis tools like…
Lonzak
  • 9,334
  • 5
  • 57
  • 88
77
votes
9 answers

BeanNotOfRequiredTypeException due to autowired fields

I'm still new to Spring MVC and while building my test project, I got this message from Tomcat logs: SEVERE: Exception sending context initialized event to listener instance of class…
Valknut
  • 1,010
  • 2
  • 15
  • 19
76
votes
10 answers

How to create an instance of an annotation

I am trying to do some Java annotation magic. I must say I am still catching up on annotation tricks and that certain things are still not quite clear to me. So... I have some annotated classes, methods and fields. I have a method, which uses…
carlspring
  • 31,231
  • 29
  • 115
  • 197
74
votes
3 answers

What does @Override mean?

public class NaiveAlien extends Alien { @Override public void harvest(){} } I was trying to understand my friend's code, and I do not get the syntax, @Override in the code. What does that do and why do we need in coding? Thanks.
Woong-Sup Jung
  • 2,337
  • 4
  • 21
  • 20
74
votes
3 answers

What is the use of @Order annotation in Spring?

I have come across a glance of code which uses @Order annotation. I want to know what is the use of this annotation with respect to Spring Security or Spring MVC. Here is an example: @Order(1) public class StatelessAuthenticationSecurityConfig…
Qasim
  • 9,058
  • 8
  • 36
  • 50
73
votes
4 answers

What is the use of @EnableWebSecurity in Spring?

As per the Spring documantation: Add this annotation to an @Configuration class to have the Spring Security configuration defined in any WebSecurityConfigurer or more likely by extending the WebSecurityConfigurerAdapter base class and overriding…
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
73
votes
6 answers

How to use an array constant in an annotation

I would like to use constants for annotation values. interface Client { @Retention(RUNTIME) @Target(METHOD) @interface SomeAnnotation { String[] values(); } interface Info { String A = "a"; String B = "b"; …
Thomas Jung
  • 32,428
  • 9
  • 84
  • 114
72
votes
3 answers

What does the length attribute do when set on the @Column JPA annontation?

What exactly does setting the length on a column do in JPA? @Column(name = "middle_name", nullable = false, length = 32) public String getMiddleName() { return this.middleName; } I understand that you can use the annotations to generate the…
James McMahon
  • 48,506
  • 64
  • 207
  • 283
69
votes
4 answers

JPA Criteria Tutorial

I've been trying to find a JPA Criteria API tutorial but haven't been much successful. Do you know about any for beginners? I'd like to start using it in an Java5/Maven app to build complex search queries.
John Manak
  • 13,328
  • 29
  • 78
  • 119
69
votes
5 answers

Use Enum type as a value parameter for @RolesAllowed-Annotation

I'm developing a Java enterprise application, currently doing Java EE security stuff to restrict access for particular functions to specific users. I configured the application server and everything, and now I'm using the RolesAllowed-annotation to…
Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
69
votes
1 answer

How to use @inherited annotation in Java?

I am not getting the @Inherited annotation in Java. If it automatically inherits the methods for you then if I need to implement the method in my own way then what about that ? How does will it come to know my way of implementation ? Plus it is said…
StrugglingCoder
  • 4,781
  • 16
  • 69
  • 103
68
votes
4 answers

spring autowiring with unique beans: Spring expected single matching bean but found 2

I am trying to autowire some beans (for dependency injection) using Spring for a webapp. One controller bean contains another bean which in turn holds a hashmap of another set of beans. For now the map only has one entry. When i run in tomcat and…
Rob McFeely
  • 2,823
  • 8
  • 33
  • 50
68
votes
3 answers

What's the difference between the name argument in @Entity and @Table when using JPA?

I'm using JPA2 and both @Entity and @Table have a name attribute, e. g.: @Entity(name="Foo") @Table (name="Bar") class Baz What should I use, which ones are optional? In my specific case I have a class User and a class Group, which have additional…
soc
  • 27,983
  • 20
  • 111
  • 215
68
votes
1 answer

collections.Iterable vs typing.Iterable in type annotation and checking for Iterable

I found that in Python both collections.Iterable and typing.Iterable can be used in type annotation and checking for whether an object is iterable, i.e., both isinstance(obj, collections.Iterable) and isinstance(obj, typing.Iterable) works. My…
Benjamin Du
  • 1,391
  • 1
  • 17
  • 25