byte-buddy

Define field with generic type using ByteBuddy

久未见 提交于 2020-08-04 20:07:32
问题 I just started playing with ByteBuddy and I am working on a couple examples in order to get the hang of it. What I am trying to accomplish with this exercise is to replace some code that uses ASM, with ByteBuddy. So far I have been successful when it comes to non-generic types. For example I can easily define a field for example like this builder.defineField("names", List.class, Visibility.PRIVATE) if all I want to do is create a field of raw List type. When it comes to introducing generics

Define field with generic type using ByteBuddy

社会主义新天地 提交于 2020-08-04 20:06:44
问题 I just started playing with ByteBuddy and I am working on a couple examples in order to get the hang of it. What I am trying to accomplish with this exercise is to replace some code that uses ASM, with ByteBuddy. So far I have been successful when it comes to non-generic types. For example I can easily define a field for example like this builder.defineField("names", List.class, Visibility.PRIVATE) if all I want to do is create a field of raw List type. When it comes to introducing generics

How do I define a private static final field using ByteBuddy with an initial complex value?

此生再无相见时 提交于 2020-07-22 21:32:08
问题 After reading the documentation and a few Github issues, which proved to be particularly useful, I came to understand that in ByteBuddy terminology a static field must be set to its initial value by a LoadedTypeInitializer of some kind. I didn't know what to do with a LoadedTypeInitializer.ForStaticField once I had created it. Then, thankfully, I just happened to stumble across the initializer() method of DynamicType.Builder . Here is my recipe (which does not work): builder = builder

Load a package and inside classes

有些话、适合烂在心里 提交于 2020-05-26 09:32:10
问题 I'm creating a package with some classes that I generated with wsimport on the fly and now I'm trying to load it to use, how can I do this? natively? or with some lib like byte-buddy, I tried the bellow code to load each class in a package: File [] files = new File("<Path to package in filesystem with classes (*.class)>").listFiles(); List<URL> classUrls = new ArrayList<>(); for(File file : files) { classUrls.add(new URL("file://" + file.getAbsolutePath())); } URL[] classz = new URL[classUrls

Create a java class dynamically and compile and instantiate at run time

风流意气都作罢 提交于 2020-04-10 05:59:24
问题 I have a String that I need to convert to java class, compile and create an instance at run time: Suppose my String is: String s = " public class Test { public Double add(Double x, Double y){ return (x+y); } }" How can I convert it to a class Test .class, instantiate it and call the method add(Double x, Double y) at run time? I read about Byte Buddy , but the examples I see has a class already defined. In a situation like the above, could anyone give an example how can I use ByteBuddy or any

Byte Buddy Member Substitution throwing IllegalStateException error

社会主义新天地 提交于 2020-03-23 11:59:08
问题 I'm trying to write a java instrumentation agent using byte buddy. My goal is to replace a java standard library method call with a proxy call of my own. I was suggested to use Byte Buddy's MemberSubstitution to achieve this. I used this and this questions from SO for my reference. I'm using Intellij IDEA for coding. My Agent code is split into multiple files as follows: MyFirstAgent.java public class MyFirstAgent { public static void premain(String agentArgs, Instrumentation inst) { new