org.omnifaces.utils.annotation.Annotations Maven / Gradle / Ivy
package org.omnifaces.utils.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.Map;
public final class Annotations {
private Annotations() {
//
}
/**
* Create an instance of the specified annotation type with default attributes.
* @param The generic annotation type.
* @param type The annotation type.
* @return An annotation instance of the specified annotation type with default attributes.
*/
public static A createAnnotationInstance(Class type) {
return createAnnotationInstances(type)[0];
}
/**
* Create an array of instances of the specified annotation types with default attributes. Useful for varargs calls
* such as CDI.current().select(type, createAnnotationInstances(Qualifier1.class, Qualifier2.class)).
* @param The generic annotation type.
* @param types The annotation types.
* @return An array of instances of the specified annotation types with default attributes.
*/
@SafeVarargs
@SuppressWarnings("unchecked")
public static A[] createAnnotationInstances(Class... types) {
if (types == null) {
return null;
}
A[] instances = (A[]) Array.newInstance(Annotation.class, types.length);
for (int i = 0; i < types.length; i++) {
instances[i] = createAnnotationInstance(types[i], Collections.emptyMap());
}
return instances;
}
/**
* Create an instance of the specified annotation type with given attributes.
* @param The generic annotation type.
* @param type The annotation type.
* @param attributes The annotation attributes. May be a partial attribute map or even an empty map.
* @return An annotation instance of the specified annotation type with given attributes.
*/
public static A createAnnotationInstance(Class type, Map attributes) {
InvocationHandler handler = new AnnotationInvocationHandler(type, attributes);
return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type }, handler));
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy