1 32 package net.sf.retrotranslator.runtime.java.lang; 33 34 import java.security.*; 35 import net.sf.retrotranslator.runtime.impl.*; 36 import net.sf.retrotranslator.runtime.java.lang.annotation.Annotation_; 37 38 41 public class _Package { 42 43 public static Annotation_ getAnnotation(Package aPackage, Class annotationType) { 44 return getPackageInfo(aPackage).getAnnotation(annotationType); 45 } 46 47 public static Annotation_[] getAnnotations(Package aPackage) { 48 return getPackageInfo(aPackage).getAnnotations(); 49 } 50 51 public static Annotation_[] getDeclaredAnnotations(Package aPackage) { 52 return getPackageInfo(aPackage).getDeclaredAnnotations(); 53 } 54 55 public static boolean isAnnotationPresent(Package aPackage, Class annotationType) { 56 return getPackageInfo(aPackage).isAnnotationPresent(annotationType); 57 } 58 59 private static ClassDescriptor getPackageInfo(Package aPackage) { 60 String resourceName = "/" + aPackage.getName().replace('.', '/') + "/package-info.class"; 61 ClassDescriptor packageInfo = createPackageInfo(_Package.class, resourceName); 62 return packageInfo != null ? packageInfo : getPrivilegedInfo(resourceName); 63 } 64 65 private static ClassDescriptor createPackageInfo(Class loader, String resourceName) { 66 byte[] bytecode = RuntimeTools.readResourceToByteArray(loader, resourceName); 67 return bytecode == null ? null : new ClassDescriptor(loader, bytecode); 68 } 69 70 private static ClassDescriptor getPrivilegedInfo(final String resourceName) { 71 return AccessController.doPrivileged(new PrivilegedAction<ClassDescriptor>() { 72 public ClassDescriptor run() { 73 return getContextInfo(resourceName); 74 } 75 }); 76 } 77 78 private static ClassDescriptor getContextInfo(String resourceName) { 79 try { 80 for (Class contextClass : new ExecutionContext().getClassContext()) { 81 try { 82 ClassDescriptor packageInfo = createPackageInfo(contextClass, resourceName); 83 if (packageInfo != null) return packageInfo; 84 } catch (Throwable e) { 85 } 87 } 88 } catch (Throwable e) { 89 } 91 return ClassDescriptor.getInstance(_Package.class); 92 } 93 94 private static class ExecutionContext extends SecurityManager { 95 public Class [] getClassContext() { 96 return super.getClassContext(); 97 } 98 } 99 100 } 101 | Popular Tags |