javassist 应用问题
RT 先上代码 这是调用轮子的代码(实际应用时候的代码) ↓
ClassEditor editor = new ClassEditor("ItemLoreOrigin", clazz);
editor
.addNewMethod("isAlreadyRemake", "public boolean isAlreadyRemake(){return true;}");
CtMethod ctMethod = editor.copyMethod(methodName,"getEquipments");
editor
.addMethod(ctMethod)
.overrideMethod("getValidItem",
"{" + "" + "if ($1 == null){" + "return new java.util.ArrayList();" + "}" + "" + "return getEquipments($1,$2);" + "" + "}")
.insertForMethod("getEquipments",
"{" +"java.util.UUID uuid = $1.getUniqueId();" +
"java.util.List itemStacks = com.frostmourne.itemloreoriginapi.ItemLoreOriginAPI.getAllSourceItems(uuid);" +
"$_.addAll(itemStacks);" + "}"
, true)
.remakeFile();
这是轮子的相关实现代码
/**
*
* 向类文件的方法中插入内容<br/>
* @param methodName 方法名称
* @param src 方法内容
* @param after 插入位置是否在后
*
**/
public ClassEditor insertForMethod(String methodName,String src,boolean after){
if (this.ctClass.isFrozen()){
this.ctClass.defrost();
}
try {
CtMethod declaredMethod = this.ctClass.getDeclaredMethod(methodName);
if (after){
declaredMethod.insertAfter(src);
}else{
declaredMethod.insertBefore(src);
}
} catch (NotFoundException e) {
System.out.println("未找到要插桩的方法 " + methodName);
System.out.println("如果你想新增方法请使用 addNewMethod 方法");
return this;
} catch (CannotCompileException e) {
System.out.println("编译错误,请检查编译内容");
System.out.println(src);
return this;
}
return this;
}
/**
*
* 重写类文件的方法<br/>
* @param methodName 方法名称
* @param src 方法内容
*
**/
public ClassEditor overrideMethod(String methodName,String src){
if (this.ctClass.isFrozen()){
this.ctClass.defrost();
}
try {
CtMethod declaredMethod = this.ctClass.getDeclaredMethod(methodName);
declaredMethod.setBody(src);
} catch (NotFoundException e) {
System.out.println("未找到要覆写的方法 " + methodName);
System.out.println("如果你想新增方法请使用 addNewMethod 方法");
return this;
} catch (CannotCompileException e) {
System.out.println("编译错误,请检查编译内容");
System.out.println(src);
return this;
}
return this;
}
public CtMethod copyMethod(String originalMethod,String copiedMethodName){
try {
CtMethod ctMethod = this.ctClass.getDeclaredMethod(originalMethod);
return CtNewMethod.copy(ctMethod,copiedMethodName,this.ctClass,null);
} catch (NotFoundException | CannotCompileException e) {
throw new RuntimeException(e);
}
}
/**
*
* 写入修改后的类文件并重新加载插件(热加载)<br/>
* 推荐使用/stop关服后重载<br/>
*
**/
public void remakeFile(){
try {
System.out.println("正在备份要修改的插件");
String path = clazz.getProtectionDomain().getCodeSource().getLocation().getPath().substring(1);
MXStarsEx.getFileManager().copyFileUsingStream(new File(path),new File(path + ".bak"));
byte[] bytes = this.ctClass.toBytecode();
String fileName = ctClass.getName().replace(".","/") + ".class";
boolean unload = MXStarsEx.getPluginManager().unload(Bukkit.getConsoleSender(), plugin);
if (unload){
System.out.println("插件 " + plugin + " 已卸载");
}else{
System.out.println("插件 " + plugin + " 卸载失败");
}
MXStarsEx.getFileManager().replaceJarFile(path,bytes,fileName);
Plugin plugin1 = Bukkit.getPluginManager().loadPlugin(new File(path));
Bukkit.getPluginManager().enablePlugin(plugin1);
System.out.println("警告: 使用热加载后的插件可能会存在一些隐患");
System.out.println("警告: 推荐使用/stop重启服务器");
ClassPool.getDefault().removeClassPath(new ClassClassPath(clazz));
} catch (IOException | InvalidPluginException | InvalidDescriptionException e) {
throw new RuntimeException(e);
} catch (CannotCompileException e) {
System.out.println("编译错误,请检查编译内容");
}
}
但是运行后却出现只能成功往同一个ClassEditor注入一项代码的情况
如getValidItem方法确实复制了一份并将其更名为getEquipments并将原方法的内容修改掉了
但是后面向getEquipments内插入内容的部分却被无视了
并未执行 反编译文件看之后发现getValidItems还是没有被按照代码正确修改
且addNewMethod方法一直是成功的
求问 这种情况是什么问题导致的 如何修复 万分感谢!