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

How to get annotations of a member variable?

I want to know a class's some member variable's annotations , I use BeanInfo beanInfo = Introspector.getBeanInfo(User.class) to introspect a class , and use BeanInfo.getPropertyDescriptors() , to find specific property , and use Class type =…
smallufo
  • 11,516
  • 20
  • 73
  • 111
68
votes
3 answers

What do Java annotation ElementType constants mean?

java.lang.annotation.ElementType: A program element type. The constants of this enumerated type provide a simple classification of the declared elements in a Java program. These constants are used with the Target meta-annotation type to specify…
Action Jackson
  • 853
  • 1
  • 8
  • 9
68
votes
3 answers

java custom annotation: make an attribute optional

I defined my own custom annotation @Target(value={ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface MyCustomAnnotation { Class myType(); } how, if at all, can I make the attribute optional
flybywire
  • 261,858
  • 191
  • 397
  • 503
67
votes
5 answers

Unnecessary @SuppressWarnings("unused")

I'm getting a compiler warning for the @SuppressWarnings annotation in eclipse for the code: @Override public boolean doSomething(@SuppressWarnings("unused") String whatever) throws AnException { throw new AnException("I'm still in bed and can't…
Edd
  • 8,402
  • 14
  • 47
  • 73
67
votes
5 answers

Telling IntelliJ IDEA which methods not to identify as unused

IntelliJ IDEA has a handy feature to detect unused methods and show them in grey, hinting a potential warning for dead code. Some methods, however, are not executed directly but via reflection. A good example would be @RequestMapping-annotated…
mindas
  • 26,463
  • 15
  • 97
  • 154
66
votes
6 answers

How to extend Java annotation?

In my project I use pre-defined annotation @With: @With(Secure.class) public class Test { //.... The source code of @With: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface With { Class[] value() default…
bvitaliyg
  • 3,155
  • 4
  • 30
  • 46
66
votes
3 answers

Inject and Resource and Autowired annotations

What's the difference between @Inject and @Resource and @Autowired annotations? When should we use each of them?
oxygenan
  • 1,023
  • 2
  • 12
  • 21
66
votes
3 answers

Spring MVC: difference between and tags?

Some days ago I began to study this Spring Hello World Tutorial: http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/ In this tutorial Spring DispatcherServlet is configured using the spring-servlet.xml file, this…
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
64
votes
17 answers

Java Getters and Setters

Is there a better standard way to create getters and setters in Java? It is quite verbose to have to explicitly define getters and setters for each variable. Is there a better standard annotations approach? Does Spring have something like this? Even…
Verhogen
  • 27,221
  • 34
  • 90
  • 109
63
votes
3 answers

@WebServlet annotation with Tomcat 7

In my application, I had a servlet which was defined like this in the web.xml: Notification Servlet NotificationServlet
Dvora
  • 1,175
  • 1
  • 16
  • 26
63
votes
3 answers

How to persist a List of Strings in Hibernate?

I seem to have problems with mapping a List in Hibernate. In our project there a class Card with contains a class Answer with Answer containing a List. Is a List mappable by Hibernate using annotations? I mean, since it does not have…
TeaOverflow
  • 2,468
  • 3
  • 28
  • 40
63
votes
3 answers

What is the significance of @javax.persistence.Lob annotation in JPA?

When should I use @javax.persistence.Lob annotation in JPA? What datatypes can be annotated by this annotation?
Dev
  • 13,492
  • 19
  • 81
  • 174
62
votes
3 answers

Is it possible to make a Java annotation's value mandatory?

Java allows the definition of values in annotations, for example: public @interface MyAnnotation { int MyValue(); } Although it is possible to set a default value for the MyValue annotation, I was wondering whether it is possible to make it…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
62
votes
2 answers

@interface default declaration usage in Java

I have just discovered this feature. Declaring an interface using the "@interface" syntax allows you to put a default value. public @interface HelloWorld { public String sayHello() default "hello world"; } This is something new for me. How…
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
62
votes
8 answers

How to correctly specify a default value in the Spring @Value annotation?

Initially, I have the following spec: @Value("#{props.isFPL}") private boolean isFPL=false; This works fine correctly getting the value from the property file: isFPL = true However, the following expression with default results in the…
Alex
  • 7,007
  • 18
  • 69
  • 114