Guest User

Untitled

a guest
Jan 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. package org.Test.start;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.net.URISyntaxException;
  6. import java.net.URL;
  7. import java.util.ArrayList;
  8. import java.util.Enumeration;
  9. import java.util.jar.JarEntry;
  10. import java.util.jar.JarFile;
  11.  
  12. public class Test {
  13.    
  14.     public static void main(String[] args) {
  15.        
  16.         System.out.println(Test.getClassesForPackage(------[[[[[PACKAGE VAR GOES HERE]]]]]------));
  17.        
  18.     }
  19.  
  20.     public static ArrayList<Class<?>> getClassesForPackage(Package pkg) {
  21.         String pkgname = pkg.getName();
  22.         ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
  23.         // Get a File object for the package
  24.         File directory = null;
  25.         String fullPath;
  26.         String relPath = pkgname.replace('.', '/');
  27.         System.out.println("ClassDiscovery: Package: " + pkgname + " becomes Path:" + relPath);
  28.         URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
  29.         System.out.println("ClassDiscovery: Resource = " + resource);
  30.         if (resource == null) {
  31.             throw new RuntimeException("No resource for " + relPath);
  32.         }
  33.         fullPath = resource.getFile();
  34.         System.out.println("ClassDiscovery: FullPath = " + resource);
  35.  
  36.         try {
  37.             directory = new File(resource.toURI());
  38.         } catch (URISyntaxException e) {
  39.             throw new RuntimeException(pkgname + " (" + resource + ") does not appear to be a valid URL / URI.  Strange, since we got it from the system...", e);
  40.         } catch (IllegalArgumentException e) {
  41.             directory = null;
  42.         }
  43.         System.out.println("ClassDiscovery: Directory = " + directory);
  44.  
  45.         if (directory != null && directory.exists()) {
  46.             // Get the list of the files contained in the package
  47.             String[] files = directory.list();
  48.             for (int i = 0; i < files.length; i++) {
  49.                 // we are only interested in .class files
  50.                 if (files[i].endsWith(".class")) {
  51.                     // removes the .class extension
  52.                     String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
  53.                     System.out.println("ClassDiscovery: className = " + className);
  54.                     try {
  55.                         classes.add(Class.forName(className));
  56.                     }
  57.                     catch (ClassNotFoundException e) {
  58.                         throw new RuntimeException("ClassNotFoundException loading " + className);
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.         else {
  64.             try {
  65.                 String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
  66.                 JarFile jarFile = new JarFile(jarPath);        
  67.                 Enumeration<JarEntry> entries = jarFile.entries();
  68.                 while(entries.hasMoreElements()) {
  69.                     JarEntry entry = entries.nextElement();
  70.                     String entryName = entry.getName();
  71.                     if(entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) {
  72.                         System.out.println("ClassDiscovery: JarEntry: " + entryName);
  73.                         String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
  74.                         System.out.println("ClassDiscovery: className = " + className);
  75.                         try {
  76.                             classes.add(Class.forName(className));
  77.                         }
  78.                         catch (ClassNotFoundException e) {
  79.                             throw new RuntimeException("ClassNotFoundException loading " + className);
  80.                         }
  81.                     }
  82.                 }
  83.             } catch (IOException e) {
  84.                 throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
  85.             }
  86.         }
  87.         return classes;
  88.     }
  89.  
  90. }
Add Comment
Please, Sign In to add comment