All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.rasc.bsoncodec.AnnotationHierarchyUtil Maven / Gradle / Ivy

Go to download

Java 8 Annotation Processor creating org.bson.codecs.Codec implementations for POJOs

There is a newer version: 1.0.6
Show newest version
/**
 * Written by Michael Karneim
 *
 * Part of the project
 * https://github.com/mkarneim/pojobuilder
 *
 * License:
 * https://github.com/mkarneim/pojobuilder/blob/master/COPYING
 */
package ch.rasc.bsoncodec;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.util.Types;

public class AnnotationHierarchyUtil {

	private final Types types;

	public AnnotationHierarchyUtil(Types types) {
		this.types = types;
	}

	public Set filterTriggeringAnnotations(
			Set aAnnotations, TypeElement bsonDocumentAnnotation) {
		Set result = new HashSet<>();
		for (TypeElement annoEl : aAnnotations) {
			Set hierarchy = getAnnotationHierarchy(annoEl);
			if (containsAnnotation(hierarchy, bsonDocumentAnnotation)) {
				result.add(annoEl);
			}
		}
		return result;
	}

	private Set getAnnotationHierarchy(TypeElement annoTypeEl) {
		Set result = new HashSet<>();
		getAnnotationHierarchy(annoTypeEl, result);
		return result;
	}

	private void getAnnotationHierarchy(TypeElement annoTypeEl, Set result) {
		if (result.add(annoTypeEl)) {
			List annos = annoTypeEl.getAnnotationMirrors();
			for (AnnotationMirror anno : annos) {
				DeclaredType annoDeclType = anno.getAnnotationType();
				Element annoEl = annoDeclType.asElement();
				if (annoEl.getKind() == ElementKind.ANNOTATION_TYPE) {
					getAnnotationHierarchy((TypeElement) annoEl, result);
				}
			}
		}
	}

	private boolean containsAnnotation(Set elems, TypeElement annoEl) {
		for (TypeElement el : elems) {
			if (this.types.isSameType(annoEl.asType(), el.asType())) {
				return true;
			}
		}
		return false;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy