diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2003-04-23 17:08:37 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2003-04-23 17:08:37 +0000 |
commit | cdeddfd6fc34a06734f9fa525cf5c7437a6c8fb6 (patch) | |
tree | 0471a4d9b985b11969ecd6f521f660e3d468f1d1 /src/main/javassist/ClassPoolTail.java | |
parent | fb431982111b03608b888953f7ed8ba7e98f421c (diff) | |
download | javassist-cdeddfd6fc34a06734f9fa525cf5c7437a6c8fb6.tar.gz javassist-cdeddfd6fc34a06734f9fa525cf5c7437a6c8fb6.zip |
Changed the copyright notices and removed tab characters.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@9 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main/javassist/ClassPoolTail.java')
-rw-r--r-- | src/main/javassist/ClassPoolTail.java | 379 |
1 files changed, 201 insertions, 178 deletions
diff --git a/src/main/javassist/ClassPoolTail.java b/src/main/javassist/ClassPoolTail.java index 1c6c7058..c3f86d83 100644 --- a/src/main/javassist/ClassPoolTail.java +++ b/src/main/javassist/ClassPoolTail.java @@ -1,28 +1,17 @@ /* - * This file is part of the Javassist toolkit. + * Javassist, a Java-bytecode translator toolkit. + * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved. * - * The contents of this file are subject to the Mozilla Public License - * Version 1.1 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * either http://www.mozilla.org/MPL/. + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See - * the License for the specific language governing rights and limitations - * under the License. - * - * The Original Code is Javassist. - * - * The Initial Developer of the Original Code is Shigeru Chiba. Portions - * created by Shigeru Chiba are Copyright (C) 1999-2003 Shigeru Chiba. - * All Rights Reserved. - * - * Contributor(s): - * - * The development of this software is supported in part by the PRESTO - * program (Sakigake Kenkyu 21) of Japan Science and Technology Corporation. + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. */ - package javassist; import java.io.*; @@ -33,8 +22,8 @@ final class ClassPathList { ClassPath path; ClassPathList(ClassPath p, ClassPathList n) { - next = n; - path = p; + next = n; + path = p; } } @@ -43,24 +32,26 @@ final class SystemClassPath implements ClassPath { Class thisClass; SystemClassPath() { - /* The value of thisClass was this.getClass() in early versions: - * - * thisClass = this.getClass(); - * - * However, this made openClassfile() not search all the system - * class paths if javassist.jar is put in jre/lib/ext/ - * (with JDK1.4). - */ - thisClass = java.lang.Object.class; + /* The value of thisClass was this.getClass() in early versions: + * + * thisClass = this.getClass(); + * + * However, this made openClassfile() not search all the system + * class paths if javassist.jar is put in jre/lib/ext/ + * (with JDK1.4). + */ + thisClass = java.lang.Object.class; } public InputStream openClassfile(String classname) { - String jarname = "/" + classname.replace('.', '/') + ".class"; - return thisClass.getResourceAsStream(jarname); + String jarname = "/" + classname.replace('.', '/') + ".class"; + return thisClass.getResourceAsStream(jarname); } + public void close() {} + public String toString() { - return "*system class path*"; + return "*system class path*"; } } @@ -69,23 +60,25 @@ final class DirClassPath implements ClassPath { String directory; DirClassPath(String dirName) { - directory = dirName; + directory = dirName; } public InputStream openClassfile(String classname) { - try { - char sep = File.separatorChar; - String filename = directory + sep - + classname.replace('.', sep) + ".class"; - return new FileInputStream(filename.toString()); - } - catch (FileNotFoundException e) {} - catch (SecurityException e) {} - return null; + try { + char sep = File.separatorChar; + String filename = directory + sep + + classname.replace('.', sep) + ".class"; + return new FileInputStream(filename.toString()); + } + catch (FileNotFoundException e) {} + catch (SecurityException e) {} + return null; } + public void close() {} + public String toString() { - return directory; + return directory; } } @@ -94,34 +87,42 @@ final class JarClassPath implements ClassPath { ZipFile jarfile; JarClassPath(String pathname) throws NotFoundException { - try { - jarfile = new ZipFile(pathname); - return; - } - catch (ZipException e) {} - catch (IOException e) {} - throw new NotFoundException(pathname); + try { + jarfile = new ZipFile(pathname); + return; + } + catch (ZipException e) {} + catch (IOException e) {} + throw new NotFoundException(pathname); } public InputStream openClassfile(String classname) - throws NotFoundException + throws NotFoundException { - try { - String jarname = classname.replace('.', '/') + ".class"; - ZipEntry ze = jarfile.getEntry(jarname); - if (ze != null) - return jarfile.getInputStream(ze); - else - return null; // not found - } - catch (ZipException e) {} - catch (IOException e) {} - throw new NotFoundException("broken jar file?: " - + jarfile.getName()); + try { + String jarname = classname.replace('.', '/') + ".class"; + ZipEntry ze = jarfile.getEntry(jarname); + if (ze != null) + return jarfile.getInputStream(ze); + else + return null; // not found + } + catch (ZipException e) {} + catch (IOException e) {} + throw new NotFoundException("broken jar file?: " + + jarfile.getName()); + } + + public void close() { + try { + jarfile.close(); + jarfile = null; + } + catch (IOException e) {} } public String toString() { - return jarfile.toString(); + return jarfile == null ? "<null>" : jarfile.toString(); } } @@ -130,178 +131,200 @@ final class ClassPoolTail extends ClassPool { private Class thisClass; public ClassPoolTail() { - pathList = null; - thisClass = getClass(); + pathList = null; + thisClass = getClass(); } public String toString() { - StringBuffer buf = new StringBuffer(); - buf.append("[class path: "); - ClassPathList list = pathList; - while (list != null) { - buf.append(list.path.toString()); - buf.append(File.pathSeparatorChar); - list = list.next; - } - - buf.append(']'); - return buf.toString(); + StringBuffer buf = new StringBuffer(); + buf.append("[class path: "); + ClassPathList list = pathList; + while (list != null) { + buf.append(list.path.toString()); + buf.append(File.pathSeparatorChar); + list = list.next; + } + + buf.append(']'); + return buf.toString(); } public byte[] write(String classname) - throws NotFoundException, IOException + throws NotFoundException, IOException { - return readClassfile(classname); + return readClassfile(classname); } public void write(String classname, DataOutputStream out) - throws NotFoundException, CannotCompileException, IOException + throws NotFoundException, CannotCompileException, IOException { - byte[] b = write(classname); - out.write(b, 0, b.length); + byte[] b = write(classname); + out.write(b, 0, b.length); } public CtClass get(String classname) throws NotFoundException { - throw new RuntimeException("fatal error"); + throw new RuntimeException("fatal error"); } public CtClass makeClass(String classname) { - throw new RuntimeException("fatal error"); + throw new RuntimeException("fatal error"); } void checkClassName(String classname) - throws NotFoundException + throws NotFoundException { - InputStream fin = openClassfile(classname); - try { - fin.close(); - } - catch (IOException e) { /* ignore */ } + InputStream fin = openClassfile(classname); + try { + fin.close(); + } + catch (IOException e) { /* ignore */ } + } + + public synchronized ClassPath insertClassPath(ClassPath cp) { + pathList = new ClassPathList(cp, pathList); + return cp; } - public synchronized void insertClassPath(ClassPath cp) { - pathList = new ClassPathList(cp, pathList); + public synchronized ClassPath appendClassPath(ClassPath cp) { + ClassPathList tail = new ClassPathList(cp, null); + ClassPathList list = pathList; + if (list == null) + pathList = tail; + else { + while (list.next != null) + list = list.next; + + list.next = tail; + } + + return cp; } - public synchronized void appendClassPath(ClassPath cp) { - ClassPathList tail = new ClassPathList(cp, null); - ClassPathList list = pathList; - if (list == null) - pathList = tail; - else { - while (list.next != null) - list = list.next; - - list.next = tail; - } + public synchronized void removeClassPath(ClassPath cp) { + ClassPathList list = pathList; + if (list != null) + if (list.path == cp) + pathList = list.next; + else { + while (list.next != null) + if (list.next.path == cp) + list.next = list.next.next; + else + list = list.next; + } + + cp.close(); } - public void appendSystemPath() { - appendClassPath(new SystemClassPath()); + public ClassPath appendSystemPath() { + return appendClassPath(new SystemClassPath()); } - public void insertClassPath(String pathname) - throws NotFoundException + public ClassPath insertClassPath(String pathname) + throws NotFoundException { - insertClassPath(makePathObject(pathname)); + return insertClassPath(makePathObject(pathname)); } - public void appendClassPath(String pathname) - throws NotFoundException + public ClassPath appendClassPath(String pathname) + throws NotFoundException { - appendClassPath(makePathObject(pathname)); + return appendClassPath(makePathObject(pathname)); } private static ClassPath makePathObject(String pathname) - throws NotFoundException + throws NotFoundException { - if (pathname.endsWith(".jar") || pathname.endsWith(".zip") - || pathname.endsWith(".JAR") || pathname.endsWith(".ZIP")) - return new JarClassPath(pathname); - else - return new DirClassPath(pathname); + int i = pathname.lastIndexOf('.'); + if (i >= 0) { + String ext = pathname.substring(i).toLowerCase(); + if (ext.equals(".jar") || ext.equals(".zip")) + return new JarClassPath(pathname); + } + + return new DirClassPath(pathname); } /** * Obtains the contents of the class file for the class * specified by <code>classname</code>. * - * @param classname a fully-qualified class name + * @param classname a fully-qualified class name */ byte[] readClassfile(String classname) - throws NotFoundException, IOException + throws NotFoundException, IOException { - InputStream fin = openClassfile(classname); - byte[] b = readStream(fin); - fin.close(); - return b; + InputStream fin = openClassfile(classname); + byte[] b = readStream(fin); + fin.close(); + return b; } /** * Opens the class file for the class specified by * <code>classname</code>. * - * @param classname a fully-qualified class name + * @param classname a fully-qualified class name */ public InputStream openClassfile(String classname) - throws NotFoundException + throws NotFoundException { - ClassPathList list = pathList; - InputStream ins = null; - NotFoundException error = null; - while (list != null) { - try { - ins = list.path.openClassfile(classname); - } - catch (NotFoundException e) { - if (error == null) - error = e; - } - - if (ins == null) - list = list.next; - else - return ins; - } - - if (error != null) - throw error; - else - throw new NotFoundException(classname); + ClassPathList list = pathList; + InputStream ins = null; + NotFoundException error = null; + while (list != null) { + try { + ins = list.path.openClassfile(classname); + } + catch (NotFoundException e) { + if (error == null) + error = e; + } + + if (ins == null) + list = list.next; + else + return ins; + } + + if (error != null) + throw error; + else + throw new NotFoundException(classname); } /** * Reads an input stream until it reaches the end. * - * @return the contents of that input stream + * @return the contents of that input stream */ public static byte[] readStream(InputStream fin) throws IOException { - byte[][] bufs = new byte[8][]; - int bufsize = 4096; - - for (int i = 0; i < 8; ++i) { - bufs[i] = new byte[bufsize]; - int size = 0; - int len = 0; - do { - len = fin.read(bufs[i], size, bufsize - size); - if (len >= 0) - size += len; - else { - byte[] result = new byte[bufsize - 4096 + size]; - int s = 0; - for (int j = 0; j < i; ++j) { - System.arraycopy(bufs[j], 0, result, s, s + 4096); - s = s + s + 4096; - } - - System.arraycopy(bufs[i], 0, result, s, size); - return result; - } - } while (size < bufsize); - bufsize *= 2; - } - - throw new IOException("too much data"); + byte[][] bufs = new byte[8][]; + int bufsize = 4096; + + for (int i = 0; i < 8; ++i) { + bufs[i] = new byte[bufsize]; + int size = 0; + int len = 0; + do { + len = fin.read(bufs[i], size, bufsize - size); + if (len >= 0) + size += len; + else { + byte[] result = new byte[bufsize - 4096 + size]; + int s = 0; + for (int j = 0; j < i; ++j) { + System.arraycopy(bufs[j], 0, result, s, s + 4096); + s = s + s + 4096; + } + + System.arraycopy(bufs[i], 0, result, s, size); + return result; + } + } while (size < bufsize); + bufsize *= 2; + } + + throw new IOException("too much data"); } } |