Skip to content

Document is hard to read #661

Open
Open

Description

@harrybin815
  1. Missing is a description of each component and its common methods (like TypePool, DynamicType, ClassLoadingStrategy etc.)
  2. 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.Bar

Much more complex than using javassist

Activity

  1. harrybin815 commented on May 30, 2019

    @harrybin815
    Author
    .load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION); 

    So it works. By passing ClassLoadingStrategy.Default.INJECTION.

    the default ClassLoadingStrategy.Default.WRAPPER use another ByteArrayClassLoader and it's parent is SystemLoader.

  2. raphw commented on May 30, 2019

    @raphw
    Owner

    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.

  3. self-assigned this
    on May 30, 2019
  4. added this to the 1.9.13 milestone on May 30, 2019
  5. harrybin815 commented on May 31, 2019

    @harrybin815
    Author

    Thanks for answering questions and considering Suggestions

  6. harrybin815 commented on Jun 3, 2019

    @harrybin815
    Author

    The examples from Section "Accessing fields" of this tutorial (http://bytebuddy.net/#/tutorial) will not work in version 1.9.13.

    When running to

            InstanceCreator factory = new ByteBuddy()
                    .subclass(InstanceCreator.class)
                    .method(not(isDeclaredBy(Object.class)))
                    .intercept(MethodDelegation.toConstructor(dynamicUserType))
                    .make()
                    .load(I.class.getClassLoader())
                    .getLoaded()
                    .newInstance();

    An exception is thrown here, beause:

    MethodDelegationBinder$Processor#bind(Implementation.Target implementationTarget,
                                      MethodDescription source,
                                      TerminationHandler terminationHandler,
                                      MethodInvoker methodInvoker,
                                      Assigner assigne)

    How to solve this problem?

  7. harrybin815 commented on Jun 3, 2019

    @harrybin815
    Author

    in @Argumen$Binder#bind(...) :

    Argument argument = annotation.loadSilent();
                if (argument.value() < 0) {
                    throw new IllegalArgumentException("@Argument annotation on " + target + " specifies negative index");
                } else if (source.getParameters().size() <= argument.value()) {
                    return MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
                }

    source.getParameters().size() <= argument.value() make this exception.

    1. source is InstanceCreator and it's argument length is empty,
    2. argument.value() also zero
      In such a case, the judgment must be illegal. The logic here is confusing to me.
  8. harrybin815 commented on Jun 3, 2019

    @harrybin815
    Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions