int numstaticfields = 0;
// for interfaces
- boolean isInterface=false;
Vector<String> superinterfaces;
SymbolTable superIFdesc;
- public ClassDescriptor(String classname) {
- this("", classname);
+ public ClassDescriptor(String classname, boolean isInterface) {
+ this("", classname, isInterface);
}
- public ClassDescriptor(String packagename, String classname) {
+ public ClassDescriptor(String packagename, String classname, boolean isInterface) {
super(classname);
superclass=null;
flags=new SymbolTable();
fields=new SymbolTable();
fieldvec=new Vector();
methods=new SymbolTable();
- classid=UIDCount++;
+ if(isInterface) {
+ classid = -2;
+ } else {
+ classid=UIDCount++;
+ }
this.packagename=packagename;
superinterfaces = new Vector<String>();
superIFdesc = new SymbolTable();
}
public boolean isInterface() {
- return this.isInterface;
- }
-
- public void setAsInterface() {
- this.isInterface = true;
+ return this.classid == -2;
}
}
classit=state.getClassSymbolTable().getDescriptorsIterator();
while(classit.hasNext()) {
ClassDescriptor cd=(ClassDescriptor)classit.next();
+ if(cd.isInterface()) {
+ continue;
+ }
if (state.DSM||state.SINGLETM)
fillinRow(cd, lbvirtualtable, cd.getId());
else
cdarray[0] = null;
while(it.hasNext()) {
ClassDescriptor cd=(ClassDescriptor)it.next();
- cdarray[cd.getId()]=cd;
+ if(!cd.isInterface()) {
+ cdarray[cd.getId()]=cd;
+ }
}
arraytable=new TypeDescriptor[state.numArrays()];
if (classes.contains(tdn.getSymbol()))
throw new Error("Class "+tdn.getSymbol()+" defined twice");
classes.add(tdn);
- numclasses++;
+ if(!tdn.isInterface()) {
+ numclasses++;
+ }
if((tdn.numstaticfields != 0) || (tdn.numstaticblocks != 0)) {
sclasses.add(tdn);
}
}
public ClassDescriptor parseInterfaceDecl(ParseNode pn) {
- ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
- cn.setAsInterface();
+ ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), true);
+ //cn.setAsInterface();
if (!isEmpty(pn.getChild("superIF").getTerminal())) {
/* parse inherited interface name */
ParseNode snlist=pn.getChild("superIF").getChild("extend_interface_list");
}
public ClassDescriptor parseTypeDecl(ParseNode pn) {
- ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
+ ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
if (!isEmpty(pn.getChild("super").getTerminal())) {
/* parse superclass name */
ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
for(Iterator it=cd.getMethods(); it.hasNext();) {
MethodDescriptor md=(MethodDescriptor)it.next();
if (md.isStatic()||md.getReturnType()==null)
- continue;
+ continue;
if (superdesc!=null) {
- Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
- boolean foundmatch=false;
- for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
- MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
- if (md.matches(matchmd)) {
- int num=((Integer)methodnumber.get(matchmd)).intValue();
- methodnumber.put(md, new Integer(num));
- foundmatch=true;
- break;
- }
- }
- if(state.MGC) {
- // TODO add version for normal Java later
- if(!foundmatch) {
- // check if there is a matched method in inherited interfaces
- Iterator it_sifs = cd.getSuperInterfaces();
- while(it_sifs.hasNext() && !foundmatch) {
- ClassDescriptor superif = (ClassDescriptor)it_sifs.next();
- Set possiblematches_if=superif.getMethodTable().getSet(md.getSymbol());
- for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) {
- MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
- if (md.matches(matchmd)) {
- int num=((Integer)methodnumber.get(matchmd)).intValue();
- methodnumber.put(md, new Integer(num));
- foundmatch=true;
- break;
+ Set possiblematches=superdesc.getMethodTable().getSet(md.getSymbol());
+ boolean foundmatch=false;
+ for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
+ MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
+ if (md.matches(matchmd)) {
+ int num=((Integer)methodnumber.get(matchmd)).intValue();
+ methodnumber.put(md, new Integer(num));
+ foundmatch=true;
+ break;
+ }
+ }
+ if(state.MGC) {
+ // TODO add version for normal Java later
+ if(!foundmatch) {
+ // check if there is a matched method in inherited interfaces
+ Iterator it_sifs = cd.getSuperInterfaces();
+ while(it_sifs.hasNext() && !foundmatch) {
+ ClassDescriptor superif = (ClassDescriptor)it_sifs.next();
+ Set possiblematches_if=superif.getMethodTable().getSet(md.getSymbol());
+ for(Iterator matchit=possiblematches_if.iterator(); matchit.hasNext();) {
+ MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
+ if (md.matches(matchmd)) {
+ int num=((Integer)methodnumber.get(matchmd)).intValue();
+ methodnumber.put(md, new Integer(num));
+ foundmatch=true;
+ break;
+ }
+ }
}
}
}
- }
- }
- if (!foundmatch)
- methodnumber.put(md, new Integer(start++));
+ if (!foundmatch)
+ methodnumber.put(md, new Integer(start++));
} else {
- methodnumber.put(md, new Integer(start++));
+ methodnumber.put(md, new Integer(start++));
}
}
classmethodcount.put(cd, new Integer(start));
RESULT=(new ParseNode("method")).addChild(method).getRoot();
:}
/* repeat the prod for 'class_declaration' here: */
-// | modifiers_opt CLASS IDENTIFIER super_opt class_body
+// | modifiers_opt:mo CLASS IDENTIFIER:id super_opt:so interfaces_opt:ifo class_body:body
+// {:
+// ParseNode pn=new ParseNode("inner_class_declaration");
+// pn.addChild("modifiers").addChild(mo);
+// pn.addChild("name").addChild(id);
+// pn.addChild("super").addChild(so);
+// pn.addChild("superIF").addChild(ifo);
+// pn.addChild("classbody").addChild(body);
+// RESULT=pn;
+// :}
| interface_declaration:interfaced {:
RESULT=(new ParseNode("interface")).addChild(interfaced).getRoot();
:}
void adjust();
}
-class Wind implements Instrument {
+public interface Instrument2 {
+ // Cannot have method definitions:
+ void play(int n); // Automatically public
+}
+
+class Wind implements Instrument,Instrument2 {
public Wind(){}
public void play(int n) {
System.out.println("Wind.play() " + n);
public void adjust() { System.out.println("Wind.adjust()"); }
}
-class Percussion implements Instrument {
+class Percussion implements Instrument,Instrument2 {
public Percussion(){}
public void play(int n) {
System.out.println("Percussion.play() " + n);
public void adjust() { System.out.println("Percussion.adjust()"); }
}
-class Stringed implements Instrument {
+class Stringed implements Instrument,Instrument2 {
public Stringed(){}
public void play(int n) {
System.out.println("Stringed.play() " + n);
tune(i);
}
}
+ static void tune2(Instrument2 i) {
+ // ...
+ i.play(9);
+ }
+ static void tuneAll2(Instrument2[] e) {
+ for(int k = 0; k < e.length; k++) {
+ Instrument2 i = e[k];
+ tune2(i);
+ }
+ }
public static void main(String[] args) {
// Upcasting during addition to the array:
Instrument.VALUE=5;
orchestra[3] = new Brass();
orchestra[4] = new Woodwind();
tuneAll(orchestra);
+ Instrument2[] orchestra2 = new Instrument2[5];
+ orchestra2[0] = new Wind();
+ orchestra2[1] = new Percussion();
+ orchestra2[2] = new Stringed();
+ orchestra2[3] = new Brass();
+ orchestra2[4] = new Woodwind();
+ tuneAll2(orchestra2);
}
} /* Output:
Wind.play() MIDDLE_C