How to make a copy of a Java class in runtime?
问题 I have a class class Foo { int increment(int x) { return x + 1; } } I want to obtain a copy of this class in runtime, e. g. a class like class Foo$Copy1 { int increment(int x) { return x + 1; } } Which has all the same methods, but a different name. Proxy seem to help with delegating , but not copying the methods with all their bodies. 回答1: You can use Byte Buddy for this: Class<?> type = new ByteBuddy() .redefine(Foo.class) .name("Foo$Copy1") .make() .load(Foo.class.getClassLoader())