byte-buddy

ByteBuddy fails when trying to redefine sun.reflect.GeneratedMethodAccessor1

家住魔仙堡 提交于 2019-12-23 01:46:51
问题 Driven by curiosity, I tried to export the bytecode of GeneratedMethodAccessor1 (generated by the JVM when using reflection). I try to get the bytecode of the class the following way: public class MethodExtractor { public static void main(String[] args) throws Exception { ExampleClass example = new ExampleClass(); Method exampleMethod = ExampleClass.class .getDeclaredMethod("exampleMethod"); exampleMethod.setAccessible(true); int rndSum = 0; for (int i = 0; i < 20; i++) { rndSum += (Integer)

ByteBuddy fails when trying to redefine sun.reflect.GeneratedMethodAccessor1

笑着哭i 提交于 2019-12-23 01:46:09
问题 Driven by curiosity, I tried to export the bytecode of GeneratedMethodAccessor1 (generated by the JVM when using reflection). I try to get the bytecode of the class the following way: public class MethodExtractor { public static void main(String[] args) throws Exception { ExampleClass example = new ExampleClass(); Method exampleMethod = ExampleClass.class .getDeclaredMethod("exampleMethod"); exampleMethod.setAccessible(true); int rndSum = 0; for (int i = 0; i < 20; i++) { rndSum += (Integer)

Java Bytecode: Customized setter/getter with byte buddy

孤人 提交于 2019-12-22 08:30:17
问题 I am trying to create a "custom" setter method for a field with byte buddy. Buddy's own mechanism allows for standard setter/getter methods to be implemented very easily, however, I am looking for an elegant way to extend the setter with some additional logic. To simplify the example, let's assume we have a class A, which has a method setChanged(String). Goal is to make a sub-class of A, add a field with corresponding access methods. The catch is, that I want to call setChanged("fieldName")

Change behaviour of static method in Java - byte code manipulation

岁酱吖の 提交于 2019-12-20 02:59:12
问题 I am trying to manipulate a static method. For this, Byte Buddy or any other framework can be used. There is one library that is called Pi4J that is used for controlling GPIO of Raspberry Pi. This library has a method called: GpioController gpio = GpioFactory.getInstance(); And this call is called in several places of a program that I might not have control such that I need to modify the invocation. What I would like to do is that when GpioFactory.getInstance is executed in some way detect

How to implement a wrapper decorator in Java?

陌路散爱 提交于 2019-12-18 21:19:00
问题 The problem is to create a dynamic enhanced version of existing objects. I cannot modify the object's Class . Instead I have to: subclass it wrap the existing object in the new Class delegate all the original method calls to the wrapped object implement all methods that are defined by another interface The interface to add to existing objects is: public interface EnhancedNode { Node getNode(); void setNode(Node node); Set getRules(); void setRules(Set rules); Map getGroups(); void setGroups

How to implement a wrapper decorator in Java?

∥☆過路亽.° 提交于 2019-12-18 21:18:32
问题 The problem is to create a dynamic enhanced version of existing objects. I cannot modify the object's Class . Instead I have to: subclass it wrap the existing object in the new Class delegate all the original method calls to the wrapped object implement all methods that are defined by another interface The interface to add to existing objects is: public interface EnhancedNode { Node getNode(); void setNode(Node node); Set getRules(); void setRules(Set rules); Map getGroups(); void setGroups

How do you change imports with Byte Buddy?

Deadly 提交于 2019-12-14 02:06:50
问题 I'd like to change the imports of a class so that they point to a different package. Byte Buddy docs don't give much info on how one can achieve this. This is what I have so far: public class ProxyPlugin implements net.bytebuddy.build.Plugin { public DynamicType.Builder apply(DynamicType.Builder builder, TypeDescription typeDescription) { return builder.name(typeDescription.getPackage().getName() + ".proxy." + typeDescription.getSimpleName()); } public boolean matches(TypeDescription

how to avoid recursive calls with byte buddy - java.lang.StackOverflowError

蓝咒 提交于 2019-12-13 18:26:34
问题 I have an advice which calls a similar method in the advice. How do we make sure the advice gets called once and only once. Right now as the method I am calling within advice is the same as the one being instrumented, it goes into recursive calling and results in java.lang.StackOverflowError. transform( new AgentBuilder.Transformer.ForAdvice() .include(JettyHandlerAdvice.class.getClassLoader()) .advice(named("addFilterWithMapping").and(ElementMatchers.takesArgument(0,named("org.eclipse.jetty

Java - Create anonymous Exception subclass with a certain name

爷,独闯天下 提交于 2019-12-13 07:32:41
问题 We're using Fabric / Crashlytics for sending non-crash issues to their servers and later checking out the logs and stacktraces. Unfortunately, the Crashlytics API requires to use a different Exception subclass instance for different issue types. (In the iOS SDK, you can simply use a string for different issue types, but in the Android version, you can't). public static void craslyticsRecordError(String issueType, String errorMsg) { Exception e = new Exception(issueType + "-" + errorMsg);

How can I get Annotation class from TypeDescription

守給你的承諾、 提交于 2019-12-13 03:57:59
问题 I'm trying to work with ByteBuddy. How, with given TypeDescription , can I get annotation class? So far I found getActualName . @Override public boolean matches(final TypeDescription target) { //System.out.printf("matches(%1$s)\n", target); //System.out.printf("\tcanonicalName: %1$s\n", target.getCanonicalName()); //System.out.printf("\tcomponentType: %1$s\n", target.getComponentType()); { final AnnotationList inheritedAnnotations = target.getInheritedAnnotations(); inheritedAnnotations