LookupScriptFiles“真”在 loadClass() 方法中

发布于 2024-12-27 02:40:05 字数 1739 浏览 4 评论 0原文

扩展 GroovyClassloader 并覆盖 loadclass 方法

如果我在 loadClass() 方法中将 lookupScriptFiles 设置为“true”,则脚本将运行并且不需要导入引用不同包中的 groovy 类的语句

我扩展了 GroovyClassloader,并重写了 loadclass 方法,在 loadclass 中参数 lookupScriptFiles =true

当这是 true 时,它​​成功即使是first.groovy 也可以编译,当 lookupScriptFiles=false 没有 import 语句

时,它会按预期抛出编译错误。

my source code snippet

    C:\>cat first.groovy
def cos=new Second()
==============================================================
C:>cat Second.groovy
package com.test
class Second
{
Second()
{
        println "Anish"
}

}
=========================================================

C:\bin>echo %CLASSPATH%
C:\zGroovy\bin;C:\vsexclude\opt\groovy-1.7.2\embeddable\groovy-all-1.7.2.jar
===============================================
C:\vsexclude\trees\bac-4.2\workspace\zGroovy\bin>java GCtest
path------>>C:\first.groovy
Anish
=================================

import groovy.lang.GroovyClassLoader;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilerConfiguration;
/**
 * @author Anish
 */
public class GCloader extends GroovyClassLoader {
    public GCloader(ClassLoader parent) {
        super(parent, new CompilerConfiguration());
    }
    @Override
    public Class<?> loadClass(final String name, boolean lookupScriptFiles,
            boolean preferClassOverScript, boolean resolve)
            throws ClassNotFoundException, CompilationFailedException {
        //return loadFiles(name, true, preferClassOverScript, resolve);
        return super.loadClass(name, true,
                preferClassOverScript, resolve);
    }
}

Extended GroovyClassloader and override loadclass method

If I make lookupScriptFiles "true" in the loadClass() method the script run and doesn't require an import statement referencing a groovy class in a different package

i have extended GroovyClassloader, and override the loadclass method, in the loadclass the argument lookupScriptFiles =true

When this is true, it sucessfully compiles even first.groovy don't have import statement

when lookupScriptFiles=false it throws compilation error as expected.

my source code snippet

    C:\>cat first.groovy
def cos=new Second()
==============================================================
C:>cat Second.groovy
package com.test
class Second
{
Second()
{
        println "Anish"
}

}
=========================================================

C:\bin>echo %CLASSPATH%
C:\zGroovy\bin;C:\vsexclude\opt\groovy-1.7.2\embeddable\groovy-all-1.7.2.jar
===============================================
C:\vsexclude\trees\bac-4.2\workspace\zGroovy\bin>java GCtest
path------>>C:\first.groovy
Anish
=================================

import groovy.lang.GroovyClassLoader;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilerConfiguration;
/**
 * @author Anish
 */
public class GCloader extends GroovyClassLoader {
    public GCloader(ClassLoader parent) {
        super(parent, new CompilerConfiguration());
    }
    @Override
    public Class<?> loadClass(final String name, boolean lookupScriptFiles,
            boolean preferClassOverScript, boolean resolve)
            throws ClassNotFoundException, CompilationFailedException {
        //return loadFiles(name, true, preferClassOverScript, resolve);
        return super.loadClass(name, true,
                preferClassOverScript, resolve);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁微风不噪 2025-01-03 02:40:05

假设你的问题是:

如果我将 lookupScriptFiles 设置为 true,我可以从我的 groovy 脚本中删除 import 语句吗?

那么答案是否定的。类加载器将尝试查找它不知道的脚本,但是您仍然需要导入来告诉它在哪些包中查找每个类


更新

因此,您在同一目录中有两个 groovy 文件,其中一个有随意添加了一个package语句。

我假设您直接从脚本加载类(还有您在问题中没有说的另一件事)

如果是这种情况,那么您将需要告诉类加载器查找其他脚本以编译为类。

如果你不这样做——正如你所看到的——它将不起作用(导入或不导入)

但是,将两个 groovy 文件放在同一个文件夹中,然后仅向其中一个文件添加一个包行是糟糕的编码实践,并且我很惊讶你有任何进展

Assuming your question is:

If I set lookupScriptFiles to true, can I remove the import statements from my groovy scripts?

Then the answer is no. The classloader will try to lookup scripts it doesn't know about, but you will still need to imports to tell it in which packages to look for each class


Update

So, you have two groovy files in the same directory, one of which you have arbitrarily added a package statement to.

I assume you are loading the classes straight from scripts (yet another thing you don't say in your question)

If this is the case, then you will need to tell the classloader to lookup the other scripts to compile to classes.

If you don't -- as you have seen -- it will not work (imports or no imports)

However, putting two groovy files in the same folder, and just adding a package line to one of them is awful coding practice, and I'm surprised you got anything working

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文