Java Code Examples for java.lang.annotation.ElementType#TYPE

The following examples show how to use java.lang.annotation.ElementType#TYPE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MonitoringConstraintViolation.java    From cm_ext with Apache License 2.0 6 votes vote down vote up
/**
 * Build a ReferenceConstraintViolation from a reference violaiton.
 */
public static <T> MonitoringConstraintViolation<T> forViolation(
    String message,
    Object currentBean,
    Object currentValue,
    DescriptorPath path) {

  DescriptorNode first = path.getTailNode();
  T rootBean = (T)first.as(BeanDescriptorNode.class).getBean();
  Class<T> rootBeanClass = (Class<T>)rootBean.getClass();

  return new MonitoringConstraintViolation<T>(
    message,
    message,
    rootBeanClass,
    rootBean,
    currentBean,
    currentValue,
    path,
    null,
    ElementType.TYPE,
    null,
    null
  );
}
 
Example 2
Source File: AnnotationClassReader.java    From jetbrick-template-1x with Apache License 2.0 6 votes vote down vote up
private boolean readAttributes(DataInput di, ElementType type) throws IOException {
    final int count = di.readUnsignedShort();

    for (int i = 0; i < count; ++i) {
        final String name = resolveUtf8(di);
        // in bytes, use this to skip the attribute info block
        final int length = di.readInt();

        if (type == ElementType.TYPE && ("RuntimeVisibleAnnotations".equals(name) || "RuntimeInvisibleAnnotations".equals(name))) {
            if (readAnnotations(di)) {
                return true;
            }
        } else {
            di.skipBytes(length);
        }
    }
    return false;
}
 
Example 3
Source File: GenericConfig.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
GenericConfig(Class<X> annotationType, Class<?> beanClass, Method method) {
    this(beanClass, method, null, annotationType,
            method.isAnnotationPresent(annotationType)
                    ? method.getAnnotation(annotationType)
                    : getAnnotationFromClass(annotationType, beanClass),
            method.isAnnotationPresent(annotationType) ? ElementType.METHOD : ElementType.TYPE);
}
 
Example 4
Source File: GenericConfig.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
GenericConfig(Class<X> annotationType, AnnotatedMethod<?> annotatedMethod) {
    this(annotatedMethod.getDeclaringType()
            .getJavaClass(), annotatedMethod.getJavaMember(), annotatedMethod, annotationType,
            annotatedMethod.isAnnotationPresent(annotationType) ? annotatedMethod.getAnnotation(annotationType)
                    : annotatedMethod.getDeclaringType()
                            .getAnnotation(annotationType),
            annotatedMethod.isAnnotationPresent(annotationType) ? ElementType.METHOD : ElementType.TYPE);
}
 
Example 5
Source File: AnnotationId.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Construct an instance. It initially contains no elements.
 *
 * @param declaringType    the type declaring the program element.
 * @param type             the annotation type.
 * @param annotatedElement the program element type to be annotated.
 * @return an annotation {@code AnnotationId<D,V>} instance.
 */
public static <D, V> AnnotationId<D, V> get(TypeId<D> declaringType, TypeId<V> type,
                                            ElementType annotatedElement) {
    if (annotatedElement != ElementType.TYPE &&
            annotatedElement != ElementType.METHOD &&
            annotatedElement != ElementType.FIELD &&
            annotatedElement != ElementType.PARAMETER) {
        throw new IllegalArgumentException("element type is not supported to annotate yet.");
    }

    return new AnnotationId<>(declaringType, type, annotatedElement);
}
 
Example 6
Source File: ClassInfo.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public ElementType getElementType() {
    if (getClassName().endsWith("package-info")) {
        return ElementType.PACKAGE;
    } else if (isAnnotation()) {
        return ElementType.ANNOTATION_TYPE;
    }
    return ElementType.TYPE;

}
 
Example 7
Source File: AnnotationId.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
/**
 *  Construct an instance. It initially contains no elements.
 *
 * @param declaringType the type declaring the program element.
 * @param type the annotation type.
 * @param annotatedElement the program element type to be annotated.
 * @return an annotation {@code AnnotationId<D,V>} instance.
 */
public static <D, V> AnnotationId<D, V> get(TypeId<D> declaringType, TypeId<V> type,
                                            ElementType annotatedElement) {
    if (annotatedElement != ElementType.TYPE &&
            annotatedElement != ElementType.METHOD &&
            annotatedElement != ElementType.FIELD &&
            annotatedElement != ElementType.PARAMETER) {
        throw new IllegalArgumentException("element type is not supported to annotate yet.");
    }

    return new AnnotationId<>(declaringType, type, annotatedElement);
}
 
Example 8
Source File: AnnotationUtils.java    From simple-robot-core with Apache License 2.0 4 votes vote down vote up
/**
 * 从某个类上获取注解对象,注解可以深度递归
 * 如果存在多个继承注解,则优先获取浅层第一个注解,如果浅层不存在,则返回第一个获取到的注解
 * 请尽可能保证仅存在一个或者一种继承注解,否则获取到的类型将不可控
 *
 * @param from           获取注解的某个类
 * @param annotationType 想要获取的注解类型
 * @param ignored        获取注解列表的时候的忽略列表
 * @return 获取到的第一个注解对象
 */
public static <T extends Annotation> T getAnnotation(AnnotatedElement from, Class<T> annotationType, Class<T>... ignored) {
    // 首先尝试获取缓存
    T cache = getCache(from, annotationType);
    if(cache != null){
        return cache;
    }

    if(isNull(from, annotationType)){
        return null;
    }


    //先尝试直接获取
    T annotation = from.getAnnotation(annotationType);

    //如果存在直接返回,否则查询
    if (annotation != null) {
        saveCache(from, annotation);
        return annotation;
    }

    // 获取target注解
    Target target = annotationType.getAnnotation(Target.class);
    // 判断这个注解能否标注在其他注解上,如果不能,则不再深入获取
    boolean annotationable = false;
    if (target != null) {
        for (ElementType elType : target.value()) {
            if (elType == ElementType.TYPE || elType == ElementType.ANNOTATION_TYPE) {
                annotationable = true;
                break;
            }
        }
    }

    Annotation[] annotations = from.getAnnotations();
    annotation = annotationable ? getAnnotationFromArrays(annotations, annotationType, ignored) : null;


    // 如果还是获取不到,看看查询的注解类型有没有对应的ByNameType
    if (annotation == null) {
        annotation = getByNameAnnotation(from, annotationType);
    }

    // 如果无法通过注解本身所指向的byName注解获取,看看有没有反向指向此类型的注解
    // 此情况下不进行深层获取
    if(annotation == null){
        annotation = getAnnotationFromByNames(annotations, annotationType);
    }

    // 如果最终不是null,计入缓存
    if(annotation != null){
        saveCache(from, annotation);
    }else{
        nullCache(from, annotationType);
    }

    return annotation;
}
 
Example 9
Source File: AnnotationPredicates.java    From android-test with Apache License 2.0 4 votes vote down vote up
static Predicate<InfoPb> newClassAnnotationPresentPredicate(
    String annotationClassName, Predicate<List<AnnotationValuePb>> valuePredicate) {
  return new AnnotationPresent(ElementType.TYPE, annotationClassName, valuePredicate);
}