123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- /* *******************************************************************
- * Copyright (c) 1999-2001 Xerox Corporation,
- * 2002 Palo Alto Research Center, Incorporated (PARC).
- * All rights reserved.
- * This program and the accompanying materials are made available
- * under the terms of the Common Public License v1.0
- * which accompanies this distribution and is available at
- * http://www.eclipse.org/legal/cpl-v10.html
- *
- * Contributors:
- * Xerox/PARC initial implementation
- * Mik Kersten port to AspectJ 1.1+ code base
- * ******************************************************************/
-
- package org.aspectj.tools.ajdoc;
-
- import java.io.*;
- import java.util.Hashtable;
- import java.util.Iterator;
- import java.util.List;
-
- import org.aspectj.asm.AsmManager;
- import org.aspectj.asm.IProgramElement;
-
- /**
- * @author Mik Kersten
- */
- class StubFileGenerator{
-
- static Hashtable declIDTable = null;
-
- static void doFiles (Hashtable table,
- SymbolManager symbolManager,
- File[] inputFiles,
- File[] signatureFiles) throws DocException {
- declIDTable = table;
- for (int i = 0; i < inputFiles.length; i++) {
- processFile(symbolManager, inputFiles[i], signatureFiles[i]);
- }
- }
-
-
- static void processFile(SymbolManager symbolManager, File inputFile, File signatureFile) throws DocException {
- try {
- String path = StructureUtil.translateAjPathName(signatureFile.getCanonicalPath());
- PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(path)));
-
- String packageName = StructureUtil.getPackageDeclarationFromFile(inputFile);
-
- if (packageName != null && packageName != "") {
- writer.println( "package " + packageName + ";" );
- }
-
- IProgramElement fileNode = (IProgramElement)AsmManager.getDefault().getHierarchy().findElementForSourceFile(inputFile.getAbsolutePath());
- for (Iterator it = fileNode.getChildren().iterator(); it.hasNext(); ) {
- IProgramElement node = (IProgramElement)it.next();
- if (node.getKind().equals(IProgramElement.Kind.IMPORT_REFERENCE)) {
- processImportDeclaration(node, writer);
- } else {
- try {
- processTypeDeclaration(node, writer);
- } catch (DocException d){
- throw new DocException("File name invalid: " + inputFile.toString());
- }
- }
- }
-
- // if we got an error we don't want the contents of the file
- writer.close();
- } catch (IOException e) {
- System.err.println(e.getMessage());
- e.printStackTrace();
- }
- }
-
- private static void processImportDeclaration(IProgramElement node, PrintWriter writer) throws IOException {
- List imports = node.getChildren();
- for (Iterator i = imports.iterator(); i.hasNext();) {
- IProgramElement importNode = (IProgramElement) i.next();
- writer.print("import ");
- writer.print(importNode.getName());
- writer.println(';');
- }
- }
-
- private static void processTypeDeclaration(IProgramElement classNode, PrintWriter writer) throws DocException {
-
- String formalComment = addDeclID(classNode, classNode.getFormalComment());
- writer.println(formalComment);
-
- String signature = genSourceSignature(classNode);// StructureUtil.genSignature(classNode);
- if (signature == null){
- throw new DocException("The java file is invalid");
- }
-
- // System.err.println("######" + signature + ", " + classNode.getName());
- if (!StructureUtil.isAnonymous(classNode) && !classNode.getName().equals("<undefined>")) {
- writer.println(signature + " {" );
- processMembers(classNode.getChildren(), writer, classNode.getKind().equals(IProgramElement.Kind.INTERFACE));
- writer.println();
- writer.println("}");
- }
- }
-
- private static void processMembers(List/*IProgramElement*/ members, PrintWriter writer, boolean declaringTypeIsInterface) throws DocException {
- for (Iterator it = members.iterator(); it.hasNext();) {
- IProgramElement member = (IProgramElement) it.next();
-
- if (member.getKind().isType()) {
- if (!member.getParent().getKind().equals(IProgramElement.Kind.METHOD)
- && !StructureUtil.isAnonymous(member)) {// don't print anonymous types
- // System.err.println(">>>>>>>>>>>>>" + member.getName() + "<<<<" + member.getParent());
- processTypeDeclaration(member, writer);
- }
- } else {
- String formalComment = addDeclID(member, member.getFormalComment());;
- writer.println(formalComment);
-
- String signature = "";
- if (!member.getKind().equals(IProgramElement.Kind.POINTCUT)
- && !member.getKind().equals(IProgramElement.Kind.ADVICE)) {
- signature = member.getSourceSignature();//StructureUtil.genSignature(member);
- if (member.getKind().equals(IProgramElement.Kind.ENUM_VALUE)){
- if (((IProgramElement)members.get(members.indexOf(member)+1)).getKind().equals(IProgramElement.Kind.ENUM_VALUE)){
- // if the next member is also an ENUM_VALUE:
- signature = signature + ",";
- } else {
- signature = signature + ";";
- }
- }
- }
-
- if (member.getKind().isDeclare()) {
- // System.err.println("> Skipping declare (ajdoc limitation): " + member.toLabelString());
- } else if (signature != null &&
- signature != "" &&
- !member.getKind().isInterTypeMember() &&
- !member.getKind().equals(IProgramElement.Kind.INITIALIZER) &&
- !StructureUtil.isAnonymous(member)) {
- writer.print(signature);
- } else {
- // System.err.println(">> skipping: " + member.getKind());
- }
-
- if (member.getKind().equals(IProgramElement.Kind.METHOD) ||
- member.getKind().equals(IProgramElement.Kind.CONSTRUCTOR)) {
- if (member.getParent().getKind().equals(IProgramElement.Kind.INTERFACE) ||
- signature.indexOf("abstract ") != -1) {
- writer.println(";");
- } else {
- writer.println(" { }");
- }
-
- } else if (member.getKind().equals(IProgramElement.Kind.FIELD)) {
- // writer.println(";");
- }
- }
- }
- }
-
- /**
- * Translates "aspect" to "class", as long as its not ".aspect"
- */
- private static String genSourceSignature(IProgramElement classNode) {
- String signature = classNode.getSourceSignature();
- if (signature != null){
- int index = signature.indexOf("aspect");
- if (index == 0 || (index != -1 && signature.charAt(index-1) != '.') ) {
- signature = signature.substring(0, index) +
- "class " +
- signature.substring(index + 6, signature.length());
- }
- }
- return signature;
- }
-
- static int nextDeclID = 0;
- static String addDeclID(IProgramElement decl, String formalComment) {
- String declID = "" + ++nextDeclID;
- declIDTable.put(declID, decl);
- return addToFormal(formalComment, Config.DECL_ID_STRING + declID + Config.DECL_ID_TERMINATOR);
- }
-
- /**
- * We want to go:
- * just before the first period
- * just before the first @
- * just before the end of the comment
- *
- * Adds a place holder for the period ('#') if one will need to be
- * replaced.
- */
- static String addToFormal(String formalComment, String string) {
- boolean appendPeriod = true;
- if ( (formalComment == null) || formalComment.equals("")) {
- //formalComment = "/**\n * . \n */\n";
- formalComment = "/**\n * \n */\n";
- appendPeriod = false;
- }
- formalComment = formalComment.trim();
-
- int atsignPos = formalComment.indexOf('@');
- int endPos = formalComment.indexOf("*/");
- int periodPos = formalComment.indexOf("/**")+2;
- int position = 0;
- String periodPlaceHolder = "";
- if ( periodPos != -1 ) {
- position = periodPos+1;
- }
- else if ( atsignPos != -1 ) {
- string = string + "\n * ";
- position = atsignPos;
- }
- else if ( endPos != -1 ) {
- string = "* " + string + "\n";
- position = endPos;
- }
- else {
- // !!! perhaps this error should not be silent
- throw new Error("Failed to append to formal comment for comment: " +
- formalComment );
- }
-
- return
- formalComment.substring(0, position) + periodPlaceHolder +
- string +
- formalComment.substring(position);
- }
-
- }
|