title: ByteBuddy Proxy Interface
body: <p>I am trying to convert Cglib proxy to ByteBuddy. Cglib has <a href="http://cglib.sourceforge.net/apidocs/net/sf/cglib/proxy/Proxy.html" rel="noreferrer">net.sf.cglib.proxy.Proxy</a> interface to intercept all method calls. I check the documentation of ByteBuddy but couldnt find such an example. Without such interface for every object that i instantiate with ByteBuddy i am repeating same thing again and agin. Is there a better way to do this with ByteBuddy?</p>
  
  <p>Here is my example code snippet:</p>
  
  <p>Service:</p>
  
  <pre><code>public class MyService {
  
      public void sayFoo() {
          System.out.println("foo");
      }
  
      public void sayBar() {
          System.out.println("bar");
      }
  }
  </code></pre>
  
  <p>Interceptor:</p>
  
  <pre><code>public class MyServiceInterceptor {
  
      public void sayFoo(@SuperCall Callable&lt;Void&gt; zuper) {
          try {
              zuper.call();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public void sayBar(@SuperCall Callable&lt;Void&gt; zuper) {
          try {
              zuper.call();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>import net.bytebuddy.ByteBuddy;
  import net.bytebuddy.ClassFileVersion;
  import net.bytebuddy.dynamic.ClassLoadingStrategy;
  import net.bytebuddy.instrumentation.MethodDelegation;
  import net.bytebuddy.instrumentation.method.matcher.MethodMatchers;
  
  public class Main {
  
      public static void main(String[] args) throws Exception {
          ByteBuddy buddy = new ByteBuddy(ClassFileVersion.forCurrentJavaVersion());
          Class&lt;? extends MyService&gt; serviceClass =
                  buddy.subclass(MyService.class)
                  .method(MethodMatchers.named("sayFoo").or(MethodMatchers.named("sayBar")))
                  .intercept(MethodDelegation.to(new MyServiceInterceptor()))
                  .make()
                  .load(Main.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
                  .getLoaded();
  
          MyService service = serviceClass.newInstance();
  
          service.sayFoo();
          service.sayBar();
      }
  }
  </code></pre>