Java Class Find getClasses(ClassLoader cl, String pack)

Here you can find the source of getClasses(ClassLoader cl, String pack)

Description

get Classes

License

Open Source License

Declaration

public static List<Class> getClasses(ClassLoader cl, String pack) throws IOException 

Method Source Code

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

import java.io.DataInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<Class> getClasses(ClassLoader cl, String pack) throws IOException {

        String dottedPackage = pack.replaceAll("[/]", ".");
        List<Class> classes = new ArrayList<Class>();
        URL upackage = cl.getResource(pack);

        DataInputStream dis = new DataInputStream((InputStream) upackage.getContent());
        String line = null;//from  ww  w.  ja va 2  s. c  o  m
        while ((line = dis.readLine()) != null) {
            if (line.endsWith(".class")) {
                try {
                    classes.add(Class.forName(dottedPackage + "." + line.substring(0, line.lastIndexOf('.'))));
                } catch (Exception e) {
                }
            }
        }
        return classes;
    }

    public static String substring(String str, int offset, int length) {

        if ((offset + length) > str.length()) {
            length = str.length() - offset;
        }

        return str.substring(offset, offset + length);
    }
}

Related

  1. getAllClassesFromPackage(final String packageName)
  2. getAllClassesIn(String... packageNames)
  3. getAllClassesInPackage(String packageName)
  4. getClasses(Class clazz)
  5. getClasses(Class cls)
  6. getClasses(final String packageName)
  7. getClasses(final String packageName, final Class annotation)
  8. getClasses(final String pckgname)
  9. getClasses(String pack)