gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:38:52 24 4
gpt4 key购买 nike

我正在尝试通过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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com