bytecode - How do I build an concrete implementation of a Java Class from an interface using Byte-Buddy? -


i have interface, lets looks this.

public interface testobject {     string getstring();      long getlong(); } 

i want build concrete implementation of object using bytebuddy.

here's tried.

public class runme {      public static void main(string[] args) {         bytebuddy bb = new bytebuddy();          class<?> clz = bb                 .subclass(testobject.class)       .method(any()).intercept(methoddelegation.to(interceptor.class))             .make()             .load(object.class.getclassloader(), classloadingstrategy.default.wrapper)             .getloaded();      try {         object test = clz.newinstance();      } catch (instantiationexception e) {         e.printstacktrace();     } catch (illegalaccessexception e) {         e.printstacktrace();     }  }  public class interceptor {      public object intercept(@origin string method, @allarguments object[] args) throws throwable {         system.out.println("i have intercepted call");          return "hello";      }  } 

}

i error

exception in thread "main" java.lang.illegalargumentexception: none of [] allows delegation public boolean java.lang.object.equals(java.lang.object) @ net.bytebuddy.implementation.bind.methoddelegationbinder$processor.process(methoddelegationbinder.java:881) @ net.bytebuddy.implementation.methoddelegation$appender.apply(methoddelegation.java:1218) @ net.bytebuddy.dynamic.scaffold.typewriter$methodpool$record$fordefinedmethod$withbody.applybody(typewriter.java:510) @ net.bytebuddy.dynamic.scaffold.typewriter$methodpool$record$fordefinedmethod.apply(typewriter.java:444) @ net.bytebuddy.dynamic.scaffold.typewriter$default$forcreation.create(typewriter.java:3193) @ net.bytebuddy.dynamic.scaffold.typewriter$default.make(typewriter.java:1481) @ net.bytebuddy.dynamic.scaffold.subclass.subclassdynamictypebuilder.make(subclassdynamictypebuilder.java:234) @ net.bytebuddy.dynamic.dynamictype$builder$abstractbase$abstractdelegatingbuilder.make(dynamictype.java:2177) @ com.meta.testbytebuddy.runme.main(runme.java:22) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ com.intellij.rt.execution.application.appmain.main(appmain.java:144) 

what doing wrong ++.. if want implement multiple interfaces?

i using bytebuddy code generation purposes.

the problem delegating interceptor's static methods methoddelegation.to(interceptor.class) while interceptor class declared non-static members. can either declare interceptor method static or delegate instance instead of class. think former approach more suitable.

this alone still not work. returning object type interceptor while, @ same time, intercepting any() method. includes methods of testobject interface methods declared object, implicit super class. define interceptor follows make class compile byte buddy cast return type each method's return type result in classcastexception:

public class interceptor {   @runtimetype   public static object intercept(@origin string method, @allarguments object[] args) {     system.out.println("i have intercepted call");     return "hello";   } } 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -