5 public class ClassDescriptor extends Descriptor {
6 private static int UIDCount=1; // start from 1 instead of 0 for multicore gc
7 private final int classid;
9 ClassDescriptor superdesc;
10 boolean hasFlags=false;
20 int numstaticblocks = 0;
21 int numstaticfields = 0;
24 Vector<String> superinterfaces;
25 SymbolTable superIFdesc;
26 private boolean isInterface;
29 boolean isInnerClass=false;
31 // inner classes/enum can have these
32 String surroundingclass=null;
33 ClassDescriptor surroudingdesc=null;
34 SymbolTable innerdescs;
37 boolean isEnum = false;
38 SymbolTable enumdescs;
39 HashMap<String, Integer> enumConstantTbl;
40 int enumconstantid = 0;
42 public ClassDescriptor(String classname, boolean isInterface) {
43 this("", classname, isInterface);
46 public ClassDescriptor(String packagename, String classname, boolean isInterface) {
49 flags=new SymbolTable();
50 fields=new SymbolTable();
51 fieldvec=new Vector();
52 methods=new SymbolTable();
53 this.isInterface = isInterface;
55 this.packagename=packagename;
56 superinterfaces = new Vector<String>();
57 superIFdesc = new SymbolTable();
58 this.innerdescs = new SymbolTable();
59 this.enumdescs = new SymbolTable();
66 public Iterator getMethods() {
67 return methods.getDescriptorsIterator();
70 public Iterator getFields() {
71 return fields.getDescriptorsIterator();
74 public Iterator getFlags() {
75 return flags.getDescriptorsIterator();
78 public Iterator getSuperInterfaces() {
79 return this.superIFdesc.getDescriptorsIterator();
82 public SymbolTable getFieldTable() {
86 public Vector getFieldVec() {
90 public SymbolTable getFlagTable() {
94 public SymbolTable getMethodTable() {
98 public SymbolTable getSuperInterfaceTable() {
99 return this.superIFdesc;
102 public String getSafeDescriptor() {
103 return "L"+safename.replace('.','/');
106 public String printTree(State state) {
108 String st=modifiers.toString()+"class "+getSymbol();
109 if (superclass!=null)
110 st+="extends "+superclass.toString();
111 if(this.superinterfaces != null) {
113 boolean needcomma = false;
114 for(int i = 0; i < this.superinterfaces.size(); i++) {
118 st += this.superinterfaces.elementAt(i);
123 indent=TreeNode.INDENT;
124 boolean printcr=false;
126 for(Iterator it=getFlags(); it.hasNext();) {
127 FlagDescriptor fd=(FlagDescriptor)it.next();
128 st+=TreeNode.printSpace(indent)+fd.toString()+"\n";
136 for(Iterator it=getFields(); it.hasNext();) {
137 FieldDescriptor fd=(FieldDescriptor)it.next();
138 st+=TreeNode.printSpace(indent)+fd.toString()+"\n";
144 for(Iterator it=this.getInnerClasses(); it.hasNext();) {
145 ClassDescriptor icd=(ClassDescriptor)it.next();
146 st+=icd.printTree(state)+"\n";
152 for(Iterator it=this.getEnum(); it.hasNext();) {
153 ClassDescriptor icd = (ClassDescriptor)it.next();
154 st += icd.getModifier().toString() + " enum " + icd.getSymbol() + " {\n ";
155 Set keys = icd.getEnumConstantTbl().keySet();
156 String[] econstants = new String[keys.size()];
157 Iterator it_keys = keys.iterator();
158 while(it_keys.hasNext()) {
159 String key = (String)it_keys.next();
160 econstants[icd.getEnumConstant(key)] = key;
162 for(int i = 0; i < econstants.length; i++) {
164 if(i < econstants.length-1) {
174 for(Iterator it=getMethods(); it.hasNext();) {
175 MethodDescriptor md=(MethodDescriptor)it.next();
176 st+=TreeNode.printSpace(indent)+md.toString()+" ";
177 BlockNode bn=state.getMethodBody(md);
178 st+=bn.printNode(indent)+"\n\n";
184 public MethodDescriptor getCalledMethod(MethodDescriptor md) {
185 ClassDescriptor cn=this;
187 Iterator methodit=cn.getMethods();
188 //Iterator through methods
189 while(methodit.hasNext()) {
190 Set possiblematches=cn.getMethodTable().getSet(md.getSymbol());
191 boolean foundmatch=false;
192 for(Iterator matchit=possiblematches.iterator(); matchit.hasNext();) {
193 MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
194 if (md.matches(matchmd)) {
199 //Not found...walk one level up
200 cn=cn.getSuperDesc();
204 public void addFlag(FlagDescriptor fd) {
205 if (flags.contains(fd.getSymbol()))
206 throw new Error(fd.getSymbol()+" already defined");
211 public boolean hasFlags() {
212 return hasFlags||getSuperDesc()!=null&&getSuperDesc().hasFlags();
215 public void addField(FieldDescriptor fd) {
216 if (fields.contains(fd.getSymbol()))
217 throw new Error(fd.getSymbol()+" already defined");
221 this.incStaticFields();
223 fd.setClassDescriptor(this);
226 public void addMethod(MethodDescriptor md) {
230 public void setModifiers(Modifiers modifiers) {
231 this.modifiers=modifiers;
234 public void setSuper(String superclass) {
235 this.superclass=superclass;
238 public ClassDescriptor getSuperDesc() {
242 public void setSuper(ClassDescriptor scd) {
246 public String getSuper() {
250 public void addSuperInterface(String superif) {
251 this.superinterfaces.addElement(superif);
254 public Vector<String> getSuperInterface() {
255 return this.superinterfaces;
258 public void addSuperInterfaces(ClassDescriptor sif) {
259 this.superIFdesc.add(sif);
262 public void incStaticBlocks() {
263 this.numstaticblocks++;
266 public int getNumStaticBlocks() {
267 return this.numstaticblocks;
270 public void incStaticFields() {
271 this.numstaticfields++;
274 public int getNumStaticFields() {
275 return this.numstaticfields;
278 public boolean isAbstract() {
279 return this.modifiers.isAbstract();
282 public boolean isInterface() {
283 return this.isInterface;
286 public boolean isStatic() {
287 return this.modifiers.isStatic();
290 public void setAsInnerClass() {
291 this.isInnerClass = true;
294 public boolean isInnerClass() {
295 return this.isInnerClass;
298 public void setSurroundingClass(String sclass) {
299 this.surroundingclass=sclass;
302 public String getSurrounding() {
303 return this.surroundingclass;
306 public ClassDescriptor getSurroundingDesc() {
307 return this.surroudingdesc;
310 public void setSurrounding(ClassDescriptor scd) {
311 this.surroudingdesc=scd;
314 public void addInnerClass(ClassDescriptor icd) {
315 this.innerdescs.add(icd);
318 public Iterator getInnerClasses() {
319 return this.innerdescs.getDescriptorsIterator();
322 public SymbolTable getInnerClassTable() {
323 return this.innerdescs;
326 public void setAsEnum() {
330 public boolean isEnum() {
334 public void addEnum(ClassDescriptor icd) {
335 this.enumdescs.add(icd);
338 public Iterator getEnum() {
339 return this.enumdescs.getDescriptorsIterator();
342 public SymbolTable getEnumTable() {
343 return this.enumdescs;
346 public void addEnumConstant(String econstant) {
347 if(this.enumConstantTbl == null) {
348 this.enumConstantTbl = new HashMap<String, Integer>();
350 if(this.enumConstantTbl.containsKey(econstant)) {
353 this.enumConstantTbl.put(econstant, this.enumconstantid++);
358 public int getEnumConstant(String econstant) {
359 if(this.enumConstantTbl.containsKey(econstant)) {
360 return this.enumConstantTbl.get(econstant).intValue();
366 public HashMap<String, Integer> getEnumConstantTbl() {
367 return this.enumConstantTbl;
370 public Modifiers getModifier() {
371 return this.modifiers;