001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.lang;
032:
033: import java.lang.annotation.Annotation;
034: import java.security.*;
035: import net.sf.retrotranslator.runtime.impl.*;
036:
037: /**
038: * @author Taras Puchko
039: */
040: public class _Package {
041:
042: public static Annotation getAnnotation(Package aPackage,
043: Class annotationType) {
044: return getPackageInfo(aPackage).getAnnotation(annotationType);
045: }
046:
047: public static Annotation[] getAnnotations(Package aPackage) {
048: return getPackageInfo(aPackage).getAnnotations();
049: }
050:
051: public static Annotation[] getDeclaredAnnotations(Package aPackage) {
052: return getPackageInfo(aPackage).getDeclaredAnnotations();
053: }
054:
055: public static boolean isAnnotationPresent(Package aPackage,
056: Class annotationType) {
057: return getPackageInfo(aPackage).isAnnotationPresent(
058: annotationType);
059: }
060:
061: private static ClassDescriptor getPackageInfo(Package aPackage) {
062: ClassDescriptor packageInfo = createPackageInfo(_Package.class,
063: aPackage);
064: return packageInfo != null ? packageInfo
065: : getPrivilegedInfo(aPackage);
066: }
067:
068: private static ClassDescriptor createPackageInfo(Class loader,
069: Package aPackage) {
070: byte[] bytecode = getBytecode(loader, aPackage);
071: if (bytecode != null) {
072: return new ClassDescriptor(loader, bytecode);
073: }
074: for (String simpleName : new String[] { "package$info",
075: "package-info" }) {
076: Class infoClass = getClass(loader, aPackage, simpleName);
077: if (infoClass != null) {
078: return ClassDescriptor.getInstance(infoClass);
079: }
080: }
081: return null;
082: }
083:
084: private static Class getClass(Class loader, Package aPackage,
085: String simpleName) {
086: try {
087: return Class.forName(aPackage.getName() + "." + simpleName,
088: true, loader.getClassLoader());
089: } catch (Exception e) {
090: return null;
091: }
092: }
093:
094: private static byte[] getBytecode(Class loader, Package aPackage) {
095: String resourceName = "/"
096: + aPackage.getName().replace('.', '/')
097: + "/package-info.class";
098: return RuntimeTools.readResourceToByteArray(loader,
099: resourceName);
100: }
101:
102: private static ClassDescriptor getPrivilegedInfo(
103: final Package aPackage) {
104: return AccessController
105: .doPrivileged(new PrivilegedAction<ClassDescriptor>() {
106: public ClassDescriptor run() {
107: return getContextInfo(aPackage);
108: }
109: });
110: }
111:
112: private static ClassDescriptor getContextInfo(Package aPackage) {
113: try {
114: for (Class contextClass : new ExecutionContext()
115: .getClassContext()) {
116: try {
117: ClassDescriptor packageInfo = createPackageInfo(
118: contextClass, aPackage);
119: if (packageInfo != null)
120: return packageInfo;
121: } catch (Throwable e) {
122: //continue;
123: }
124: }
125: } catch (Throwable e) {
126: //continue;
127: }
128: return ClassDescriptor.getInstance(_Package.class);
129: }
130:
131: private static class ExecutionContext extends SecurityManager {
132: public Class[] getClassContext() {
133: return super.getClassContext();
134: }
135: }
136:
137: }
|