Coverage Report - com.sun.tools.javafx.script.MemoryClassLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
MemoryClassLoader
83%
29/35
64%
9/14
0
 
 1  
 /*
 2  
  * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
 3  
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  
  *
 5  
  * This code is free software; you can redistribute it and/or modify it
 6  
  * under the terms of the GNU General Public License version 2 only, as
 7  
  * published by the Free Software Foundation.  Sun designates this
 8  
  * particular file as subject to the "Classpath" exception as provided
 9  
  * by Sun in the LICENSE file that accompanied this code.
 10  
  *
 11  
  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  
  * version 2 for more details (a copy is included in the LICENSE file that
 15  
  * accompanied this code).
 16  
  *
 17  
  * You should have received a copy of the GNU General Public License version
 18  
  * 2 along with this work; if not, write to the Free Software Foundation,
 19  
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  
  *
 21  
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 22  
  * CA 95054 USA or visit www.sun.com if you need additional information or
 23  
  * have any questions.
 24  
  */
 25  
 
 26  
 package com.sun.tools.javafx.script;
 27  
 
 28  
 import java.io.File;
 29  
 import java.net.MalformedURLException;
 30  
 import java.net.URL;
 31  
 import java.net.URLClassLoader;
 32  
 import java.util.ArrayList;
 33  
 import java.util.Map;
 34  
 import java.util.List;
 35  
 import java.util.StringTokenizer;
 36  
 
 37  
 /**
 38  
  * ClassLoader that loads .class bytes from memory.
 39  
  *
 40  
  * @author A. Sundararajan
 41  
  */
 42  
 public final class MemoryClassLoader extends URLClassLoader {
 43  
     private Map<String, byte[]> classBytes;
 44  
     private URL source;
 45  
 
 46  
     public MemoryClassLoader(URL source, Map<String, 
 47  
                byte[]> classBytes, String classPath, ClassLoader parent) {
 48  18
         super(toURLs(classPath), parent);
 49  18
         this.source = source;
 50  18
         this.classBytes = classBytes;
 51  18
     }
 52  
 
 53  
     public Class load(String className) throws ClassNotFoundException {
 54  0
         return loadClass(className);
 55  
     }
 56  
 
 57  
     public Iterable<Class> loadAll() throws ClassNotFoundException {
 58  18
         List<Class> classes = new ArrayList<Class>(classBytes.size());
 59  18
         for (String name : classBytes.keySet()) {
 60  40
             classes.add(loadClass(name));
 61  
         }
 62  18
         return classes;
 63  
     }
 64  
 
 65  
     @Override
 66  
     protected Class findClass(String className) throws ClassNotFoundException {
 67  40
         byte[] buf = classBytes.get(className);
 68  40
         if (buf != null) {
 69  
             // clear the bytes in map -- we don't need it anymore
 70  40
             classBytes.put(className, null);
 71  40
             return defineClass(className, buf, 0, buf.length);
 72  
         } else {
 73  0
             return super.findClass(className);
 74  
         }
 75  
     }
 76  
 
 77  
     @Override
 78  
     public URL findResource(String name) {
 79  1
         if (name.endsWith(".class")) {
 80  1
             name = name.substring(0, name.length() - 6).replace('/', '.');
 81  1
             if (classBytes.containsKey(name))
 82  1
                 return source;
 83  
         }
 84  0
         return super.findResource(name);
 85  
     }
 86  
 
 87  
     private static URL[] toURLs(String classPath) {
 88  18
         if (classPath == null) {
 89  0
             return new URL[0];
 90  
         }
 91  
 
 92  18
         List<URL> list = new ArrayList<URL>();
 93  18
         StringTokenizer st = new StringTokenizer(classPath, File.pathSeparator);
 94  378
         while (st.hasMoreTokens()) {
 95  360
             String token = st.nextToken();
 96  360
             File file = new File(token);
 97  360
             if (file.exists()) {
 98  
                 try {
 99  360
                     list.add(file.toURI().toURL());
 100  360
                 } catch (MalformedURLException mue) {}
 101  
             } else {
 102  
                 try {
 103  0
                     list.add(new URL(token));
 104  0
                 } catch (MalformedURLException mue) {}
 105  
             }
 106  360
         }
 107  18
         URL[] res = new URL[list.size()];
 108  18
         list.toArray(res);
 109  18
         return res;
 110  
     }
 111  
 }