is Field Required for Element - Java Reflection

Java examples for Reflection:Field

Description

is Field Required for Element

Demo Code


//package com.java2s;

import javax.lang.model.element.AnnotationMirror;

import javax.lang.model.element.Element;

public class Main {
    private static final String NON_NULL_ANNOTATION_NAME = "NonNull";
    private static final String NOT_NULL_ANNOTATION_NAME = "NotNull";

    public static boolean isFieldRequired(Element element) {
        return hasAnnotationWithName(element, NOT_NULL_ANNOTATION_NAME)
                || hasAnnotationWithName(element, NON_NULL_ANNOTATION_NAME);
    }//from   ww  w.  ja va 2s.c  o  m

    public static boolean hasAnnotationWithName(Element element,
            String simpleName) {
        for (AnnotationMirror mirror : element.getAnnotationMirrors()) {
            String annotationName = mirror.getAnnotationType().asElement()
                    .getSimpleName().toString();
            if (simpleName.equals(annotationName)) {
                return true;
            }
        }
        return false;
    }
}

Related Tutorials