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