PackageDesc.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /**********************************************************************/
  2. /* Package Desscriptor class for j2cps */
  3. /* */
  4. /* (c) copyright QUT, John Gough 2000-2012, John Gough, 2012-2017 */
  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. import j2cpsfiles.j2cpsfiles;
  13. public class PackageDesc {
  14. public static int pub = 0;
  15. public static int tot = 0;
  16. static final ExtFilter classFilter = ExtFilter.NewExtFilter(".class");
  17. /**
  18. * Work-list of all the packages encountered during this
  19. * invocation of <code>j2cps</code>, either directly or through reference.
  20. */
  21. static final ArrayList<PackageDesc> toDo = new ArrayList<>(2);
  22. /**
  23. * List of packages that will be emitted as symbol files.
  24. */
  25. static final ArrayList<PackageDesc> syms = new ArrayList<>(2);
  26. /**
  27. * Dictionary of packages known to this invocation of <code>j2cps</code>.
  28. */
  29. static final HashMap<String,PackageDesc> packageList = new HashMap<>();
  30. /**
  31. * The file associated with this package.
  32. */
  33. File theFile;
  34. int status = Util.PRISTINE;
  35. /**
  36. * This list contains all the classes defined in this package.
  37. */
  38. public ArrayList<ClassDesc> myClasses;
  39. /**
  40. * The name of the package with <code>qSepCh</code> separators
  41. */
  42. public String name;
  43. /**
  44. * The name of the package converted to a legal
  45. * Component Pascal module identifier.
  46. */
  47. public String cpName;
  48. /**
  49. * Package name in java language (dotted) format.
  50. */
  51. public String javaName;
  52. /**
  53. * Directory name relative to class file hierarchy root with
  54. * notation corrected for environment-specific file separators
  55. */
  56. public String dirName;
  57. /**
  58. * List of imports into this package.
  59. */
  60. public ArrayList<PackageDesc> pkgImports = new ArrayList<>();
  61. Object blame;
  62. public int impNum = -1;
  63. public boolean anonPackage = false;
  64. /**
  65. * Boolean true indicates that this package has been
  66. * processed, and (if not just <i> referenced</i>) has
  67. * been added to the <code>syms</code> list.
  68. */
  69. public boolean processed = false;
  70. public String blameStr() {
  71. if (this.blame == null)
  72. return "no-blame";
  73. else if (this.blame instanceof MemberInfo) {
  74. MemberInfo f = (MemberInfo)this.blame;
  75. return f.owner.objName + "::" + f.name;
  76. }
  77. else if (this.blame instanceof ClassDesc) {
  78. ClassDesc c = (ClassDesc)this.blame;
  79. return "ClassDesc " + c.name;
  80. }
  81. else
  82. return this.blame.toString();
  83. }
  84. /**
  85. * Adds another package to the <code>toDo</code>
  86. * and package lists.
  87. * @param pName the full name of the package without file-extension
  88. * @param anon indicates if this package is the anonymous package
  89. * @return
  90. */
  91. static PackageDesc MakeNewPackageDesc(String pName, boolean anon) {
  92. PackageDesc desc = new PackageDesc(pName, anon);
  93. if (!anon) {
  94. // Add to package list
  95. packageList.put(desc.name, desc);
  96. desc.set(Util.ONLIST);
  97. }
  98. // Add to toDo worklist
  99. toDo.add(desc);
  100. /*
  101. if (pName.startsWith("com") || pName.startsWith("sun"))
  102. System.err.println(pName);
  103. */
  104. desc.set(Util.ONTODO);
  105. return desc;
  106. }
  107. /**
  108. * Creates a new package descriptor, which will be the root
  109. * (and element-zero) of the work-list for non-jar based
  110. * invocations.
  111. * <p>
  112. * Adds another package to the <code>toDo</code>
  113. * and package lists.
  114. * @param pName the full name of the package without file-extension
  115. * @param anon indicates if this package is the anonymous package
  116. */
  117. public static void MakeRootPackageDescriptor(String pName, boolean anon) {
  118. PackageDesc desc = new PackageDesc(pName, anon);
  119. if (!anon) { // Then insert in HashMap
  120. packageList.put(desc.name, desc);
  121. desc.set(Util.ONLIST);
  122. }
  123. // And in any case put on the worklist
  124. toDo.add(desc);
  125. /*
  126. if (pName.startsWith("com") || pName.startsWith("sun"))
  127. System.err.println(pName);
  128. */
  129. desc.set(Util.ONTODO);
  130. }
  131. private PackageDesc(String pName, boolean anon) {
  132. if (anon) {
  133. this.name = pName;
  134. this.cpName = pName;
  135. this.javaName = pName;
  136. this.anonPackage = true;
  137. } else {
  138. MakeName(pName);
  139. }
  140. this.myClasses = new ArrayList<>();
  141. }
  142. private void MakeName(String pName) {
  143. this.name = pName.replace(Util.JAVADOT,Util.FWDSLSH);
  144. this.name = this.name.replace(Util.FILESEP,Util.FWDSLSH); /* name is now .../... */
  145. this.cpName = this.name.replace(Util.FWDSLSH,Util.LOWLINE);
  146. this.javaName = this.name.replace(Util.FWDSLSH,Util.JAVADOT);
  147. if (Util.FWDSLSH != Util.FILESEP) {
  148. this.dirName = this.name.replace(Util.FWDSLSH,Util.FILESEP);
  149. } else {
  150. this.dirName = this.name;
  151. }
  152. }
  153. /** Create or fetch package descriptor by name */
  154. public static PackageDesc getPackage(String packName) {
  155. packName = packName.replace(Util.JAVADOT,Util.FWDSLSH);
  156. PackageDesc pack = (PackageDesc)packageList.get(packName);
  157. if (pack == null) {
  158. pack = PackageDesc.MakeNewPackageDesc(packName,false);
  159. }
  160. return pack;
  161. }
  162. /**
  163. * Create or fetch the package descriptor object for the class
  164. * with the given java (dotted) name. In the case that the
  165. * package has not been previously seen, the name of the newly
  166. * created package descriptor is the prefix of the className.
  167. * @param className the full java-name of the class.
  168. * @return the package descriptor to which the corresponding
  169. * class descriptor should be added.
  170. */
  171. public static PackageDesc getClassPackage(String className) {
  172. className = className.replace(Util.JAVADOT,Util.FWDSLSH);
  173. String pName = className.substring(0,className.lastIndexOf(Util.FWDSLSH));
  174. PackageDesc pack = (PackageDesc)packageList.get(pName);
  175. if (pack == null) {
  176. pack = PackageDesc.MakeNewPackageDesc(pName,false);
  177. }
  178. return pack;
  179. }
  180. public void AddPkgImport(TypeDesc ty) {
  181. if (ty instanceof ClassDesc) {
  182. ClassDesc aClass = (ClassDesc)ty;
  183. if (aClass.parentPkg == null) {
  184. System.err.println("ERROR: Class <"+aClass.javaName+"> has no package");
  185. System.exit(0);
  186. }
  187. if ((this!=aClass.parentPkg)&&
  188. (!this.pkgImports.contains(aClass.parentPkg))){
  189. this.pkgImports.add(aClass.parentPkg);
  190. }
  191. }
  192. }
  193. public void AddImport(PackageDesc pack) {
  194. if ((this != pack) && (!this.pkgImports.contains(pack))){
  195. boolean ignoreMe = this.pkgImports.add(pack);
  196. }
  197. }
  198. public void ResetImports() {
  199. for (int i=0; i < this.pkgImports.size(); i++) {
  200. this.pkgImports.get(i).impNum = -1;
  201. }
  202. }
  203. private void AddImportList(ArrayList impList) {
  204. for (int i=0; i < impList.size(); i++) {
  205. // this.AddImport((PackageDesc)impList.get(i));
  206. PackageDesc p = (PackageDesc)impList.get(i);
  207. this.AddImport(p);
  208. System.out.printf("package %s, adding import %s, with APINEEDS=%s\n",
  209. this.cpName, p.cpName, (p.has(Util.APINEEDS) ? "true" : "false"));
  210. }
  211. }
  212. public void ReadPackage() throws IOException, FileNotFoundException {
  213. boolean ignoreMe;
  214. this.processed = true;
  215. ignoreMe = syms.add(this);
  216. if (this.anonPackage) {
  217. ClassDesc newClass;
  218. // this.classes = new ClassDesc[1];
  219. //
  220. // Create a new class descriptor with the given name
  221. // in the anonomous package represented by "this".
  222. // Then read the class file with the given name.
  223. //
  224. newClass = ClassDesc.GetClassDesc(this.name,this);
  225. this.myClasses.add( newClass );
  226. //
  227. // this.classes[0] = newClass;
  228. //
  229. ignoreMe = newClass.ReadPkgClassFile(j2cpsfiles.OpenClassFile(this.name));
  230. } else {
  231. this.theFile = j2cpsfiles.getPackageFile(this.dirName);
  232. //
  233. // classFilter gets files ending with ".class"
  234. //
  235. String[] clsNames = this.theFile.list(classFilter);
  236. //
  237. // Create a descriptor array and populate with the class
  238. // descriptors corresponding to the class files in the
  239. // directory for the current package.
  240. //
  241. // this.classes = new ClassDesc[clsNames.length];
  242. for (int i = 0; i < clsNames.length; i++) {
  243. String cName = name + Util.FWDSLSH +
  244. clsNames[i].substring(0,clsNames[i].lastIndexOf('.'));
  245. ClassDesc nextClass = ClassDesc.GetClassDesc(cName,this);
  246. if (nextClass.ReadPkgClassFile(j2cpsfiles.OpenClassFile(this.theFile, clsNames[i]))) {
  247. // this.classes[i] = nextClass;
  248. this.myClasses.add(nextClass);
  249. }
  250. }
  251. }
  252. }
  253. public static void ProcessPkgWorklist() throws IOException, FileNotFoundException {
  254. int j = 0;
  255. {
  256. PackageDesc pkg0 = toDo.get(0);
  257. pkg0.ReadPackage();
  258. if (ClassDesc.summary) // Lightweight progress indicator ...
  259. System.out.printf("INFO: locating dependencies of package <%s>\n", pkg0.name);
  260. }
  261. //
  262. // Reading package pkg0 may have added to the toDo list
  263. //
  264. for (int i=1; i < toDo.size(); i++) {
  265. PackageDesc pkgDesc = toDo.get(i);
  266. if (!pkgDesc.has(Util.APINEEDS)) {
  267. if (ClassDesc.verbose)
  268. System.out.println("Non API package skipped " + pkgDesc.cpName);
  269. continue;
  270. }
  271. /* look for symbol file first */
  272. pkgDesc.theFile = j2cpsfiles.FindSymbolFile(pkgDesc.cpName);
  273. if (pkgDesc.theFile == null) { // No symbol file, look for package
  274. pkgDesc.ReadPackage();
  275. } else { // Symbol file was found ==> Read it.
  276. pkgDesc.set(Util.FOUNDCPS);
  277. if (ClassDesc.summary) {
  278. System.out.println("Found Symbol file for dependency <" + pkgDesc.cpName + ">");
  279. if (ClassDesc.verbose)
  280. System.out.println(" Symbol file " + pkgDesc.theFile.getPath());
  281. }
  282. }
  283. }
  284. }
  285. public static void ProcessJarDependencies() throws IOException, FileNotFoundException {
  286. for (PackageDesc desc : toDo) {
  287. if (desc.processed) {
  288. syms.add(desc);
  289. } else {
  290. desc.theFile = j2cpsfiles.FindSymbolFile(desc.cpName);
  291. System.out.println("INFO: Searching for imported package " + desc.javaName);
  292. if (desc.theFile == null)
  293. System.err.printf(
  294. " Symbolfile %s not found on CPSYM path\n", desc.cpName);
  295. else if (ClassDesc.summary)
  296. System.out.println(
  297. "Found symbol file for dependency <" + desc.cpName + ">");
  298. }
  299. }
  300. }
  301. public static void WriteSymbolFiles() throws IOException {
  302. for (PackageDesc nextPack : syms) {
  303. SymbolFile.WriteSymbolFile(nextPack);
  304. }
  305. }
  306. public boolean has(int tag) {
  307. return (this.status & tag) != Util.PRISTINE;
  308. }
  309. public void clear(int tag) {
  310. this.status ^= tag;
  311. }
  312. public void set(int tag) {
  313. this.status |= tag;
  314. int mask = Util.FROMJAR | Util.FROMCLS;
  315. if (( this.status & mask) == mask)
  316. throw new IllegalArgumentException();
  317. }
  318. }