1 16 17 package de.gulden.util.javasource; 18 19 import de.gulden.util.javasource.jjt.Node; 20 import de.gulden.util.xml.XMLToolbox; 21 import org.w3c.dom.*; 22 import java.io.*; 23 import java.util.*; 24 25 31 public class Class extends SourceObjectDeclaredVisible implements PackageMember { 32 33 37 40 public Vector myMember; 41 42 45 public Vector myClassInner; 46 47 50 public Vector myImport; 51 52 55 public Vector staticInitializers; 56 57 60 public Vector instanceInitializers; 61 62 65 protected boolean interfaceFlag; 66 67 70 protected Vector interfaceNames; 71 72 75 protected String superclassName; 76 77 81 transient boolean pass2 = false; 82 83 89 private transient Node rootnode; 90 91 94 private HashMap qualifyCache; 95 96 97 101 104 public Class() { 105 super(); 106 myImport=new Vector(); 107 interfaceFlag=false; 108 staticInitializers = new Vector(); 109 instanceInitializers = new Vector(); 110 interfaceNames=new Vector(); 111 myMember=new Vector(); 112 myClassInner=new Vector(); 113 qualifyCache = new HashMap(); 114 } 115 116 117 121 124 public void setPackage(Package p) { 125 myPackage=p; 126 registerAtPackage(p); 127 } 128 129 132 public NamedIterator getImports() { 133 return new NamedIterator(myImport); 134 } 135 136 141 public String getSuperclassName() { 142 return superclassName!=null ? superclassName : "java.lang.Object"; 143 } 144 145 148 public void setSuperclassName(String n) { 149 superclassName=n; 150 } 151 152 155 public Enumeration getInterfaceNames() { 156 return interfaceNames.elements(); 157 } 158 159 162 public void setInterfaceNames(String [] names) { 163 stringsIntoVector(names,interfaceNames); 164 } 165 166 169 public Code[] getStaticInitializers() { 170 Code[] c = new Code[staticInitializers.size()]; 171 staticInitializers.copyInto(c); 172 return c; 173 } 174 175 178 public Code[] getInstanceInitializers() { 179 Code[] c = new Code[instanceInitializers.size()]; 180 instanceInitializers.copyInto(c); 181 return c; 182 } 183 184 187 public void addStaticInitializer(Code c) { 188 staticInitializers.addElement(c); 189 } 190 191 194 public void addInstanceInitializer(Code c) { 195 instanceInitializers.addElement(c); 196 } 197 198 201 public NamedIterator getMembers(Class type, int modifiers) { 202 return new NamedIterator(myMember,type,modifiers); 203 } 204 205 209 public NamedIterator getAllMembers() { 210 return new NamedIterator(myMember); 211 } 212 213 216 public NamedIterator getInnerClasses() { 217 return new NamedIterator(myClassInner); 218 } 219 220 224 public boolean isInterface() { 225 return interfaceFlag; 226 } 227 228 232 public void setInterface(boolean inter) { 233 interfaceFlag=inter; 234 } 235 236 242 public String qualify(String name) { 243 String q; 244 String cached = (String )qualifyCache.get(name); 245 if (cached != null) { 246 return cached; 247 } else { 248 if (qualifyCache.containsKey(name)) { q = null; 250 } else { q=qualifyInternal(name); 252 qualifyCache.put(name, q); 253 } 254 if (q!=null) { 255 return q; 256 } else { 257 throw new NoClassDefFoundError ("cannot qualify class name "+name+" in class "+getName()); 258 } 259 } 260 261 262 } 263 264 267 public void addToPackage(Package p) { 268 String packageName=getPackage().getName(); 269 Package pp=p.findPackage(packageName); if (pp!=null) { 271 setPackage(pp); pp.registerClass(this); 273 } 274 else { p.add(myPackage); } 277 } 278 279 285 public Element buildXML(Document d) { 286 Element e=super.buildXML(d); 288 for (NamedIterator it=getImports();it.hasMore();) { 290 Import im=(Import)it.next(); 291 e.appendChild(im.buildXML(d)); 292 } 293 294 if ( ! isInterface() ) { 295 Element sup=d.createElement("extends"); 297 sup.setAttribute("class",superclassName); 298 e.appendChild(sup); 299 300 for (Enumeration en=getInterfaceNames();en.hasMoreElements();) { 302 String in=(String )en.nextElement(); 303 Element inE=d.createElement("implements"); 304 inE.setAttribute("interface",in); 305 e.appendChild(inE); 306 } 307 } else { for (Enumeration en=getInterfaceNames();en.hasMoreElements();) { 310 String in=(String )en.nextElement(); 311 Element inE=d.createElement("extends"); 312 inE.setAttribute("interface",in); 313 e.appendChild(inE); 314 } 315 } 316 317 Code[] initializers = getStaticInitializers(); 319 for (int i = 0; i<initializers.length; i++) { 320 Element initializer=d.createElement("initializer"); 321 initializer.setAttribute("static","yes"); 322 initializer.appendChild(initializers[i].buildXML(d)); 323 e.appendChild(initializer); 324 } 325 initializers = getInstanceInitializers(); 326 for (int i = 0; i<initializers.length; i++) { 327 Element initializer=d.createElement("initializer"); 328 initializer.appendChild(initializers[i].buildXML(d)); 329 e.appendChild(initializer); 330 } 331 332 for (NamedIterator it=getAllMembers();it.hasMore();) { 334 Member m=(Member)it.next(); 335 e.appendChild(m.buildXML(d)); 336 } 337 338 for (NamedIterator it=getInnerClasses();it.hasMore();) { 340 Class c=(Class )it.next(); 341 e.appendChild(c.buildXML(d)); 342 } 343 344 return e; 345 } 346 347 353 public void initFromXML(Element element) throws IOException { 354 super.initFromXML(element); 356 357 interfaceFlag=element.getTagName().equals("interface"); 358 359 myImport.removeAllElements(); 361 NodeList nl=XMLToolbox.getChildren(element,"import"); 362 for (int i=0;i<nl.getLength();i++) { 363 Import im=Import.createFromXML(getPackage(),(Element)nl.item(i)); 364 myImport.addElement(im); 365 } 366 367 Element sup=XMLToolbox.getChild(element,"extends"); 369 if (sup!=null) { 370 superclassName=XMLToolbox.getAttributeRequired(sup,"class"); 371 } 372 else { 373 superclassName="java.lang.Object"; 374 } 375 376 interfaceNames.removeAllElements(); 378 nl=XMLToolbox.getChildren(element,"implements"); 379 for (int i=0;i<nl.getLength();i++) { 380 Element imE=(Element)nl.item(i); 381 String imp=XMLToolbox.getAttributeRequired(imE,"interface"); 382 interfaceNames.addElement(imp); 383 } 384 385 nl = XMLToolbox.getChildren(element,"initializer"); 387 for (int i=0;i<nl.getLength();i++) { 388 Element iniE=(Element)nl.item(i); 389 Code initializer=new Code(); 390 initializer.initFromXML(XMLToolbox.getChildRequired(iniE,"code")); 391 boolean isStatic = XMLToolbox.isYesAttribute(iniE, "static"); 392 if (isStatic) { 393 staticInitializers.addElement(initializer); 394 } else { 395 instanceInitializers.addElement(initializer); 396 } 397 } 398 399 myMember.removeAllElements(); 401 402 nl=XMLToolbox.getChildren(element,"field"); 404 for (int i=0;i<nl.getLength();i++) { 405 Field fi=new Field(this); 406 fi.initFromXML((Element)nl.item(i)); 407 myMember.addElement(fi); 408 } 409 410 nl=XMLToolbox.getChildren(element,"constructor"); 412 for (int i=0;i<nl.getLength();i++) { 413 Constructor co=new Constructor(this); 414 co.initFromXML((Element)nl.item(i)); 415 myMember.addElement(co); 416 } 417 418 nl=XMLToolbox.getChildren(element,"method"); 420 for (int i=0;i<nl.getLength();i++) { 421 Method me=new Method(this); 422 me.initFromXML((Element)nl.item(i)); 423 myMember.addElement(me); 424 } 425 426 myClassInner.removeAllElements(); 428 429 nl=XMLToolbox.getChildren(element,"class"); 431 for (int i=0;i<nl.getLength();i++) { 432 ClassInner ci=new ClassInner(); 433 ci.setDeclaringClass(this); 434 ci.initFromXML((Element)nl.item(i)); 435 myClassInner.addElement(ci); 436 } 437 438 nl=XMLToolbox.getChildren(element,"interface"); 440 for (int i=0;i<nl.getLength();i++) { 441 ClassInner ci=new ClassInner(); 442 ci.setDeclaringClass(this); 443 ci.setInterface(true); 444 ci.initFromXML((Element)nl.item(i)); 445 myClassInner.addElement(ci); 446 } 447 } 448 449 public ClassInner findInnerClass(String name) { 450 String selfName = getName(); 451 String selfUnqualified=getUnqualifiedName(); 452 if (name.startsWith(selfName+".")) { 454 name=name.substring(selfName.length()+1); 455 } else if (name.startsWith(selfUnqualified+".")) { 457 name=name.substring(selfUnqualified.length()+1); 458 } 459 String find; 464 int dot = name.indexOf('.'); if (dot != -1) { 466 find = name.substring(0, dot); 467 } else { 468 find = name; 469 } 470 Class searchClass = this; 471 while (find != null) { 472 NamedIterator it = searchClass.getInnerClasses(); 473 searchClass = null; 474 while ((searchClass==null) && it.hasMore()) { 475 ClassInner ci=(ClassInner)it.next(); 476 if (ci.getUnqualifiedName().equals(find)) { 477 searchClass = ci; 478 } 484 } 485 if (searchClass != null) { 486 if (dot!=-1) { int nextDot = name.indexOf(',', dot+1); 488 if (nextDot!=-1) { 489 find = name.substring(dot+1, nextDot); 490 } else { 491 find = name.substring(dot+1); 492 } 493 dot = nextDot; 494 } else { 495 find = null; 496 } 497 } else { 498 return null; 499 } 500 } 501 return (ClassInner)searchClass; } 504 505 protected void registerAtPackage(Package p) { 506 p.registerClass(this); 508 } 509 510 516 protected String qualifyInternal(String name) { 517 String q; 518 NamedIterator it; 519 520 521 this.initFromASTPass2(); 523 524 if (isPrimitive(name)) { 525 return name; 526 } 527 528 String selfUnqualified=getUnqualifiedName(); 529 530 if (name.equals(selfUnqualified)) { 532 return getName(); 533 } 534 535 Package basePackage=getPackage().getBasePackage(); 536 537 if (basePackage.findClass(name)!=null) { 540 return name; 541 } 542 try { 544 java.lang.Class.forName(name); 545 return name; 546 } 547 catch (ClassNotFoundException cnfe) { 548 } 550 551 it=getImports(); 553 while (it.hasMore()) { 554 Import im=(Import)it.next(); 555 if (im instanceof ImportClass) { 556 q=im.qualify(name); 557 if (q!=null) { 558 return q; 559 } 560 } 561 } 562 563 String packageName=getPackage().getName(); 565 if ((!packageName.equals("")) && (!name.startsWith(packageName+"."))) { 566 q=packageName+"."+name; 567 } 568 else { 569 q=name; 570 } 571 Class c=basePackage.findClass(q); if (c!=null) { 573 return q; 574 } 575 576 ClassInner ci = findInnerClass(name); 578 if (ci != null) { 579 return ci.getName(); 580 } 581 582 int lastdot = name.lastIndexOf('.'); 583 584 609 610 if (lastdot != -1) { 612 String outerName = name.substring(0, lastdot); 613 String outerQ; 615 if (Package.isSourcePackage(basePackage, outerName)) { outerQ = null; 617 } else { 618 try { 619 outerQ = qualify(outerName); } catch (NoClassDefFoundError ncdfe) { 621 outerQ = null; 622 } 623 } 624 if (outerQ != null) { 625 q = outerQ + "." + name.substring(lastdot+1); 627 Class qcl = basePackage.findClass(q); 628 if (qcl!= null) { 629 return q; 630 } 631 String q2 = outerQ + "$" + name.substring(lastdot+1); 633 try { 634 ClassLoader cl = ClassLoader.getSystemClassLoader(); java.lang.Class dummy = cl.loadClass(q2); 637 return q; 638 } 639 catch (ClassNotFoundException cnfe) { 640 } 642 } 645 } 646 647 if (lastdot == -1) { 649 String sup = this.getSuperclassName(); 650 while ((sup!=null) && (!sup.equals("java.lang.Object"))) { 651 Class superclass = basePackage.findClass(sup); 652 if (!name.startsWith(sup+".")) { 653 try { 654 String supInner; 655 Class qc; 656 if (superclass != null) { 657 qc = superclass; 658 } else { 659 qc = this; 660 } 661 String supInnerName = sup + "." + name; 662 supInner = qc.qualify(supInnerName); return supInner; 664 } catch (NoClassDefFoundError ncdfe) { 665 } 667 } 668 if (superclass != null) { superclass.initFromASTPass2(); sup = superclass.getSuperclassName(); 673 } else { 674 try { 675 java.lang.Class cl = java.lang.Class.forName(sup); 676 cl = cl.getSuperclass(); 677 if (cl != null) { 678 sup = cl.getName(); 679 } else { 680 sup = null; 681 } 682 } catch (ClassNotFoundException cnfe) { 683 sup = null; 684 } 685 } 686 } 687 } 688 689 if ( (this.rootnode != null) && (! (this instanceof ClassInner))) { String sourcename; 692 if ((!packageName.equals("")) && (name.startsWith(packageName+"."))) { 693 sourcename = name.substring(packageName.length()+1); 694 }else { 695 sourcename = name; 696 } 697 if (sourcename.indexOf('.')==-1) { 698 sourcename += ".java"; 699 File thisSource = new File(this.rootnode.getSource()); 700 File source = new File(thisSource.getParentFile(), sourcename); 701 if (source.exists()) { 702 try { 703 SourceParser.parse(source, basePackage, null); 704 return qualifyInternal(name); } catch (java.lang.Exception e) { 707 } 709 } 710 } 711 } 712 713 if (this instanceof ClassInner) { 715 try { 716 String s = getDeclaringClass().qualify(name); return s; 718 } catch (NoClassDefFoundError ncdfe) { 719 return null; } 722 } 723 724 it=getImports(); 726 while (it.hasMore()) { 727 Import im=(Import)it.next(); 728 if (im instanceof ImportPackage) { 729 q=im.qualify(name); 730 if (q!=null) { 731 return q; 732 } 733 } 734 } 735 736 q="java.lang."+name; 738 try { 739 java.lang.Class.forName(q); 740 return q; 741 } 742 catch (ClassNotFoundException cnfe) { 743 } 745 746 return null; 748 } 749 750 753 protected boolean isPrimitive(String type) { 754 return type.equals("boolean") 755 || type.equals("byte") 756 || type.equals("char") 757 || type.equals("short") 758 || type.equals("int") 759 || type.equals("long") 760 || type.equals("float") 761 || type.equals("double") 762 || type.equals("void"); 763 } 764 765 768 protected String getXMLName() { 769 return interfaceFlag?"interface":"class"; 771 } 772 773 778 void initFromAST(Node rootnode) { 779 Node n; 780 Node[] nodes; 781 782 super.initFromAST(rootnode); String packageName=getPackage().getName(); 785 if (packageName.length()>0) { 786 name=packageName+"."+name; } 788 789 myClassInner.removeAllElements(); 791 nodes=rootnode.getChildren(JJT_INNERCLASS); 793 for (int i=0;i<nodes.length;i++) { 794 ClassInner c=new ClassInner(); 795 c.setDeclaringClass(this); 796 c.initFromAST(nodes[i]); 797 myClassInner.addElement(c); 798 } 799 nodes=rootnode.getChildren(JJT_INNERINTERFACE); 801 for (int i=0;i<nodes.length;i++) { 802 ClassInner c=new ClassInner(); 803 c.setDeclaringClass(this); 804 c.setInterface(true); 805 c.initFromAST(nodes[i]); 806 myClassInner.addElement(c); 807 } 808 809 this.rootnode=rootnode; } 811 812 815 void initFromASTPass2() { 816 Node n; 817 Node[] nodes; 818 819 if ((!pass2) && (rootnode != null)) { 821 pass2 = true; 822 823 Class q; if (this instanceof ClassInner) { 825 q = this.getDeclaringClass(); 826 } else { 827 q = this; 828 } 829 830 n=rootnode.getChild(JJT_SUPERCLASS); 832 if (n!=null) { 833 superclassName= q.qualify( n.getName() ); 834 } 835 else { 836 superclassName="java.lang.Object"; 837 } 838 839 interfaceNames.removeAllElements(); 841 nodes=rootnode.getChildren(JJT_IMPLEMENTS); 842 for (int i=0;i<nodes.length;i++) { 843 String name=nodes[i].getName(); 844 interfaceNames.addElement( q.qualify(name) ); 845 } 846 847 staticInitializers.removeAllElements(); 848 instanceInitializers.removeAllElements(); 849 nodes=rootnode.getChildren(JJT_INITIALIZER); 850 for (int i=0;i<nodes.length;i++) { 851 Code ini = new Code(); 852 ini.initFromAST(nodes[i].getChild(JJT_CODE)); 853 boolean isStatic = nodes[i].hasChild(JJT_STATIC); 854 if (isStatic) { 855 addStaticInitializer(ini); 856 } else { 857 addInstanceInitializer(ini); 858 } 859 } 860 861 863 NamedIterator it; 865 it=getInnerClasses(); 866 while (it.hasMore()) { 867 ClassInner c=(ClassInner)it.next(); 868 c.initFromASTPass2(); 869 } 870 871 myMember.removeAllElements(); 873 nodes=rootnode.getChildren(JJT_FIELD); 874 for (int i=0;i<nodes.length;i++) { 875 Node[] vars=nodes[i].getChildren(JJT_FIELDVAR); 876 for (int j=0;j<vars.length;j++) { 877 Field f=new Field(this); 878 f.initFromAST(nodes[i],vars[j]); 879 myMember.addElement(f); 880 } 881 } 882 883 nodes=rootnode.getChildren(JJT_CONSTRUCTOR); 885 for (int i=0;i<nodes.length;i++) { 886 Constructor c=new Constructor(this); 887 c.initFromAST(nodes[i]); 888 myMember.addElement(c); 889 } 890 891 nodes=rootnode.getChildren(JJT_METHOD); 893 for (int i=0;i<nodes.length;i++) { 894 Method c=new Method(this); 895 c.initFromAST(nodes[i]); 896 myMember.addElement(c); 897 } 898 899 rootnode=null; } 901 } 902 903 } | Popular Tags |