java - 使用Varargs调用方法时出现javassist.CannotCompileException

标签 java compiler-errors variadic-functions javassist

我正在尝试通过javassist调用java.util.Arrays.asList(..)。但是得到下面的异常。

javassist.CannotCompileException: [source error] asList(java.lang.Double,java.lang.Double,java.lang.Double) not found in java.util.Arrays
我将修改器设置为:
m.setModifiers(m.getModifiers() | javassist.Modifier.VARARGS);
即使那样,我也要异常(exception)。
也就是说,Javassist无法理解我在打电话:
public static <T> List<T> asList(T... a) {
    return new ArrayList<>(a);
}
它正在显式地寻找类似的方法:
public static <T> List<T> asList(T a, Ta1, Ta2) {
    return new ArrayList<>(a);
}
如何使Javassist识别使用varargs的方法?

最佳答案

@javaseeker,您为什么不对我的评论使用react并提供更多上下文信息?您告诉我们您希望调用哪种方法,但不显示实际准备和执行调用的代码。那没有帮助。
无论如何,我都会冒一个有根据的猜测:可能是您尝试使用多个参数调用目标方法,就像从普通Java源代码中调用它一样。但是像T... a这样的varargs参数实际上只是参数列表末尾T[]的语法糖。因此,如果准备了T[]并将其作为参数传递给方法,则它应该可以按预期工作,请参见Javassist tutorial, chapter "varargs":

To call this method in the source code compiled by the compiler embedded in Javassist, you must write:

length(new int[] { 1, 2, 3 });

instead of this method call using the varargs mechanism:

length(1, 2, 3);

顺便说一句,从您的错误消息来看,您的情况似乎需要Double[]

关于java - 使用Varargs调用方法时出现javassist.CannotCompileException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65855580/

相关文章:

c++ - C2975 'N' 的无效模板参数,预期的编译时常量表达式

c# - 我不断收到相同的错误, “CS1525: Unexpected symbol ` string'。我只是找不到我出错的地方

ios - 尝试使用ASIHTTP时出现mach-o链接器错误

java - Lucene 4.0 中的词频

java - 如何使用 JDBC 连接 XAMPP MySQL 本地数据库?

java - HTTP 状态 404 -/EventTracker/greeting

java - 如何将 JavaRDD<Row> 转换为 JavaRDD<List<String>>?

scala - 将变长参数传递给期望相同的另一个函数?

c++ - Boost MPL排序模板参数包

java - 将元素添加到 Java vararg 调用的最易读的方式