简体   繁体   中英

Groovy method “no signature” String argument and String return

I have a beginner question about using Groovy. I had thought this is a simple, unchallenging code snippet.

  def getName( str ){
    def rslt = "(none)";
    str.eachMatch( /$[TABLE_NAME:[a-zA-Z]]^/ ){
        -> patrn

        if( "(none)" == rslt ){
            rslt = patrn;
        }
    }//eachMatch

    return rslt;
}//getName

The idea is to return the first match to regex from the string ' str ' passed, and the matching string in ' rslt '.

Instead I have this kind of error.

Exception in thread "main" groovy.lang.MissingMethodException: 
    No signature of method: All_Tables$_main_closure1.getName() 
      is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) 
        values: [Action]

        Possible solutions: getAt(java.lang.String), getAt(java.lang.String) 
          at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:379)
          at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)
          at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:730)
          at ...
          at All_Tables$_main_closure1.doCall(All_Tables.groovy:31)
          at ...
          at groovy.sql.Sql.eachRow(Sql.java:1186)
          at ... 
          at All_Tables.main(All_Tables.groovy:26)

The call, it seemed straightforward:

static void main( String args[] ){
    def sql = Sql.newInstance(
            "jdbc:mysql://dev:3306/dbname",
            "dbuser", "dbpass", "com.mysql.jdbc.Driver" );

    sql.eachRow( "SHOW TABLES;" ){
        str = it[0].toString();
        nam = getName( str );
    };

}//main

What curled my toes is that the Groovy documents mention that basic Java and Groovy libraries (such as java.lang.String) are included. Yet the signature fails on String.

I know this much. When I do this small piece of code with jRuby, is just works. None of this ' signature ' nonsense.

... Any advice welcome :-)

Will

It's hard to be exact as you omit a lot of context from your question, but it looks like you are trying to call a non-static method from the static main method.

Try either making getName static, or call it on an instance of your enclosing class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM