无敌三脚猫
本帖最后由 无敌三脚猫 于 2023-3-2 22:57 编辑

https://www.mcbbs.net/thread-874131-1-1.html
这边我看过很多次了,最近才发现一个问题
跟没接触过cnpc的人解释一下,这个mod有一个脚本系统,可以用Nashorn引擎写js脚本
上述脚本中出现了一个奇怪的东西
  1. var Event=Java.type("net.minecraftforge.event.ServerChatEvent");
  2. var event=new Event();
复制代码
咦?这哪儿来的无参构造?
如果在java里写new net.minecraftforge.event.ServerChatEvent(),会显示构造函数 ServerChatEvent()未定义
因为ServerChatEvent真的只有这一个构造函数
  1.     public ServerChatEvent(EntityPlayerMP player, String message, ITextComponent component)
  2.     {
  3.         super();
  4.         this.message = message;
  5.         this.player = player;
  6.         this.username = player.getGameProfile().getName();
  7.         this.component = component;
  8.     }
复制代码
可能不同版本具体的参数不一样,不过这不重要,这个类确实是在每个版本都没有无参构造,所以Nashorn是怎么把它new出来的?而且没有报错(我写过了)
不关nashorn的事,是forge干的

youyihj
Unsafe 的 allocateInstance 方法?没用过 Nashorn 盲猜的。

Neige
本帖最后由 Neige 于 2023-3-2 13:23 编辑

我拿bukkit的AsyncPlayerChatEvent试了一下,不行,会报they do not match any of its method signatures
你这很怪

美味的曲奇
Nashorn 调用无参构造会报出
TypeError: Can not create new object with constructor ClassA with the passed arguments; they do not match any of its method signatures.

Unsafe.allocateInstance 可以做到强制实例 但从我这里来看Nashorn 没有采用这个
也许是版本不同

下一页 最后一页