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

org.umlg.java.metamodel.annotation.AnnotationHelper Maven / Gradle / Ivy

There is a newer version: 2.0.15
Show newest version
package org.umlg.java.metamodel.annotation;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.umlg.java.metamodel.OJElement;
import org.umlg.java.metamodel.OJPathName;

public class AnnotationHelper {
	public static Set getImportsFrom(Collection sources) {
		Set result = new HashSet();
		for (OJElement v : sources) {
			if (v instanceof OJAnnotatedElement) {
				addTypesUsed((OJAnnotatedElement) v, result);
			}
		}
		return result;
	}
	public static Set getImportsFrom(OJAnnotatedElement element) {
		Set result = new HashSet();
		addTypesUsed(element, result);
		return result;
	}
	private static void addTypesUsed(OJAnnotatedElement element, Set result) {
		for (OJAnnotationValue v : element.getAnnotations()) {
			result.addAll(v.getAllTypesUsed());
		}
	}
	/**
	 * Adds an annotation to the target element, but only if an annotation value of this type has not been associated
	 * with the target yet. Returns true if the annotation value was added, false if an exeisting value was found for
	 * that annotation
	 * 
	 * @param value
	 * @param target
	 * @return
	 */
	public static boolean maybeAddAnnotation(OJAnnotationValue value, OJAnnotatedElement target) {
		Collection sourceSet = target.getAnnotations();
		for (Iterator iter = sourceSet.iterator(); iter.hasNext();) {
			if (value.getType().equals(iter.next().getType())) {
				return false;
			}
		}
		sourceSet.add(value);
		return true;
	}
	public static OJAnnotationValue getAnnotation(OJAnnotatedElement target, OJPathName path) {
		for (OJAnnotationValue v:target.getAnnotations()) {
			if (v.getType().equals(path)) {
				return v;
			}
		}
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy