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
6
votes
1 answer

Symfony3 Hook Up Annotation Routes

I'm writing my own PHP framework built on top of Symfony components as a learning exercise. I followed the tutorial found at http://symfony.com/doc/current/create_framework/index.html to create my framework. I'd now like to wire up my routes against…
nfplee
  • 7,643
  • 12
  • 63
  • 124
6
votes
4 answers

How do I retrieve only the ID instead of the Entity of an association?

I have a class that looks something like this: @Entity public class EdgeInnovation { @Id public long id; @ManyToOne public NodeInnovation destination; @ManyToOne public NodeInnovation origin; } and another one that looks…
jørgen k. s.
  • 135
  • 1
  • 2
  • 8
6
votes
1 answer

GET request in URL that has params appended by `&` using Retrofit

How to make HTTP GET request in URL that has params appended by & using Retrofit in Android? URL: http://api.com?name=remote&class=TV Currently, i am using: @GET("http://api.com?name={name}&class={class}") Call get( …
Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
6
votes
2 answers

Gradle - Executing custom annotation processor during compile time

I have a custom annotation processor (that extends AbstractProcessor) which adds a properties file to the project based on the annotations. I want this to be run everytime when a compilation is happening. The project is a java project using…
ennovation
  • 366
  • 2
  • 9
6
votes
1 answer

What is the use of RetentionPolicy.CLASS and RetentionPolicy.SOURCE?

What is the intended need for RetentionPolicy.CLASS and RetentionPolicy.SOURCE. In which annotation scenario, We can use these? I wanted some examples. From the Java doc: CLASS: Annotations are to be recorded in the class file by the compiler but…
6
votes
2 answers

Using a default class literal value on an annotation

I want to annotate some of the fields of a given bean class with the following annotation: @Target({FIELD}) @Retention(RUNTIME) public @interface Process { Class using() default…
GaryF
  • 23,950
  • 10
  • 60
  • 73
6
votes
1 answer

Hibernate and Inheritance (TABLE_PER_CLASS)

I use Hibernate to persist inherited objects but I got this message when I try to persist objects in database: org.springframework.dao.InvalidDataAccessResourceUsageException: Could not execute JDBC batch update; SQL [update Widget set…
Romain
  • 61
  • 1
  • 1
  • 2
6
votes
1 answer

Annotations containing lambda expressions in Java 8

I am experimenting with static fields in annotations and I am stumbling upon something I do not understand. I use the following code: @a public class myAnnotMinimal { public static void main(String[] args) { …
miselico
  • 355
  • 3
  • 9
6
votes
1 answer

Best Practices for Polymorphic JPA via Annotations

I'm trying to set up polymorphic behaviour using Hibernate with JPA annotations. It seems sensible (maybe even necessary) to create an (abstract) class encapsulating the state and behaviours required for the inheritance hierarchy to participate in…
brabster
  • 42,504
  • 27
  • 146
  • 186
6
votes
2 answers

Identify stability of types in Java annotation processor

I would like to write an annotation processor that generates source code based on the set of JavaBeans properties of processed types. This works in general, but I am struggling with doing so correctly if other annotation processors are around.…
Gunnar
  • 18,095
  • 1
  • 53
  • 73
6
votes
2 answers

Plugging in to Java compilers

I have a post-compilation step that manipulates the Java bytecode of generated classes. I'd like to make life as painless as possible for library consumers, so I'm looking at ways I can make this process automatic and (if possible) compiler…
McDowell
  • 107,573
  • 31
  • 204
  • 267
6
votes
1 answer

Why does isAnnotationPresent work differently between Java 7 and Java 8?

I just discovered this today when one of my unit tests failed because of upgrading from Java 7 to Java 8. The unit test calls a method which tries to find an annotation on a method which is annotated on a child class but with a different return…
Adam Burley
  • 5,551
  • 4
  • 51
  • 72
6
votes
1 answer

Accessing Java annotations from a Taglet

I'm working on a project where we have some custom Taglet classes that are used to modify the Javadocs (such as linking to source code in SVN, adding citations) and so on.One of the things we'd like to do is to be able to get the annotations that…
Rajarshi Guha
6
votes
4 answers

Adding Invariants to Interfaces in Java

I've been thinking about creating a Java framework that would allow programmers to specify invariants (pre- and post-conditions) on interfaces. The purpose would be to make code more robust and reduce the number of unit tests that would need to be…
Tarski
  • 5,360
  • 4
  • 38
  • 47
6
votes
1 answer

Why is there no @DoubleRange annotation in Android Studio Support Annotations like @IntRange and @FloatRange

I read this article on Android Studio Support Annotations today and started using these annotations in my code, here is an example: public final static class GoogleMapsZoomLevel { public static final int MIN_ZOOM_LEVEL = 0; public static…
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
1 2 3
99
100