170 likes | 360 Vues
This paper presents a groundbreaking implementation of multiple code inheritance in Java by modifying the Java Virtual Machine (JVM). By introducing mechanisms for adding code to interfaces, this approach enhances code reusability while maintaining a clear separation of inheritance concepts. The proposed methodology leverages minimal changes in the JVM, ensuring performance consistency with single inheritance programs. Validation shows that both single and multiple inheritance programs operate correctly, demonstrating significant reduction in code complexity and improved clarity in implementation.
E N D
Multiple Code Inheritance in Java Maria Cutumisu Supervisors: Duane Szafron, Paul Lu
Outline • Overview • Motivation • Implementation • Validation • Syntax Support for Compilation • Conclusions
Type Separation Mechanisms in Java Type Interface Class We want separation of types. Interface-Type Code-Type Data-Type C++ Java Our Java MI (class) MI (class) MI (class) MI (interface) SI (class) SI (class) MI (interface) MI (code-type) SI* (class) *We do not support multiple data inheritance.
… … … … … … … … writeFloat() readFloat() readFloat() writeFloat() Code Promotion Measurements - Methods DataInput DataOutput DataInputStream RandomAccessFile DataOutputStream
Basic Implementation - I RandomAccessFile VMT DataOutput DataInput readFloat() readInt() … MT DataInput MT RandomAccessFile 12 readFloat() readInt readInt 13 readInt() Interface Class RandomAccessFile Code Code Class Loader Changed. Code was in the class! Code is now in the interface!
Dispatch Scenarios - Ambiguities We detect ambiguities at load-time. InterfaceA x; x = new ClassB(); x.alpha(); Scenario 3 Scenario 4 InterfaceA InterfaceA alpha() alpha() InterfaceB InterfaceC InterfaceB ClassA alpha() alpha() alpha() ClassB ClassB
Basic Implementation - II writeFloat() DataOutput DataInput readFloat() readInt() RandomAccessFile hypothetical () in DataInput DataOutput output; DataInput input; output = new RAF(); input = new RAF(); output.writeFloat(); this.readFloat(); input.readFloat(); invokeinterface invokeinterface invokevirtual invokevirtual invokeinterface
Super Call Implementation New syntax proposed for super calls to interfaces Bytecode proposed invokemulti-super #InterfaceA/alpha() Execution-time change. super(InterfaceA).alpha(); Bytecode previously generated for super calls Bytecode used invokespecial #superclass/alpha() invokeinterface #InterfaceA/alpha() compiler script
Summary of Changes to the JVM • Small and localized changes: • class loader algorithm (load-time) - 11 lines of pseudo-code. • execution of invokeinterface_quick (execution-time) - 5 lines of code.
Validation • Semantics and performance are preserved for single inheritance programs. The following single inheritance Java programs ran correctly with our JVM: javac, jasper, and javap. • Multiple inheritance programs show correct answers with the re-factored java.io library. • Traditional super calls generate the same results under both JVMs.
Validation - MI Programs Multiple Inheritance Test Program Sun JVM java.io library output
Multiple Inheritance Test Program Tests the super call mechanism Our JVM java.io library Uses the re-factored java.io library output Validation - MI Programs
Adding Code in Interfaces interface DataInput{ public float readFloat() throws IOException; /*MI_CODE { return Float.intBitsToFloat(readInt()); } MI_CODE*/ } abstract class DataInput{ public float readFloat() throws IOException { return Float.intBitsToFloat(readInt()); } ; } Script javac javac DataInput.class DataInput.class Script jasper jasper DataInput_MI.j DataInput.j DataInput.j jasmin DataInput.class
Conclusions - I • The first implementation of multiple code inheritance in Java is provided by modifying Sun’s JVM. It is based on the novel concept of adding code to interfaces. • Facilitates code re-use, supports separation of inheritance concepts, and improves clarity and expressiveness of implementation. • Only small and localized changes are made to the JVM. • Java syntax and compiler are not changed. A set of scripts allows a programmer to add code in interfaces.
Conclusions - II • We defined a super call mechanism for super calls to interfaces, resembling the one in C++. • Single inheritance programs achieve the same performance as with the original JVM. • Single and multiple inheritance programs run correctly with our JVM, for both our basic and super call mechanism implementations. • Multiple code inheritance measurements show significant code decrease.
Load-time Change if (imb.code <> null) //code in interface currentmb = class.vmt[vmtIndex]; if (currentmb.code == null) //no code in MT class.vmt[vmtIndex] = imb; //point VMT to imb else //potential code ambiguity if (!currentmb.class.imt.contains(imb) && !imb.class.imt.contains(currentmb)) throw ambiguous method exception end if end if end if
Execution-time Change case opc_invokeinterface_quick: imb = constant_pool[GET_INDEX(pc+1)].mb; interface = imb.class; offset = imb.offset; … //args_size = pc[3]; //REMOVED args_size = imb.args_size; //ADDED optop -= args_size; … if (pc[3] == 255) //ADDED mb = interface.MT[offset]; //ADDED goto callmethod; //ADDED end if //ADDED … end case