Tue, 11 Aug 2009 01:13:14 +0100
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
Summary: javac should warn/complain about identifiers with the same name as synthetic symbol
Reviewed-by: jjg
1 /*
2 * Copyright 1997-2009 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 */
26 package com.sun.tools.javadoc;
28 import java.io.IOException;
29 import java.util.Locale;
30 import javax.tools.JavaFileObject;
31 import javax.tools.StandardJavaFileManager;
33 import com.sun.javadoc.*;
35 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
36 import com.sun.tools.javac.util.List;
37 import com.sun.tools.javac.util.ListBuffer;
38 import com.sun.tools.javac.util.Position;
40 /**
41 * This class holds the information from one run of javadoc.
42 * Particularly the packages, classes and options specified
43 * by the user..
44 *
45 * @since 1.2
46 * @author Robert Field
47 * @author Atul M Dambalkar
48 * @author Neal Gafter (rewrite)
49 */
50 public class RootDocImpl extends DocImpl implements RootDoc {
52 /**
53 * list of classes specified on the command line.
54 */
55 private List<ClassDocImpl> cmdLineClasses;
57 /**
58 * list of packages specified on the command line.
59 */
60 private List<PackageDocImpl> cmdLinePackages;
62 /**
63 * a collection of all options.
64 */
65 private List<String[]> options;
67 /**
68 * Constructor used when reading source files.
69 *
70 * @param env the documentation environment, state for this javadoc run
71 * @param classes list of classes specified on the commandline
72 * @param packages list of package names specified on the commandline
73 * @param options list of options
74 */
75 public RootDocImpl(DocEnv env, List<JCClassDecl> classes, List<String> packages, List<String[]> options) {
76 super(env, null);
77 this.options = options;
78 setPackages(env, packages);
79 setClasses(env, classes);
80 }
82 /**
83 * Constructor used when reading class files.
84 *
85 * @param env the documentation environment, state for this javadoc run
86 * @param classes list of class names specified on the commandline
87 * @param options list of options
88 */
89 public RootDocImpl(DocEnv env, List<String> classes, List<String[]> options) {
90 super(env, null);
91 this.options = options;
92 cmdLinePackages = List.nil();
93 ListBuffer<ClassDocImpl> classList = new ListBuffer<ClassDocImpl>();
94 for (String className : classes) {
95 ClassDocImpl c = env.loadClass(className);
96 if (c == null)
97 env.error(null, "javadoc.class_not_found", className);
98 else
99 classList = classList.append(c);
100 }
101 cmdLineClasses = classList.toList();
102 }
104 /**
105 * Initialize classes information. Those classes are input from
106 * command line.
107 *
108 * @param env the compilation environment
109 * @param classes a list of ClassDeclaration
110 */
111 private void setClasses(DocEnv env, List<JCClassDecl> classes) {
112 ListBuffer<ClassDocImpl> result = new ListBuffer<ClassDocImpl>();
113 for (JCClassDecl def : classes) {
114 //### Do we want modifier check here?
115 if (env.shouldDocument(def.sym)) {
116 ClassDocImpl cd = env.getClassDoc(def.sym);
117 if (cd != null) {
118 cd.isIncluded = true;
119 result.append(cd);
120 } //else System.out.println(" (classdoc is null)");//DEBUG
121 } //else System.out.println(" (env.shouldDocument() returned false)");//DEBUG
122 }
123 cmdLineClasses = result.toList();
124 }
126 /**
127 * Initialize packages information.
128 *
129 * @param env the compilation environment
130 * @param packages a list of package names (String)
131 */
132 private void setPackages(DocEnv env, List<String> packages) {
133 ListBuffer<PackageDocImpl> packlist = new ListBuffer<PackageDocImpl>();
134 for (String name : packages) {
135 PackageDocImpl pkg = env.lookupPackage(name);
136 if (pkg != null) {
137 pkg.isIncluded = true;
138 packlist.append(pkg);
139 } else {
140 env.warning(null, "main.no_source_files_for_package", name);
141 }
142 }
143 cmdLinePackages = packlist.toList();
144 }
146 /**
147 * Command line options.
148 *
149 * <pre>
150 * For example, given:
151 * javadoc -foo this that -bar other ...
152 *
153 * This method will return:
154 * options()[0][0] = "-foo"
155 * options()[0][1] = "this"
156 * options()[0][2] = "that"
157 * options()[1][0] = "-bar"
158 * options()[1][1] = "other"
159 * </pre>
160 *
161 * @return an array of arrays of String.
162 */
163 public String[][] options() {
164 return options.toArray(new String[options.length()][]);
165 }
167 /**
168 * Packages specified on the command line.
169 */
170 public PackageDoc[] specifiedPackages() {
171 return (PackageDoc[])cmdLinePackages
172 .toArray(new PackageDocImpl[cmdLinePackages.length()]);
173 }
175 /**
176 * Classes and interfaces specified on the command line.
177 */
178 public ClassDoc[] specifiedClasses() {
179 ListBuffer<ClassDocImpl> classesToDocument = new ListBuffer<ClassDocImpl>();
180 for (ClassDocImpl cd : cmdLineClasses) {
181 cd.addAllClasses(classesToDocument, true);
182 }
183 return (ClassDoc[])classesToDocument.toArray(new ClassDocImpl[classesToDocument.length()]);
184 }
186 /**
187 * Return all classes and interfaces (including those inside
188 * packages) to be documented.
189 */
190 public ClassDoc[] classes() {
191 ListBuffer<ClassDocImpl> classesToDocument = new ListBuffer<ClassDocImpl>();
192 for (ClassDocImpl cd : cmdLineClasses) {
193 cd.addAllClasses(classesToDocument, true);
194 }
195 for (PackageDocImpl pd : cmdLinePackages) {
196 pd.addAllClassesTo(classesToDocument);
197 }
198 return classesToDocument.toArray(new ClassDocImpl[classesToDocument.length()]);
199 }
201 /**
202 * Return a ClassDoc for the specified class/interface name
203 *
204 * @param qualifiedName qualified class name
205 * (i.e. includes package name).
206 *
207 * @return a ClassDocImpl holding the specified class, null if
208 * this class is not referenced.
209 */
210 public ClassDoc classNamed(String qualifiedName) {
211 return env.lookupClass(qualifiedName);
212 }
214 /**
215 * Return a PackageDoc for the specified package name
216 *
217 * @param name package name
218 *
219 * @return a PackageDoc holding the specified package, null if
220 * this package is not referenced.
221 */
222 public PackageDoc packageNamed(String name) {
223 return env.lookupPackage(name);
224 }
226 /**
227 * Return the name of this Doc item.
228 *
229 * @return the string <code>"*RootDocImpl*"</code>.
230 */
231 public String name() {
232 return "*RootDocImpl*";
233 }
235 /**
236 * Return the name of this Doc item.
237 *
238 * @return the string <code>"*RootDocImpl*"</code>.
239 */
240 public String qualifiedName() {
241 return "*RootDocImpl*";
242 }
244 /**
245 * Return true if this Doc is include in the active set.
246 * RootDocImpl isn't even a program entity so it is always false.
247 */
248 public boolean isIncluded() {
249 return false;
250 }
252 /**
253 * Print error message, increment error count.
254 *
255 * @param msg message to print
256 */
257 public void printError(String msg) {
258 env.printError(msg);
259 }
261 /**
262 * Print error message, increment error count.
263 *
264 * @param msg message to print
265 */
266 public void printError(SourcePosition pos, String msg) {
267 env.printError(pos, msg);
268 }
270 /**
271 * Print warning message, increment warning count.
272 *
273 * @param msg message to print
274 */
275 public void printWarning(String msg) {
276 env.printWarning(msg);
277 }
279 /**
280 * Print warning message, increment warning count.
281 *
282 * @param msg message to print
283 */
284 public void printWarning(SourcePosition pos, String msg) {
285 env.printWarning(pos, msg);
286 }
288 /**
289 * Print a message.
290 *
291 * @param msg message to print
292 */
293 public void printNotice(String msg) {
294 env.printNotice(msg);
295 }
297 /**
298 * Print a message.
299 *
300 * @param msg message to print
301 */
302 public void printNotice(SourcePosition pos, String msg) {
303 env.printNotice(pos, msg);
304 }
306 /**
307 * Return the path of the overview file and null if it does not exist.
308 * @return the path of the overview file and null if it does not exist.
309 */
310 private JavaFileObject getOverviewPath() {
311 for (String[] opt : options) {
312 if (opt[0].equals("-overview")) {
313 if (env.fileManager instanceof StandardJavaFileManager) {
314 StandardJavaFileManager fm = (StandardJavaFileManager) env.fileManager;
315 return fm.getJavaFileObjects(opt[1]).iterator().next();
316 }
317 }
318 }
319 return null;
320 }
322 /**
323 * Do lazy initialization of "documentation" string.
324 */
325 protected String documentation() {
326 if (documentation == null) {
327 int cnt = options.length();
328 JavaFileObject overviewPath = getOverviewPath();
329 if (overviewPath == null) {
330 // no doc file to be had
331 documentation = "";
332 } else {
333 // read from file
334 try {
335 documentation = readHTMLDocumentation(
336 overviewPath.openInputStream(),
337 overviewPath);
338 } catch (IOException exc) {
339 documentation = "";
340 env.error(null, "javadoc.File_Read_Error", overviewPath.getName());
341 }
342 }
343 }
344 return documentation;
345 }
347 /**
348 * Return the source position of the entity, or null if
349 * no position is available.
350 */
351 public SourcePosition position() {
352 JavaFileObject path;
353 return ((path = getOverviewPath()) == null) ?
354 null :
355 SourcePositionImpl.make(path, Position.NOPOS, null);
356 }
358 /**
359 * Return the locale provided by the user or the default locale value.
360 */
361 public Locale getLocale() {
362 return env.doclocale.locale;
363 }
364 }