Home Categories Java Software Tutorial

Inherited annotations in Java

Using the library of inherited annotations in Java

1.0/5.0 (1 votes total)

Mikhail V Milonov
July 03, 2007

Mikhail V Milonov

Michael 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 (http://www.fusionsoft-online.com)

Mikhail V Milonov has written 2 articles for WebKnowHow.
View all articles by Mikhail V Milonov...


Michael Milonov
© Fusionsoft

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 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 are 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 which 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

Let's 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 for describing one superclass and one superinterface. There was created a derived class inherited from the superclass and superinterface.

According to Java annotation practice, neither 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 using reflection mechanism. One could cache annotations in a unified storage and get access to them using names of classes and signatures of methods as keys.

The product provides 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 = classToAnnotatedMap.get(theClass);
		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, 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 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
		for (Annotation annotation : getTheClass().
                      getInterfaces()[0], annotation);
		return result;


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


Using the library of inherited Java annotations

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

For our 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 we have used empty array new Class[0] as parameter of function getAnnotatedMethod.



Using meta-information and declarative description of code behavior is very convenient and allows 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: http://fusionsoft-online.com/download/static/products/annotation.zip. We welcome your feedback, opinions and suggestions. Send your mail today to: [email protected].


Add commentAdd comment (Comments: 0)  



Related Resources

Other Resources