Create Annotation instance with defaults, in Java

To create an instance you need to create a class that implements: java.lang.annotation.Annotation and the annotation you want to “simulate” For example: public class MySettings implements Annotation, Settings But you need to pay special attention to the correct implementation of equals and hashCode according to the Annotation interface. http://download.oracle.com/javase/1.5.0/docs/api/java/lang/annotation/Annotation.html If you do not want to … Read more

How to get annotation class name, attribute values using reflection

Contrary to what one might expect, the elements of an annotation are not attributes – they are actually methods that return the provided value or a default value. You have to iterate through the annotations’ methods and invoke them to get the values. Use annotationType() to get the annotation’s class, the object returned by getClass() … Read more

Spring annotation-based DI vs xml configuration?

After reading some related posts here and having further discussion in the team we come to the following conclusions. I hope the would be useful to others here. About XML configuration (which we are using up to now), we decided to keep it for dependencies defined by libraries (regardless if being developed by us, or … Read more

Java Getters and Setters

I’m not sure if you’d consider it ‘standard’, but Project Lombok addresses this problem. They use annotations to replace much of the verbosity of Java. Some people are looking at alternative Java sibling languages, such as Groovy or Scala. I’m afraid it will take some years -if at all- before the JSR figures out a … Read more

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

I got the same problem with @ManyToOne column. It was solved… in stupid way. I had all other annotations for public getter methods, because they were overridden from parent class. But last field was annotated for private variable like in all other classes in my project. So I got the same MappingException without the reason. … Read more

Java Annotations

Annotations are primarily used by code that is inspecting other code. They are often used for modifying (i.e. decorating or wrapping) existing classes at run-time to change their behavior. Frameworks such as JUnit and Hibernate use annotations to minimize the amount of code you need to write yourself to use the frameworks. Oracle has a … Read more

Attribute on Interface members does not work

Attributes on interface properties doesn’t get inherited to the class, you may make your interface an Abstract Class. Found an answer from Microsoft: The product team does not want to implement this feature, for two main reasons: Consistency with DataAnnotations.Validator Consistency with validation behavior in ASP.Net MVC tricky scenario: a class implements two interfaces that … Read more