aboutsummaryrefslogtreecommitdiffstats
path: root/aspectj-attic/ajdoc-src/org/aspectj/tools/ajdoc/RootDocImpl.java
blob: f0befd631b70fca464d4203558945b09a75bf6d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * This file is part of the debugger and core tools for the AspectJ(tm)
 * programming language; see http://aspectj.org
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is AspectJ.
 *
 * The Initial Developer of the Original Code is Xerox Corporation. Portions
 * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
 * All Rights Reserved.
 */
package org.aspectj.tools.ajdoc;

import org.aspectj.compiler.base.ast.TypeDec;
import org.aspectj.compiler.base.ast.World;

import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.PackageDoc;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * This is responsible for constituting the world
 * of specified[classes|packages] and all classes.
 * It ensures that any classes compiled are included (if appropriate)
 * but does not ensure that linked classes are.
 */
public class RootDocImpl
    extends DocImpl
    implements org.aspectj.ajdoc.RootDoc,
               Quietable {

    /** The collection of packages specified to be documented. */
    private final Set specifiedPackages;

    /** The collection of types specified to be documented. */
    private final Set specifiedClasses;

    /** The collection of packages visible in this world. */
    private final Set packages = new HashSet();

    /** The collection of classes visible in this world. */
    private final Set classes = new HashSet();

    /** The documentation options. */
    private final String[][] options;

    /** The World delegate. */
    private final World world;

    /** Determines whether items are included */
    private final AccessChecker filter;

    public RootDocImpl(World world, String[][] options,
                       Collection pkgnames, Collection classnames,
                       AccessChecker filter) {
        this.world = world;
        this.options = options;
        this.filter = (null != filter ? filter : AccessChecker.PUBLIC);
        Set set = createSpecifiedPackages(pkgnames);
        specifiedPackages = set; // modifiable to prune empty packages
        set = createSpecifiedClasses(classnames);
        specifiedClasses = Collections.unmodifiableSet(set);
        // adds all world classes and packages for classes and packages
        // addWorldTypes();  // todo re-enable as needed
        // make sure specified are added - should duplicate world
        // but should come after since packages are removed if empty
        addSpecifiedPackages();
        addSpecifiedClasses();
        setupDominatesRelations();
        ensureWorldInclusion();
    }
    /* ------------------------------------------------------------
     * Implementation of RootDoc
     * ------------------------------------------------------------
     */

    /**
     * Returns the classes visible in this world.
     *
     * @return an array of ClassDoc representing the visible
     *         classes in this world.
     */
    public ClassDoc[] classes() {
        return (ClassDoc[])classes.toArray
            (new org.aspectj.ajdoc.ClassDoc[classes.size()]);
    }
    
    /**
     * Returns a type visible in this world
     * for the name <code>className</code>.  If there is
     * no visible package, this method will return
     * <code>null</code>.
     *
     * @return an instance of ClassDoc in this world
     *         that corresponds to <code>className</code>.
     *         <code>null</code> is returned if there exists
     *         no such visible type named <code>className</code>.
     */
    public ClassDoc classNamed(String className) {
        ClassDoc[] docs = classes();
        for (int i = 0; i < docs.length; i++) {
            ClassDoc doc = docs[i];
            if (doc.name().equals(className)) {
                return doc;
            }
        }
        return null;
    }

    /**
     * Returns a package visible in this world
     * for the name <code>packageName</code>.  If there is
     * no visible package, this method will return
     * <code>null</code>.
     *
     * @return an instance of PackageDoc in this world
     *         that corresponds to <code>packageName</code>.
     *         <code>null</code> is returned if there exists
     *         no such visible package named <code>packageName</code>.
     */
    public PackageDoc packageNamed(String packageName) {
        for (Iterator i = packages.iterator(); i.hasNext();) {
            PackageDoc doc = (PackageDoc)i.next();
            if (doc.name().equals(packageName)) {
                return doc;
            }
        }
        return null;
    }

    /**
     * Returns the underlying world.
     *
     * @return an instance of World representing all
     *         the CompilationUnits.
     */
    public World world() {
        return world;
    }

    /**
     * Returns the documentation options.
     *
     * @return the documentation options.
     */
    public String[][] options() {
        return options;
    }

    /**
     * Returns the types specified to be documented.
     *
     * @return an array of ClassDoc representing the
     *         specified types.
     */
    public ClassDoc[] specifiedClasses() {
        return (ClassDoc[])specifiedClasses.toArray
            (new org.aspectj.ajdoc.ClassDoc[specifiedClasses.size()]);
    }

    /**
     * Returns the packages specified to be documented.
     *
     * @return an array of PackageDoc representing the
     *         specified packages.
     */
    public PackageDoc[] specifiedPackages() {
        return (PackageDoc[])specifiedPackages.toArray
            (new org.aspectj.ajdoc.PackageDoc[specifiedPackages.size()]);
    }


    /* ------------------------------------------------------------
     * Implementation of Quietable
     * ------------------------------------------------------------
     */

    /** <code>true</code> when notices should be printed. */
    private boolean notice = true;

    /** Supresses output notices. */
    public void quiet() { notice = false; }

    /** Allows output notices. */
    public void speak() { notice = true; }


    /* ------------------------------------------------------------
     * Implementation of DocErrReporter
     * ------------------------------------------------------------
     */

    /**
     * Prints the error message <code>msg</code> using
     * the current error handler.
     *
     * @param msg the error message.
     */
    public void printError(String msg) {
        err().printError(msg);
    }

    /**
     * Prints the notice message <code>msg</code> using
     * the current error handler.
     *
     * @param msg the notice message.
     */
    public void printNotice(String msg) {
        if (notice) err().printNotice(msg);
    }

    /**
     * Prints the warning message <code>msg</code> using
     * the current error handler.
     *
     * @param msg the warning message.
     */
    public void printWarning(String msg) {
        err().printWarning(msg);
    }


    /* ------------------------------------------------------------
     * Implementation of Doc
     * ------------------------------------------------------------
     */

    /**
     * Returns <code>null</code>.
     *
     * @return <code>null</code>.
     */
    public String name() {
        return "who knows???";
    }


    /* ------------------------------------------------------------
     * Helper methods
     * ------------------------------------------------------------
     */
    
    /**
     * Creates only PackageDocs that were included on the command
     * line, even if they are empty.  Should be used only for 
     * specifiedPackages.
     */
    private HashSet createSpecifiedPackages(Collection pkgnames) {
        HashSet result = new HashSet();
        for (Iterator i = pkgnames.iterator(); i.hasNext();) {
            String pkgname = (String)i.next();
            PackageDocImpl pkgdoc = PackageDocImpl.getPackageDoc(pkgname);
            pkgdoc.setIncluded(true);
            result.add(pkgdoc);
        }
        return result;
    }
    private void addWorldTypes() {
        for (Iterator i = world.getTypes().iterator(); i.hasNext();) {
            TypeDec td = (TypeDec)i.next();
            ClassDocImpl cd = ClassDocImpl.getInstance(td);
            addClass(cd);
            cd.setIncluded(filter.canAccess(td));
        }
    }

    /**
     * Creates only ClassDocs that were included on the command
     * line, and then only if they pass the filter.  
     * Should be used only for specifiedClasses.
     * todo: createClasses uses to use all classes if no names 
     */
    private HashSet createSpecifiedClasses(Collection classnames) {
        HashSet result = new HashSet();
        if (classnames != null) {
            for (Iterator i = classnames.iterator(); i.hasNext();) {
                String classname = (String)i.next();
                for (Iterator j = world.getTypes().iterator(); j.hasNext();) {
                    TypeDec td = (TypeDec)j.next();
                    if (filter.canAccess(td)) {
                        ClassDoc cd = ClassDocImpl.getInstance(td);
                        if (cd.qualifiedName().equals(classname)) {
                            result.add(cd);
                            // add inner classes since not specified explicitly
                            ClassDoc[] inners = cd.innerClasses(); // no cycles, right?
                            if (null != inners) {
                                for (int l = 0; l < inners.length; l++) {
                                    result.add(inners[l]); 
                                }
                            }
                            break;
                        }
                    }
                }
                // todo: warn if class specified but not in world? 
            }
        }
        return result;
    }

    private void addSpecifiedClasses() {
        for (Iterator i = new ArrayList(specifiedClasses).iterator(); i.hasNext();) {
            ClassDoc cd = (ClassDoc)i.next();
            addClass(cd);
        }
    }

    private void addSpecifiedPackages() {
        for (Iterator i = new ArrayList(specifiedPackages).iterator(); i.hasNext();) {
            PackageDoc pd = (PackageDoc)i.next();
            ClassDoc[] allClasses = pd.allClasses();
            if (allClasses.length == 0) {
                specifiedPackages.remove(pd);
            } else {
                for (int j = 0; j < allClasses.length; j++) {
                    addClass(allClasses[j]);
                }
            }
        }
    }

    /** 
     * If filter accepts this ClassDoc,
     * Add it and and inner classes to classes
     * and add package to packages.
     */
    private void addClass(ClassDoc cd) {
        if (null == cd) return;
        ClassDocImpl impl = (ClassDocImpl) cd;
        if (filter.canAccess(impl.typeDec()) 
            && (!classes.contains(impl))) {
            impl.setIncluded(true);
            classes.add(impl);
            packages.add(impl.containingPackage());
            ClassDoc[] inners = impl.innerClasses();
            for (int i = 0; i < inners.length; i++) {
                addClass(inners[i]);
            }
        } // todo: flag classes not added?
    }

    /** Read all classes to find any dominates relations */
    private void setupDominatesRelations() {
        // Find just the aspects
        List aspects = new ArrayList();
        ClassDoc[] classes = classes();
        for (int i = 0; i < classes.length; i++) {
            ClassDocImpl cd = (ClassDocImpl)classes[i];
            if (cd.isAspect()) {
                aspects.add(cd);
            }
        }

        // Iterate over the aspects, if
        for (Iterator i = aspects.iterator(); i.hasNext();) {
            AspectDocImpl aspect1 = (AspectDocImpl)i.next();
            for (Iterator j = aspects.iterator(); j.hasNext();) {
                AspectDocImpl aspect2 = (AspectDocImpl)j.next();
                if (aspect1.dominates(aspect2)) {
                    aspect1.addDominatee(aspect2);
                    aspect2.addDominator(aspect1);
                }
            }
        }
    }

    /**
     * Ensure compiled classes are included if they pass the filter
     * and excluded otherwise.
     * todo: The set of types available includes the world plus reachable
     * types from there; I would like to exclude the reachable ones,
     * but do not know how.
     */
    private void ensureWorldInclusion() {
        for (Iterator i = world.getTypes().iterator(); i.hasNext();) {
            TypeDec td = (TypeDec)i.next();
            ClassDocImpl cd = ClassDocImpl.getInstance(td);
            boolean isIncluded = cd.isIncluded();
            // todo: update to consider enclosing class privileges
            boolean shouldInclude = filter.canAccess(td);
            if (shouldInclude != isIncluded) {
                cd.setIncluded(shouldInclude);
            }
        }
    }
}