<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>PPPJ WiP Session - Implementing Dynamic Mixins for the Java Virtual Machine</article-title>
      </title-group>
      <contrib-group>
        <aff id="aff0">
          <label>0</label>
          <institution>Kerstin Breiteneder, Christoph Wimberger, Thomas Würthinger Institute for System Software Johannes Kepler University Linz</institution>
          ,
          <country country="AT">Austria</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>A mixin is a set of functionalities that can be added to many different classes. Typically, there are two common situations where mixins are used: (1) if a feature should be added to a large number of different classes; (2) if a class should have many optional features. In recent years mixins gained a lot of popularity, particularly in dynamically typed scripting languages such as Ruby or JavaScript. The topic of our research is the integration of dynamic mixins into Java using the Dynamic Code Evolution Virtual Machine (DCE VM) [1]. We aim for a stable, easy to use API and hope for a performance gain in comparison to other approaches.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>
        Historically, mixins were used to realize a sort of multiple inheritance [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]. They are
classes implementing certain aspects that can be added to other classes without
having to subclass them from the same super class. This is not possible in languages
with single inheritance. In such languages, the code of mixin aspects would have to
be pasted into all classes that should have these aspects, which would lead to
undesired code duplication. Smalltalk and CLOS were among the earliest languages that
supported mixins. Nowadays, mixins can also be found in languages such as Ruby,
JavaScript, and Perl.
      </p>
      <p>In Java, mixin behaviour can be partially achieved through interfaces. Extra
functionality can be specified as an interface that can be added to classes which should
have this functionality. However, the implementation of the interface methods still
has to be written manually for every class that implements the interface. In this paper
we propose a solution for Java where not only the interface but also the
implementation of extra functionality can be added to classes on demand. This can even be done
dynamically at run time.</p>
      <p>The basic idea of our approach is to add mixin classes to other classes by using
dynamic class redefinition. The method call Mixin.addMixin(A.class, B.class)
redefines class A in such a way that it now also contains all fields and methods of class
B. We first explain this with a motivating example and then sketch the
implementation.</p>
    </sec>
    <sec id="sec-2">
      <title>2 Motivating Example</title>
      <p>The following example shows how mixins can be used in our approach to extend
the functionality of an existing class. Assume, that we have a class Point2D, which
implements two-dimensional points.</p>
      <p>public class Point2D {
private int x;
private int y;
public int getX() { return x; }
public void setX(int x) { this.x = x; }
public int getY() { return y; }
public void setY(int y) { this.y = y; }
We would like to extend this class so that it can handle three-dimensional points. First
we specify the 3D behaviour by an interface Point3D.</p>
      <p>Then we implement the 3D behaviour by a mixin class Mixin3D which implements
the interface Point3D.</p>
      <p>interface Point3D {
public int getZ();
public void setZ(int z);
}
public class Mixin3D implements Point3D {
private int z;
public int getZ() { return z; }
public void setZ(int z) { this.z = z; }
In order to add the 3D behaviour to the class Point2D we call:</p>
      <p>Mixin.addMixin(Point2D.class, Mixin3D.class);
This will add all features of the class Mixin3D to the class Point2D. In other words,
the class Point2D is dynamically redefined so that it contains a field z as well as
the methods getZ() and setZ() with their bytecodes. Since Mixin3D implements
the interface Point3D, the class resulting from adding Mixin3D to Point2D will
implement Point3D as well. We can now create an instance of Point2D and use it
as a Point3D object, for example:</p>
      <p>Point2D point2d = new Point2D();
Point3D point3d = (Point3D) point2d;
point2d.setX(10);
point2d.setY(20);
point3d.setZ(40);
System.out.println("Point at " + point2d.getX() + "/" +</p>
      <p>point2d.getY() + "/" + point3d.getZ());</p>
      <p>Note, that our approach does not require new Java syntax. A mixin is an ordinary
Java class with fields and methods and possibly implementing some interfaces. Also
the target class does not need special syntax for allowing it to be extended by mixin
classes. Any number of mixin classes can be added to a target class using a sequence
of addMixin() calls. This happens at run time and allows the dynamic extension of
classes with extra functionality.</p>
      <p>On the downside, however, it might be difficult to see from the source code, which
functionality a class like Point2D really has. This depends on whether it has been
extended by mixins or not. As a solution to this problem one could add all mixins to
a class C in the static constructor of C so that they can easily be found.</p>
    </sec>
    <sec id="sec-3">
      <title>3 Implementation</title>
      <p>
        Since the introduction of the instrumentation API of J2SE 5.0 (1.5.0) in 2004,
the Java VM can handle class redefinitions during runtime. However, there are
some restrictions: It is not allowed to add, remove or rename fields or
methods, to change method signatures or to redefine the inheritance relationship [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ].
Therefore, our implementation uses the Dynamic Code Evolution Virtual
Machine (DCE VM), [
        <xref ref-type="bibr" rid="ref1 ref4">1, 4</xref>
        ] which removes these restrictions. An installer that adds
the DCE VM features to an installed Java SDK or JRE can be downloaded from
http://ssw.jku.at/dcevm/binaries/. Once installed, a mixin-enabled VM
could be started like this (where PATH_TO_JRE is typically the jre subdirectory of
the Java SDK installation directory or the Java JRE installation directory itself):
$ java -javaagent:&lt;PATH_TO_JRE&gt;/lib/ext/dcevm.jar &lt;MainClass&gt;
      </p>
      <p>
        Our implementation can be divided into two parts: The first part handles the
creation of the mixed class from the bytecodes of the mixin class and the target class.
We use the ASM framework [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] to load the target class and to translate it into a data
structure that can be easily modified. Then we add the fields and the methods of the
mixin class using the ASM API. Since mixin methods can override methods of the
target class we also have to fix certain bytecode instructions. Finally, the data structure
is translated back to the Java class file format and loaded by the DCE VM. The other
part uses this functionality to implement a Java agent that keeps a list of all mixins
for a class and transforms the loaded bytecodes of the target class by mixing to it the
bytecodes of all mixin classes in this list.
      </p>
    </sec>
    <sec id="sec-4">
      <title>4 Future Work</title>
      <p>This paper only described the very basic features of our mixin approach. Private fields
and methods of the mixin class can be handled in such a way that they do not
accidently override fields and methods of the target class. Public fields on the other hand
can act as a reference to the field with the same name and type in the target class, so
that field values of the target can also be modified. Another nice feature would be the
ability to invoke methods of the target class from within the mixin class. But since the
mixin class does not know the target class in advance, there is no guarantee that this
method even exists. So a way to provide a default implementation of such a method
would make sense. To increase the user friendliness it would help if the invocation of
the class redefinition in the VM would not need an agent and could be done through
a native call to the virtual machine.</p>
    </sec>
    <sec id="sec-5">
      <title>Acknowledgments</title>
      <p>The work presented was supported by Oracle Corporation and Guidewire Software,
Inc.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <surname>Würthinger</surname>
            <given-names>T.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Wimmer</surname>
            <given-names>C.</given-names>
          </string-name>
          , and Stadler L.
          <article-title>Dynamic code evolution for java</article-title>
          .
          <source>In 8th Intl. Conf. on Principles and Practice of Programming in Java (PPPJ'10)</source>
          , Vienna, Austria,
          <year>September 2010</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>Gilad</given-names>
            <surname>Bracha</surname>
          </string-name>
          and
          <string-name>
            <given-names>William</given-names>
            <surname>Cook</surname>
          </string-name>
          .
          <article-title>Mixin-based inheritance</article-title>
          .
          <source>In OOPSLA/ECOOP '90: Proceedings of the European conference on object-oriented programming on Objectoriented programming systems, languages, and applications</source>
          , pages
          <fpage>303</fpage>
          -
          <lpage>311</lpage>
          , New York, NY, USA,
          <year>1990</year>
          . ACM.
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>Oracle</given-names>
            <surname>Corporation</surname>
          </string-name>
          .
          <source>Interface Instrumentation</source>
          ,
          <year>2010</year>
          . http://java.sun.com/javase/6/docs/api/java/lang/instrument/ Instrumentation.html.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          <article-title>[4] Institute for System Software</article-title>
          , Johannes Kepler University Linz.
          <source>Homepage of the Dynamic Code Evolution VM</source>
          ,
          <year>2010</year>
          . http://ssw.jku.at/dcevm/.
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>OW2</given-names>
            <surname>Consortium. ASM</surname>
          </string-name>
          <article-title>Java bytecode framework</article-title>
          ,
          <year>2010</year>
          . http://asm.ow2.org/.
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>