1 /*
2  * Copyright (c) 2003, 2020, 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 
26 package jdk.javadoc.internal.doclets.toolkit.builders;
27 
28 import java.util.HashSet;
29 import java.util.Set;
30 
31 import javax.lang.model.element.ModuleElement;
32 import javax.lang.model.element.PackageElement;
33 import javax.lang.model.element.TypeElement;
34 
35 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
36 import jdk.javadoc.internal.doclets.toolkit.ClassWriter;
37 import jdk.javadoc.internal.doclets.toolkit.PropertyWriter;
38 import jdk.javadoc.internal.doclets.toolkit.WriterFactory;
39 import jdk.javadoc.internal.doclets.toolkit.util.ClassTree;
40 
41 
42 
43 
44 /**
45  * The factory for constructing builders.
46  *
47  *  <p><b>This is NOT part of any supported API.
48  *  If you write code that depends on this, you do so at your own risk.
49  *  This code and its internal interfaces are subject to change or
50  *  deletion without notice.</b>
51  */
52 
53 public class BuilderFactory {
54 
55     /**
56      * The factory to retrieve the required writers from.
57      */
58     private final WriterFactory writerFactory;
59 
60     private final AbstractBuilder.Context context;
61 
62     /**
63      * Construct a builder factory using the given configuration.
64      * @param configuration the configuration for the current doclet
65      * being executed.
66      */
BuilderFactory(BaseConfiguration configuration)67     public BuilderFactory (BaseConfiguration configuration) {
68         this.writerFactory = configuration.getWriterFactory();
69 
70         Set<PackageElement> containingPackagesSeen = new HashSet<>();
71         context = new AbstractBuilder.Context(configuration, containingPackagesSeen);
72     }
73 
74     /**
75      * Return the builder that builds the constant summary.
76      * @return the builder that builds the constant summary.
77      */
getConstantsSummaryBuilder()78     public AbstractBuilder getConstantsSummaryBuilder() {
79         return ConstantsSummaryBuilder.getInstance(context);
80     }
81 
82     /**
83      * Return the builder that builds the package summary.
84      *
85      * @param pkg the package being documented.
86      * @return the builder that builds the package summary.
87      */
getPackageSummaryBuilder(PackageElement pkg)88     public AbstractBuilder getPackageSummaryBuilder(PackageElement pkg) {
89         return PackageSummaryBuilder.getInstance(context, pkg,
90             writerFactory.getPackageSummaryWriter(pkg));
91     }
92 
93     /**
94      * Return the builder that builds the module summary.
95      *
96      * @param mdle the module being documented.
97      * @return the builder that builds the module summary.
98      */
getModuleSummaryBuilder(ModuleElement mdle)99     public AbstractBuilder getModuleSummaryBuilder(ModuleElement mdle) {
100         return ModuleSummaryBuilder.getInstance(context, mdle,
101             writerFactory.getModuleSummaryWriter(mdle));
102     }
103 
104     /**
105      * Return the builder for the class.
106      *
107      * @param typeElement the class being documented.
108      * @param classTree the class tree.
109      * @return the writer for the class.  Return null if this
110      * writer is not supported by the doclet.
111      */
getClassBuilder(TypeElement typeElement, ClassTree classTree)112     public AbstractBuilder getClassBuilder(TypeElement typeElement, ClassTree classTree) {
113         return ClassBuilder.getInstance(context, typeElement,
114             writerFactory.getClassWriter(typeElement, classTree));
115     }
116 
117     /**
118      * Return an instance of the method builder for the given class.
119      *
120      * @param classWriter the writer for the enclosing class
121      * @return an instance of the method builder for the given class.
122      */
getMethodBuilder(ClassWriter classWriter)123     public AbstractMemberBuilder getMethodBuilder(ClassWriter classWriter) {
124         return MethodBuilder.getInstance(context, classWriter.getTypeElement(),
125             writerFactory.getMethodWriter(classWriter));
126     }
127 
128     /**
129      * Return an instance of the annotation type member builder for the given
130      * class.
131      *
132      * @param classWriter the writer for the enclosing annotation type
133      * @return an instance of the annotation type member builder for the given
134      *         annotation type.
135      */
getAnnotationTypeOptionalMemberBuilder( ClassWriter classWriter)136     public AbstractMemberBuilder getAnnotationTypeOptionalMemberBuilder(
137             ClassWriter classWriter) {
138         return AnnotationTypeOptionalMemberBuilder.getInstance(context,
139             classWriter.getTypeElement(),
140             writerFactory.getAnnotationTypeOptionalMemberWriter(classWriter));
141     }
142 
143     /**
144      * Return an instance of the annotation type member builder for the given
145      * class.
146      *
147      * @param classWriter the writer for the enclosing annotation type
148      * @return an instance of the annotation type member builder for the given
149      *         annotation type.
150      */
getAnnotationTypeRequiredMemberBuilder( ClassWriter classWriter)151     public AbstractMemberBuilder getAnnotationTypeRequiredMemberBuilder(
152             ClassWriter classWriter) {
153         return AnnotationTypeRequiredMemberBuilder.getInstance(context,
154             classWriter.getTypeElement(),
155             writerFactory.getAnnotationTypeRequiredMemberWriter(classWriter));
156     }
157 
158     /**
159      * Return an instance of the enum constants builder for the given class.
160      *
161      * @param classWriter the writer for the enclosing class
162      * @return an instance of the enum constants builder for the given class.
163      */
getEnumConstantsBuilder(ClassWriter classWriter)164     public AbstractMemberBuilder getEnumConstantsBuilder(ClassWriter classWriter) {
165         return EnumConstantBuilder.getInstance(context, classWriter.getTypeElement(),
166                 writerFactory.getEnumConstantWriter(classWriter));
167     }
168 
169     /**
170      * Return an instance of the field builder for the given class.
171      *
172      * @param classWriter the writer for the enclosing class
173      * @return an instance of the field builder for the given class.
174      */
getFieldBuilder(ClassWriter classWriter)175     public AbstractMemberBuilder getFieldBuilder(ClassWriter classWriter) {
176         return FieldBuilder.getInstance(context, classWriter.getTypeElement(),
177             writerFactory.getFieldWriter(classWriter));
178     }
179 
180     /**
181      * Return an instance of the property builder for the given class.
182      *
183      * @param classWriter the writer for the enclosing class
184      * @return an instance of the field builder for the given class.
185      */
getPropertyBuilder(ClassWriter classWriter)186     public AbstractMemberBuilder getPropertyBuilder(ClassWriter classWriter) {
187         final PropertyWriter propertyWriter =
188                 writerFactory.getPropertyWriter(classWriter);
189         return PropertyBuilder.getInstance(context,
190                                            classWriter.getTypeElement(),
191                                            propertyWriter);
192     }
193 
194     /**
195      * Return an instance of the constructor builder for the given class.
196      *
197      * @param classWriter the writer for the enclosing class
198      * @return an instance of the constructor builder for the given class.
199      */
getConstructorBuilder(ClassWriter classWriter)200     public AbstractMemberBuilder getConstructorBuilder(ClassWriter classWriter) {
201         return ConstructorBuilder.getInstance(context, classWriter.getTypeElement(),
202             writerFactory.getConstructorWriter(classWriter));
203     }
204 
205     /**
206      * Return an instance of the member summary builder for the given class.
207      *
208      * @param classWriter the writer for the enclosing class
209      * @return an instance of the member summary builder for the given class.
210      */
getMemberSummaryBuilder(ClassWriter classWriter)211     public MemberSummaryBuilder getMemberSummaryBuilder(ClassWriter classWriter) {
212         return MemberSummaryBuilder.getInstance(classWriter, context);
213     }
214 
215     /**
216      * Return the builder that builds the serialized form.
217      *
218      * @return the builder that builds the serialized form.
219      */
getSerializedFormBuilder()220     public AbstractBuilder getSerializedFormBuilder() {
221         return SerializedFormBuilder.getInstance(context);
222     }
223 }
224