title: ByteBuddy: How to implement field access interceptor?
body: <p>I'am trying to make a OGM to translate object to Vertex for the OrientDB. Currently i'am using GCLib but i read that ByteBuddy could implements two critical things that if work, it will improve the OGM speed.</p>
  
  <ol>
  <li><p>Could ByteBuddy implement field access control? I read the doc but it's not clear or I do not understand it.</p></li>
  <li><p>Dinamically add default empty constructor.</p></li>
  </ol>
  
  <p>The current problem is this: We do not know the class definition that will be passed as a parameter. The idea is to redefine the class and implement the empty constructor if it not have one, add a field named __BB__Dirty to set the object as dirty if an assign operation was detected and force the implementation of an interface to talk with the object.</p>
  
  <p>Example:
  A generic class:</p>
  
  <pre><code>public class Example {
     int i = 0;
     String stringField;
  
     public Example(Strinf s) {
         stringField = s;
     }
  
     public void addToI(){
         i++;
     }
  }
  </code></pre>
  
  <p>Now we have an interface like this:</p>
  
  <pre><code>public interface DirtyCheck {
      public boolean isDirty();
  }
  </code></pre>
  
  <p>So, I want to force the Example class to implement the interface, the method isDirty(), a field to work on and a default contructor so the class should be translated to:</p>
  
  <pre><code>public class Example implements DirtyCheck {
     int i = 0;
     String stringField;
  
     boolean __BB__dirty = false;
  
     public Example() {
  
     }
  
     public Example(Strinf s) {
         stringField = s;
     }
  
     public void addToI(){
         i++;
     }
  
     public boolean isDirty() {
         return this.__BB__dirty;
     }
  }
  </code></pre>
  
  <p>and the some magically assigner so if any field (except __BB__dirty) is modified, the __BB__dirty field is set to True;  </p>
  
  <p>I have tried the first part of this but I fail :(</p>
  
  <pre><code>...
  ByteBuddyAgent.install();
  
  Example ex = new ByteBuddy()
                  .redefine(Example.class)
                  .defineField("__BB__Dirty", boolean.class, Visibility.PUBLIC)
                  .make()
                  .load(Example.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent())
                  .getLoaded().newInstance();
   ....
  
   ex.addToI();    // &lt;--- this should set __BB__dirty to true since it
                   //      assign a value to i.
  </code></pre>
  
  <p>But i get this error:</p>
  
  <pre><code>Exception in thread "main" java.lang.UnsupportedOperationException: class redefinition failed: attempted to change the schema (add/remove fields)
  at sun.instrument.InstrumentationImpl.redefineClasses0(Native Method)
  at sun.instrument.InstrumentationImpl.redefineClasses(InstrumentationImpl.java:170)
  at net.bytebuddy.dynamic.loading.ClassReloadingStrategy$Strategy$1.apply(ClassReloadingStrategy.java:297)
  at net.bytebuddy.dynamic.loading.ClassReloadingStrategy.load(ClassReloadingStrategy.java:173)
  at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4350)
  at Test.TestBB.&lt;init&gt;(TestBB.java:33)
  at Test.TestBB.main(TestBB.java:23)
  </code></pre>
  
  <p>I'am stuck in the very first stage to solve the problem with BB.
  Thanks</p>