Java URL Connection scanPackageByJar(List> classes, final String packageName, final URL jarUrl, final boolean recursive)

Here you can find the source of scanPackageByJar(List> classes, final String packageName, final URL jarUrl, final boolean recursive)

Description

scan Package By Jar

License

Open Source License

Declaration

private static void scanPackageByJar(List<Class<?>> classes, final String packageName, final URL jarUrl,
        final boolean recursive) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.net.JarURLConnection;
import java.net.URL;

import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {

    private static void scanPackageByJar(List<Class<?>> classes, final String packageName, final URL jarUrl,
            final boolean recursive) {

        String packagePath = packageName.replace(".", "/");
        try {/*from   w  w w  . j av  a2s.c  o  m*/
            JarFile jarFile = ((JarURLConnection) jarUrl.openConnection()).getJarFile();
            Enumeration<JarEntry> entrys = jarFile.entries();
            while (entrys.hasMoreElements()) {
                JarEntry jarEntry = entrys.nextElement();
                String entryName = jarEntry.getName();
                if (!entryName.startsWith(packagePath) || jarEntry.isDirectory()) {
                    continue;
                }

                if (!recursive && entryName.lastIndexOf('/') != packagePath.length()) {
                    continue;
                }

                String clazzSimpleName = entryName.substring(entryName.lastIndexOf('/') + 1);
                if (filterClassName(clazzSimpleName)) {
                    String clazzName = entryName.replace('/', '.');
                    clazzName = clazzName.substring(0, clazzName.length() - ".class".length());

                    Class<?> clazz = null;

                    try {
                        clazz = Thread.currentThread().getContextClassLoader().loadClass(clazzName);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }

                    if (clazz != null) {
                        classes.add(clazz);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static boolean filterClassName(final String fileName) {
        return fileName.endsWith(".class");
    }
}

Related

  1. renderRequest(URLConnection connection)
  2. resolveModuleEntriesFromJar(URL url, String _prefix)
  3. retrieveData(URL url)
  4. retrieveFromUrl(String url, boolean useCache, boolean deleteOnExit)
  5. retrieveHtml(URLConnection http)
  6. sendGetRequest(String resourceUrl, String contentType)
  7. sendGetRequest(String urlStr)
  8. sendPost(String url, String param)
  9. setTimeout(URLConnection conn, int milliseconds)