001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.renderer;
018    
019    import kotlin.KotlinPackage;
020    import kotlin.jvm.functions.Function1;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.kotlin.name.FqName;
023    import org.jetbrains.kotlin.types.JetType;
024    
025    import java.util.Collection;
026    import java.util.Collections;
027    import java.util.EnumSet;
028    import java.util.Set;
029    
030    public class DescriptorRendererBuilder {
031        private NameShortness nameShortness = NameShortness.SOURCE_CODE_QUALIFIED;
032        private boolean withDefinedIn = true;
033        private Set<DescriptorRenderer.Modifier> modifiers = EnumSet.allOf(DescriptorRenderer.Modifier.class);
034        private boolean startFromName = false;
035        private boolean debugMode = false;
036        private boolean classWithPrimaryConstructor = false;
037        private boolean verbose = false;
038        private boolean unitReturnType = true;
039        private boolean normalizedVisibilities = false;
040        private boolean showInternalKeyword = true;
041        private boolean prettyFunctionTypes = true;
042        private boolean uninferredTypeParameterAsName = false;
043        private boolean includePropertyConstant = false;
044        private boolean withoutTypeParameters = false;
045        private boolean withoutSuperTypes = false;
046        private Function1<JetType, JetType> typeNormalizer = new Function1<JetType, JetType>() {
047            @Override
048            public JetType invoke(JetType type) {
049                return type;
050            }
051        };
052        private boolean renderDefaultValues = true;
053        private boolean flexibleTypesForCode = false;
054        private boolean secondaryConstructorsAsPrimary = true;
055        private DescriptorRenderer.OverrideRenderingPolicy overrideRenderingPolicy = DescriptorRenderer.OverrideRenderingPolicy.RENDER_OPEN;
056        private DescriptorRenderer.ValueParametersHandler valueParametersHandler = new DescriptorRenderer.DefaultValueParameterHandler();
057        private DescriptorRenderer.TextFormat textFormat = DescriptorRenderer.TextFormat.PLAIN;
058        private DescriptorRenderer.ParameterNameRenderingPolicy parameterNameRenderingPolicy = DescriptorRenderer.ParameterNameRenderingPolicy.ALL;
059        private Collection<FqName> excludedAnnotationClasses = Collections.emptyList();
060        private boolean receiverAfterName = false;
061        private boolean renderCompanionObjectName = false;
062        private boolean renderAccessors = false;
063    
064        // See JvmAnnotationNames#ANNOTATIONS_COPIED_TO_TYPES
065        private Collection<FqName> excludedTypeAnnotationClasses = KotlinPackage.setOf(
066                new FqName("org.jetbrains.annotations.ReadOnly"),
067                new FqName("org.jetbrains.annotations.Mutable"),
068                new FqName("org.jetbrains.annotations.NotNull"),
069                new FqName("org.jetbrains.annotations.Nullable")
070        );
071    
072        public DescriptorRendererBuilder() {
073        }
074    
075        @NotNull
076        public DescriptorRendererBuilder setNameShortness(NameShortness shortness) {
077            this.nameShortness = shortness;
078            return this;
079        }
080    
081        @NotNull
082        public DescriptorRendererBuilder setWithDefinedIn(boolean withDefinedIn) {
083            this.withDefinedIn = withDefinedIn;
084            return this;
085        }
086    
087        @NotNull
088        public DescriptorRendererBuilder setModifiers(Set<DescriptorRenderer.Modifier> modifiers) {
089            this.modifiers = modifiers;
090            return this;
091        }
092    
093        @NotNull
094        public DescriptorRendererBuilder setModifiers(DescriptorRenderer.Modifier... modifiers) {
095            return setModifiers(KotlinPackage.setOf(modifiers));
096        }
097    
098        @NotNull
099        public DescriptorRendererBuilder setStartFromName(boolean startFromName) {
100            this.startFromName = startFromName;
101            return this;
102        }
103    
104        @NotNull
105        public DescriptorRendererBuilder setDebugMode(boolean debugMode) {
106            this.debugMode = debugMode;
107            return this;
108        }
109    
110        @NotNull
111        public DescriptorRendererBuilder setClassWithPrimaryConstructor(boolean classWithPrimaryConstructor) {
112            this.classWithPrimaryConstructor = classWithPrimaryConstructor;
113            return this;
114        }
115    
116        @NotNull
117        public DescriptorRendererBuilder setVerbose(boolean verbose) {
118            this.verbose = verbose;
119            return this;
120        }
121    
122        @NotNull
123        public DescriptorRendererBuilder setUnitReturnType(boolean unitReturnType) {
124            this.unitReturnType = unitReturnType;
125            return this;
126        }
127    
128        @NotNull
129        public DescriptorRendererBuilder setNormalizedVisibilities(boolean normalizedVisibilities) {
130            this.normalizedVisibilities = normalizedVisibilities;
131            return this;
132        }
133    
134        @NotNull
135        public DescriptorRendererBuilder setShowInternalKeyword(boolean showInternalKeyword) {
136            this.showInternalKeyword = showInternalKeyword;
137            return this;
138        }
139    
140        @NotNull
141        public DescriptorRendererBuilder setOverrideRenderingPolicy(@NotNull DescriptorRenderer.OverrideRenderingPolicy overrideRenderingPolicy) {
142            this.overrideRenderingPolicy = overrideRenderingPolicy;
143            return this;
144        }
145    
146        @NotNull
147        public DescriptorRendererBuilder setValueParametersHandler(@NotNull DescriptorRenderer.ValueParametersHandler valueParametersHandler) {
148            this.valueParametersHandler = valueParametersHandler;
149            return this;
150        }
151    
152        @NotNull
153        public DescriptorRendererBuilder setTextFormat(@NotNull DescriptorRenderer.TextFormat textFormat) {
154            this.textFormat = textFormat;
155            return this;
156        }
157    
158        @NotNull
159        public DescriptorRendererBuilder setExcludedAnnotationClasses(@NotNull Collection<FqName> excludedAnnotationClasses) {
160            this.excludedAnnotationClasses = excludedAnnotationClasses;
161            return this;
162        }
163    
164        @NotNull
165        public DescriptorRendererBuilder setExcludedTypeAnnotationClasses(@NotNull Collection<FqName> excludedTypeAnnotationClasses) {
166            this.excludedTypeAnnotationClasses = excludedTypeAnnotationClasses;
167            return this;
168        }
169    
170        @NotNull
171        public DescriptorRendererBuilder setPrettyFunctionTypes(boolean prettyFunctionTypes) {
172            this.prettyFunctionTypes = prettyFunctionTypes;
173            return this;
174        }
175    
176        @NotNull
177        public DescriptorRendererBuilder setUninferredTypeParameterAsName(boolean uninferredTypeParameterAsName) {
178            this.uninferredTypeParameterAsName = uninferredTypeParameterAsName;
179            return this;
180        }
181    
182        @NotNull
183        public DescriptorRendererBuilder setIncludePropertyConstant(boolean includePropertyConstant) {
184            this.includePropertyConstant = includePropertyConstant;
185            return this;
186        }
187    
188        @NotNull
189        public DescriptorRendererBuilder setParameterNameRenderingPolicy(@NotNull DescriptorRenderer.ParameterNameRenderingPolicy parameterNameRenderingPolicy) {
190            this.parameterNameRenderingPolicy = parameterNameRenderingPolicy;
191            return this;
192        }
193    
194        @NotNull
195        public DescriptorRendererBuilder setWithoutTypeParameters(boolean withoutTypeParameters) {
196            this.withoutTypeParameters = withoutTypeParameters;
197            return this;
198        }
199    
200        @NotNull
201        public DescriptorRendererBuilder setReceiverAfterName(boolean receiverAfterName) {
202            this.receiverAfterName = receiverAfterName;
203            return this;
204        }
205    
206        @NotNull
207        public DescriptorRendererBuilder setRenderCompanionObjectName(boolean renderCompanionObjectName) {
208            this.renderCompanionObjectName = renderCompanionObjectName;
209            return this;
210        }
211    
212        @NotNull
213        public DescriptorRendererBuilder setWithoutSuperTypes(boolean withoutSuperTypes) {
214            this.withoutSuperTypes = withoutSuperTypes;
215            return this;
216        }
217    
218        @NotNull
219        public DescriptorRendererBuilder setRenderDefaultValues(boolean renderDefaultValues) {
220            this.renderDefaultValues = renderDefaultValues;
221            return this;
222        }
223    
224        @NotNull
225        public DescriptorRendererBuilder setTypeNormalizer(@NotNull Function1<JetType, JetType> typeNormalizer) {
226            this.typeNormalizer = typeNormalizer;
227            return this;
228        }
229    
230        @NotNull
231        public DescriptorRendererBuilder setFlexibleTypesForCode(boolean flexibleTypesForCode) {
232            this.flexibleTypesForCode = flexibleTypesForCode;
233            return this;
234        }
235    
236        @NotNull
237        public DescriptorRendererBuilder setSecondaryConstructorsAsPrimary(boolean secondaryConstructorsAsPrimary) {
238            this.secondaryConstructorsAsPrimary = secondaryConstructorsAsPrimary;
239            return this;
240        }
241    
242        @NotNull
243        public DescriptorRendererBuilder setRenderAccessors(boolean renderAccessors) {
244            this.renderAccessors = renderAccessors;
245            return this;
246        }
247    
248        @NotNull
249        public DescriptorRenderer build() {
250            return new DescriptorRendererImpl(
251                    nameShortness, withDefinedIn, modifiers, startFromName, debugMode, classWithPrimaryConstructor, verbose, unitReturnType,
252                    normalizedVisibilities, showInternalKeyword, prettyFunctionTypes, uninferredTypeParameterAsName,
253                    overrideRenderingPolicy, valueParametersHandler, textFormat, excludedAnnotationClasses, excludedTypeAnnotationClasses,
254                    includePropertyConstant, parameterNameRenderingPolicy, withoutTypeParameters, receiverAfterName, renderCompanionObjectName,
255                    withoutSuperTypes, typeNormalizer, renderDefaultValues, flexibleTypesForCode, secondaryConstructorsAsPrimary,
256                    renderAccessors
257            );
258        }
259    
260    }