6 public class ClassDescriptor extends Descriptor {
7 private static int UIDCount=1; // start from 1 instead of 0 for multicore gc
8 private final int classid;
10 ClassDescriptor superdesc;
11 boolean hasFlags=false;
21 int numstaticblocks = 0;
22 int numstaticfields = 0;
25 Vector<String> superinterfaces;
26 SymbolTable superIFdesc;
27 private int interfaceid;
30 boolean isInnerClass=false;
32 // inner classes/enum can have these
33 String surroundingclass=null;
34 ClassDescriptor surroudingdesc=null;
35 SymbolTable innerdescs;
38 boolean isEnum = false;
39 SymbolTable enumdescs;
40 HashMap<String, Integer> enumConstantTbl;
41 int enumconstantid = 0;
43 boolean isClassLibrary=false;
45 public ClassDescriptor(String classname, boolean isInterface) {
46 this("", classname, isInterface);
49 public ClassDescriptor(String packagename, String classname, boolean isInterface) {
52 flags=new SymbolTable();
53 fields=new SymbolTable();
54 fieldvec=new Vector();
55 methods=new SymbolTable();
58 this.interfaceid = -1;
62 this.packagename=packagename;
63 superinterfaces = new Vector<String>();
64 superIFdesc = new SymbolTable();
65 this.innerdescs = new SymbolTable();
66 this.enumdescs = new SymbolTable();
70 if(this.isInterface()) {
71 return this.interfaceid;
76 public Iterator getMethods() {
77 return methods.getDescriptorsIterator();
80 public Iterator getFields() {
81 return fields.getDescriptorsIterator();
84 public Iterator getFlags() {
85 return flags.getDescriptorsIterator();
88 public Iterator getSuperInterfaces() {
89 return this.superIFdesc.getDescriptorsIterator();
92 public SymbolTable getFieldTable() {
96 public Vector getFieldVec() {
100 public SymbolTable getFlagTable() {
104 public SymbolTable getMethodTable() {
108 public SymbolTable getSuperInterfaceTable() {
109 return this.superIFdesc;
112 public String getSafeDescriptor() {
113 return "L"+safename.replace('.','/');
116 public String printTree(State state) {
118 String st=modifiers.toString()+"class "+getSymbol();
119 if (superclass!=null)
120 st+="extends "+superclass.toString();
121 if(this.superinterfaces != null) {
123 boolean needcomma = false;
124 for(int i = 0; i < this.superinterfaces.size(); i++) {
128 st += this.superinterfaces.elementAt(i);
133 indent=TreeNode.INDENT;
134 boolean printcr=false;
136 for(Iterator it=getFlags(); it.hasNext();) {
137 FlagDescriptor fd=(FlagDescriptor)it.next();
138 st+=TreeNode.printSpace(indent)+fd.toString()+"\n";
146 for(Iterator it=getFields(); it.hasNext();) {
147 FieldDescriptor fd=(FieldDescriptor)it.next();
148 st+=TreeNode.printSpace(indent)+fd.toString()+"\n";
154 for(Iterator it=this.getInnerClasses(); it.hasNext();) {
155 ClassDescriptor icd=(ClassDescriptor)it.next();
156 st+=icd.printTree(state)+"\n";
162 for(Iterator it=this.getEnum(); it.hasNext();) {
163 ClassDescriptor icd = (ClassDescriptor)it.next();
164 st += icd.getModifier().toString() + " enum " + icd.getSymbol() + " {\n ";
165 Set keys = icd.getEnumConstantTbl().keySet();
166 String[] econstants = new String[keys.size()];
167 Iterator it_keys = keys.iterator();
168 while(it_keys.hasNext()) {
169 String key = (String)it_keys.next();
170 econstants[icd.getEnumConstant(key)] = key;
172 for(int i = 0; i < econstants.length; i++) {
174 if(i < econstants.length-1) {
184 for(Iterator it=getMethods(); it.hasNext();) {
185 MethodDescriptor md=(MethodDescriptor)it.next();
186 st+=TreeNode.printSpace(indent)+md.toString()+" ";
187 BlockNode bn=state.getMethodBody(md);
188 st+=bn.printNode(indent)+"\n\n";
194 public MethodDescriptor getCalledMethod(MethodDescriptor md) {
195 ClassDescriptor cn=this;
200 Set possiblematches=cn.getMethodTable().getSet(md.getSymbol());
201 boolean foundmatch=false;
202 for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
203 MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
204 if (md.matches(matchmd)) {
209 //Not found...walk one level up
210 cn=cn.getSuperDesc();
214 public void addFlag(FlagDescriptor fd) {
215 if (flags.contains(fd.getSymbol()))
216 throw new Error(fd.getSymbol()+" already defined");
221 public boolean hasFlags() {
222 return hasFlags||getSuperDesc()!=null&&getSuperDesc().hasFlags();
225 public void addField(FieldDescriptor fd) {
226 if (fields.contains(fd.getSymbol()))
227 throw new Error(fd.getSymbol()+" already defined");
231 this.incStaticFields();
233 fd.setClassDescriptor(this);
236 public void addMethod(MethodDescriptor md) {
240 public void setModifiers(Modifiers modifiers) {
241 this.modifiers=modifiers;
244 public void setSuper(String superclass) {
245 this.superclass=superclass;
248 public ClassDescriptor getSuperDesc() {
252 public void setSuper(ClassDescriptor scd) {
256 public String getSuper() {
260 public void addSuperInterface(String superif) {
261 this.superinterfaces.addElement(superif);
264 public Vector<String> getSuperInterface() {
265 return this.superinterfaces;
268 public void addSuperInterfaces(ClassDescriptor sif) {
269 this.superIFdesc.add(sif);
272 public void incStaticBlocks() {
273 this.numstaticblocks++;
276 public int getNumStaticBlocks() {
277 return this.numstaticblocks;
280 public void incStaticFields() {
281 this.numstaticfields++;
284 public int getNumStaticFields() {
285 return this.numstaticfields;
288 public boolean isAbstract() {
289 return this.modifiers.isAbstract();
292 public boolean isInterface() {
293 return (this.classid == -2);
296 public void setInterfaceId(int id) {
297 this.interfaceid = id;
300 public boolean isStatic() {
301 return this.modifiers.isStatic();
304 public void setAsInnerClass() {
305 this.isInnerClass = true;
308 public boolean isInnerClass() {
309 return this.isInnerClass;
312 public void setSurroundingClass(String sclass) {
313 this.surroundingclass=sclass;
316 public String getSurrounding() {
317 return this.surroundingclass;
320 public ClassDescriptor getSurroundingDesc() {
321 return this.surroudingdesc;
324 public void setSurrounding(ClassDescriptor scd) {
325 this.surroudingdesc=scd;
328 public void addInnerClass(ClassDescriptor icd) {
329 this.innerdescs.add(icd);
332 public Iterator getInnerClasses() {
333 return this.innerdescs.getDescriptorsIterator();
336 public SymbolTable getInnerClassTable() {
337 return this.innerdescs;
340 public void setAsEnum() {
344 public boolean isEnum() {
348 public void addEnum(ClassDescriptor icd) {
349 this.enumdescs.add(icd);
352 public Iterator getEnum() {
353 return this.enumdescs.getDescriptorsIterator();
356 public SymbolTable getEnumTable() {
357 return this.enumdescs;
360 public void addEnumConstant(String econstant) {
361 if(this.enumConstantTbl == null) {
362 this.enumConstantTbl = new HashMap<String, Integer>();
364 if(this.enumConstantTbl.containsKey(econstant)) {
367 this.enumConstantTbl.put(econstant, this.enumconstantid++);
372 public int getEnumConstant(String econstant) {
373 if(this.enumConstantTbl.containsKey(econstant)) {
374 return this.enumConstantTbl.get(econstant).intValue();
380 public HashMap<String, Integer> getEnumConstantTbl() {
381 return this.enumConstantTbl;
384 public Modifiers getModifier() {
385 return this.modifiers;
388 public void setClassLibrary(){
389 this.isClassLibrary=true;
392 public boolean isClassLibrary(){
393 return isClassLibrary;