package io.indexr.plugin;

import com.google.common.reflect.ClassPath;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Set;

public class Plugins {
    private static final Logger logger = LoggerFactory.getLogger(Plugins.class);
    private static volatile boolean loaded = false;

    public static boolean loadPluginsNEX() {
        try {
            return loadPlugins();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean loadPlugins() throws Exception {
        synchronized (Plugins.class) {
            if (loaded) {
                return true;
            }
            loaded = true;

            boolean sucOne = false;
            ClassPath classPath = ClassPath.from(Plugins.class.getClassLoader());
            Set<ClassPath.ClassInfo> classes = classPath.getTopLevelClasses(Plugins.class.getPackage().getName());
            for (ClassPath.ClassInfo ci : classes) {
                Method[] methods = ci.load().getDeclaredMethods();
                for (Method m : methods) {
                    int mod = m.getModifiers();
                    if (!Modifier.isStatic(mod) && m.getParameterCount() > 0) {
                        continue;
                    }
                    boolean ok = false;
                    for (Annotation ann : m.getAnnotations()) {
                        if (ann instanceof InitPlugin) {
                            ok = true;
                            break;
                        }
                    }
                    if (ok) {
                        try {
                            logger.info("Start run plugin method: {}#{}", ci.getSimpleName(), m.getName());
                            m.invoke(null);
                            sucOne = true;
                            logger.info("Finish run plugin method: {}#{}", ci.getSimpleName(), m.getName());
                        } catch (Exception e) {
                            logger.error("Plugin method failed: {}#{} ", ci.getSimpleName(), m.getName(), e);
                        }
                    }
                }
            }
            return sucOne;
        }
    }
}

@@ -24,53 +24,8 @@
 n;%0A%0A
-import com.google.common.reflect.ClassPath;%0A%0A
 impo
@@ -189,30 +189,8 @@
 ier;
-%0Aimport java.util.Set;
 %0A%0Apu
@@ -342,16 +342,92 @@
 = false;
+%0A    private static String%5B%5D pluginClasses = %7B%22io.indexr.plugin.VLTPlugin%22%7D;
 %0A%0A    pu
@@ -852,77 +852,41 @@
     
-ClassPath classPath = ClassPath.from(Plugins.class.getClassLoader());
+for (String cn : pluginClasses) %7B
 %0A   
@@ -898,163 +898,46 @@
     
-Set%3C
+    
 Class
-Path.ClassInfo%3E classes = classPath.getTopLevelClasses(Plugins.class.getPackage().getName());%0A            for (ClassPath.ClassInfo ci : classes) %7B
+%3C?%3E clzz = Class.forName(cn);
 %0A   
@@ -973,16 +973,11 @@
  = c
-i.load()
+lzz
 .get
@@ -1640,33 +1640,17 @@
 %7D#%7B%7D%22, c
-i.getSimpleName()
+n
 , m.getN
@@ -1820,33 +1820,17 @@
 %7D#%7B%7D%22, c
-i.getSimpleName()
+n
 , m.getN
@@ -1963,25 +1963,9 @@
 %22, c
-i.getSimpleName()
+n
 , m.

remove guava dependency