aboutsummaryrefslogtreecommitdiffstats
path: root/aspectj-attic/ajdoc-src/org/aspectj/tools/doclets/standard/AbstractStandard.java
blob: e490eabd6c92b6ae16e7fcfa80c3e955a2d4aaf5 (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* -*- 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.doclets.standard;

import org.aspectj.ajdoc.AspectDoc;

import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.DocErrorReporter;
import com.sun.javadoc.PackageDoc;
import com.sun.javadoc.RootDoc;
import com.sun.tools.doclets.ClassTree;
import com.sun.tools.doclets.DocletAbortException;
import com.sun.tools.doclets.HtmlDocWriter;
import com.sun.tools.doclets.IndexBuilder;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Arrays;

/**
 * An abstract allowing one to customize the writers
 * used in ajdoc.  Subclasses should define the three
 * generate methods to specify the documentation made.
 *
 * @see #preGenerationClasses()
 * @see #postGenerationClasses()
 * @see #checkClasses()
 * @author Jeff Palm
 */
public abstract class AbstractStandard 
    extends com.sun.tools.doclets.standard.Standard {

    // todo wes removed restriction, but implemented Standard as singleton via proxy
    private static int refCount = 0;
    {
        if (refCount > 0) {
            System.err.println("Warning: " + refCount + " AbstractStandard already ");
        }
        refCount++;
    }

    /**
     * The ClassTree that is available to subclasses and it gaurateed
     * to be created before pre-generating classes.
     */
    protected ClassTree classtree;
    
    protected static boolean start(AbstractStandard as,
                                   RootDoc root) throws IOException {
        try {
            as.getConfiguration().setOptions(root);
            as.startGeneration(root);
        } catch (DocletAbortException exc) {
            return false;
        }
        return true;
    }

    /**
     * Returns the types of {@link Pass}es that will
     * run before generating classes.
     *
     * @return an array of Class, where each entry
     *         is a subclass of {@link Pass}, ordered
     *         to run directly before generating the
     *         classes.
     */
    protected abstract Class[] preGenerationClasses();

    /**
     * Returns the types of {@link Pass}es that will
     * run after generating classes.
     *
     * @return an array of Class, where each entry
     *         is a subclass of {@link Pass}, ordered
     *         to run directly after generating the
     *         classes.
     */
    protected abstract Class[] postGenerationClasses();

    /**
     * Returns the types of {@link CheckPass}es that will
     * run to check the classes.
     *
     * @return an array of Class, where each entry
     *         is a subclass of {@link CheckPass}, ordered
     *         to run in order to check the classes passed
     *         into the class generation phase.
     */
    protected abstract Class[] checkClasses();

    /**
     * Return the configuration used by a subclass.  This
     * allows the subclass to specify it's own kind.
     *
     * @return a customized configuration.
     */
    public abstract ConfigurationStandard getConfiguration();

    protected ConfigurationStandard makeConfiguration() {
        return new ConfigurationStandard();
    }


    /**
     * Returns the configuration, and ensures that
     * HtmlDocWriter.configuration is of the type used by
     * this class.
     *
     * @return the current instanceof ConfigurationStandard being
     *         used and creates one if needed.  This will <b>not</b>
     *         be null.
     */
    // todo these are the heinous globals that impose one process per classloader
    public static com.sun.tools.doclets.standard.ConfigurationStandard
        configuration() {
        if (HtmlDocWriter.configuration == null ||
            !(HtmlDocWriter.configuration instanceof ConfigurationStandard)) {
            HtmlDocWriter.configuration = new ConfigurationStandard();
            //TODO: change to makeConfiguration()
        }
        return (ConfigurationStandard)HtmlDocWriter.configuration;
    }

    /**
     * Creates and returns an IndexBuilder that includes aspects.
     *
     * @param root        RootDoc to pass the new IndexBuilder.
     * @param classesOnly <code>true</code> if only classes
     *                    should be included.
     * @return            an IndexBuilder that includes aspects.
     */
    protected IndexBuilder indexBuilder(RootDoc root, boolean classesOnly) {
        class MyIndexBuilder extends IndexBuilder {
            public MyIndexBuilder(RootDoc r, boolean n) {
                super(r, n);
            }
            public MyIndexBuilder(RootDoc r, boolean n, boolean b) {
                super(r, n, b);
            }
            protected void putMembersInIndexMap(ClassDoc classdoc) {
                super.putMembersInIndexMap(classdoc);
                if (classdoc instanceof org.aspectj.ajdoc.ClassDoc) {
                    org.aspectj.ajdoc.ClassDoc cd =
                        (org.aspectj.ajdoc.ClassDoc)classdoc;
                    adjustIndexMap(cd.pointcuts());
                    if (cd instanceof AspectDoc) {
                        adjustIndexMap(((AspectDoc)cd).advice());
                    }
                }
            }
        }
        return new MyIndexBuilder(root, configuration().nodeprecated, classesOnly);
    }


    /**
     * Does the work in generating the documentation.
     * First, call all the passes return from {@link #generateCheckPasses}
     * them perform some copying.  Second build the classtree, run the
     * pre-classgeneration passes, generate the packages, generate the
     * classes, then call all the postGenerationClasses.
     *
     * @param root the root of the documentation.
     */
    protected void startGeneration(RootDoc root) throws DocletAbortException {

        if (!generateCheckPasses(getConfiguration(), root)) return;

        performCopy(getConfiguration().destdirname,
                    getConfiguration().helpfile);
        performCopy(getConfiguration().destdirname,
                    getConfiguration().stylesheetfile);

        classtree = new ClassTree(root, getConfiguration().nodeprecated);

        generatePrePasses(getConfiguration(), root);

        generatePackageCycle(getConfiguration().packages,
                             getConfiguration().createtree,
                             getConfiguration().nodeprecated);
         generateClassFiles(root, classtree);
        generatePostPasses(getConfiguration(), root);
    }

    /**
     * A class representing a single pass in the generation cycles.  It
     * does some of the dirty work for you.
     */
    public static abstract class Pass {

        /** The root available to this pass. */
        protected RootDoc root;

        /** The configuration available to this pass. */
        protected ConfigurationStandard cs;

        /** The doclet available to this pass. */
        protected AbstractStandard std;
        
        public Pass() {}

        /**
         * Returns the title of the pass for logging.
         *
         * @return the unique title of this pass.  This can
         *         be <code>null</code> to disable display.
         */
        public abstract String title();

        /**
         * Do the generation work.  All instance variables
         * are guaranteed to be set.
         */
        protected abstract void gen() throws DocletAbortException;

        /**
         * Do the actual generation if {@link #cond} returns
         * <code>true</code>.  Do some other logging, too.
         *
         * @param std  the AbstractStandard to use.
         * @param cs   the ConfigurationStandard to use.
         * @param root the RootDoc to use.
         */
        public final void generate(AbstractStandard std,
                                   ConfigurationStandard cs,
                                   RootDoc root)
            throws DocletAbortException {
            this.std = std;
            this.cs = cs;
            this.root = root;
            if (cond()) {
                String title = title();
                long start = System.currentTimeMillis();
                if (cs.log && title != null) {
                    cs.standardmessage.notice("doclet.pass_msg", title);
                }
                gen();
                if (cs.log && title != null) {
                    long stop = System.currentTimeMillis();
                    cs.standardmessage.notice("doclet.done_msg",
                                              title, (stop-start)+"");
                }
            }
        }

        /**
         * Returns whether the generation should proceed.  Override
         * this method for conditional passes.
         *
         * @return <code>true</code> is this pass shoulud proceed.
         */
        protected boolean cond() {
            return true;
        }
    }

    /**
     * A convenience class for doing checks.
     */
    public abstract static class Check extends Pass {

        /**
         * Returns the error message if check fails.
         *
         * @return error message if check fails.
         */
        protected abstract String message();

        /**
         * Returns whether check has failed or not.
         *
         * @return <code>true</code> is check fails.
         */
        protected abstract boolean cond();

        /**
         * Prints message, because we've failed and throws
         * a DocletAbortException to notify the doclet
         * that we've failed.
         */
        protected void gen() throws DocletAbortException {
            cs.standardmessage.error(message());
            throw new DocletAbortException();
        }

        /**
         * Returns null, because we don't want to be displayed.
         *
         * @return <code>null</code>.
         */
        public String title() { return null; }
    }

    /**
     * Generates the passes to run before generating the classes.
     */
    private final void generatePrePasses(ConfigurationStandard cs,
                                         RootDoc root)
        throws DocletAbortException {
        generatePasses(cs, root, preGenerationClasses());
    }

    /**
     * Generates the passes to run after generating the classes.
     */
    private final void generatePostPasses(ConfigurationStandard cs,
                                          RootDoc root)
        throws DocletAbortException {
        generatePasses(cs, root, postGenerationClasses());
    }

    /**
     * Generates the passes that run before doing anything.  These
     * passes check that it's OK to do anything.
     */
    private final boolean generateCheckPasses(ConfigurationStandard cs,
                                              RootDoc root)
        throws DocletAbortException {
        try {
            generatePasses(cs, root, checkClasses());
        } catch (DocletAbortException e) {
            return false;
        }
        return true;
    }

    /**
     * Generates passes from <code>classes</code>.  For each
     * class found in <code>classes</code> a constructor taking zero
     * or one-argument is called.  Then the generate method is
     * called on that Pass passing it <code>this</code>, the
     * configuration, and root.
     *
     * @param cs      configuration to use.
     * @param root    root we're documenting.
     * @param classes list of subtypes of {@link Pass} that
     *                will be run.
     */
    private final void generatePasses(ConfigurationStandard cs,
                                      RootDoc root,
                                      Class[] classes)
        throws DocletAbortException {
        if (classes == null) return;
        nextClass:
        for (int i = 0; i < classes.length; i++) {
            try {
                Constructor[] ctrs = classes[i].getConstructors();
            nextCtr:
                for (int j = 0; j < ctrs.length; j++) {
                    Pass pass = null;
                    if (ctrs[j].getParameterTypes().length == 0) {
                        pass = (Pass)ctrs[j].newInstance(new Object[]{});
                    } else if (ctrs[j].getParameterTypes().length == 1) {
                        pass = (Pass)ctrs[j].newInstance(new Object[]{this});
                    }
                    if (pass != null) {
                        pass.generate(this,cs,root);
                        continue nextClass;
                    }
                }
                throw new Exception("Can't create pass for class " + classes[i]);
            } catch (Exception e) {
                e.printStackTrace();
                Standard.configuration().standardmessage.
                    error("doclet.exception", e+"");
                throw new DocletAbortException();
            }
        }
    }


    /**
     * Generates the packages.
     */
    protected void generatePackageCycle(PackageDoc[] pkgs,
                                        boolean createtree,
                                        boolean nodeprecated)
        throws DocletAbortException {
        Arrays.sort(pkgs);
        for (int i = 0; i < pkgs.length; i++) {
            PackageDoc prev = i == 0 ? null : pkgs[i-1];
            PackageDoc curr = pkgs[i];
            PackageDoc next = i == pkgs.length-1 ? null : pkgs[i+1];
            generatePackages(prev, curr, next,
                             createtree, nodeprecated);
        }
    }

    /**
     * Generates a package doc for the three PackageDocs passed.
     */
    protected void generatePackages(PackageDoc prev,
                                    PackageDoc curr,
                                    PackageDoc next,
                                    boolean createtree,
                                    boolean nodeprecated)
        throws DocletAbortException {
        PackageWriter.generate(curr, prev, next);
        if (createtree) {
            PackageTreeWriter.generate(curr, prev,
                                       next, nodeprecated);
        }
        PackageFrameWriter.generate(curr);
    }

    /**
     * Generates all the classes.
     */
    protected void generateClassCycle(ClassDoc[] cs,
                                      ClassTree classtree,
                                      boolean nopackage)
        throws DocletAbortException {
        Arrays.sort(cs);
        for(int i = 0; i < cs.length; i++) {
            if (configuration().nodeprecated && 
                cs[i].tags("deprecated").length > 0) {
                continue;
            }
            ClassDoc prev = i == 0 ? null : cs[i-1];
            ClassDoc curr = cs[i];
            ClassDoc next = i == cs.length-1 ? null : cs[i+1];
            generateClasses(prev, curr, next,
                            classtree, nopackage);
        }
    }

    /**
     * Generates class docs for the three ClassDocs passed.
     */
    protected void generateClasses(ClassDoc  prev,
                                   ClassDoc  curr,
                                   ClassDoc  next,
                                   ClassTree classtree,
                                   boolean nopackage)
        throws DocletAbortException {
        ClassWriter.generate(curr, prev, next,
                             classtree, nopackage);
    }

    /**
     * Returns the delegation to {@link #configuration()}.
     */
    public static int optionLength(String option) {
        return configuration().optionLength(option);
    }

    /**
     * Returns the delegation to {@link #configuration()}.
     */
    public static boolean validOptions(String options[][], 
                                       DocErrorReporter reporter) 
        throws IOException {
        return configuration().validOptions(options, reporter);
    }
}