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

How to delete all Annotations on a MKMapView

Is there a simple way to delete all the annotations on a map without iterating through all the displayed annotations in Objective-c?
kevin Mendoza
  • 1,211
  • 2
  • 12
  • 16
104
votes
5 answers

Where should I put @Transactional annotation: at an interface definition or at an implementing class?

The question from the title in code: @Transactional (readonly = true) public interface FooService { void doSmth (); } public class FooServiceImpl implements FooService { ... } vs public interface FooService { void doSmth…
Roman
  • 64,384
  • 92
  • 238
  • 332
103
votes
2 answers

How to retrieve annotated instance from Guice's injector?

Let's say I have a module: Module extends AbstractModule { @Override protected void configure() { bind(String.class). annotatedWith(Names.named("annotation")). toInstance("DELIRIOUS"); } } and I want to test the module and…
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
103
votes
3 answers

Why doesn't a missing annotation cause a ClassNotFoundException at runtime?

Consider the following code: A.java: import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @interface A{} C.java: import java.util.*; @A public class C { public static void…
Matt McHenry
  • 20,009
  • 8
  • 65
  • 64
103
votes
8 answers

How to inject a Map using the @Value Spring Annotation?

How can I inject values into a Map from the properties file using the @Value annotation in Spring? My Spring Java class is and I tried using the $, but got the following error message: Could not autowire field: private java.util.Map Test.standard;…
yathirigan
  • 5,619
  • 22
  • 66
  • 104
102
votes
3 answers

How to create optional parameters for own annotations?

Following is the annotation code public @interface ColumnName { String value(); String datatype(); } I would like to make datatype an optional parameter, for example @ColumnName(value="password") should be a valid code.
Biju CD
  • 4,999
  • 11
  • 34
  • 55
101
votes
2 answers

Get rid of "The value for annotation attribute must be a constant expression" message

I use annotation in my code, and I try to use value which determine in run time. I define my list as static final (lst), and I add to this list some elements. When I use lst.get(i), I get compilation error: The value for annotation attribute must…
Cons
  • 1,133
  • 3
  • 8
  • 8
100
votes
8 answers

Multiple annotations of the same type on one element?

I'm attempting to slap two or more annotations of the same type on a single element, in this case, a method. Here's the approximate code that I'm working with: public class Dupe { public @interface Foo { String bar(); } …
Max A.
  • 4,842
  • 6
  • 29
  • 27
98
votes
1 answer

How to set String Array in Java Annotation

I have declared a annotation like this: public @interface CustomAnnot { String[] author() default "me"; String description() default ""; } A valid Annotation therefore would be @CustomAnnot(author="author1", description="test") What I…
Jakob Abfalter
  • 4,980
  • 17
  • 54
  • 94
97
votes
6 answers

How to supply Enum value to an annotation from a Constant in Java

I'm unable to use an Enum taken from a Constant as a parameter in an annotation. I get this compilation error: "The value for annotation attribute [attribute] must be an enum constant expression". This is a simplified version of the code for the…
user1118312
  • 1,253
  • 1
  • 9
  • 11
96
votes
4 answers

EnableWebMvc annotation meaning

I read javadoc about @EnableWebMvc. But I don't understand what this annotation mean? Can you expalin it clearly?
user2740224
92
votes
9 answers

org.hibernate.MappingException: Could not determine type for: java.util.Set

Although this question asked many times and I have already used all the suggestion but still I am getting this error. The User.java is @Entity @Table(name = "USER") public class User implements UserDetails, Serializable { private static final…
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
92
votes
4 answers

When to use @RunWith and when @ExtendWith

My team and I have been working on a bunch of microservices using Spring boot. Since the services went through JUnit and Spring Boot upgrades (We're using now Spring Boot 2 and JUnit 5), different JUnit implemented by different devs, are now using…
AR1
  • 4,507
  • 4
  • 26
  • 42
90
votes
3 answers

@Documented annotation in java

What's the purpose of @Documented annotation in java? I saw the documentation, but could not get much from it. Can someone point out with the help of an clear example
praveenkjvs
  • 931
  • 1
  • 6
  • 8
90
votes
7 answers

Error setting a default null value for an annotation's field

Why am I getting an error "Attribute value must be constant". Isn't null constant??? @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface SomeInterface { Class bar() default null;// this doesn't…
ripper234
  • 222,824
  • 274
  • 634
  • 905