Coverage Report - sk.baka.webvm.analyzer.classloader.ClassLoaderUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassLoaderUtils
0%
0/32
0%
0/16
3.2
 
 1  
 /**
 2  
  * Copyright 2009 Martin Vysny.
 3  
  *
 4  
  * This file is part of WebVM.
 5  
  *
 6  
  * WebVM is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License, or
 9  
  * (at your option) any later version.
 10  
  *
 11  
  * WebVM is distributed in the hope that it will be useful,
 12  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14  
  * GNU General Public License for more details.
 15  
  *
 16  
  * You should have received a copy of the GNU General Public License
 17  
  * along with WebVM.  If not, see <http://www.gnu.org/licenses/>.
 18  
  */
 19  
 package sk.baka.webvm.analyzer.classloader;
 20  
 
 21  
 import java.net.URI;
 22  
 import java.net.URISyntaxException;
 23  
 import java.net.URL;
 24  
 import java.net.URLClassLoader;
 25  
 import java.util.ArrayList;
 26  
 import java.util.HashMap;
 27  
 import java.util.Iterator;
 28  
 import java.util.List;
 29  
 import java.util.Map;
 30  
 
 31  
 /**
 32  
  * Contains utilities for classloader analysis.
 33  
  * @author Martin Vysny
 34  
  */
 35  
 public final class ClassLoaderUtils {
 36  
 
 37  0
     private ClassLoaderUtils() {
 38  0
         throw new AssertionError();
 39  
     }
 40  0
     private static final URL[] EMPTY_URL_ARRAY = new URL[0];
 41  
 
 42  
     /**
 43  
      * Returns URLs which given class loader claims to see. Uses {@link URLClassLoader#getURLs()}.
 44  
      * @param cl the class loader to analyze
 45  
      * @return never null, may be empty array in case the classloader is not an URLClassLoader.
 46  
      */
 47  
     public static URL[] getURLs(final ClassLoader cl) {
 48  0
         if (!(cl instanceof URLClassLoader)) {
 49  0
             return EMPTY_URL_ARRAY;
 50  
         }
 51  0
         final URL[] result = ((URLClassLoader) cl).getURLs();
 52  0
         if (result == null) {
 53  0
             return EMPTY_URL_ARRAY;
 54  
         }
 55  0
         return result;
 56  
     }
 57  
 
 58  
     /**
 59  
      * Return the class loader parent chain as a list.
 60  
      * @param cl the class loader to analyze
 61  
      * @return the chain with given class loader first and the root class loader last.
 62  
      */
 63  
     public static List<ClassLoader> getClassLoaderChain(final ClassLoader cl) {
 64  0
         final List<ClassLoader> result = new ArrayList<ClassLoader>();
 65  0
         for (ClassLoader c = cl; c != null; c = c.getParent()) {
 66  0
             result.add(c);
 67  
         }
 68  0
         return result;
 69  
     }
 70  
 
 71  
     /**
 72  
      * Returns a map of URIs visible to class loaders.
 73  
      * @param cl the class loader to analyze
 74  
      * @return maps URIs loadable by a class loader to a list of class loaders which are able to load the URI.
 75  
      * @throws java.net.URISyntaxException
 76  
      */
 77  
     public static Map<URI, List<ClassLoader>> getClassLoaderURIs(final ClassLoader cl) throws URISyntaxException {
 78  0
         final Map<URI, List<ClassLoader>> result = new HashMap<URI, List<ClassLoader>>();
 79  0
         final List<ClassLoader> cls = getClassLoaderChain(cl);
 80  0
         for (final ClassLoader c : cls) {
 81  0
             final URL[] urls = getURLs(c);
 82  0
             for (final URL url : urls) {
 83  0
                 final URI uri = url.toURI();
 84  0
                 List<ClassLoader> list = result.get(uri);
 85  0
                 if (list == null) {
 86  0
                     list = new ArrayList<ClassLoader>();
 87  0
                     result.put(uri, list);
 88  
                 }
 89  0
                 list.add(c);
 90  
             }
 91  0
         }
 92  0
         return result;
 93  
     }
 94  
 
 95  
     /**
 96  
      * Filters out regular class loader items, leaving only URIs loadable by multiple class loaders.
 97  
      * @param map URI - ClassLoaders map, must not be null.
 98  
      */
 99  
     public static void filterClashes(final Map<URI, List<ClassLoader>> map) {
 100  0
         for (final Iterator<Map.Entry<URI, List<ClassLoader>>> i = map.entrySet().iterator(); i.hasNext();) {
 101  0
             final Map.Entry<URI, List<ClassLoader>> e = i.next();
 102  0
             if (e.getValue().size() <= 1) {
 103  0
                 i.remove();
 104  
             }
 105  0
         }
 106  0
     }
 107  
 }