Javassist工厂 – 设置自定义类加载器

huangapple go评论58阅读模式
英文:

Javassist factory - setting a custom classloader

问题

使用 CGLIB,我可以这样做:

final var enhancer = new Enhancer();
enhancer.setUseCache(false);
enhancer.setSuperclass(superClazz);
enhancer.setCallback(...);
enhancer.setClassLoader(getClass().getClassLoader());   // 这里

然而,使用 Javassist,我找不到设置 ClassLoader 的方法。有什么想法吗?

编辑:天哪,我应该只是继承 ProxyFactory 吗?那感觉很奇怪。

英文:

With CGLIB I can do

final var enhancer = new Enhancer();
enhancer.setUseCache(false);
enhancer.setSuperclass(superClazz);
enhancer.setCallback(...);
enhancer.setClassLoader(getClass().getClassLoader());   // This

However with Javassist I can't find a way to set the ClassLoader. Any idea?

EDIT: omg, should I just subclass ProxyFactory? That's weird.

答案1

得分: 1

你可以通过向CtClass添加ClassPaths来设置类加载器。
参见问题的回答:https://stackoverflow.com/questions/60789950/get-ctclass-using-specific-classloader/60792151#60792151

英文:

You can set the classloader by adding ClassPaths to the CtClass.
See answer to question: https://stackoverflow.com/questions/60789950/get-ctclass-using-specific-classloader/60792151#60792151

答案2

得分: 0

// Kotlin
ProxyFactory.classLoaderProvider = ProxyFactory.ClassLoaderProvider { javaClass.classLoader }

这个在我看来似乎不安全。但据文档所述,这就是实际操作方法。
或者,可以选择继承 `ProxyFactory`。
英文:
// Kotlin
ProxyFactory.classLoaderProvider = ProxyFactory.ClassLoaderProvider { javaClass.classLoader }

This doesn't seem safe imho. But that's how it's done apparently, per documentation.
Alternatively, just subclass ProxyFactory.

huangapple
  • 本文由 发表于 2020年4月5日 22:05:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61043840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定