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.transformer;
032:
033: import java.io.*;
034: import java.net.*;
035: import java.util.jar.Manifest;
036: import net.sf.retrotranslator.runtime.impl.RuntimeTools;
037:
038: /**
039: * @author Taras Puchko
040: */
041: public class TransformingJarClassLoader extends JarClassLoader {
042:
043: private final ClassTransformer transformer;
044:
045: public TransformingJarClassLoader(File file, ClassLoader parent,
046: ClassTransformer transformer) throws IOException {
047: super (file, parent);
048: this .transformer = transformer;
049: }
050:
051: protected Class findClass(final String name)
052: throws ClassNotFoundException {
053: URL resource = findResource(name.replace('.', '/').concat(
054: RuntimeTools.CLASS_EXTENSION));
055: if (resource == null) {
056: throw new ClassNotFoundException(name);
057: }
058: try {
059: URLConnection connection = resource.openConnection();
060: initPackage(name, connection, resource);
061: byte[] content = RuntimeTools.readAndClose(connection
062: .getInputStream());
063: content = transformer.transform(content, 0, content.length);
064: return defineClass(name, content, 0, content.length);
065: } catch (IOException e) {
066: throw new ClassNotFoundException(name, e);
067: }
068: }
069:
070: private void initPackage(String className,
071: URLConnection connection, URL resource) throws IOException {
072: String packageName = getPackageName(className);
073: if (packageName == null || getPackage(packageName) != null) {
074: return;
075: }
076: Manifest manifest = getManifest(connection);
077: try {
078: if (manifest != null) {
079: definePackage(packageName, manifest, resource);
080: } else {
081: definePackage(packageName, null, null, null, null,
082: null, null, null);
083: }
084: } catch (IllegalArgumentException e) {
085: // ignore
086: }
087: }
088:
089: private static String getPackageName(String className) {
090: int dotIndex = className.lastIndexOf('.');
091: return dotIndex < 0 ? null : className.substring(0, dotIndex);
092: }
093:
094: private static Manifest getManifest(URLConnection connection)
095: throws IOException {
096: if (connection instanceof JarURLConnection) {
097: return ((JarURLConnection) connection).getManifest();
098: }
099: return null;
100: }
101:
102: }
|