PackageDesc.java 6.0 KB

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