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.Function1;
020    import kotlin.KotlinPackage;
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 includeSynthesizedParameterNames = true;
045        private boolean withoutFunctionParameterNames = false;
046        private boolean withoutTypeParameters = false;
047        private boolean withoutSuperTypes = false;
048        private Function1<JetType, JetType> typeNormalizer = new Function1<JetType, JetType>() {
049            @Override
050            public JetType invoke(JetType type) {
051                return type;
052            }
053        };
054        private boolean renderDefaultValues = true;
055        private boolean flexibleTypesForCode = false;
056    
057        @NotNull
058        private DescriptorRenderer.OverrideRenderingPolicy overrideRenderingPolicy = DescriptorRenderer.OverrideRenderingPolicy.RENDER_OPEN;
059        @NotNull
060        private DescriptorRenderer.ValueParametersHandler valueParametersHandler = new DescriptorRenderer.DefaultValueParameterHandler();
061        @NotNull
062        private DescriptorRenderer.TextFormat textFormat = DescriptorRenderer.TextFormat.PLAIN;
063        @NotNull
064        private Collection<FqName> excludedAnnotationClasses = Collections.emptyList();
065        private boolean receiverAfterName = false;
066        private boolean renderClassObjectName = false;
067    
068        public DescriptorRendererBuilder() {
069        }
070    
071        @NotNull
072        public DescriptorRendererBuilder setNameShortness(NameShortness shortness) {
073            this.nameShortness = shortness;
074            return this;
075        }
076    
077        @NotNull
078        public DescriptorRendererBuilder setWithDefinedIn(boolean withDefinedIn) {
079            this.withDefinedIn = withDefinedIn;
080            return this;
081        }
082    
083        @NotNull
084        public DescriptorRendererBuilder setModifiers(Set<DescriptorRenderer.Modifier> modifiers) {
085            this.modifiers = modifiers;
086            return this;
087        }
088    
089        @NotNull
090        public DescriptorRendererBuilder setModifiers(DescriptorRenderer.Modifier... modifiers) {
091            return setModifiers(KotlinPackage.setOf(modifiers));
092        }
093    
094        @NotNull
095        public DescriptorRendererBuilder setStartFromName(boolean startFromName) {
096            this.startFromName = startFromName;
097            return this;
098        }
099    
100        @NotNull
101        public DescriptorRendererBuilder setDebugMode(boolean debugMode) {
102            this.debugMode = debugMode;
103            return this;
104        }
105    
106        @NotNull
107        public DescriptorRendererBuilder setClassWithPrimaryConstructor(boolean classWithPrimaryConstructor) {
108            this.classWithPrimaryConstructor = classWithPrimaryConstructor;
109            return this;
110        }
111    
112        @NotNull
113        public DescriptorRendererBuilder setVerbose(boolean verbose) {
114            this.verbose = verbose;
115            return this;
116        }
117    
118        @NotNull
119        public DescriptorRendererBuilder setUnitReturnType(boolean unitReturnType) {
120            this.unitReturnType = unitReturnType;
121            return this;
122        }
123    
124        @NotNull
125        public DescriptorRendererBuilder setNormalizedVisibilities(boolean normalizedVisibilities) {
126            this.normalizedVisibilities = normalizedVisibilities;
127            return this;
128        }
129    
130        @NotNull
131        public DescriptorRendererBuilder setShowInternalKeyword(boolean showInternalKeyword) {
132            this.showInternalKeyword = showInternalKeyword;
133            return this;
134        }
135    
136        @NotNull
137        public DescriptorRendererBuilder setOverrideRenderingPolicy(@NotNull DescriptorRenderer.OverrideRenderingPolicy overrideRenderingPolicy) {
138            this.overrideRenderingPolicy = overrideRenderingPolicy;
139            return this;
140        }
141    
142        @NotNull
143        public DescriptorRendererBuilder setValueParametersHandler(@NotNull DescriptorRenderer.ValueParametersHandler valueParametersHandler) {
144            this.valueParametersHandler = valueParametersHandler;
145            return this;
146        }
147    
148        @NotNull
149        public DescriptorRendererBuilder setTextFormat(@NotNull DescriptorRenderer.TextFormat textFormat) {
150            this.textFormat = textFormat;
151            return this;
152        }
153    
154        @NotNull
155        public DescriptorRendererBuilder setExcludedAnnotationClasses(@NotNull Collection<FqName> excludedAnnotationClasses) {
156            this.excludedAnnotationClasses = excludedAnnotationClasses;
157            return this;
158        }
159    
160        @NotNull
161        public DescriptorRendererBuilder setPrettyFunctionTypes(boolean prettyFunctionTypes) {
162            this.prettyFunctionTypes = prettyFunctionTypes;
163            return this;
164        }
165    
166        @NotNull
167        public DescriptorRendererBuilder setUninferredTypeParameterAsName(boolean uninferredTypeParameterAsName) {
168            this.uninferredTypeParameterAsName = uninferredTypeParameterAsName;
169            return this;
170        }
171    
172        @NotNull
173        public DescriptorRendererBuilder setIncludePropertyConstant(boolean includePropertyConstant) {
174            this.includePropertyConstant = includePropertyConstant;
175            return this;
176        }
177    
178        @NotNull
179        public DescriptorRendererBuilder setIncludeSynthesizedParameterNames(boolean includeSynthesizedParameterNames) {
180            this.includeSynthesizedParameterNames = includeSynthesizedParameterNames;
181            return this;
182        }
183    
184        @NotNull
185        public DescriptorRendererBuilder setWithoutTypeParameters(boolean withoutTypeParameters) {
186            this.withoutTypeParameters = withoutTypeParameters;
187            return this;
188        }
189    
190        @NotNull
191        public DescriptorRendererBuilder setWithoutFunctionParameterNames(boolean withoutFunctionParameterNames) {
192            this.withoutFunctionParameterNames = withoutFunctionParameterNames;
193            return this;
194        }
195    
196        @NotNull
197        public DescriptorRendererBuilder setReceiverAfterName(boolean receiverAfterName) {
198            this.receiverAfterName = receiverAfterName;
199            return this;
200        }
201    
202        @NotNull
203        public DescriptorRendererBuilder setRenderClassObjectName(boolean renderClassObjectName) {
204            this.renderClassObjectName = renderClassObjectName;
205            return this;
206        }
207    
208        @NotNull
209        public DescriptorRendererBuilder setWithoutSuperTypes(boolean withoutSuperTypes) {
210            this.withoutSuperTypes = withoutSuperTypes;
211            return this;
212        }
213    
214        public DescriptorRendererBuilder setRenderDefaultValues(boolean renderDefaultValues) {
215            this.renderDefaultValues = renderDefaultValues;
216            return this;
217        }
218    
219        @NotNull
220        public DescriptorRendererBuilder setTypeNormalizer(@NotNull Function1<JetType, JetType> typeNormalizer) {
221            this.typeNormalizer = typeNormalizer;
222            return this;
223        }
224    
225        public DescriptorRendererBuilder setFlexibleTypesForCode(boolean flexibleTypesForCode) {
226            this.flexibleTypesForCode = flexibleTypesForCode;
227            return this;
228        }
229    
230        @NotNull
231        public DescriptorRenderer build() {
232            return new DescriptorRendererImpl(
233                    nameShortness, withDefinedIn, modifiers, startFromName, debugMode, classWithPrimaryConstructor, verbose, unitReturnType,
234                    normalizedVisibilities, showInternalKeyword, prettyFunctionTypes, uninferredTypeParameterAsName,
235                    overrideRenderingPolicy, valueParametersHandler, textFormat, excludedAnnotationClasses, includePropertyConstant,
236                    includeSynthesizedParameterNames, withoutFunctionParameterNames, withoutTypeParameters, receiverAfterName,
237                    renderClassObjectName, withoutSuperTypes, typeNormalizer, renderDefaultValues, flexibleTypesForCode);
238        }
239    
240    }