Java Reflection Annotation getAnnotation(Class clazz, Class annotationClass)

Here you can find the source of getAnnotation(Class clazz, Class annotationClass)

Description

This method scans the class and super-class hierarchy (up to, but not including java.lang.Object) for the specified annotation class.

License

Open Source License

Parameter

Parameter Description
T The type of annotation desired
clazz The class to be scanned, including its hierarchy
annotationClass The class of the annotation desired.

Return

The annotation if found, null if not found.

Declaration

private static <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> annotationClass) 

Method Source Code


//package com.java2s;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class Main {
    /**//from w  ww .  j  ava2s  .co m
     * This method scans the class and super-class hierarchy (up to, but not including java.lang.Object) for the
     * specified annotation class. If found, the annotation is returned. If not found, null is returned.
     * 
     * @param <T>
     *            The type of annotation desired
     * @param clazz
     *            The class to be scanned, including its hierarchy
     * @param annotationClass
     *            The class of the annotation desired.
     * @return The annotation if found, null if not found.
     */
    private static <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> annotationClass) {
        if (clazz.getName().equals(Object.class.getName())) {
            return null;
        }
        T annotation = clazz.getAnnotation((Class<T>) annotationClass);
        if (annotation == null) {
            annotation = getAnnotation(clazz.getSuperclass(), annotationClass);
        }

        return annotation;
    }

    /**
     * This method checks to see if two objects are equal and returns true if they are, and false if they are not.
     * <p>
     * Equality as defined by this method means that the two objects must both either be null (null == null), or they
     * must be both non-null and of the same type. If they are both non-null, additionally all fields of the various
     * types, as well as all superclass fields (recursively up to java.lang.Object), must either be null or must be
     * equal. If any of these conditions are not true, the objects are assumed to be unequal. If all of these conditions
     * are true, the objects are equal.
     * </p>
     * 
     * @param o1
     *            The first object to be compared
     * @param o2
     *            The second object to be compared
     * @return True if they are equal, false if not
     */
    public static boolean equals(Object o1, Object o2) {
        boolean result = false;

        if (o1 == null && o2 == null) {
            result = true;
        } else if (o1 != null && o2 != null) {
            Class<?> o1Class = o1.getClass();
            Class<?> o2Class = o2.getClass();
            if (o1Class.equals(o2Class)) {
                result = true; // assume true until we find otherwise
                while (result && !o1Class.equals(Object.class)) {
                    Field[] fields = o1Class.getDeclaredFields();
                    for (Field field : fields) {
                        result = result && checkEqualField(field.getName(), o1, o2);
                        if (!result) {
                            break;
                        }
                    }
                    o1Class = o1Class.getSuperclass();
                }
            }
        }

        return result;
    }

    /**
     * This method is called to check if two fields are equal on two objects of the same class. This can be used to test
     * for equality in an "equals" method where checking for null or non-null conditions must be accounted for.
     * 
     * @param name
     *            The name of the field
     * @param obj1
     *            The first object instance
     * @param obj2
     *            The second object instance
     * @return True if both objects are non-null and the field either exists in both objects and are equal, or both
     *         fields are null. False under any other condition.
     */
    public static boolean checkEqualField(String name, Object obj1, Object obj2) {
        boolean result = false;
        if (obj1 != null && obj2 != null) {
            try {
                Field field = getPropertyField(obj1.getClass(), name); // obj1.getClass().getDeclaredField(name);
                if (field != null) {
                    field.setAccessible(true);
                    try {
                        Object value1 = field.get(obj1);
                        Object value2 = field.get(obj2);
                        if (value1 != null && value2 != null) {
                            result = value1.equals(value2);
                        } else if (value1 == null && value2 == null) {
                            result = true;
                        }
                    } finally {
                        field.setAccessible(false);
                    }
                }
            } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        } else if (obj1 == null && obj2 == null) {
            result = true;
        }
        return result;
    }

    /**
     * This method searches the class hierarchy for a field of the specified name
     * 
     * @param clazz
     *            The class to be searched, as well as its super class(es)
     * @param name
     *            The name of the field
     * @return The field if found, or null if not
     */
    public static Field getPropertyField(Class<?> clazz, String name) {
        Field result = null;
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals(name)) {
                result = field;
                break;
            }
        }
        if (result == null) {
            Class<?> sc = clazz.getSuperclass();
            if (Object.class.getName().equals(sc.getName())) {
                result = null;
            } else {
                result = getPropertyField(sc, name);
            }
        }

        return result;
    }

    /**
     * Returns the field that matches the provided property name, if found
     * 
     * @param obj
     *            The object to be searched
     * @param name
     *            The property name to be found
     * @return The field that corresponds to that property, or null if no field exists
     */
    public static Field getPropertyField(Object obj, String name) {
        Class<?> clazz = obj.getClass();
        return getPropertyField(clazz, name);
    }
}

Related

  1. getAnnotation(Class clazz, Class annotationClass)
  2. getAnnotation(Class clazz, Class ann)
  3. getAnnotation(Class clazz, Class annClazz)
  4. getAnnotation(Class clazz, Class annotation)
  5. getAnnotation(Class clazz, Class annotation)
  6. getAnnotation(Class clazz, Class annotationClass)
  7. getAnnotation(Class clazz, Class annotationClass)
  8. getAnnotation(Class clazz, Class annotationType)
  9. getAnnotation(Class clazz, Class annotationType)