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.JetType;
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.DescriptorUtilPackage.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.EMPTY, true, Kind.DECLARATION, source);
038                initialize(Collections.<TypeParameterDescriptor>emptyList(), 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);
052        }
053    
054        @NotNull
055        public static PropertySetterDescriptorImpl createSetter(
056                @NotNull PropertyDescriptor propertyDescriptor,
057                @NotNull Annotations annotations,
058                boolean isDefault
059        ) {
060            PropertySetterDescriptorImpl setterDescriptor =
061                    new PropertySetterDescriptorImpl(propertyDescriptor, annotations, propertyDescriptor.getModality(),
062                                                     propertyDescriptor.getVisibility(), !isDefault, isDefault,
063                                                     CallableMemberDescriptor.Kind.DECLARATION, null, propertyDescriptor.getSource());
064            setterDescriptor.initializeDefault();
065            return setterDescriptor;
066        }
067    
068        @NotNull
069        public static PropertyGetterDescriptorImpl createDefaultGetter(
070                @NotNull PropertyDescriptor propertyDescriptor,
071                @NotNull Annotations annotations
072        ) {
073            return createGetter(propertyDescriptor, annotations, true);
074        }
075    
076        @NotNull
077        public static PropertyGetterDescriptorImpl createGetter(
078                @NotNull PropertyDescriptor propertyDescriptor,
079                @NotNull Annotations annotations,
080                boolean isDefault) {
081            return new PropertyGetterDescriptorImpl(propertyDescriptor, annotations, propertyDescriptor.getModality(),
082                                                    propertyDescriptor.getVisibility(), !isDefault, isDefault,
083                                                    CallableMemberDescriptor.Kind.DECLARATION, null, propertyDescriptor.getSource());
084        }
085    
086        @NotNull
087        public static ConstructorDescriptorImpl createPrimaryConstructorForObject(
088                @NotNull ClassDescriptor containingClass,
089                @NotNull SourceElement source
090        ) {
091            return new DefaultConstructorDescriptor(containingClass, source);
092        }
093    
094        public static boolean isDefaultPrimaryConstructor(@NotNull ConstructorDescriptor constructor) {
095            return constructor instanceof DefaultConstructorDescriptor;
096        }
097    
098        @NotNull
099        public static SimpleFunctionDescriptor createEnumValuesMethod(@NotNull ClassDescriptor enumClass) {
100            SimpleFunctionDescriptorImpl values =
101                    SimpleFunctionDescriptorImpl.create(enumClass, Annotations.EMPTY, DescriptorUtils.ENUM_VALUES,
102                                                        CallableMemberDescriptor.Kind.SYNTHESIZED, enumClass.getSource());
103            return values.initialize(null, null, Collections.<TypeParameterDescriptor>emptyList(),
104                                     Collections.<ValueParameterDescriptor>emptyList(),
105                                     getBuiltIns(enumClass).getArrayType(Variance.INVARIANT, enumClass.getDefaultType()), Modality.FINAL,
106                                     Visibilities.PUBLIC);
107        }
108    
109        @NotNull
110        public static SimpleFunctionDescriptor createEnumValueOfMethod(@NotNull ClassDescriptor enumClass) {
111            SimpleFunctionDescriptorImpl valueOf =
112                    SimpleFunctionDescriptorImpl.create(enumClass, Annotations.EMPTY, DescriptorUtils.ENUM_VALUE_OF,
113                                                        CallableMemberDescriptor.Kind.SYNTHESIZED, enumClass.getSource());
114            ValueParameterDescriptor parameterDescriptor = new ValueParameterDescriptorImpl(
115                    valueOf, null, 0, Annotations.EMPTY, Name.identifier("value"), getBuiltIns(enumClass).getStringType(), false, null,
116                    enumClass.getSource()
117            );
118            return valueOf.initialize(null, null, Collections.<TypeParameterDescriptor>emptyList(),
119                                      Collections.singletonList(parameterDescriptor), enumClass.getDefaultType(), Modality.FINAL,
120                                      Visibilities.PUBLIC);
121        }
122    
123        @Nullable
124        public static ReceiverParameterDescriptor createExtensionReceiverParameterForCallable(
125                @NotNull CallableDescriptor owner,
126                @Nullable JetType receiverParameterType
127        ) {
128            return receiverParameterType == null
129                   ? null
130                   : new ReceiverParameterDescriptorImpl(owner, new ExtensionReceiver(owner, receiverParameterType));
131        }
132    }