Multiple Code Inheritance in Java Maria Cutumisu Supervisors: Duane Szafron, Paul Lu.
-
Upload
richard-walsh -
Category
Documents
-
view
212 -
download
0
Transcript of Multiple Code Inheritance in Java Maria Cutumisu Supervisors: Duane Szafron, Paul Lu.
Multiple Code Inheritance in Java
Maria Cutumisu
Supervisors: Duane Szafron, Paul Lu
2
Outline
• Overview• Motivation• Implementation• Validation• Syntax Support for Compilation• Conclusions
3
MI (class) MI (class) MI (class)
MI (interface) SI (class) SI (class)
MI (interface) MI (code-type) SI* (class)
We want separation of types.
*We do not support multiple data inheritance.
C++
Java
Our Java
Interface-Type Code-Type Data-Type
Interface Class
Type
Type Separation Mechanisms in Java
4
Code Promotion Measurements - Methods
Class Identical AbstractAbstract
and Super
TotalMethod
Decrease
DataInputStream 4/19 8/19 0/19 12/19 63%
DataOutputStream 2/17 0/17 6*/17 2+6*/17 12%
RandomAccessFile 6/45 8/45 6/45 20/45 44%
RandomAccessFile
DataOutput
DataOutputStreamDataInputStream
DataInput
……readFloat() ……readFloat() ……writeFloat() ……writeFloat()
5
Basic Implementation - I
DataInput
RandomAccessFile
readFloat()readInt()
DataOutput
readIntreadInt
MT DataInput
MT RandomAccessFile
Interface Class
Code is nowin the interface!
Code wasin the class!
RandomAccessFile VMT
12 readFloat()
13 readInt()
…
Code Code
Class Loader Changed.
6
Dispatch Scenarios - Ambiguities
InterfaceB
ClassB
alpha() InterfaceC alpha()
Scenario 3
InterfaceB
ClassB
alpha()
InterfaceA alpha()
ClassA
Scenario 4
InterfaceA alpha()
InterfaceA x;x = new ClassB();x.alpha();
We detect ambiguities at load-time.
7
Basic Implementation - II
DataInput
RandomAccessFile
readFloat()readInt()
DataOutputwriteFloat()
DataOutput output;DataInput input;output = new RAF();input = new RAF();output.writeFloat();this.readFloat();input.readFloat();
hypothetical () in DataInput
invokeinterfaceinvokevirtual invokeinterfaceinvokevirtual invokeinterface
8
Super Call Implementation
invokespecial #superclass/alpha()
super(InterfaceA).alpha();
invokemulti-super #InterfaceA/alpha()
invokeinterface #InterfaceA/alpha()
compiler script
Bytecode previouslygenerated for super calls
Bytecodeproposed
Bytecodeused
New syntax proposed for super calls to interfaces
Execution-time change.
9
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.
10
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.
11
Validation - MI Programs
java.io library Sun JVM
Multiple InheritanceTest Program
output
12
Validation - MI Programs
java.io library Our JVM
Multiple InheritanceTest Program
output
Tests the supercall mechanism
Uses the re-factored java.io library
13
Adding Code in Interfaces
Script
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());};}
javac
DataInput.class
jasper
javac
jasper
DataInput.class
DataInput.j DataInput_MI.j
DataInput.j
DataInput.class
Script
jasmin
14
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.
15
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.
16
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
17
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]; //REMOVEDargs_size = imb.args_size; //ADDEDoptop -= args_size;…if (pc[3] == 255) //ADDED
mb = interface.MT[offset]; //ADDEDgoto callmethod; //ADDED
end if //ADDED…
end case