8
|
1 |
/*
|
|
2 |
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.codemodel.internal;
|
|
27 |
|
|
28 |
import java.util.HashMap;
|
|
29 |
import java.util.Map;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* JavaDoc comment.
|
|
33 |
*
|
|
34 |
* <p>
|
|
35 |
* A javadoc comment consists of multiple parts. There's the main part (that comes the first in
|
|
36 |
* in the comment section), then the parameter parts (@param), the return part (@return),
|
|
37 |
* and the throws parts (@throws).
|
|
38 |
*
|
|
39 |
* TODO: it would be nice if we have JComment class and we can derive this class from there.
|
|
40 |
*/
|
|
41 |
public class JDocComment extends JCommentPart implements JGenerable {
|
|
42 |
|
|
43 |
|
|
44 |
/** list of @param tags */
|
|
45 |
private final Map<String,JCommentPart> atParams = new HashMap<String,JCommentPart>();
|
|
46 |
|
|
47 |
/** list of xdoclets */
|
|
48 |
private final Map<String,Map<String,String>> atXdoclets = new HashMap<String,Map<String,String>>();
|
|
49 |
|
|
50 |
/** list of @throws tags */
|
|
51 |
private final Map<JClass,JCommentPart> atThrows = new HashMap<JClass,JCommentPart>();
|
|
52 |
|
|
53 |
/**
|
|
54 |
* The @return tag part.
|
|
55 |
*/
|
|
56 |
private JCommentPart atReturn = null;
|
|
57 |
|
|
58 |
/** The @deprecated tag */
|
|
59 |
private JCommentPart atDeprecated = null;
|
|
60 |
|
|
61 |
private final JCodeModel owner;
|
|
62 |
|
|
63 |
|
|
64 |
public JDocComment(JCodeModel owner) {
|
|
65 |
this.owner = owner;
|
|
66 |
}
|
|
67 |
|
|
68 |
public JDocComment append(Object o) {
|
|
69 |
add(o);
|
|
70 |
return this;
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Append a text to a @param tag to the javadoc
|
|
75 |
*/
|
|
76 |
public JCommentPart addParam( String param ) {
|
|
77 |
JCommentPart p = atParams.get(param);
|
|
78 |
if(p==null)
|
|
79 |
atParams.put(param,p=new JCommentPart());
|
|
80 |
return p;
|
|
81 |
}
|
|
82 |
|
|
83 |
/**
|
|
84 |
* Append a text to an @param tag.
|
|
85 |
*/
|
|
86 |
public JCommentPart addParam( JVar param ) {
|
|
87 |
return addParam( param.name() );
|
|
88 |
}
|
|
89 |
|
|
90 |
|
|
91 |
/**
|
|
92 |
* add an @throws tag to the javadoc
|
|
93 |
*/
|
|
94 |
public JCommentPart addThrows( Class exception ) {
|
|
95 |
return addThrows( owner.ref(exception) );
|
|
96 |
}
|
|
97 |
|
|
98 |
/**
|
|
99 |
* add an @throws tag to the javadoc
|
|
100 |
*/
|
|
101 |
public JCommentPart addThrows( JClass exception ) {
|
|
102 |
JCommentPart p = atThrows.get(exception);
|
|
103 |
if(p==null)
|
|
104 |
atThrows.put(exception,p=new JCommentPart());
|
|
105 |
return p;
|
|
106 |
}
|
|
107 |
|
|
108 |
/**
|
|
109 |
* Appends a text to @return tag.
|
|
110 |
*/
|
|
111 |
public JCommentPart addReturn() {
|
|
112 |
if(atReturn==null)
|
|
113 |
atReturn = new JCommentPart();
|
|
114 |
return atReturn;
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
|
118 |
* add an @deprecated tag to the javadoc, with the associated message.
|
|
119 |
*/
|
|
120 |
public JCommentPart addDeprecated() {
|
|
121 |
if(atDeprecated==null)
|
|
122 |
atDeprecated = new JCommentPart();
|
|
123 |
return atDeprecated;
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* add an xdoclet.
|
|
128 |
*/
|
|
129 |
public Map<String,String> addXdoclet(String name) {
|
|
130 |
Map<String,String> p = atXdoclets.get(name);
|
|
131 |
if(p==null)
|
|
132 |
atXdoclets.put(name,p=new HashMap<String,String>());
|
|
133 |
return p;
|
|
134 |
}
|
|
135 |
|
|
136 |
/**
|
|
137 |
* add an xdoclet.
|
|
138 |
*/
|
|
139 |
public Map<String,String> addXdoclet(String name, Map<String,String> attributes) {
|
|
140 |
Map<String,String> p = atXdoclets.get(name);
|
|
141 |
if(p==null)
|
|
142 |
atXdoclets.put(name,p=new HashMap<String,String>());
|
|
143 |
p.putAll(attributes);
|
|
144 |
return p;
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* add an xdoclet.
|
|
149 |
*/
|
|
150 |
public Map<String,String> addXdoclet(String name, String attribute, String value) {
|
|
151 |
Map<String,String> p = atXdoclets.get(name);
|
|
152 |
if(p==null)
|
|
153 |
atXdoclets.put(name,p=new HashMap<String,String>());
|
|
154 |
p.put(attribute, value);
|
|
155 |
return p;
|
|
156 |
}
|
|
157 |
|
|
158 |
public void generate(JFormatter f) {
|
|
159 |
// I realized that we can't use StringTokenizer because
|
|
160 |
// this will recognize multiple \n as one token.
|
|
161 |
|
|
162 |
f.p("/**").nl();
|
|
163 |
|
|
164 |
format(f," * ");
|
|
165 |
|
|
166 |
f.p(" * ").nl();
|
|
167 |
for (Map.Entry<String,JCommentPart> e : atParams.entrySet()) {
|
|
168 |
f.p(" * @param ").p(e.getKey()).nl();
|
|
169 |
e.getValue().format(f,INDENT);
|
|
170 |
}
|
|
171 |
if( atReturn != null ) {
|
|
172 |
f.p(" * @return").nl();
|
|
173 |
atReturn.format(f,INDENT);
|
|
174 |
}
|
|
175 |
for (Map.Entry<JClass,JCommentPart> e : atThrows.entrySet()) {
|
|
176 |
f.p(" * @throws ").t(e.getKey()).nl();
|
|
177 |
e.getValue().format(f,INDENT);
|
|
178 |
}
|
|
179 |
if( atDeprecated != null ) {
|
|
180 |
f.p(" * @deprecated").nl();
|
|
181 |
atDeprecated.format(f,INDENT);
|
|
182 |
}
|
|
183 |
for (Map.Entry<String,Map<String,String>> e : atXdoclets.entrySet()) {
|
|
184 |
f.p(" * @").p(e.getKey());
|
|
185 |
if (e.getValue() != null) {
|
|
186 |
for (Map.Entry<String,String> a : e.getValue().entrySet()) {
|
|
187 |
f.p(" ").p(a.getKey()).p("= \"").p(a.getValue()).p("\"");
|
|
188 |
}
|
|
189 |
}
|
|
190 |
f.nl();
|
|
191 |
}
|
|
192 |
f.p(" */").nl();
|
|
193 |
}
|
|
194 |
|
|
195 |
private static final String INDENT = " * ";
|
|
196 |
}
|