bytebuddy.net
Open in
urlscan Pro
2606:50c0:8002::153
Public Scan
URL:
http://bytebuddy.net/
Submission: On February 14 via manual from CA — Scanned from CA
Submission: On February 14 via manual from CA — Scanned from CA
Form analysis
0 forms found in the DOMText Content
Byte Buddy Byte Buddy is a code generation library for creating Java classes during the runtime of a Java application and without the help of a compiler. Other than the code generation utilities that ship with the Java Class Library, Byte Buddy allows the creation of arbitrary classes and is not limited to implementing interfaces for the creation of runtime proxies. Toggle navigation * Welcome * Learn * Develop * API * Welcome * Hello world * Getting Byte Buddy * Dependency management * Support WELCOME, I'M YOUR BYTE BUDDY! Byte Buddy is a code generation and manipulation library for creating and modifying Java classes during the runtime of a Java application and without the help of a compiler. Other than the code generation utilities that ship with the Java Class Library, Byte Buddy allows the creation of arbitrary classes and is not limited to implementing interfaces for the creation of runtime proxies. Furthermore, Byte Buddy offers a convenient API for changing classes either manually, using a Java agent or during a build. In order to use Byte Buddy, one does not require an understanding of Java byte code or the class file format. In contrast, Byte Buddy's API aims for code that is concise and easy to understand for everybody. Nevertheless, Byte Buddy remains fully customizable down to the possibility of defining custom byte code. Furthermore, the API was designed to be as non-intrusive as possible and as a result, Byte Buddy does not leave any trace in the classes that were created by it. For this reason, the generated classes can exist without requiring Byte Buddy on the class path. Because of this feature, Byte Buddy's mascot was chosen to be a ghost. Byte Buddy is written in Java 5 but supports the generation of classes for any Java version. Byte Buddy is a light-weight library and only depends on the visitor API of the Java byte code parser library ASM which does itself not require any further dependencies. At first sight, runtime code generation can appear to be some sort of black magic that should be avoided and only few developers write applications that explicitly generate code during their runtime. However, this picture changes when creating libraries that need to interact with arbitrary code and unknown type hierarchies. In this context, a library implementer must often choose between either requiring a user to implement library-proprietary interfaces or to generate code at runtime when the user's type hierarchy becomes first known to the library. Many known libraries such as for example Spring or Hibernate choose the latter approach which is popular among their users under the term of using Plain Old Java Objects. As a result, code generation has become an ubiquitous concept in the Java space. Byte Buddy is an attempt to innovate the runtime creation of Java types in order to provide a better tool set to those relying on such functionality. -------------------------------------------------------------------------------- In October 2015, Byte Buddy was distinguished with a Duke's Choice award by Oracle. The award appreciates Byte Buddy for its "tremendous amount of innovation in Java Technology". We feel very honored for having received this award and want to thank all users and everybody else who helped making Byte Buddy the success it has become. We really appreciate it! -------------------------------------------------------------------------------- Byte Buddy offers excellent performance at production quality. It is stable and in use by distinguished frameworks and tools such as Mockito, Hibernate, Google's Bazel build system and many others. Byte Buddy is also used by a large number of commercial products to great result. It is currently downloaded over 75 million times a year. HELLO WORLD! Saying Hello World with Byte Buddy is as easy as it can get. Any creation of a Java class starts with an instance of the ByteBuddy class which represents a configuration for creating new types: 1. Class<?> dynamicType = new ByteBuddy() 2. .subclass(Object.class) 3. .method(ElementMatchers.named("toString")) 4. .intercept(FixedValue.value("Hello World!")) 5. .make() 6. .load(getClass().getClassLoader()) 7. .getLoaded(); 8. 9. assertThat(dynamicType.newInstance().toString(), is("Hello World!")); The default ByteBuddy configuration which is used in the above example creates a Java class in the newest version of the class file format that is understood by the processing Java virtual machine. As hopefully obvious from the example code, the created type will extend the Object class and overrides its toString method which should return a fixed value of Hello World!. The method to be overridden is identified by a so-called ElementMatcher. In the above example, a predefined element matcher named(String) is used which identifies methods by their exact names. Byte Buddy comes with numerous predefined and well-tested matchers which are collected in the ElementMatchers class and which can be easily composed. The creation of custom matchers is however as simple as implementing the (functional) ElementMatcher interface. For implementing the toString method, the FixedValue class defines a constant return value for the overridden method. Defining a constant value is only one example of many method interceptors that ship with Byte Buddy. By implementing the Implementation interface, a method could however even be defined by custom byte code. Finally, the described Java class is created and then loaded into the Java virtual machine. For this purpose, a target class loader is required which is read from the surrounding class. Eventually, we can convince ourselves of the result by calling the toString method on an instance of the created class and finding the return value to represent the constant value we expected. Of course, Byte Buddy is capable of much more sophisticated class generation. Furthermore, Byte Buddy is not limited to creating subclasses but can transform existing code. Byte Buddy also offers a convenient API for defining so-called Java agents which allow code transformations during the runtime of any Java application. For a taste of such features, have a look at the readme on Byte Buddy's GitHub page or get started with reading the full tutorial on this page! GETTING BYTE BUDDY Download You can download Byte Buddy directly from Maven Central. Alternatively, you can also download the source code from GitHub. Repositories Byte Buddy binaries are available from the Maven Central. Using a build manager, it is therefore easy to define a project dependency on Byte Buddy. Before releasing your project, please take the time to read through the information on maintaining a project dependency on Byte Buddy. * Maven * Gradle * SBT * Ivy * Buildr * Grape * Leiningen <dependency> <groupId>net.bytebuddy</groupId> <artifactId>byte-buddy</artifactId> <version>LATEST</version> </dependency> net.bytebuddy:byte-buddy:LATEST libraryDependencies += "net.bytebuddy" % "byte-buddy" % "LATEST" <dependency org="net.bytebuddy" name="byte-buddy" rev="LATEST" /> 'net.bytebuddy:byte-buddy:jar:LATEST' @Grapes( @Grab(group='net.bytebuddy', module='byte-buddy', version='LATEST') ) [net.bytebuddy/byte-buddy "LATEST"] The artifacts signatures can be validated against this PGP public key beginning with Byte Buddy 1.10.3. Older versions can be validated against this older and weaker certificate. Licence Byte Buddy is an open source project distributed under the liberal and business-friendly Apache 2.0 licence. Its source code is freely available on GitHub. Please note that Byte Buddy depends on the ASM library which is distributed under a 3-clause BSD license. Documentation There exists an extensive documentation of all of Byte Buddy's features on the tutorial page. Furthermore, Byte Buddy's in-code documentation is available from its source code repositories and on javadoc.io. A list of blog articles and conference presentations on Byte Buddy is available on the project's wiki. GETTING SUPPORT Commercial The use of Byte Buddy is free and does not require the purchase of a license. To get the most out of the library or to secure an easy start, it is however possible to purchase training, development hours or support plans. Rates are dependent on the scope and duration of an engagement. Please get in touch with rafael.wth@gmail.com for further information. Tidelift Byte Buddy is listed on Tidelift. If you are not using Byte Buddy to an extend where you want to purchase explicit support and want to support the open source community in general, please consider a subscription. GitHub Sponsors You can support my work via GitHub sponsors. Note that this option is only meant for commercial actors who are looking for a simple payment channel and that do not expect support in return. Support via GitHub Sponsors is not possible to maintain VAT compliance. Please reach out for a direct support agreement instead. Free General questions can be asked on Stack Overflow or on the Byte Buddy mailing list which also serve as an archive for questions. Of course, bug reports will be considered also outside of a commercial plan. For open source projects, it is sometimes possible to receive extended help for taking Byte Buddy into use. DEPENDENCY MAINTENANCE Byte Buddy is written on top of ASM, a mature and well-tested library for reading and writing compiled Java classes. In order to allow for advanced type manipulations, Byte Buddy is intentionally exposing the ASM API to its users. Of course, the direct use of ASM remains fully optional and most users will most likely never require it. This choice was made such that a user of Byte Buddy is not restrained to its higher-level functionality but can implement custom implementations without a fuss when it is necessary. ASM has previously changed its public API but added a mechanism for API compatibility starting with version 4 of the library. In order to avoid version conflicts with such older versions, Byte Buddy repackages the ASM dependency into its own namespace. If you want to use ASM directly, use the byte-buddy-dep artifact offers a version of Byte Buddy with an explicit dependency to ASM. When doing so, you must repackage both Byte Buddy and ASM into your namespace to avoid version conflicts. NEED HELP? Commercial plans Commercial support is available in form of training, consulting and development hours. Please contact rafael.wth@gmail.com for further information. General questions Feel free to ask questions about Byte Buddy on Stack Overflow and tag them with byte-buddy. We are monitoring questions with this tag and will normally answer as quickly as possible. We think that Stack Overflow is a great place to share knowledge which is why we decided to not maintain our own support platform. If you have a question that does not fit into Stack Overflow's Q&A format, please ask your question on our mailing list instead. Bugs and feature requests You can register any bug or misbehavior in Byte Buddy's issue tracker. If possible, please provide a code sample that reproduces the bug. Furthermore, you should include information on the Java version that you were using. If you have a suggestion for a new feature, you can also use the issue tracker. If you want to discuss this feature first, start such a discussion on out mailing list. Development If you have fixed a bug or developed a feature for Byte Buddy that you want to share, simply create a pull request for it. If you are planning to implement an extensive feature for Byte Buddy, please get in touch before starting its development. This way, we can coordinate the development process. You can find more information on our development page. Thank you for improving Byte Buddy! Supporters The work on Byte Buddy is also possible thanks to a row of supporters that have dedicated regular resources and attention to the project. Please take your time to have a look at those supporters and their offerings. Discover Byte Buddy is maintained by Rafael Winterhalter and was first published in 2014. Thank you for spreading the word! Legal 2024 © Rafael Winterhalter, Oslo, Norway