Groovy:方法无法应用于(捕获<;?>;)
我正在使用Groovy的静态类型检查器,遇到了一个有趣的问题。我有一个Groovy方法调用Java方法。在好的方面,我有Groovy:方法无法应用于(捕获<;?>;),groovy,typechecking,Groovy,Typechecking,我正在使用Groovy的静态类型检查器,遇到了一个有趣的问题。我有一个Groovy方法调用Java方法。在好的方面,我有 import com.restfb.Parameter import com.restfb.Connection import com.restfb.Version import com.restfb.DefaultFacebookClient @TypeChecked static List<Map<String, String>> getPage
import com.restfb.Parameter
import com.restfb.Connection
import com.restfb.Version
import com.restfb.DefaultFacebookClient
@TypeChecked
static List<Map<String, String>> getPages(accessToken, query) {
Connection<Page> conn = new DefaultFacebookClient(accessToken, Version.VERSION_2_5).fetchConnection(
"search", Page.class,
Parameter.with("q", query),
Parameter.with("type", "page"),
Parameter.with("fields", "name,about"),
Parameter.with("limit", 100)
);
conn.data.flatten().collect { p -> page2map(p) } // error here
}
public static Map<String, String> page2map(Page page) {
Map<String, String> res = new HashMap<>();
res.put("likes", page.getName());
// some more stuff here
return res;
}
import com.restfb.Parameter
导入com.restfb.Connection
导入com.restfb.Version
导入com.restfb.DefaultFacebookClient
@打字检查
静态列表getPages(accessToken,查询){
连接连接=新的DefaultFacebookClient(accessToken,Version.Version\u 2\u 5)。获取连接(
“搜索”,Page.class,
参数。带(“q”,查询),
参数。带有(“类型”、“页面”),
参数。带有(“字段”、“名称、关于”),
参数。带(“限制”,100)
);
conn.data.flatte()。在此处收集{p->page2map(p)}//错误
}
在Java方面,我有
import com.restfb.Parameter
import com.restfb.Connection
import com.restfb.Version
import com.restfb.DefaultFacebookClient
@TypeChecked
static List<Map<String, String>> getPages(accessToken, query) {
Connection<Page> conn = new DefaultFacebookClient(accessToken, Version.VERSION_2_5).fetchConnection(
"search", Page.class,
Parameter.with("q", query),
Parameter.with("type", "page"),
Parameter.with("fields", "name,about"),
Parameter.with("limit", 100)
);
conn.data.flatten().collect { p -> page2map(p) } // error here
}
public static Map<String, String> page2map(Page page) {
Map<String, String> res = new HashMap<>();
res.put("likes", page.getName());
// some more stuff here
return res;
}
公共静态地图page2map(第页){
Map res=新的HashMap();
res.put(“likes”,page.getName());
//这里还有一些东西
返回res;
}
如果不进行类型检查,此代码运行良好。通过类型检查,我得到的
方法无法应用于(捕获)
Groovy代码最后一行的编译器错误。我正在运行Groovy 2.4.6。有什么建议吗?Groovy默认设置并假设您非常通用。这让我在尝试与Java对话时措手不及
我怀疑flatten
或collect
会给您一个ArrayList
或HashMap
。该内联闭包可能返回对象
由于您没有明确指定collect
返回的内容,因此我希望它将返回ArrayList
,并且内联闭包将是签名对象f(对象x)
试着用(粗略地)以下内容非常明确
Map<String, String> my_method(Page p) = { return page2map(p) }
List<Map<String, String>> container;
container = conn.data.flatten().collect(my_method)
return container
Map my_方法(p页)={return page2map(p)}
列表容器;
container=conn.data.flatte().collect(my_方法)
返回容器
我还将查看生成的groovy存根,以更深入地了解正在发生的事情,从而确切地了解正在抛出的类型。您正在使用的闭包将出现在类$getPages$Inline$closure$1的
您也可以考虑使用<代码> @ Type Cuths<代码>,因为不必处理类型是首先使用Groovy的优点。
另外,您可能指的是ClassName.page2map
,而不是page2map
尝试明确定义p
的类型:。收集{Page p->…}