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