001/*
002 * Copyright 2021 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.utils;
017
018import java.lang.reflect.Field;
019
020/**
021 * Utility method to work with annotations.
022 */
023public final class AnnotationUtils {
024
025  private AnnotationUtils() {
026  }
027
028  /**
029   * Check if a field is annotated with @Deprecated in the provided class.
030   * Mostly used on elements of an Enum but will also work on class {@link Field} (private and public).
031   *
032   * @param _class
033   * @param fieldName
034   * @return true if the specified field is annotated with @Deprecated on the provided class. False is all
035   * other cases (including if the field doesn't exist)
036   */
037  public static boolean isFieldDeprecated(Class<?> _class, String fieldName) {
038    try {
039      Field field = _class.getDeclaredField(fieldName);
040      field.setAccessible(true);
041      return field.isAnnotationPresent(Deprecated.class);
042    } catch (NoSuchFieldException ignore) {}
043    return false;
044  }
045}