package com.logzc.webzic.util;

import com.logzc.webzic.clazz.Foo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

/**
 * This is the class for loading classes.
 * Created by lishuang on 2016/7/7.
 */
public class ClassUtil {
    private static final Logger logger = LoggerFactory.getLogger(ClassUtil.class);

    public static ClassLoader getClassLoader() {
        return Thread.currentThread().getContextClassLoader();
    }

    public static Class<?> loadClass(String className, boolean isInitialized) {

        Class<?> clazz;
        try {
            clazz = Class.forName(className, isInitialized, getClassLoader());
        } catch (ClassNotFoundException e) {
            logger.debug("Cannot find " + className);
            throw new RuntimeException();
        }
        return clazz;
    }

    public static Class<?> loadClass(String className) {
        return loadClass(className, true);
    }

    //Load classes in the specific package.
    public static Set<Class<?>> getClassSet(String packageName) {
        Set<Class<?>> classSet = new HashSet<>();

        try {
            Enumeration<URL> urls = getClassLoader().getResources(packageName.replace(".", "/"));

            while (urls.hasMoreElements()) {
                URL url = urls.nextElement();
                if (url != null) {
                    String protocol = url.getProtocol();
                    if (protocol.equals("file")) {
                        String packagePath = url.getPath().replaceAll("%20", " ");
                        addClass(classSet,packagePath,packageName);

                    } else if (protocol.equals("jar")) {

                    }
                }
            }

        } catch (IOException e) {
            logger.error(e.getMessage());
        }

        return null;
    }

    private static void addClass(Set<Class<?>> classSet, String packagePath, String packageName) {
        logger.debug(packagePath);
        File[] files = new File(packagePath).listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                return file.isFile() && file.getName().endsWith(".class") || file.isDirectory();
            }
        });

        for (File file : files) {
            String fileName = file.getName();
            if (file.isFile()) {
                String className = fileName.substring(0, fileName.lastIndexOf("."));
                if (StringUtils.isNotEmpty(packageName)) {
                    className = packageName + "." + className;
                }
                doAddClass(classSet, className);
            } else {
                String subPackagePath = fileName;
                if (StringUtils.isNotEmpty(packagePath)) {
                    subPackagePath = packagePath + "/" + subPackagePath;
                }
                String subPackageName = fileName;
                if (StringUtils.isNotEmpty(packageName)) {
                    subPackageName = packageName + "." + subPackageName;
                }
                addClass(classSet, subPackagePath, subPackageName);
            }
        }

    }

    private static void doAddClass(Set<Class<?>> classSet, String className) {
        logger.debug(className+" ---------- loading");
        Class<?> cls = loadClass(className, false);
        classSet.add(cls);
    }

    public static void main(String[] args) throws Exception {

        getClassSet("com.logzc");


    }

}

@@ -60,16 +60,72 @@
 zz.Foo;%0A
+import org.apache.commons.collections4.CollectionUtils;%0A
 import o
@@ -2479,24 +2479,53 @@
        %7D);%0A%0A
+        if(files!=null)%7B%0A    
         for 
@@ -2554,24 +2554,28 @@
             
+    
 String fileN
@@ -2604,24 +2604,28 @@
             
+    
 if (file.isF
@@ -2629,24 +2629,28 @@
 isFile()) %7B%0A
+    
             
@@ -2730,32 +2730,36 @@
                 
+    
 if (StringUtils.
@@ -2777,32 +2777,36 @@
 packageName)) %7B%0A
+    
                 
@@ -2848,24 +2848,28 @@
  className;%0A
+    
             
@@ -2890,16 +2890,20 @@
         
+    
 doAddCla
@@ -2935,24 +2935,28 @@
             
+    
 %7D else %7B%0A   
@@ -2960,32 +2960,36 @@
                 
+    
 String subPackag
@@ -2998,32 +2998,36 @@
 ath = fileName;%0A
+    
                 
@@ -3081,32 +3081,36 @@
                 
+    
 subPackagePath =
@@ -3154,34 +3154,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -3230,32 +3230,36 @@
                 
+    
 if (StringUtils.
@@ -3277,32 +3277,36 @@
 packageName)) %7B%0A
+    
                 
@@ -3370,34 +3370,95 @@
                 
-%7D%0A
+    %7D%0A%0A                    //recursive to add all classes.%0A    
                 
@@ -3505,24 +3505,42 @@
 ckageName);%0A
+                %7D%0A
             
@@ -3548,24 +3548,25 @@
 %0A        %7D%0A%0A
+%0A
     %7D%0A%0A    p

Add another classe.