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.5KB

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