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.resolve;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.descriptors.*;
022    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
023    import org.jetbrains.kotlin.descriptors.impl.*;
024    import org.jetbrains.kotlin.name.Name;
025    import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver;
026    import org.jetbrains.kotlin.types.KotlinType;
027    import org.jetbrains.kotlin.types.Variance;
028    
029    import java.util.Collections;
030    
031    import static org.jetbrains.kotlin.resolve.DescriptorUtils.getDefaultConstructorVisibility;
032    import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt.getBuiltIns;
033    
034    public class DescriptorFactory {
035        private static class DefaultConstructorDescriptor extends ConstructorDescriptorImpl {
036            public DefaultConstructorDescriptor(@NotNull ClassDescriptor containingClass, @NotNull SourceElement source) {
037                super(containingClass, null, Annotations.Companion.getEMPTY(), true, Kind.DECLARATION, source);
038                initialize(Collections.<ValueParameterDescriptor>emptyList(),
039                           getDefaultConstructorVisibility(containingClass));
040            }
041        }
042    
043        private DescriptorFactory() {
044        }
045    
046        @NotNull
047        public static PropertySetterDescriptorImpl createDefaultSetter(
048                @NotNull PropertyDescriptor propertyDescriptor,
049                @NotNull Annotations annotations
050        ) {
051            return createSetter(propertyDescriptor, annotations, true, false, propertyDescriptor.getSource());
052        }
053    
054        @NotNull
055        public static PropertySetterDescriptorImpl createSetter(
056                @NotNull PropertyDescriptor propertyDescriptor,
057                @NotNull Annotations annotations,
058                boolean isDefault,
059                boolean isExternal,
060                @NotNull SourceElement sourceElement
061        ) {
062            return createSetter(propertyDescriptor, annotations, isDefault, isExternal, propertyDescriptor.getVisibility(), sourceElement);
063        }
064    
065        @NotNull
066        public static PropertySetterDescriptorImpl createSetter(
067                @NotNull PropertyDescriptor propertyDescriptor,
068                @NotNull Annotations annotations,
069                boolean isDefault,
070                boolean isExternal,
071                @NotNull Visibility visibility,
072                @NotNull SourceElement sourceElement
073        ) {
074            PropertySetterDescriptorImpl setterDescriptor = new PropertySetterDescriptorImpl(
075                    propertyDescriptor, annotations, propertyDescriptor.getModality(), visibility, isDefault, isExternal,
076                    CallableMemberDescriptor.Kind.DECLARATION, null, sourceElement
077            );
078            setterDescriptor.initializeDefault();
079            return setterDescriptor;
080        }
081    
082        @NotNull
083        public static PropertyGetterDescriptorImpl createDefaultGetter(
084                @NotNull PropertyDescriptor propertyDescriptor,
085                @NotNull Annotations annotations
086        ) {
087            return createGetter(propertyDescriptor, annotations, true, false);
088        }
089    
090        @NotNull
091        public static PropertyGetterDescriptorImpl createGetter(
092                @NotNull PropertyDescriptor propertyDescriptor,
093                @NotNull Annotations annotations,
094                boolean isDefault,
095                boolean isExternal
096        ) {
097            return createGetter(propertyDescriptor, annotations, isDefault, isExternal, propertyDescriptor.getSource());
098        }
099    
100        @NotNull
101        public static PropertyGetterDescriptorImpl createGetter(
102                @NotNull PropertyDescriptor propertyDescriptor,
103                @NotNull Annotations annotations,
104                boolean isDefault,
105                boolean isExternal,
106                @NotNull SourceElement sourceElement
107        ) {
108            return new PropertyGetterDescriptorImpl(
109                    propertyDescriptor, annotations, propertyDescriptor.getModality(), propertyDescriptor.getVisibility(),
110                    isDefault, isExternal, CallableMemberDescriptor.Kind.DECLARATION, null, sourceElement
111            );
112        }
113    
114        @NotNull
115        public static ConstructorDescriptorImpl createPrimaryConstructorForObject(
116                @NotNull ClassDescriptor containingClass,
117                @NotNull SourceElement source
118        ) {
119            return new DefaultConstructorDescriptor(containingClass, source);
120        }
121    
122        @NotNull
123        public static SimpleFunctionDescriptor createEnumValuesMethod(@NotNull ClassDescriptor enumClass) {
124            SimpleFunctionDescriptorImpl values =
125                    SimpleFunctionDescriptorImpl.create(enumClass, Annotations.Companion.getEMPTY(), DescriptorUtils.ENUM_VALUES,
126                                                        CallableMemberDescriptor.Kind.SYNTHESIZED, enumClass.getSource());
127            return values.initialize(null, null, Collections.<TypeParameterDescriptor>emptyList(),
128                                     Collections.<ValueParameterDescriptor>emptyList(),
129                                     getBuiltIns(enumClass).getArrayType(Variance.INVARIANT, enumClass.getDefaultType()),
130                                     Modality.FINAL, Visibilities.PUBLIC);
131        }
132    
133        @NotNull
134        public static SimpleFunctionDescriptor createEnumValueOfMethod(@NotNull ClassDescriptor enumClass) {
135            SimpleFunctionDescriptorImpl valueOf =
136                    SimpleFunctionDescriptorImpl.create(enumClass, Annotations.Companion.getEMPTY(), DescriptorUtils.ENUM_VALUE_OF,
137                                                        CallableMemberDescriptor.Kind.SYNTHESIZED, enumClass.getSource());
138            ValueParameterDescriptor parameterDescriptor = new ValueParameterDescriptorImpl(
139                    valueOf, null, 0, Annotations.Companion.getEMPTY(), Name.identifier("value"), getBuiltIns(enumClass).getStringType(),
140                    /* declaresDefaultValue = */ false,
141                    /* isCrossinline = */ false,
142                    /* isNoinline = */ false,
143                    null,
144                    enumClass.getSource()
145            );
146            return valueOf.initialize(null, null, Collections.<TypeParameterDescriptor>emptyList(),
147                                      Collections.singletonList(parameterDescriptor), enumClass.getDefaultType(),
148                                      Modality.FINAL, Visibilities.PUBLIC);
149        }
150    
151        @Nullable
152        public static ReceiverParameterDescriptor createExtensionReceiverParameterForCallable(
153                @NotNull CallableDescriptor owner,
154                @Nullable KotlinType receiverParameterType
155        ) {
156            return receiverParameterType == null
157                   ? null
158                   : new ReceiverParameterDescriptorImpl(owner, new ExtensionReceiver(owner, receiverParameterType));
159        }
160    }