1. /*
  2. * @(#)AnnotatedElement.java 1.3 04/02/03
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang.reflect;
  8. import java.lang.annotation.Annotation;
  9. /**
  10. * Represents an annotated element of the program currently running in this
  11. * VM. This interface allows annotations to be read reflectively. All
  12. * annotations returned by methods in this interface are immutable and
  13. * serializable. It is permissible for the caller to modify the
  14. * arrays returned by accessors for array-valued enum members; it will
  15. * have no affect on the arrays returned to other callers.
  16. *
  17. * <p>If an annotation returned by a method in this interface contains
  18. * (directly or indirectly) a {@link Class}-valued member referring to
  19. * a class that is not accessible in this VM, attempting to read the class
  20. * by calling the relevant Class-returning method on the returned annotation
  21. * will result in a {@link TypeNotPresentException}.
  22. *
  23. * <p>Similarly, attempting to read an enum-valued member will result in
  24. * a {@link EnumConstantNotPresentException} if the enum constant in the
  25. * annotation is no longer present in the enum type.
  26. *
  27. * <p>Finally, Attempting to read a member whose definition has evolved
  28. * incompatibly will result in a {@link
  29. * java.lang.annotation.AnnotationTypeMismatchException} or an
  30. * {@link java.lang.annotation.IncompleteAnnotationException}.
  31. *
  32. * @since 1.5
  33. * @author Josh Bloch
  34. */
  35. public interface AnnotatedElement {
  36. /**
  37. * Returns true if an annotation for the specified type
  38. * is present on this element, else false. This method
  39. * is designed primarily for convenient access to marker annotations.
  40. *
  41. * @param annotationType the Class object corresponding to the
  42. * annotation type
  43. * @return true if an annotation for the specified annotation
  44. * type is present on this element, else false
  45. * @throws NullPointerException if annotationType is null
  46. * @since 1.5
  47. */
  48. boolean isAnnotationPresent(Class<? extends Annotation> annotationType);
  49. /**
  50. * Returns this element's annotation for the specified type if
  51. * such an annotation is present, else null.
  52. *
  53. * @param annotationType the Class object corresponding to the
  54. * annotation type
  55. * @return this element's annotation for the specified annotation type if
  56. * present on this element, else null
  57. * @throws NullPointerException if annotationType is null
  58. * @since 1.5
  59. */
  60. <T extends Annotation> T getAnnotation(Class<T> annotationType);
  61. /**
  62. * Returns all annotations present on this element. (Returns an array
  63. * of length zero if this element has no annotations.) The caller of
  64. * this method is free to modify the returned array; it will have no
  65. * effect on the arrays returned to other callers.
  66. *
  67. * @return all annotations present on this element
  68. * @since 1.5
  69. */
  70. Annotation[] getAnnotations();
  71. /**
  72. * Returns all annotations that are directly present on this
  73. * element. Unlike the other methods in this interface, this method
  74. * ignores inherited annotations. (Returns an array of length zero if
  75. * no annotations are directly present on this element.) The caller of
  76. * this method is free to modify the returned array; it will have no
  77. * effect on the arrays returned to other callers.
  78. *
  79. * @return All annotations directly present on this element
  80. * @since 1.5
  81. */
  82. Annotation[] getDeclaredAnnotations();
  83. }