001    /*
002     * Copyright 2010-2013 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.jet.lang.resolve;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.descriptors.*;
022    import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
023    import org.jetbrains.jet.lang.descriptors.impl.*;
024    import org.jetbrains.jet.lang.resolve.name.Name;
025    import org.jetbrains.jet.lang.resolve.scopes.receivers.ExtensionReceiver;
026    import org.jetbrains.jet.lang.types.JetType;
027    import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
028    
029    import java.util.Collections;
030    
031    import static org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER;
032    import static org.jetbrains.jet.lang.resolve.DescriptorUtils.getDefaultConstructorVisibility;
033    
034    public class DescriptorFactory {
035        public static final Name VALUE_OF_METHOD_NAME = Name.identifier("valueOf");
036        public static final Name VALUES_METHOD_NAME = Name.identifier("values");
037    
038        private static class DefaultConstructorDescriptor extends ConstructorDescriptorImpl {
039            public DefaultConstructorDescriptor(@NotNull ClassDescriptor containingClass) {
040                super(containingClass, null, Annotations.EMPTY, true, Kind.DECLARATION);
041                initialize(Collections.<TypeParameterDescriptor>emptyList(), Collections.<ValueParameterDescriptor>emptyList(),
042                           getDefaultConstructorVisibility(containingClass), true);
043            }
044        }
045    
046        private DescriptorFactory() {
047        }
048    
049        @NotNull
050        public static PropertySetterDescriptorImpl createDefaultSetter(@NotNull PropertyDescriptor propertyDescriptor) {
051            return createSetter(propertyDescriptor, true);
052        }
053    
054        @NotNull
055        public static PropertySetterDescriptorImpl createSetter(@NotNull PropertyDescriptor propertyDescriptor, boolean isDefault) {
056            PropertySetterDescriptorImpl setterDescriptor =
057                    new PropertySetterDescriptorImpl(propertyDescriptor, Annotations.EMPTY, propertyDescriptor.getModality(),
058                                                     propertyDescriptor.getVisibility(), !isDefault, isDefault,
059                                                     CallableMemberDescriptor.Kind.DECLARATION, null);
060            setterDescriptor.initializeDefault();
061            return setterDescriptor;
062        }
063    
064        @NotNull
065        public static PropertyGetterDescriptorImpl createDefaultGetter(@NotNull PropertyDescriptor propertyDescriptor) {
066            return createGetter(propertyDescriptor, true);
067        }
068    
069        @NotNull
070        public static PropertyGetterDescriptorImpl createGetter(@NotNull PropertyDescriptor propertyDescriptor, boolean isDefault) {
071            return new PropertyGetterDescriptorImpl(propertyDescriptor, Annotations.EMPTY, propertyDescriptor.getModality(),
072                                                    propertyDescriptor.getVisibility(), !isDefault, isDefault,
073                                                    CallableMemberDescriptor.Kind.DECLARATION, null);
074        }
075    
076        @NotNull
077        public static ConstructorDescriptorImpl createPrimaryConstructorForObject(@NotNull ClassDescriptor containingClass) {
078            return new DefaultConstructorDescriptor(containingClass);
079        }
080    
081        public static boolean isDefaultPrimaryConstructor(@NotNull ConstructorDescriptor constructor) {
082            return constructor instanceof DefaultConstructorDescriptor;
083        }
084    
085        @NotNull
086        public static SimpleFunctionDescriptor createEnumClassObjectValuesMethod(
087                @NotNull ClassDescriptor classObject,
088                @NotNull JetType returnType
089        ) {
090            SimpleFunctionDescriptorImpl values =
091                    SimpleFunctionDescriptorImpl.create(classObject, Annotations.EMPTY, VALUES_METHOD_NAME,
092                                                        CallableMemberDescriptor.Kind.SYNTHESIZED);
093            return values.initialize(null, classObject.getThisAsReceiverParameter(), Collections.<TypeParameterDescriptor>emptyList(),
094                                     Collections.<ValueParameterDescriptor>emptyList(),
095                                     returnType, Modality.FINAL,
096                                     Visibilities.PUBLIC);
097        }
098    
099        @NotNull
100        public static SimpleFunctionDescriptor createEnumClassObjectValueOfMethod(
101                @NotNull ClassDescriptor classObject,
102                @NotNull JetType returnType
103        ) {
104            SimpleFunctionDescriptorImpl values =
105                    SimpleFunctionDescriptorImpl.create(classObject, Annotations.EMPTY, VALUE_OF_METHOD_NAME,
106                                                        CallableMemberDescriptor.Kind.SYNTHESIZED);
107            ValueParameterDescriptor parameterDescriptor = new ValueParameterDescriptorImpl(
108                    values,
109                    null,
110                    0,
111                    Annotations.EMPTY,
112                    Name.identifier("value"),
113                    KotlinBuiltIns.getInstance().getStringType(),
114                    false,
115                    null);
116            return values.initialize(null, classObject.getThisAsReceiverParameter(),
117                                     Collections.<TypeParameterDescriptor>emptyList(),
118                                     Collections.singletonList(parameterDescriptor),
119                                     returnType, Modality.FINAL,
120                                     Visibilities.PUBLIC);
121        }
122    
123        @Nullable
124        public static ReceiverParameterDescriptor createReceiverParameterForCallable(
125                @NotNull CallableDescriptor owner,
126                @Nullable JetType receiverParameterType
127        ) {
128            return receiverParameterType == null
129                   ? NO_RECEIVER_PARAMETER
130                   : new ReceiverParameterDescriptorImpl(owner, receiverParameterType, new ExtensionReceiver(owner, receiverParameterType));
131        }
132    }