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