Repository navigation
Open
Description
- Missing is a description of each component and its common methods (like
TypePool,DynamicType,ClassLoadingStrategyetc.) - Class calls may vary from version to version, such as the current version is:
package foo;
class Bar { }
class MyApplication {
public static void main(String[] args) {
TypePool typePool = TypePool.Default.ofClassPath();
new ByteBuddy()
.redefine(typePool.describe("foo.Bar").resolve(), // do not use 'Bar.class'
ClassFileLocator.ForClassLoader.ofClassPath())
.defineField("qux", String.class) // we learn more about defining fields later
.make()
.load(ClassLoader.getSystemClassLoader());
assertThat(Bar.class.getDeclaredField("qux"), notNullValue());
}
}It was not compiled in 1.9.13, but the 1.9.13 version of the document could not be found. Try to change it to:
package foo;
class Bar { }
class MyApplication {
public static void main(String[] args) {
TypePool typePool = TypePool.Default.of(Thread.currentThread().getContextClassLoader());
new ByteBuddy()
.redefine(typePool.describe("foo.Bar").resolve(), // do not use 'Bar.class'
ClassFileLocator.ForClassLoader.of(Thread.currentThread().getContextClassLoader()))
.defineField("qux", String.class) // we learn more about defining fields later
.make()
.load(ClassLoader.getSystemClassLoader());
assertThat(Bar.class.getDeclaredField("qux"), notNullValue());
}
}But in the end:
Exception in thread "main" java.lang.IllegalStateException: Class already loaded: foo.BarMuch more complex than using javassist
Reacted by james.xu and wyt
Activity
harrybin815 commentedon May 30, 2019
So it works. By passing ClassLoadingStrategy.Default.INJECTION.
the default
ClassLoadingStrategy.Default.WRAPPERuse another ByteArrayClassLoader and it's parent is SystemLoader.raphw commentedon May 30, 2019
A feature was added to Byte Buddy that throws an exception if a class that is injected is already loaded. In Byte Buddy 1.9.*, any use unsafe (unofficial) API is opt-in, therefore, INJECTION is no longer the default.
But you are right, there should be more documentation, I simply lack the time to write it.
harrybin815 commentedon May 31, 2019
Thanks for answering questions and considering Suggestions
harrybin815 commentedon Jun 3, 2019
The examples from Section "Accessing fields" of this tutorial (http://bytebuddy.net/#/tutorial) will not work in version 1.9.13.
When running to
An exception is thrown here, beause:
How to solve this problem?
harrybin815 commentedon Jun 3, 2019
in @Argumen$Binder#bind(...) :
source.getParameters().size() <= argument.value()make this exception.sourceisInstanceCreatorand it's argument length is empty,argument.value()also zeroIn such a case, the judgment must be illegal. The logic here is confusing to me.
harrybin815 commentedon Jun 3, 2019
@raphw