Groovy 类加载器问题
我在 eclipse 环境中使用 groovy 和 java 来开发 UI 应用程序。我想在我的代码中使用 groovy 类加载器,所以我使用了传统的方式来获取 Groovy 类加载器:
def str = new File("C:/myGroovyFile.groovy").getText()
def myclass = getClass();
println myclass //facing issue here
ClassLoader parent = myclass.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(str);
这里我面临的问题是在 eclipse 环境中调用 getClass() 时不会返回GroovyCOnsole 脚本而是返回 java.lang.class,调用 getClassLoader() 时返回 null。
我想获取 getClass() 的 groovy 控制台脚本,这对于动态加载位于 C:\ 的 groovy 文件很有用,
请告诉我如何解决这个问题。
PS:我试图将此代码放入名为initialize()的方法中。只要此代码位于主类中,它就可以工作,但是当我将上述代码包含在自定义用户定义函数中时,它就不起作用。为什么会这样呢?
I am using both groovy and java within the eclipse environment to develop a UI application. I want to use groovy class loader within my code, So I've used the conventional way of getting hold of Groovy Class loader this way:
def str = new File("C:/myGroovyFile.groovy").getText()
def myclass = getClass();
println myclass //facing issue here
ClassLoader parent = myclass.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(str);
Here the problem I am facing is that the getClass() when invoked in the eclipse environment is not returning the GroovyCOnsole script rather it is returning the java.lang.class on which getClassLoader() when invoked returns a null.
I want to get hold of groovy console script for getClass() which in turn can be useful to dynamically load my groovy file located at C:\
Please tell me how can I solve this issue.
PS: I am trying to place this code within a method called initialize(). As long as this code is within the main class, it works but when I enclose the above code within a custom user defined function it doesn't work. Why is it so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑
http://groovy.codehaus.org/Embedding+Groovy
http://groovy.codehaus.org/Class+Loading
http://groovy.codehaus.org/Influencing+class+loading+at+runtime
http://groovy.codehaus.org/api/groovy/lang/GroovyClassLoader.html
既然您能够从 main 获取正确的类加载器,您是否可以不将加载器引用作为方法参数传递给initialize()?
我在 Groovlets 中遇到了类似的问题,其中类加载器似乎根据调用它的上下文(即脚本或类范围)而有所不同。我的解决方案是采用脚本范围类加载器并将加载器引用传递给类范围应用程序。
Groovy 用户列表在不同时期都涵盖了 groovy 中的类加载主题;显然,Groovy 的所有内容都是一个很好的资源;--)
http://groovy.329449.n5.nabble.com/
Edit
http://groovy.codehaus.org/Embedding+Groovy
http://groovy.codehaus.org/Class+Loading
http://groovy.codehaus.org/Influencing+class+loading+at+runtime
http://groovy.codehaus.org/api/groovy/lang/GroovyClassLoader.html
Since you are able to get correct class loader from main, can you not pass the loader reference to initialize() as a method param?
I confronted a similar issue with Groovlets, where class loader appears to differ depending on context in which it is called (i.e. script or class scope). Solution for me was to take script scope class loader and pass the loader ref to class scoped application.
Groovy user list has covered the topic of class loading in groovy at various times; obviously a great resource for all things Groovy ;--)
http://groovy.329449.n5.nabble.com/