防止GroovyDefaultMethods join()遮盖Java类的join()方法? [英] Prevent GroovyDefaultMethods join() from shadowing join() method of Java class?

查看:116
本文介绍了防止GroovyDefaultMethods join()遮盖Java类的join()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Groovy中使用Java库,但是Groovy正在使用其自己的方法覆盖方法名称join之一.

I'm trying to use a Java library in Groovy, but Groovy is overriding one of the methods name join with its own method.

以下代码失败:

%%classpath add mvn
tech.tablesaw tablesaw-core 0.32.6
tech.tablesaw tablesaw-beakerx 0.32.6
com.jimmoores quandl-tablesaw 2.0.0

%import com.jimmoores.quandl.*
%import com.jimmoores.quandl.tablesaw.*
%import tech.tablesaw.api.*

// display Tablesaw tables with BeakerX table display widget
tech.tablesaw.beakerx.TablesawDisplayer.register()

TableSawQuandlSession session = TableSawQuandlSession.create();
Table table1 = session.getDataSet(DataSetRequest.Builder.of("FRED/BCNSDODNS").build());
table1.column("Value").setName("Corporate Credit");
Table table2 = session.getDataSet(DataSetRequest.Builder.of("FRED/CMDEBT").build());
table2.column("Value").setName("Household Credit");
Table result = table1.join("Date").inner(table2, "Date");

错误是:

groovy.lang.MissingMethodException:没有方法签名:java.lang.String.inner()适用于参数类型:(tech.tablesaw.api.Table,java.lang.String)

groovy.lang.MissingMethodException: No signature of method: java.lang.String.inner() is applicable for argument types: (tech.tablesaw.api.Table, java.lang.String)

这很奇怪,Table.join(String)返回一个DataFrameJoiner:

This is strange Table.join(String) returns a DataFrameJoiner: https://static.javadoc.io/tech.tablesaw/tablesaw-core/0.32.6/tech/tablesaw/api/Table.html#join-java.lang.String...-

我认为正在发生的事情是TableIterable<Row>,因此Groovy可能正在使用

I think what's happening is that Table is an Iterable<Row> so maybe Groovy is picking up its own join method.

真正让我感到奇怪的是,即使

Where it gets really strange is that it works if I switch the Tablesaw version from 0.32.6 to 0.24.9 even though Table also implements Iterable<Row> in that version

有什么想法可以迫使Groovy使用Tablesaw库中的join方法而不是它自己的join方法吗?

Any idea how I can force Groovy to use the join method from the Tablesaw library instead of its own join method?

推荐答案

您会遇到此问题,因为Table.join()方法的原型如下:

You run into this problem because the prototype of the Table.join() method is the following:

public DataFrameJoiner join(String... columnNames)

来源:您已经注意到Groovy通过其join()方法增强了Iterable<T>类,但是其原型如下:

You have already noticed that Groovy enhances Iterable<T> class with its join() method, but its prototype is the following:

public String join(String separator)

来源: http://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Iterable.html#join(java.lang.String)

这是为什么在以下情况下的原因:

This is why in the case of the following:

table1.join("Date").inner(table2, "Date");

Groovy解析Iterable<T>.join(String separator)而不是Table类中的方法.它仅与0.24.9版本配合使用,因为它提供了具有以下原型的方法:

Groovy resolves Iterable<T>.join(String separator) instead of the method from the Table class. It worked fine with the version 0.24.9 only because it was providing a method with the following prototype:

public DataFrameJoiner join(String columnName)

来源: https://static.javadoc.io/tech.tablesaw/tablesaw-core/0.24.9/tech/tablesaw/api/Table.html#join-java.lang.String -

因此在这种情况下,Table.join(String columnName)覆盖了方法Iterable<T>.join(String separator).

So in this case, Table.join(String columnName) was overriding a method Iterable<T>.join(String separator).

您可以通过使用显式的String[]参数信息进行调用来解决此问题,因此Groovy会立即选择正确的方法.

You can solve this problem by making a call with an explicit String[] parameter information, so Groovy picks the correct method right away.

table1.join(["Date"] as String[]).inner(table2, "Date")

如果您传递两个字符串以使Groovy选择varargs方法,那么它将在没有任何显式参数类型信息的情况下工作.

It would work without any explicit parameter type information if you pass two strings to make Groovy pick the varargs method.

table1.join("Date", "Something").inner(table2, "Date")

但是,对于单个字符串参数,由于有两种重载方法定义,因此您需要进行显式的参数选择.

However, in case of a single string argument, you need to make an explicit parameter choice because of the two overloading methods definition.

这篇关于防止GroovyDefaultMethods join()遮盖Java类的join()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆