Advertisement
Guest User

ClassScope.java

a guest
Nov 30th, 2011
1,237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. import java.lang.reflect.Field;
  2. import java.util.Arrays;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Vector;
  6.  
  7. public class ClassScope {
  8.     private static final String[] EMPTY_LIBRARY_ARRAY = new String[]{};
  9.     private static final Throwable CVF_FAILURE; // set in <clinit>
  10.  
  11.     static {
  12.         Throwable failure = null;
  13.         Field tempf = null;
  14.         try {
  15.             // this can fail if this is not a Sun-compatible JVM
  16.             // or if the security is too tight:
  17.             tempf = ClassLoader.class.getDeclaredField("loadedLibraryNames");
  18.             if (tempf.getType() != Vector.class) {
  19.                 throw new RuntimeException("not of type java.util.Vector: " + tempf.getType().getName());
  20.             }
  21.             tempf.setAccessible(true);
  22.         } catch (Throwable t) {
  23.             failure = t;
  24.         }
  25.         LIBRARIES_VECTOR_FIELD = tempf;
  26.         CVF_FAILURE = failure;
  27.  
  28.         failure = null;
  29.     }
  30.  
  31.     /**
  32.      * Given a class loader instance, returns all native libraries currently loaded by
  33.      * that class loader.
  34.      *
  35.      * @param defining
  36.      *            class loader to inspect [may not be null]
  37.      * @return Libraries loaded in this class loader [never null, may be empty]
  38.      *
  39.      * @throws RuntimeException
  40.      *             if the "loadedLibraryNames" field hack is not possible in this JRE
  41.      */
  42.     @SuppressWarnings("unchecked")
  43.     public static String[] getLoadedLibraries(final ClassLoader loader) {
  44.         if (loader == null) {
  45.             throw new IllegalArgumentException("null input: loader");
  46.         }
  47.         if (LIBRARIES_VECTOR_FIELD == null) {
  48.             throw new RuntimeException("ClassScope::getLoadedLibraries() cannot be used in this JRE", CVF_FAILURE);
  49.         }
  50.  
  51.         try {
  52.             final Vector<String> libraries = (Vector<String>) LIBRARIES_VECTOR_FIELD.get(loader);
  53.             if (libraries == null)
  54.                 return EMPTY_LIBRARY_ARRAY;
  55.  
  56.             final String[] result;
  57.  
  58.             // note: Vector is synchronized in Java 2, which helps us make
  59.             // the following into a safe critical section:
  60.             synchronized (libraries) {
  61.                 result = libraries.toArray(new String[] {});
  62.             }
  63.             return result;
  64.         }
  65.         // this should not happen if <clinit> was successful:
  66.         catch (IllegalAccessException e) {
  67.             e.printStackTrace(System.out);
  68.             return EMPTY_LIBRARY_ARRAY;
  69.         }
  70.     }
  71.  
  72.     /**
  73.      * A convenience multi-loader version of
  74.      * {@link #getLoadedLibraries(ClassLoader)}.
  75.      *
  76.      * @param an
  77.      *            array of defining class loaders to inspect [may not be null]
  78.      * @return String array [never null, may be empty]
  79.      *
  80.      * @throws RuntimeException
  81.      *             if the "loadedLibrayNames" field hack is not possible in this JRE
  82.      */
  83.     public static String[] getLoadedLibraries(final ClassLoader[] loaders) {
  84.         if (loaders == null) {
  85.             throw new IllegalArgumentException("null input: loaders");
  86.         }
  87.         final List<String> resultList = new LinkedList<String>();
  88.  
  89.         for (int l = 0; l < loaders.length; ++l) {
  90.             final ClassLoader loader = loaders[l];
  91.             if (loader != null) {
  92.                 final String[] libraries = getLoadedLibraries(loaders[l]);
  93.                 resultList.addAll(Arrays.asList(libraries));
  94.             }
  95.         }
  96.  
  97.         final String[] result = new String[resultList.size()];
  98.         resultList.toArray(result);
  99.  
  100.         return result;
  101.     }
  102.  
  103.     private ClassScope() {
  104.     } // this class is not extendible
  105.  
  106.     private static final Field LIBRARIES_VECTOR_FIELD; // set in <clinit> [can be
  107.     // null]
  108.  
  109. }
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement