001 package net.sourceforge.retroweaver.runtime.java.lang;
002
003 import java.lang.annotation.Annotation;
004
005 /**
006 * A mirror of java.lang.Package
007 *
008 * @author Toby Reyelts Date: Feb 20, 2005 Time: 11:50:47 PM
009 */
010 public class Package_ {
011
012 private Package_() {
013 // private constructor
014 }
015
016 /**
017 * Implementation notes:
018 * ---------------------
019 * <p/>
020 * Package annotations are a little different from other annotations. The java compiler writes
021 * them into a synthetic interface named, <package-name>.package-info. Here, we implicitly load that
022 * class and pass it onto Class_ when asked for annotation information on a package.
023 * <p/>
024 *
025 */
026 private static Class getPackageAnnotationClass(final Package p) {
027 try {
028 return Class.forName(p.getName() + ".package-info");
029 } catch (ClassNotFoundException e) {
030 return null;
031 }
032 }
033
034 // Provide AnnotatedElement methods
035
036 // Returns this element's annotation for the specified type if such an
037 // annotation is present, else null.
038 public static <T extends Annotation> T getAnnotation(final Package p, final Class<T> annotationType) {
039 final Class c = getPackageAnnotationClass(p);
040 if (c == null) {
041 return null;
042 } else {
043 return (T) c.getAnnotation(annotationType);
044 }
045 }
046
047 private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[] {};
048
049 // Returns all annotations present on this element.
050 public static Annotation[] getAnnotations(final Package p) {
051 final Class c = getPackageAnnotationClass(p);
052 if (c == null) {
053 return EMPTY_ANNOTATION_ARRAY; // NOPMD by xlv
054 } else {
055 return c.getAnnotations();
056 }
057 }
058
059 // Returns all annotations that are directly present on this element.
060 public static Annotation[] getDeclaredAnnotations(final Package p) {
061 final Class c = getPackageAnnotationClass(p);
062 if (c == null) {
063 return EMPTY_ANNOTATION_ARRAY; // NOPMD by xlv
064 } else {
065 return c.getDeclaredAnnotations();
066 }
067 }
068
069 // Returns true if an annotation for the specified type is present on this
070 // element, else false.
071 public static boolean isAnnotationPresent(final Package p, final Class<? extends Annotation> annotationType) {
072 final Class c = getPackageAnnotationClass(p);
073 if (c == null) {
074 return false;
075 } else {
076 return c.isAnnotationPresent(annotationType);
077 }
078 }
079
080 }