使用loadClass()进行延迟编译的GroovyClassLoader支持是可行的

时间:2011-11-15 05:15:22

标签: groovy

GroovyClassLoader是否支持使用loadClass()进行延迟编译?

1 个答案:

答案 0 :(得分:0)

扩展GroovyClassloader并覆盖loadclass方法

@Override
    public Class loadClass(String name, boolean lookupScriptFiles,
            boolean preferClassOverScript) throws ClassNotFoundException,
            CompilationFailedException {
        // TODO Auto-generated method stub
        try
        {
        Class<?> loadedClass = super.loadClass(name, lookupScriptFiles, preferClassOverScript);
        if (loadedClass !=null) {
            return loadedClass;
        }
        }
        catch (ClassNotFoundException e)
        {
            System.out.println("here --------------");
        }

         int indx = name.lastIndexOf('.');
            String substr = name;
            if (indx != -1)
            {
                substr = name.substring(indx + 1);
            }
            String groovyFileName =  substr + ".groovy";
            String path = "C:\\" + groovyFileName;

            try
            {
                return parseClass(new File(path).toString(), groovyFileName);
            }
            catch (CompilationFailedException exception)
            {
                throw exception;
            }
    }