You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Groovy脚本实例中声明的所有自定义函数?

Retrieving Declared Functions from a Groovy Script Object

Got it, let's break down why you're not seeing your myStuff function when using getMetaMethods(), and walk through a few reliable ways to pull the declared methods from your script object.

Why getMetaMethods() Isn't Showing myStuff

When you parse a script with GroovyShell, the generated script class includes all inherited methods from the base Script class (and Object) alongside your custom functions. The getMetaMethods() call returns all meta-methods for the object—including tons of inherited ones like toString(), run(), etc. Your myStuff is in there, but you're probably missing it among the noise if you're not filtering the results.

That said, there are cleaner, more targeted approaches to get just your custom methods.

Solution 1: Use Reflection to Get Declared Methods

You can directly inspect the script's generated class for declared methods, then filter to find your target function:

GroovyShell shell = new GroovyShell(); 
Script script = shell.parse("def myStuff(a){ return a }");

// Filter declared methods to find myStuff (match name + parameter count)
def targetMethod = script.getClass().getDeclaredMethods().find {
    it.name == "myStuff" && it.parameterCount == 1
}

if (targetMethod) {
    println("Found method: ${targetMethod.name}")
    // You can even invoke it directly if needed:
    def result = targetMethod.invoke(script, "test input")
    println("Invoked result: ${result}")
}

Solution 2: Use Groovy's MetaClass with getMetaMethod()

For a more Groovy-idiomatic approach, use the metaClass's getMetaMethod() to directly look up your function by name and parameter types:

GroovyShell shell = new GroovyShell(); 
Script script = shell.parse("def myStuff(a){ return a }");

// Look up the method by name and parameter type (Object matches any argument type here)
def myMetaMethod = script.metaClass.getMetaMethod("myStuff", [Object] as Class[])

if (myMetaMethod) {
    println("Found meta-method: ${myMetaMethod.name}")
    def result = myMetaMethod.invoke(script, "hello")
    println("Invoked result: ${result}")
}

Solution 3: Get All User-Defined Methods

If you want to retrieve all custom methods you declared in the script (excluding inherited ones), compare the script's declared methods against the base Script class's methods:

GroovyShell shell = new GroovyShell(); 
Script script = shell.parse("def myStuff(a){ return a }\ndef anotherFunc(){ return 'hi' }");

def scriptClass = script.getClass()
// Get all method names from the parent Script class
def inheritedMethodNames = Script.class.getMethods().collect { it.name }

// Filter for non-inherited, non-synthetic methods (synthetic = compiler-generated)
def userMethods = scriptClass.getDeclaredMethods().findAll {
    !inheritedMethodNames.contains(it.name) && !it.isSynthetic()
}

println("User-defined methods: ${userMethods*.name}")
// Output: User-defined methods: [myStuff, anotherFunc]

内容的提问来源于stack exchange,提问作者Beto Neto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:30:46