PackageDesc.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**********************************************************************/
  2. /* Package Desscriptor class for J2CPS */
  3. /* */
  4. /* (c) copyright QUT */
  5. /**********************************************************************/
  6. package J2CPS;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. public class PackageDesc {
  13. private static final char qSepCh = '/';
  14. private static final char fSepCh =
  15. System.getProperty("file.separator").charAt(0);
  16. private static final char jSepCh = '.';
  17. private static final char nSepCh = '_';
  18. private static ArrayList<PackageDesc> toDo = new ArrayList<PackageDesc>(2);
  19. private static ArrayList<PackageDesc> syms = new ArrayList<PackageDesc>(2);
  20. private static HashMap<String,PackageDesc> packageList = new HashMap<String,PackageDesc>();
  21. private File packageFile;
  22. public ClassDesc[] classes;
  23. public String name, cpName, javaName, dirName;
  24. public ArrayList<PackageDesc> imports = new ArrayList<PackageDesc>();
  25. public int impNum = -1;
  26. public boolean anonPackage = false;
  27. public static PackageDesc MakeNewPackageDesc(String pName, boolean anon) {
  28. PackageDesc desc = new PackageDesc(pName, anon);
  29. if (!anon)
  30. packageList.put(desc.name, desc);
  31. toDo.add(desc);
  32. return desc;
  33. }
  34. private PackageDesc(String pName, boolean anon) {
  35. if (anon) {
  36. name = pName;
  37. cpName = pName;
  38. javaName = pName;
  39. anonPackage = true;
  40. } else {
  41. MakeName(pName);
  42. }
  43. }
  44. private void MakeName(String pName) {
  45. name = pName.replace(jSepCh,qSepCh);
  46. name = name.replace(fSepCh,qSepCh); /* name is now .../... */
  47. cpName = name.replace(qSepCh,nSepCh);
  48. javaName = name.replace(qSepCh,jSepCh);
  49. if (qSepCh != fSepCh) {
  50. dirName = name.replace(qSepCh,fSepCh);
  51. } else {
  52. dirName = name;
  53. }
  54. }
  55. public static PackageDesc getPackage(String packName) {
  56. packName = packName.replace(jSepCh,qSepCh);
  57. PackageDesc pack = (PackageDesc)packageList.get(packName);
  58. if (pack == null) { pack = PackageDesc.MakeNewPackageDesc(packName,false); }
  59. return pack;
  60. }
  61. public static PackageDesc getClassPackage(String className) {
  62. className = className.replace(jSepCh,qSepCh);
  63. String pName = className.substring(0,className.lastIndexOf(qSepCh));
  64. PackageDesc pack = (PackageDesc)packageList.get(pName);
  65. if (pack == null) { pack = PackageDesc.MakeNewPackageDesc(pName,false); }
  66. return pack;
  67. }
  68. public void AddImport(TypeDesc ty) {
  69. if (ty instanceof ClassDesc) {
  70. ClassDesc aClass = (ClassDesc)ty;
  71. if (aClass.packageDesc == null) {
  72. System.err.println("ERROR: Class "+aClass.qualName+" has no package");
  73. System.exit(0);
  74. }
  75. if ((this!=aClass.packageDesc)&&(!imports.contains(aClass.packageDesc))){
  76. imports.add(aClass.packageDesc);
  77. }
  78. }
  79. }
  80. public void AddImport(PackageDesc pack) {
  81. if ((this != pack) && (!imports.contains(pack))){
  82. boolean ok = imports.add(pack);
  83. }
  84. }
  85. public void ResetImports() {
  86. for (int i=0; i < imports.size(); i++) {
  87. imports.get(i).impNum = -1;
  88. }
  89. }
  90. private void AddImportList(ArrayList impList) {
  91. for (int i=0; i < impList.size(); i++) {
  92. AddImport((PackageDesc)impList.get(i));
  93. }
  94. }
  95. public void ReadPackage() throws IOException, FileNotFoundException {
  96. boolean ok = syms.add(this);
  97. if (anonPackage) {
  98. classes = new ClassDesc[1];
  99. classes[0] = ClassDesc.GetClassDesc(name,this);
  100. boolean ok2 = classes[0].ReadClassFile(J2CPSFiles.OpenClassFile(name));
  101. return;
  102. }
  103. packageFile = J2CPSFiles.getPackageFile(dirName);
  104. String[] classFiles = packageFile.list(new J2CPSFiles());
  105. classes = new ClassDesc[classFiles.length];
  106. for (int i = 0; i < classFiles.length; i++) {
  107. String cName = name + qSepCh +
  108. classFiles[i].substring(0,classFiles[i].lastIndexOf('.'));
  109. ClassDesc nextClass = ClassDesc.GetClassDesc(cName,this);
  110. if (nextClass.ReadClassFile(J2CPSFiles.OpenClassFile(packageFile,
  111. classFiles[i]))) {
  112. classes[i] = nextClass;
  113. }
  114. }
  115. }
  116. public static void ReadPackages() throws IOException, FileNotFoundException {
  117. int j = 0;
  118. toDo.get(0).ReadPackage();
  119. if (!ClassDesc.verbose) // Lightweight progress indicator ...
  120. System.out.println("INFO: reading dependents ");
  121. for (int i=1; i < toDo.size(); i++) {
  122. PackageDesc pack = toDo.get(i);
  123. /* look for symbol file first */
  124. pack.packageFile = J2CPSFiles.FindSymbolFile(pack.cpName);
  125. if (pack.packageFile == null) {
  126. pack.ReadPackage();
  127. if (!ClassDesc.verbose) { System.out.print('+'); j++; }
  128. } else {
  129. if (ClassDesc.verbose) {
  130. System.out.println("Reading Symbol File <" +
  131. pack.packageFile.getPath() + ">");
  132. }
  133. SymbolFile.ReadSymbolFile(pack.packageFile,pack);
  134. if (!ClassDesc.verbose) { System.out.print('-'); j++; }
  135. }
  136. if (j >= 79) { System.out.println(); j = 0; }
  137. }
  138. if (!ClassDesc.verbose && j > 0) System.out.println();
  139. }
  140. public static void WriteSymbolFiles() throws IOException {
  141. for (int i=0; i < syms.size(); i++) {
  142. HashMap<String,MethodInfo> pScope = new HashMap<String,MethodInfo>();
  143. PackageDesc nextPack = syms.get(i);
  144. for (int j=0; j < nextPack.classes.length; j++) {
  145. if (nextPack.classes[j] != null) {
  146. if (ClassDesc.overloadedNames) {
  147. nextPack.classes[j].GetSuperImports();
  148. } else {
  149. nextPack.classes[j].GetSuperFields(pScope);
  150. }
  151. nextPack.AddImportList(nextPack.classes[j].imports);
  152. ClassDesc superCl = nextPack.classes[j].superClass;
  153. while (superCl != null) {
  154. nextPack.AddImport(superCl);
  155. nextPack.AddImportList(superCl.imports);
  156. superCl = superCl.superClass;
  157. }
  158. }
  159. }
  160. }
  161. for (int i=0; i < syms.size(); i++) {
  162. PackageDesc nextPack = syms.get(i);
  163. SymbolFile.WriteSymbolFile(nextPack);
  164. }
  165. }
  166. }