在运行时在Java中扩展或添加新类 [英] Extending or adding new classes at runtime in Java

查看:508
本文介绍了在运行时在Java中扩展或添加新类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以在运行时在java中添加(或扩展现有的)类。我遇到了一个问题,其中我必须在运行时扩展一个现有的类,并将其添加到类路径,以便这个新类被拾起。

Is there a way to add (or extend existing) classes at runtime in java. I'm stuck on a problem, in which I have to extend an existing class at runtime and add this to the classpath, so that this new class get picked up.

感谢,

推荐答案

有很多方法可以做到这一点。

There are a number of ways you could do this.


  • 在运行时使用 javax.tools 包,然后使用 ClassLoader

  • 如果您正在编写接口,可以使用代理

  • 字节码操作/生成,使用 BCEL ASM (后者对语言功能(如注释)有更多的最新支持),然后使用ClassLoader加载类。

  • Compile source code at runtime using the javax.tools package and then load them using a ClassLoader.
  • If you are writing to interfaces, you can decorate classes with a Proxy.
  • Take the more complicated route of bytecode manipulation/generation using a technology like BCEL or ASM (the latter has more up-to-date support for language features, like annotations) and then load the class with a ClassLoader.

我想有其他选择。

这篇关于在运行时在Java中扩展或添加新类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆