You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StubFileGenerator.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * Mik Kersten port to AspectJ 1.1+ code base
  13. * ******************************************************************/
  14. package org.aspectj.tools.ajdoc;
  15. import java.io.BufferedWriter;
  16. import java.io.File;
  17. import java.io.FileWriter;
  18. import java.io.IOException;
  19. import java.io.PrintWriter;
  20. import java.util.Hashtable;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import org.aspectj.asm.AsmManager;
  24. import org.aspectj.asm.IProgramElement;
  25. import org.aspectj.util.FileUtil;
  26. /**
  27. * @author Mik Kersten
  28. */
  29. class StubFileGenerator {
  30. static Hashtable declIDTable = null;
  31. static void doFiles(AsmManager model, Hashtable table, File[] inputFiles, File[] signatureFiles) throws DocException {
  32. declIDTable = table;
  33. for (int i = 0; i < inputFiles.length; i++) {
  34. processFile(model, inputFiles[i], signatureFiles[i]);
  35. }
  36. }
  37. static void processFile(AsmManager model, File inputFile, File signatureFile) throws DocException {
  38. try {
  39. // Special Case for package-info.java just copy file directly.
  40. if(signatureFile.getName().equals("package-info.java")) {
  41. FileUtil.copyFile(inputFile, signatureFile);
  42. return;
  43. }
  44. String path = StructureUtil.translateAjPathName(signatureFile.getCanonicalPath());
  45. PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(path)));
  46. String packageName = StructureUtil.getPackageDeclarationFromFile(model, inputFile);
  47. if (packageName != null && packageName != "") {
  48. writer.println("package " + packageName + ";");
  49. }
  50. IProgramElement fileNode = model.getHierarchy().findElementForSourceFile(inputFile.getAbsolutePath());
  51. for (IProgramElement node : fileNode.getChildren()) {
  52. if (node.getKind().isPackageDeclaration()) {
  53. // skip
  54. } else if (node.getKind().equals(IProgramElement.Kind.IMPORT_REFERENCE)) {
  55. processImportDeclaration(node, writer);
  56. } else {
  57. try {
  58. processTypeDeclaration(node, writer);
  59. } catch (DocException d) {
  60. throw new DocException("File name invalid: " + inputFile.toString());
  61. }
  62. }
  63. }
  64. // if we got an error we don't want the contents of the file
  65. writer.close();
  66. } catch (IOException e) {
  67. System.err.println(e.getMessage());
  68. e.printStackTrace();
  69. }
  70. }
  71. private static void processImportDeclaration(IProgramElement node, PrintWriter writer) throws IOException {
  72. List imports = node.getChildren();
  73. for (Object anImport : imports) {
  74. IProgramElement importNode = (IProgramElement) anImport;
  75. writer.println(importNode.getSourceSignature());
  76. }
  77. }
  78. private static void processTypeDeclaration(IProgramElement classNode, PrintWriter writer) throws DocException {
  79. String formalComment = addDeclID(classNode, classNode.getFormalComment());
  80. writer.println(formalComment);
  81. String signature = genSourceSignature(classNode);// StructureUtil.genSignature(classNode);
  82. if (signature == null) {
  83. throw new DocException("The java file is invalid");
  84. }
  85. // System.err.println("######" + signature + ", " + classNode.getName());
  86. if (!StructureUtil.isAnonymous(classNode) && !classNode.getName().equals("<undefined>")) {
  87. writer.println(signature + " { ");
  88. processMembers(classNode.getChildren(), writer, classNode.getKind().equals(IProgramElement.Kind.INTERFACE));
  89. writer.println();
  90. writer.println("}");
  91. }
  92. }
  93. private static void processMembers(List/* IProgramElement */members, PrintWriter writer, boolean declaringTypeIsInterface)
  94. throws DocException {
  95. for (Object o : members) {
  96. IProgramElement member = (IProgramElement) o;
  97. if (member.getKind().isType()) {
  98. if (!member.getParent().getKind().equals(IProgramElement.Kind.METHOD) && !StructureUtil.isAnonymous(member)) {// don't
  99. // print
  100. // anonymous
  101. // types
  102. // System.err.println(">>>>>>>>>>>>>" + member.getName() + "<<<<" + member.getParent());
  103. processTypeDeclaration(member, writer);
  104. }
  105. } else {
  106. String formalComment = addDeclID(member, member.getFormalComment());
  107. ;
  108. writer.println(formalComment);
  109. String signature = "";
  110. if (!member.getKind().equals(IProgramElement.Kind.POINTCUT)
  111. && !member.getKind().equals(IProgramElement.Kind.ADVICE)) {
  112. signature = member.getSourceSignature();// StructureUtil.genSignature(member);
  113. if (member.getKind().equals(IProgramElement.Kind.ENUM_VALUE)) {
  114. int index = members.indexOf(member);
  115. if ((index + 1 < members.size())
  116. && ((IProgramElement) members.get(index + 1)).getKind().equals(IProgramElement.Kind.ENUM_VALUE)) {
  117. // if the next member is also an ENUM_VALUE:
  118. signature = signature + ",";
  119. } else {
  120. signature = signature + ";";
  121. }
  122. }
  123. }
  124. if (member.getKind().isDeclare()) {
  125. // System.err.println("> Skipping declare (ajdoc limitation): " + member.toLabelString());
  126. } else if (signature != null && signature != "" && !member.getKind().isInterTypeMember()
  127. && !member.getKind().equals(IProgramElement.Kind.INITIALIZER) && !StructureUtil.isAnonymous(member)) {
  128. writer.print(signature);
  129. } else {
  130. // System.err.println(">> skipping: " + member.getKind());
  131. }
  132. if (member.getKind().equals(IProgramElement.Kind.METHOD)
  133. || member.getKind().equals(IProgramElement.Kind.CONSTRUCTOR)) {
  134. if (member.getParent().getKind().equals(IProgramElement.Kind.INTERFACE) || signature.indexOf("abstract ") != -1) {
  135. writer.println(";");
  136. } else {
  137. writer.println(" { }");
  138. }
  139. } else if (member.getKind().equals(IProgramElement.Kind.FIELD)) {
  140. // writer.println(";");
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * Translates "aspect" to "class", as long as its not ".aspect"
  147. */
  148. private static String genSourceSignature(IProgramElement classNode) {
  149. String signature = classNode.getSourceSignature();
  150. if (signature != null) {
  151. int index = signature.indexOf("aspect");
  152. if (index == 0 || (index != -1 && signature.charAt(index - 1) != '.')) {
  153. signature = signature.substring(0, index) + "class " + signature.substring(index + 6, signature.length());
  154. }
  155. }
  156. return signature;
  157. }
  158. static int nextDeclID = 0;
  159. static String addDeclID(IProgramElement decl, String formalComment) {
  160. String declID = "" + ++nextDeclID;
  161. declIDTable.put(declID, decl);
  162. return addToFormal(formalComment, Config.DECL_ID_STRING + declID + Config.DECL_ID_TERMINATOR);
  163. }
  164. /**
  165. * We want to go: just before the first period just before the first @ just before the end of the comment
  166. *
  167. * Adds a place holder for the period ('#') if one will need to be replaced.
  168. */
  169. static String addToFormal(String formalComment, String string) {
  170. if (string == null || string.equals("")) {
  171. return formalComment;
  172. }
  173. // boolean appendPeriod = true;
  174. if ((formalComment == null) || formalComment.equals("")) {
  175. // formalComment = "/**\n * . \n */\n";
  176. formalComment = "/**\n * \n */\n";
  177. // appendPeriod = false;
  178. }
  179. formalComment = formalComment.trim();
  180. int atsignPos = formalComment.indexOf('@');
  181. int endPos = formalComment.indexOf("*/");
  182. int periodPos = formalComment.indexOf("/**");
  183. int position = 0;
  184. String periodPlaceHolder = "";
  185. if (periodPos != -1) {
  186. position = periodPos + 3;// length of "/**"
  187. } else if (atsignPos != -1) {
  188. string = string + "\n * ";
  189. position = atsignPos;
  190. } else if (endPos != -1) {
  191. string = "* " + string + "\n";
  192. position = endPos;
  193. } else {
  194. // !!! perhaps this error should not be silent
  195. throw new Error("Failed to append to formal comment for comment: " + formalComment);
  196. }
  197. return formalComment.substring(0, position) + periodPlaceHolder + string + formalComment.substring(position);
  198. }
  199. }