org.umlg.java.metamodel.annotation.AnnotationHelper Maven / Gradle / Ivy
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 extends OJElement> 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;
}
}