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.api.util;
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   * @return true if the specified field is annotated with @Deprecated on the provided class. False is all
033   * other cases (including if the field doesn't exist)
034   */
035  public static boolean isFieldDeprecated(Class<?> _class, String fieldName) {
036    try {
037      Field field = _class.getDeclaredField(fieldName);
038      field.setAccessible(true);
039      return field.isAnnotationPresent(Deprecated.class);
040    } catch (NoSuchFieldException ignore) {}
041    return false;
042  }
043}