Inherited Annotations in Java

Despite the fact that the fully operational annotation mechanism appeared in Java not so long ago, many developers have appreciated this new possibility of Java API and use it in their programs. Annotations have lots of advantages but have some restrictions too. One of them is the inability to inherit annotations to subclasses. This peculiarity brings in some inconvenience and could be critical if some wrappers, generated with the reflection mechanism, are used over some Java classes.

In this case, there is no possibility to manage annotations in derived classes manually and no ability to use this convenient tool of program behavior declarative description. To solve this problem, Fusionsoft Company developed the library of inherited annotations that is considered below. The library is open-source and free, with no restriction for commercial application.


Java SDK 1.5 or higher;
junit-4.2 for test purposes.

Problem to Solve

Consider the following example:

public @interface A {}

public @interface B {}

public interface BaseInterface {
   public void method1();

public class BaseClass {
   public void method2(){}

public class Derived extends BaseClass implements BaseInterface{
   public void method1(){}
   public void method2(){}

This example contains two kinds of annotations: the annotation @A for types, and the annotation @B for methods. The annotations were used to describe one superclass and one superinterface. A derived class inherited from the superclass and superinterface was created.

According to Java annotation practice, neither the derived class nor its methods inherit annotations defined in the superclass and superinterface. Therefore, the following code returns null in both cases:

   "method1", new Class[0]).getAnnotation(B.class)

   "method2", new Class[0]).getAnnotation(B.class)

Accessing Superclass Annotations

The main idea that allows getting annotations for superclasses lies in extracting them from annotated base classes by using the reflection mechanism. One could cache annotations in a unified storage area and get access to them by using the names of classes and signatures of methods as keys.

The product provides a centralized cache for annotated classes as follows:

public class AnnotationManager {
   private static Map<Class <?>, AnnotatedClass>
      classToAnnotatedMap =
      new HashMap<Class<?>, AnnotatedClass>();

    * @param theClass to wrap.
    * @return the annotated class wrapping the specified one.
   public static AnnotatedClass getAnnotatedClass(Class<?> theClass){
      AnnotatedClass annotatedClass =
      if (annotatedClass == null){
         annotatedClass = new AnnotatedClassImpl(theClass);
         classToAnnotatedMap.put(theClass, annotatedClass);
      return annotatedClass;

By calling the static method getAnnotatedClass, one can get the object of the class AnnotatedClass containing annotations inherited from ancestor classes and interfaces. AnnotattedClass uses the reflection methods: Class.getInterfaces and Class.getDeclaredAnnotations. It gets annotations recursively from all the ancestors, caches them, and associates annotations with the annotated interfaces and classes. The procedure of filling up the cache HashMap looks like the following:

private Map<Class<?>, Annotation> getAllAnnotationMapCalculated(){
   HashMap<Class<?>, Annotation> result = new
      HashMap<Class<?>, Annotation>();

   final Class<?> superClass = getTheClass().getSuperclass();
   // Get the superclass's annotations
   if (superClass != null)
      fillAnnotationsForOneClass(result, superClass);

   // Get the superinterfaces' annotations
   for (Class<?> c : getTheClass().getInterfaces())
      fillAnnotationsForOneClass(result, c);

   // Get its own annotations. They have preference to inherited
   // annotations.
   for (Annotation annotation : getTheClass().
      getInterfaces()[0], annotation);
   return result;

The lazy-initialization pattern is used here to cache annotations: Annotations are cached only when they have been requested for the first time.

Using the Library of Inherited Java Annotations

So, what does the access to Java annotations using the library of inherited annotations look like? The access to inherited annotations is identical to the standard one, but the special classes from the library are used instead of Java API classes.

For the example, it looks like the following:

AnnotatedClass annotatedClass = 

AnnotatedMethod annotatedMethod = annotatedClass
   .getAnnotatedMethod("method1", new Class[0]);

annotatedMethod = annotatedClass
  .getAnnotatedMethod("method2", new Class[0]);

As long as method1 and method2 don't have parameters, you have used the empty array new Class[0] as a parameter of the getAnnotatedMethod function.


Using meta-information and declarative description of code behavior is very convenient and allows the creation of most reusable code. And, the possibility to inherit annotations is useful here. The library of inherited Java annotations provides this possibility. You can find the latest release of the library here: We welcome your feedback, opinions, and suggestions. Send your mail today to

About the Author

Mikhail Milonov

Started writing programs 10 years ago, got qualified in various programming languages to date. As the moment widely uses Java and works in a fast growing software company in the field of semantic integration. PhD in Computer Science, Deputy director of Fusionsoft (



  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

  • This report outlines the future look of Forrester's solution for security and risk (S&R) executives working on building an identity and access management strategy for the extended enterprise. We designed this report to help you understand and navigate the major business and IT trends affecting identity and access management (IAM) during the next five years. IAM in 2012 has become a tool not just for security but also for business agility. Competitive challenges push businesses into the cloud and encourage …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date