Java元编程

发布于 2024-12-27 05:32:19 字数 626 浏览 2 评论 0原文

我正在用 Java 开发我的第一个真正的项目。尽管我对动态语言有更多的经验,但我开始熟悉这门语言。

我有一个行为类似于以下内容的类:

class Single
{
    public void doActionA() {}
    public void doActionB() {}
    public void doActionC() {}
}

然后我有一个 SingleList 类,它充当这些类的集合(具体来说,它用于 2D Sprite 库,并且“操作”都是各种变换:旋转、剪切、缩放等)。我希望能够执行以下操作:

class SingleList
{
    public void doActionA() {
        for (Single s : _innerList) {
            s.doActionA();
        }
    }

    ... etc ...
}

是否有任何方法可以简单地将方法(或已知的方法列表)推迟到内部列表的每个成员?有什么办法不需要专门列出每个方法,然后循环每个内部成员并手动应用它?

让事情变得更困难的是,这些方法的数量不同,但返回类型都是“void”。

I'm working on my first real project with Java. I'm beginning to get comfortable with the language, although I have more experience with dynamic languages.

I have a class that behave similar to the following:

class Single
{
    public void doActionA() {}
    public void doActionB() {}
    public void doActionC() {}
}

And then I have a SingleList class that acts as a collection of these classes (specifically, it's for a 2D Sprite library, and the "actions" are all sorts of transformations: rotate, shear, scale, etc). I want to be able to do the following:

class SingleList
{
    public void doActionA() {
        for (Single s : _innerList) {
            s.doActionA();
        }
    }

    ... etc ...
}

Is there any way to simply defer a method (or a known list of methods) to each member of the inner list? Any way without having to specifically list each method, then loop through each inner member and apply it manually?

To make things a bit harder, the methods are of varying arity, but are all of return type "void".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽月 2025-01-03 05:32:20

不幸的是,Java 并不容易支持在运行时创建类,而这正是您所需要的:需要使用必要的存根方法自动更新 SingleList 以匹配 Single 类。

我可以想到以下解决此问题的方法:

  1. 使用Java 反射

    • 优点:
      • 它有 Java 语言版本,您可以轻松找到文档和示例。
    • 缺点:
      • SingleList 类将不再与 Single 类接口兼容。
      • Java 编译器和任何 IDE 通常无法帮助通过反射调用方法 - 编译器捕获的错误通常会转换为运行时异常。
      • 根据您的使用案例,您可能还会发现性能明显下降。
  2. 使用构建系统和某种源代码生成器来自动创建 SingleList.java 文件。

    • 优点:
      • 设置完成后,您将无需再处理它。
    • 缺点:
      • 设置此功能有一定难度。
      • 您必须单独确保在任何 JVM(或您的 IDE)中加载的 SingleList 类实际上与加载的 Single 类匹配。
  3. 手动解决这个问题 - 创建一个接口(例如 SingleInterface)或一个供两个类使用的基本抽象类应该会有所帮助,因为任何像样的 IDE 都会指出未实现的方法。正确的类架构将最大限度地减少重复代码,并且您的 IDE 可能能够帮助生成样板部分。

    • 优点:
      • 没有需要克服的设置曲线。
      • 您的 IDE 将始终看到正确的类集。
      • 类架构通常会在之后进行改进。
    • 缺点:
      • 一切都是手动的。
  4. 使用字节码生成库,例如 Javassist 或 < a href="http://commons.apache.org/bcel/" rel="noreferrer">BCEL 动态生成/修改 SingleList 类即时。

    • 优点:
      • 此方法非常强大,从长远来看可以节省大量时间。
    • 缺点:
      • 使用字节码生成库通常不是微不足道,对于胆小的人来说也不是
      • 根据您编写代码的方式,您的 IDE 及其对动态类​​的处理也可能会出现问题。

Unfortunately Java does not readily support class creation at runtime, which is what you need: the SingleList needs to be automatically updated with the necessary stub methods to match the Single class.

I can think of the following approaches to this issue:

  1. Use Java reflection:

    • Pros:
      • It's readily available in the Java language and you can easily find documentation and examples.
    • Cons:
      • The SingleList class would not be compatible with the Single class interface any more.
      • The Java compiler and any IDEs are typically unable to help with methods called via reflection - errors that would be caught by the compiler are typically transformed into runtime exceptions.
      • Depending of your use case, you might also see a noticeable performance degradation.
  2. Use a build system along with some sort of source code generator to automatically create the SingleList.java file.

    • Pros:
      • Once you set it up you will not have to deal with it any more.
    • Cons:
      • Setting this up has a degree of difficulty.
      • You would have to separately ensure that the SingleList class loaded in any JVM - or your IDE, for that matter - actually matches the loaded Single class.
  3. Tackle this issue manually - creating an interface (e.g. SingleInterface) or a base abstract class for use by both classes should help, since any decent IDE will point out unimplemented methods. Proper class architecture would minimize the duplicated code and your IDE might be able to help with generating the boilerplate parts.

    • Pros:
      • There is no setup curve to get over.
      • Your IDE will always see the right set of classes.
      • The class architecture is usually improved afterwards.
    • Cons:
      • Everything is manual.
  4. Use a bytecode generation library such as Javassist or BCEL to dynamically generate/modify the SingleList class on-the-fly.

    • Pros:
      • This method is extremely powerful and can save a lot of time in the long term.
    • Cons:
      • Using bytecode generation libraries is typically not trivial and not for the faint-hearted.
      • Depending on how you write your code, you may also have issues with your IDE and its handling of the dynamic classes.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文