集册 Java实例教程 是元素的必填字段

是元素的必填字段

欢马劈雪     最近更新时间:2020-01-02 10:19:05

541
是元素的必填字段


//package com.nowjava;/* 来自 时 代 J a v a 公 众 号*/


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);

    }


    public static boolean hasAnnotationWithName(Element element,

            String simpleName) {

        for (AnnotationMirror mirror : element.getAnnotationMirrors()) {

            String annotationName = mirror.getAnnotationType().asElement()

                    .getSimpleName().toString()
展开阅读全文