Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

metaprogramming - Groovy - no signature of method after inserting a method

Well, I'm getting MissingMethodException even after inserting a method into a metaclass. It's strange that its says there's no signature applicable for a String, but there's for java.lang.Object Some obs:

    • yeah, the List<Class> classes that I'm iterating contains the class I'm trying to use
    • please don't suggest me to use @Log4j or any other, it's not working with any method I try to insert (even though I can manipulate the same class fields with reflections)
    • as I said, the stackstrace says that there isn't signature (java.lang.String) but there's to (java.lang.Object)
        classes.each { clazz ->
            clazz.metaClass.log = { instance.simpleLogger.log(it) }
            clazz.metaClass.debug = { instance.simpleLogger.debug(it) }
    @Override
    void enable() {
        debug("eae meu bom")
    }
groovy.lang.MissingMethodException: No signature of method: com.dont.testplugin.Terminal.debug() is applicable for argument types: (java.lang.String) values: [eae meu bom]
Possible solutions: debug(), debug(java.lang.Object), getAt(java.lang.String), log(), dump(), log(java.lang.Object)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:56) ~[?:?]
        at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:78) ~[?:?]
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49) ~[?:?]
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133) ~[?:?]
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141) ~[?:?]
question from:https://stackoverflow.com/questions/65866095/groovy-no-signature-of-method-after-inserting-a-method

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Echoing @daggett here, with the assumption that you are calling the debug method from groovy (and not java) and that the place you are calling it from is not annotated with something like @CompileStatic I would have expected that to work as well.

The following code:

class Foo {}
Foo.class.metaClass.debug = { println("debug: $it") }

def f = new Foo()
f.debug("eae meu bom")

when executed prints:

─? groovy solution.groovy
debug: eae meu bom

(Groovy Version: 3.0.6 JVM: 11.0.9.1)

I think we need more context to get you relevant answers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...