KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javadoc > hints > JavadocGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.javadoc.hints;
21
22 import com.sun.javadoc.Doc;
23 import javax.lang.model.SourceVersion;
24 import javax.lang.model.element.Element;
25 import javax.lang.model.element.ExecutableElement;
26 import javax.lang.model.element.NestingKind;
27 import javax.lang.model.element.TypeElement;
28 import javax.lang.model.element.TypeParameterElement;
29 import javax.lang.model.element.VariableElement;
30 import javax.lang.model.type.DeclaredType;
31 import javax.lang.model.type.TypeKind;
32 import javax.lang.model.type.TypeMirror;
33 import javax.swing.text.BadLocationException JavaDoc;
34 import javax.swing.text.Document JavaDoc;
35 import javax.swing.text.Position JavaDoc;
36 import org.netbeans.api.java.source.CompilationInfo;
37
38 /**
39  *
40  * @author Jan Pokorsky
41  */

42 public final class JavadocGenerator {
43     
44     private final SourceVersion srcVersion;
45     
46     /** Creates a new instance of JavadocGenerator */
47     public JavadocGenerator(SourceVersion version) {
48         this.srcVersion = version;
49     }
50     
51     public String JavaDoc generateComment(TypeElement clazz, CompilationInfo javac) {
52         StringBuilder JavaDoc builder = new StringBuilder JavaDoc(
53                 "/**\n" + // NOI18N
54
" * \n" // NOI18N
55
);
56         
57         if (clazz.getNestingKind() == NestingKind.TOP_LEVEL) {
58             builder.append(" * @author " + System.getProperty("user.name") + "\n"); // NOI18N
59
}
60         
61         if (SourceVersion.RELEASE_5.compareTo(srcVersion) <= 0) {
62             for (TypeParameterElement param : clazz.getTypeParameters()) {
63                 builder.append(" * @param " + param.getSimpleName().toString() + " \n"); // NOI18N
64
}
65         }
66         
67         if (SourceVersion.RELEASE_5.compareTo(srcVersion) <= 0 &&
68                 JavadocUtilities.isDeprecated(javac, clazz)) {
69             builder.append(" * @deprecated\n"); // NOI18N
70
}
71         
72         builder.append(" */\n"); // NOI18N
73

74         return builder.toString();
75     }
76     
77     public String JavaDoc generateComment(ExecutableElement method, CompilationInfo javac) {
78         StringBuilder JavaDoc builder = new StringBuilder JavaDoc(
79                 "/**\n" + // NOI18N
80
" * \n" // NOI18N
81
);
82         
83         for (VariableElement param : method.getParameters()) {
84             builder.append(" * @param ").append(param.getSimpleName().toString()).append(" \n"); // NOI18N
85
}
86         
87         if (method.getReturnType().getKind() != TypeKind.VOID) {
88             builder.append(" * @return \n"); // NOI18N
89
}
90         
91         for (TypeMirror exceptionType : method.getThrownTypes()) {
92             TypeElement exception = (TypeElement) ((DeclaredType) exceptionType).asElement();
93             builder.append(" * @throws ").append(exception.getQualifiedName().toString()).append(" \n"); // NOI18N
94
}
95         
96         if (SourceVersion.RELEASE_5.compareTo(srcVersion) <= 0 &&
97                 JavadocUtilities.isDeprecated(javac, method)) {
98             builder.append(" * @deprecated\n"); // NOI18N
99
}
100
101         builder.append(" */\n"); // NOI18N
102

103         return builder.toString();
104     }
105     
106     public String JavaDoc generateComment(VariableElement field, CompilationInfo javac) {
107         StringBuilder JavaDoc builder = new StringBuilder JavaDoc(
108                 "/**\n" + // NOI18N
109
" * \n" // NOI18N
110
);
111         
112         if (SourceVersion.RELEASE_5.compareTo(srcVersion) <= 0 &&
113                 JavadocUtilities.isDeprecated(javac, field)) {
114             builder.append(" * @deprecated\n"); // NOI18N
115
}
116         
117
118         builder.append(" */\n"); // NOI18N
119

120         return builder.toString();
121     }
122     
123     public String JavaDoc generateComment(Element elm, CompilationInfo javac) {
124         switch(elm.getKind()) {
125             case CLASS:
126             case ENUM:
127             case INTERFACE:
128             case ANNOTATION_TYPE:
129                 return generateComment((TypeElement) elm, javac);
130             case CONSTRUCTOR:
131             case METHOD:
132                 return generateComment((ExecutableElement) elm, javac);
133             case FIELD:
134             case ENUM_CONSTANT:
135                 return generateComment((VariableElement) elm, javac);
136             default:
137                 throw new UnsupportedOperationException JavaDoc(elm.getKind() +
138                         ", " + elm.getClass() + ": " + elm.toString()); // NOI18N
139
}
140     }
141     
142     public String JavaDoc generateInheritComment() {
143         return "/** {@inheritDoc} */"; //NOI18N
144
}
145     
146     public static String JavaDoc indentJavadoc(String JavaDoc jdoc, String JavaDoc tab) {
147         return jdoc.replace("\n *", "\n" + tab + " *") + tab; // NOI18N
148
}
149     
150     public static String JavaDoc guessIndentation(Document JavaDoc doc, Position JavaDoc pos) throws BadLocationException JavaDoc {
151         String JavaDoc content = doc.getText(0, doc.getLength());
152         int offset;
153         for (offset = pos.getOffset(); offset >= 0 && content.charAt(offset) != '\n'; offset--);
154         return content.substring(offset + 1, pos.getOffset());
155     }
156     
157     public static String JavaDoc guessJavadocIndentation(CompilationInfo javac, Document JavaDoc doc, Doc jdoc) throws BadLocationException JavaDoc {
158         Position JavaDoc[] jdBounds = JavadocUtilities.findDocBounds(javac, doc, jdoc);
159         if (jdBounds == null) {
160             return ""; // NOI18N
161
}
162         
163         String JavaDoc txt = doc.getText(0, doc.getLength());
164         int count = 0;
165         for (int offset = jdBounds[0].getOffset() - 1; offset >= 0; offset--) {
166             char c = txt.charAt(offset);
167             if (c == '\n' || !Character.isWhitespace(c)) {
168                 count = jdBounds[0].getOffset() - offset;
169                 break;
170             }
171         }
172         
173         char[] indent = new char[count];
174         for (int i = 0; i < indent.length; i++) {
175             indent[i] = ' ';
176         }
177
178         return String.valueOf(indent);
179     }
180     
181 }
182
Popular Tags