BuilderFactory.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.javadoc.internal.doclets.toolkit.builders;
27
28import java.util.HashSet;
29import java.util.Set;
30
31import javax.lang.model.element.ModuleElement;
32import javax.lang.model.element.PackageElement;
33import javax.lang.model.element.TypeElement;
34import javax.lang.model.type.TypeMirror;
35
36import jdk.javadoc.internal.doclets.toolkit.AnnotationTypeWriter;
37import jdk.javadoc.internal.doclets.toolkit.ClassWriter;
38import jdk.javadoc.internal.doclets.toolkit.Configuration;
39import jdk.javadoc.internal.doclets.toolkit.PropertyWriter;
40import jdk.javadoc.internal.doclets.toolkit.WriterFactory;
41import jdk.javadoc.internal.doclets.toolkit.util.ClassTree;
42
43/**
44 * The factory for constructing builders.
45 *
46 *  <p><b>This is NOT part of any supported API.
47 *  If you write code that depends on this, you do so at your own risk.
48 *  This code and its internal interfaces are subject to change or
49 *  deletion without notice.</b>
50 *
51 * @author Jamie Ho
52 */
53
54public class BuilderFactory {
55
56    /**
57     * The current configuration of the doclet.
58     */
59    private final Configuration configuration;
60
61    /**
62     * The factory to retrieve the required writers from.
63     */
64    private final WriterFactory writerFactory;
65
66    private final AbstractBuilder.Context context;
67
68    /**
69     * Construct a builder factory using the given configuration.
70     * @param configuration the configuration for the current doclet
71     * being executed.
72     */
73    public BuilderFactory (Configuration configuration) {
74        this.configuration = configuration;
75        this.writerFactory = configuration.getWriterFactory();
76
77        Set<PackageElement> containingPackagesSeen = new HashSet<>();
78        context = new AbstractBuilder.Context(configuration, containingPackagesSeen,
79                LayoutParser.getInstance(configuration));
80    }
81
82    /**
83     * Return the builder that builds the constant summary.
84     * @return the builder that builds the constant summary.
85     */
86    public AbstractBuilder getConstantsSummaryBuilder() throws Exception {
87        return ConstantsSummaryBuilder.getInstance(context,
88            writerFactory.getConstantsSummaryWriter());
89    }
90
91    /**
92     * Return the builder that builds the package summary.
93     *
94     * @param pkg the package being documented.
95     * @param prevPkg the previous package being documented.
96     * @param nextPkg the next package being documented.
97     * @return the builder that builds the constant summary.
98     */
99    public AbstractBuilder getPackageSummaryBuilder(PackageElement pkg, PackageElement prevPkg,
100            PackageElement nextPkg) throws Exception {
101        return PackageSummaryBuilder.getInstance(context, pkg,
102            writerFactory.getPackageSummaryWriter(pkg, prevPkg, nextPkg));
103    }
104
105    /**
106     * Return the builder that builds the module summary.
107     *
108     * @param mdle the module being documented.
109     * @param prevModule the previous module being documented.
110     * @param nextModule the next module being documented.
111     * @return the builder that builds the module summary.
112     */
113    public AbstractBuilder getModuleSummaryBuilder(ModuleElement mdle, ModuleElement prevModule,
114            ModuleElement nextModule) throws Exception {
115        return ModuleSummaryBuilder.getInstance(context, mdle,
116            writerFactory.getModuleSummaryWriter(mdle, prevModule, nextModule));
117    }
118
119    /**
120     * Return the builder for the class.
121     *
122     * @param typeElement the class being documented.
123     * @param prevClass the previous class that was documented.
124     * @param nextClass the next class being documented.
125     * @param classTree the class tree.
126     * @return the writer for the class.  Return null if this
127     * writer is not supported by the doclet.
128     */
129    public AbstractBuilder getClassBuilder(TypeElement typeElement,
130            TypeElement prevClass, TypeElement nextClass, ClassTree classTree)
131            throws Exception {
132        return ClassBuilder.getInstance(context, typeElement,
133            writerFactory.getClassWriter(typeElement, prevClass, nextClass, classTree));
134    }
135
136    /**
137     * Return the builder for the annotation type.
138     *
139     * @param annotationType the annotation type being documented.
140     * @param prevType the previous type that was documented.
141     * @param nextType the next type being documented.
142     * @return the writer for the annotation type.  Return null if this
143     * writer is not supported by the doclet.
144     */
145    public AbstractBuilder getAnnotationTypeBuilder(
146        TypeElement annotationType, TypeMirror prevType, TypeMirror nextType)
147            throws Exception {
148        return AnnotationTypeBuilder.getInstance(context, annotationType,
149            writerFactory.getAnnotationTypeWriter(annotationType, prevType, nextType));
150    }
151
152    /**
153     * Return an instance of the method builder for the given class.
154     *
155     * @return an instance of the method builder for the given class.
156     */
157    public AbstractBuilder getMethodBuilder(ClassWriter classWriter)
158           throws Exception {
159        return MethodBuilder.getInstance(context, classWriter.getTypeElement(),
160            writerFactory.getMethodWriter(classWriter));
161    }
162
163    /**
164     * Return an instance of the annotation type fields builder for the given
165     * class.
166     *
167     * @return an instance of the annotation type field builder for the given
168     *         annotation type.
169     */
170    public AbstractBuilder getAnnotationTypeFieldsBuilder(
171            AnnotationTypeWriter annotationTypeWriter)
172    throws Exception {
173        return AnnotationTypeFieldBuilder.getInstance(context,
174                annotationTypeWriter.getAnnotationTypeElement(),
175                writerFactory.getAnnotationTypeFieldWriter(annotationTypeWriter));
176    }
177
178    /**
179     * Return an instance of the annotation type member builder for the given
180     * class.
181     *
182     * @return an instance of the annotation type member builder for the given
183     *         annotation type.
184     */
185    public AbstractBuilder getAnnotationTypeOptionalMemberBuilder(
186            AnnotationTypeWriter annotationTypeWriter)
187    throws Exception {
188        return AnnotationTypeOptionalMemberBuilder.getInstance(context,
189            annotationTypeWriter.getAnnotationTypeElement(),
190            writerFactory.getAnnotationTypeOptionalMemberWriter(annotationTypeWriter));
191    }
192
193    /**
194     * Return an instance of the annotation type member builder for the given
195     * class.
196     *
197     * @return an instance of the annotation type member builder for the given
198     *         annotation type.
199     */
200    public AbstractBuilder getAnnotationTypeRequiredMemberBuilder(
201            AnnotationTypeWriter annotationTypeWriter)
202    throws Exception {
203        return AnnotationTypeRequiredMemberBuilder.getInstance(context,
204            annotationTypeWriter.getAnnotationTypeElement(),
205            writerFactory.getAnnotationTypeRequiredMemberWriter(annotationTypeWriter));
206    }
207
208    /**
209     * Return an instance of the enum constants builder for the given class.
210     *
211     * @return an instance of the enum constants builder for the given class.
212     */
213    public AbstractBuilder getEnumConstantsBuilder(ClassWriter classWriter)
214            throws Exception {
215        return EnumConstantBuilder.getInstance(context, classWriter.getTypeElement(),
216                writerFactory.getEnumConstantWriter(classWriter));
217    }
218
219    /**
220     * Return an instance of the field builder for the given class.
221     *
222     * @return an instance of the field builder for the given class.
223     */
224    public AbstractBuilder getFieldBuilder(ClassWriter classWriter) throws Exception {
225        return FieldBuilder.getInstance(context, classWriter.getTypeElement(),
226            writerFactory.getFieldWriter(classWriter));
227    }
228
229    /**
230     * Return an instance of the property builder for the given class.
231     *
232     * @return an instance of the field builder for the given class.
233     */
234    public AbstractBuilder getPropertyBuilder(ClassWriter classWriter) throws Exception {
235        final PropertyWriter propertyWriter =
236                writerFactory.getPropertyWriter(classWriter);
237        return PropertyBuilder.getInstance(context,
238                                           classWriter.getTypeElement(),
239                                           propertyWriter);
240    }
241
242    /**
243     * Return an instance of the constructor builder for the given class.
244     *
245     * @return an instance of the constructor builder for the given class.
246     */
247    public AbstractBuilder getConstructorBuilder(ClassWriter classWriter)
248            throws Exception {
249        return ConstructorBuilder.getInstance(context, classWriter.getTypeElement(),
250            writerFactory.getConstructorWriter(classWriter));
251    }
252
253    /**
254     * Return an instance of the member summary builder for the given class.
255     *
256     * @return an instance of the member summary builder for the given class.
257     */
258    public AbstractBuilder getMemberSummaryBuilder(ClassWriter classWriter)
259            throws Exception {
260        return MemberSummaryBuilder.getInstance(classWriter, context);
261    }
262
263    /**
264     * Return an instance of the member summary builder for the given annotation
265     * type.
266     *
267     * @return an instance of the member summary builder for the given
268     *         annotation type.
269     */
270    public AbstractBuilder getMemberSummaryBuilder(
271            AnnotationTypeWriter annotationTypeWriter)
272    throws Exception {
273        return MemberSummaryBuilder.getInstance(annotationTypeWriter, context);
274    }
275
276    /**
277     * Return the builder that builds the serialized form.
278     *
279     * @return the builder that builds the serialized form.
280     */
281    public AbstractBuilder getSerializedFormBuilder()
282            throws Exception {
283        return SerializedFormBuilder.getInstance(context);
284    }
285}
286