Groovy的metaClass的范围? [英] Scope of Groovy's metaClass?

查看:67
本文介绍了Groovy的metaClass的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以运行脚本来自动执行某些任务。我想在这些脚本中使用元编程来优化代码大小和可读性。因此,而不是:

  try {
def res = factory.allocate();
...用res ...做一些事情...
} finally {
res.close()
}

我想

  Factory.metaClass.withResource = { c  - > 
尝试{
def res = factory.allocate();
c(res)
} finally {
res.close()
}
}

因此脚本编写者可以编写:

  factory.withResource {res  - > ; 
...用res ...做一些事情...
}

(和我可以做适当的错误处理等)。



现在我想知道什么时候以及如何执行此操作。我可以将元类的操作附加到标题中,但是我担心如果两个用户同时运行脚本(并发访问元类)会发生什么。



元类的范围是什么?编译器?脚本环境? Java VM?加载Groovy的类加载器?



我的推理是,如果Groovy元类具有VM范围,那么我可以在启动过程中运行一次安装脚本。 $ b

解决方案

元类存在于每个类加载器 [需要的引用]

档案 /tmp/Qwop.groovy

  class Qwop {} 

文件 /tmp/Loader.groovy

  Qwop.metaClass.bar = {} 
qwop1 = new Qwop()
assert qwop1.respondsTo('bar')

loader = new GroovyClassLoader()
clazz = loader.parseClass new File(/ tmp / Qwop.groovy)

clazz.metaClass.brap = {'oh my'}

qwop = clazz.newInstance()

assert!qwop.respondsTo('bar')
assert qwop1.respondsTo('bar')

assert qwop.brap()==oh my
assert!qwop1.respondsTo('brap')

//这里是自定义类加载器
新的GroovyShell (loader).evaluate new File('/ tmp / QwopTest.groovy')

测试作用域元类( /tmp/QwopTest.groovy ):

  assert!new Qwop().respondsTo('bar')
assert new Qwop().respondsTo('brap')

执行:

  $ groovy Loaders.groovy 
$
code>

如果您有一组定义良好的类,您可以在classloader加载的类的顶部应用元编程 em>,根据添加的 brap 方法。


I have an application which can run scripts to automate certain tasks. I'd like to use meta programming in these scripts to optimize code size and readability. So instead of:

try {
    def res = factory.allocate();
    ... do something with res ...
} finally {
    res.close()
}

I'd like to

Factory.metaClass.withResource = { c -> 
    try {
        def res = factory.allocate();
        c(res)
    } finally {
        res.close()
    }
}

so the script writers can write:

factory.withResource { res ->
    ... do something with res ...
}

(and I could do proper error handling, etc).

Now I wonder when and how I can/should implement this. I could attach the manipulation of the meta class in a header which I prepend to every script but I'm worried what would happen if two users ran the script at the same time (concurrent access to the meta class).

What is the scope of the meta class? The compiler? The script environment? The Java VM? The classloader which loaded Groovy?

My reasoning is that if Groovy meta classes have VM scope, then I could run a setup script once during startup.

解决方案

Metaclasses exist per classloader [citation needed]:

File /tmp/Qwop.groovy:

class Qwop { }

File /tmp/Loader.groovy:

Qwop.metaClass.bar = { }
qwop1 = new Qwop()
assert qwop1.respondsTo('bar')

loader = new GroovyClassLoader()
clazz = loader.parseClass new File("/tmp/Qwop.groovy")

clazz.metaClass.brap = { 'oh my' }

qwop = clazz.newInstance()

assert !qwop.respondsTo('bar')
assert qwop1.respondsTo('bar')

assert qwop.brap() == "oh my"
assert !qwop1.respondsTo('brap')

// here be custom classloaders
new GroovyShell(loader).evaluate new File('/tmp/QwopTest.groovy')

And a script to test the scoped metaclass (/tmp/QwopTest.groovy):

assert !new Qwop().respondsTo('bar')
assert new Qwop().respondsTo('brap')

Execution:

$ groovy Loaders.groovy 
$ 

If you have a set of well defined classes you could apply metaprogramming on top of the classes loaded by your classloader, as per the brap method added.

这篇关于Groovy的metaClass的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆