Groovy 没有调用 Java 库的方法签名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12865191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:32:43  来源:igfitidea点击:

Groovy No signature of method calling Java library

javagroovycastingstatic-methods

提问by AndyJ

As many questions begin, this is driving me crazy.

随着许多问题的开始,这让我发疯。

I have a homegrown StarTeamjava library. I have one static method like this:

我有一个本土的StarTeamJava 库。我有一个这样的静态方法:

public static Label getLatestDeploymentLabel(com.starbase.starteam.File child) {
 // blah
}

The method works as expected when I call it from java. When I call it from Groovy, I get:

当我从 java 调用它时,该方法按预期工作。当我从 Groovy 调用它时,我得到:

Caught: groovy.lang.MissingMethodException: 
No signature of method: static pkg.starteam.StarTeamUtils.getLatestDeploymentLabel() 
is applicable for argument types: (com.starbase.starteam.File) 
values: [FILENAME-FOO.sql] at starteam.run(starteam.groovy:54)

I put in a printlnright before I call that method:

我在println调用该方法之前输入了一个权限:

chgset.elements().each() { item ->
  println "type of item is ${item.class.getName()}"
  def latestlabel = StarTeamUtils.getLatestDeploymentLabel(item)
}

And confirm that, in fact, it's iterating what I expect it's iterating over:

并确认,事实上,它正在迭代我期望它迭代的内容:

type of item is com.starbase.starteam.File

type of item is com.starbase.starteam.File

I've seen a few different similar issues in other posts relating to static methods and the responses are along the lines of "are you sure it's a static method?". I'm sure it's a static method.

我在与静态方法相关的其他帖子中看到了一些不同的类似问题,并且回答是“你确定这是一个静态方法吗?”。我确定这是一个静态方法。

There isn't much groovy code to this. What there is of it is all contained in a single script in the default package. The main method is then called implicitly and it's in the body of the script class that the call out to the java library is made. I set the classpath in a DOS batch wrapper script, e.g.:

这没有太多常规代码。它的所有内容都包含在默认包中的单个脚本中。然后隐式调用 main 方法,并且在脚本类的主体中调用 java 库。我在 DOS 批处理包装器脚本中设置了类路径,例如:

SET INITIALCLASSPATH=%CLASSPATH%
SET NEWCP=c:/libs/etc.jar;c:/etc/etc.jar
SET GROOVYPATH=c:/groovy.bat
SET CLASSPATH=%NEWCP%
%GROOVYPATH% %*
SET CLASSPATH=%INITIALCLASSPATH%

I created a simple situation which I thinkemulates my situation.

我创建了一个简单的情况,我认为它模仿了我的情况。

C:\apps\groovy-1.8.6\scripts>type Other.java
class Other {
    private String name = "notset";
    public Other(String name) {
        this.name = name;
        System.out.println("Created an other");
    }
    public String toString() {
        return name;
    }
}
C:\apps\groovy-1.8.6\scripts>type ThingList.java
import java.util.ArrayList;
import java.util.Iterator;

class ThingList {
    ArrayList ourlist = new ArrayList<Other>();
    public ThingList(){}
    public ArrayList add(Other thing) {
        ourlist.add(thing);
        return ourlist;
    }
    public Iterator iterator(){
        return ourlist.iterator();
    }
}
C:\apps\groovy-1.8.6\scripts>type JavaLib.java
class JavaLib {
    public JavaLib() {}
    public static ThingList getThingList(Other thing) {
        ThingList tl = new ThingList();
        Other one = new Other("extra one");
        tl.add(thing);
        tl.add(one);
        return ThingList;
    }
}
C:\apps\groovy-1.8.6\scripts>type testthing.groovy
def myOther = new Other("A new other")
println "type of myOther is ${myOther.class.getName()}"
def myList = getThingList(myOther)
myList.each() {
  println it
}
C:\apps\groovy-1.8.6\scripts>type wrapper.bat
@ECHO OFF
SET INITIALCLASSPATH=%CLASSPATH%
SET GROOVY=C:\apps\groovy-1.8.6\bin\groovy.bat
SET CP=.
SET CLASSPATH=%CP%

%GROOVY% %*

SET CLASSPATH=%INITIALCLASSPATH%
C:\apps\groovy-1.8.6\scripts>wrapper.bat testthing.groovy
Created an other
type of myOther is Other
Caught: groovy.lang.MissingMethodException: No signature of method: testthing.ge
tThingList() is applicable for argument types: (Other) values: [A new other]
groovy.lang.MissingMethodException: No signature of method: testthing.getThingLi
st() is applicable for argument types: (Other) values: [A new other]
        at testthing.run(testthing.groovy:3)

C:\apps\groovy-1.8.6\scripts>

Any insights or suggestions would be greatly appreciated!

任何见解或建议将不胜感激!

AndyJ

安迪

回答by Peter Niederwieser

Without a way to reproduce, it's impossible to say for sure what the problem is. One possibility is that it is a class loading problem. Is the Groovy code contained in a regular Groovy class that's sitting on the class path, or does the Groovy code get loaded dynamically (e.g. by using GroovyShell)?

如果没有重现的方法,就不可能确定问题是什么。一种可能是类加载问题。Groovy 代码是否包含在位于类路径上的常规 Groovy 类中,还是 Groovy 代码是动态加载的(例如,通过使用GroovyShell)?