简体   繁体   English

Java更改方法返回而不访问类Class

[英]Java changing method return without access to the class Class

I'm modifying someone else's code to implement a new functionality and I can't do it without changing the return of one of the functions.我正在修改其他人的代码以实现新功能,如果不更改其中一个函数的返回,我就无法做到这一点。 As I've said, this is not my code, so I can't change a single line of code.正如我所说,这不是我的代码,所以我不能更改任何一行代码。

The function itself is the following:函数本身如下:

package me.Mohamad82.MineableGems.Core;

public class DropReader {
  ...
  public DropReader() {}
  public CustomDrop readCustomDrop(ConfigurationSection section, String mined, @Nullable String sectionNumber) {
    ...
  }
}

And I'm trying to do something like this:我正在尝试做这样的事情:

package com.rogermiranda1000.mineit;

public class DropReader extends me.Mohamad82.MineableGems.Core.DropReader {
    public DropReader() {
        super();
    }

    @Override
    public CustomDrop readCustomDrop(ConfigurationSection section, String mined, @Nullable String sectionNumber) {
        CustomDrop drop = super.readCustomDrop(section, mined, sectionNumber);
        if (drop != null) {...}
        return drop;
    }
}

The problem is that I have no idea where to start.问题是我不知道从哪里开始。 I can't change their code to call the other object, and I can't change the function call either.我不能改变他们的代码来调用另一个对象,我也不能改变函数调用。

The object is created every time (inside the functions that uses DropReader) so Reflection won't work, and searching I found something named Javassist but it won't work if the class is already loaded.每次都会创建该对象(在使用 DropReader 的函数内部),因此反射不起作用,并且搜索我发现了一个名为 Javassist 的东西,但如果该类已经加载,它将不起作用。 I should be able to load by code before, but even with that I don't know if I'm approaching to the problem correctly, what do you think?我之前应该可以通过代码加载,但即使这样我也不知道我是否正确地解决了这个问题,你怎么看?

Edit: I'll try to explain better the situation.编辑:我会尝试更好地解释这种情况。 There's a class named Commands that runs the command new DropReader().readCustomDrop(section, mined, sectionNumber) .有一个名为Commands的类运行命令new DropReader().readCustomDrop(section, mined, sectionNumber) The problem is that if section.getString("mine") != null I need to change the readCustomDrop return (I need to add an aditional property).问题是如果section.getString("mine") != null我需要更改readCustomDrop返回(我需要添加一个附加属性)。

I can't change Commands's code, nor DropReader.我无法更改 Commands 的代码,也无法更改 DropReader。 Commands MUST get the modified object.命令必须获取修改后的对象。

Edit2: It should work on both Java 8 and 17. Edit2:它应该适用于 Java 8 和 17。

Probably the best way to do this is Java Instrumentation, but it was too complex for me so I did the following (assuming you have the .jar, and the .jar it's not already loaded):可能最好的方法是 Java Instrumentation,但它对我来说太复杂了,所以我做了以下操作(假设你有 .jar,而 .jar 它还没有加载):

  1. Open the .jar as a Zip using ZipFile class使用 ZipFile 类将 .jar 作为 Zip 打开
  2. Send the .java to ClassFileToJavaSourceDecompiler (from JD-Core ) and decompile it将 .java 发送到 ClassFileToJavaSourceDecompiler(来自JD-Core )并反编译它
  3. Change the .java code更改 .java 代码
  4. Compile the new code running javac with Runtime.getRuntime().exec (note: you'll probably need to add the dependencies using -classpath )使用Runtime.getRuntime().exec编译运行javac的新代码(注意:您可能需要使用-classpath添加依赖项)
  5. Add the compiled .java to the .jar将编译好的 .java 添加到 .jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM