Implementing Dynamic Language Features in Java using Dynamic Code Generation Thomas M. Breuel Xerox PARC, Palo Alto, CA tmb@parc.xerox.com Abstract Compared to dynamic object-oriented languages like CommonLisp or Smalltalk, Java has a fairly simple and restrictive object and type system. Some of the key differences between Java and these other languages is that they offer structural conformance, automatic delegation, and dynamic mixins. When such constructs are needed in a language like Java, they are usually expressed in terms of standard object-oriented design patterns, requiring the manual implementation of “glue” or “helper” classes. This paper describes ways in which such features can be provided efficiently and automatically in terms of Java’s platform-independent binary format and dynamic loading mechanisms. The implementation does not require any changes to the Java runtime, bytecodes, or class loader and yields performance comparable to manually implemented design patterns. The approach should prove useful both as a programming tool for Java and as an strategy for building efficient implementations of dynamic languages on top of the Java virtual machine. 1: Introduction Java is a programming language originally based on a simplification of the C++ program- ming language. The language has a fairly simple static type system, with some support for safe runtime type casting and reflection. Java is considerably less expressive than dynamic programming languages like Smalltalk or CommonLisp. Some of the constructs available in others languages are: • Objects can be used interchangeably as long as they implement compatible methods. This is referred to as “structural conformance” (e.g. Smalltalk-80, [5]). • Methods on instances can be “wrapped” by other methods; that is, the programmer can define methods that are executed before and/or after a method of the target instance. (e.g. CLOS, [1]). • Method calls on one object can be delegated to another object (e.g. Objective-C, [11]). • Instances can be created of classes that “mix” multiple parent classes dynamically (e.g., CLOS, [1]). These and similar constructs do not exist in standard Java. There have been suggestions for implementing some of these facilities by using preprocessing or compilation techniques