Path: blob/master/test/jdk/com/sun/jdi/ClassLoaderTarg.java
41149 views
/*1* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24*25*/26import java.util.*;27import java.io.*;2829class ClassLoaderTarg extends ClassLoader {30private Hashtable loaded = new Hashtable();31String id;3233public ClassLoaderTarg(String id) {34this.id = id;35}3637private byte[] loadClassBytes(String cname)38throws ClassNotFoundException {39StringTokenizer stk = new StringTokenizer(System.getProperty("java.class.path"),40File.pathSeparator);41/* search class path for the class file */42while (stk.hasMoreTokens()) {43File cfile = new File(stk.nextToken() + File.separator +44cname + ".class");45if (cfile.exists()) {46System.out.println("loading from: " + cfile);47return loadBytes(cfile, cname);48} else {49System.out.println("no file: " + cfile + " - trying next");50}51}52throw new ClassNotFoundException(cname);53}545556private byte[] loadBytes(File cfile, String cname)57throws ClassNotFoundException {58try {59long fsize = cfile.length();60if (fsize > 0) {61FileInputStream in = new FileInputStream(cfile);62byte[] cbytes = new byte[(int)fsize];63in.read(cbytes);64in.close();65return cbytes;66}67} catch (IOException exc) {68// drop down69}70throw new ClassNotFoundException(cname);71}7273public synchronized Class findClass(String cname)74throws ClassNotFoundException {75Class klass = (Class)loaded.get(cname);76if (klass == null) {77byte[] cbytes = loadClassBytes(cname);78klass = defineClass(cname, cbytes, 0, cbytes.length);79loaded.put(cname, klass);80System.err.println("ClassLoaderTarg (" + id +") loaded: " + cname);81}82return klass;83}8485protected void finalize() {86UnloadEventTarg.classLoaderFinalized(id);87try {88super.finalize();89} catch (Throwable thrown) {90}91}92}939495