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 DefaultClassConstructorDescriptor extends ClassConstructorDescriptorImpl {
036 public DefaultClassConstructorDescriptor(@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, 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 boolean isInline,
061 @NotNull SourceElement sourceElement
062 ) {
063 return createSetter(propertyDescriptor, annotations, isDefault, isExternal, isInline, propertyDescriptor.getVisibility(), sourceElement);
064 }
065
066 @NotNull
067 public static PropertySetterDescriptorImpl createSetter(
068 @NotNull PropertyDescriptor propertyDescriptor,
069 @NotNull Annotations annotations,
070 boolean isDefault,
071 boolean isExternal,
072 boolean isInline,
073 @NotNull Visibility visibility,
074 @NotNull SourceElement sourceElement
075 ) {
076 PropertySetterDescriptorImpl setterDescriptor = new PropertySetterDescriptorImpl(
077 propertyDescriptor, annotations, propertyDescriptor.getModality(), visibility, isDefault, isExternal,
078 isInline, CallableMemberDescriptor.Kind.DECLARATION, null, sourceElement
079 );
080 setterDescriptor.initializeDefault();
081 return setterDescriptor;
082 }
083
084 @NotNull
085 public static PropertyGetterDescriptorImpl createDefaultGetter(
086 @NotNull PropertyDescriptor propertyDescriptor,
087 @NotNull Annotations annotations
088 ) {
089 return createGetter(propertyDescriptor, annotations, true, false, false);
090 }
091
092 @NotNull
093 public static PropertyGetterDescriptorImpl createGetter(
094 @NotNull PropertyDescriptor propertyDescriptor,
095 @NotNull Annotations annotations,
096 boolean isDefault,
097 boolean isExternal,
098 boolean isInline
099 ) {
100 return createGetter(propertyDescriptor, annotations, isDefault, isExternal, isInline, propertyDescriptor.getSource());
101 }
102
103 @NotNull
104 public static PropertyGetterDescriptorImpl createGetter(
105 @NotNull PropertyDescriptor propertyDescriptor,
106 @NotNull Annotations annotations,
107 boolean isDefault,
108 boolean isExternal,
109 boolean isInline,
110 @NotNull SourceElement sourceElement
111 ) {
112 return new PropertyGetterDescriptorImpl(
113 propertyDescriptor, annotations, propertyDescriptor.getModality(), propertyDescriptor.getVisibility(),
114 isDefault, isExternal, isInline, CallableMemberDescriptor.Kind.DECLARATION, null, sourceElement
115 );
116 }
117
118 @NotNull
119 public static ClassConstructorDescriptorImpl createPrimaryConstructorForObject(
120 @NotNull ClassDescriptor containingClass,
121 @NotNull SourceElement source
122 ) {
123 return new DefaultClassConstructorDescriptor(containingClass, source);
124 }
125
126 @NotNull
127 public static SimpleFunctionDescriptor createEnumValuesMethod(@NotNull ClassDescriptor enumClass) {
128 SimpleFunctionDescriptorImpl values =
129 SimpleFunctionDescriptorImpl.create(enumClass, Annotations.Companion.getEMPTY(), DescriptorUtils.ENUM_VALUES,
130 CallableMemberDescriptor.Kind.SYNTHESIZED, enumClass.getSource());
131 return values.initialize(null, null, Collections.<TypeParameterDescriptor>emptyList(),
132 Collections.<ValueParameterDescriptor>emptyList(),
133 getBuiltIns(enumClass).getArrayType(Variance.INVARIANT, enumClass.getDefaultType()),
134 Modality.FINAL, Visibilities.PUBLIC);
135 }
136
137 @NotNull
138 public static SimpleFunctionDescriptor createEnumValueOfMethod(@NotNull ClassDescriptor enumClass) {
139 SimpleFunctionDescriptorImpl valueOf =
140 SimpleFunctionDescriptorImpl.create(enumClass, Annotations.Companion.getEMPTY(), DescriptorUtils.ENUM_VALUE_OF,
141 CallableMemberDescriptor.Kind.SYNTHESIZED, enumClass.getSource());
142 ValueParameterDescriptor parameterDescriptor = new ValueParameterDescriptorImpl(
143 valueOf, null, 0, Annotations.Companion.getEMPTY(), Name.identifier("value"), getBuiltIns(enumClass).getStringType(),
144 /* declaresDefaultValue = */ false,
145 /* isCrossinline = */ false,
146 /* isNoinline = */ false,
147 null,
148 enumClass.getSource()
149 );
150 return valueOf.initialize(null, null, Collections.<TypeParameterDescriptor>emptyList(),
151 Collections.singletonList(parameterDescriptor), enumClass.getDefaultType(),
152 Modality.FINAL, Visibilities.PUBLIC);
153 }
154
155 @Nullable
156 public static ReceiverParameterDescriptor createExtensionReceiverParameterForCallable(
157 @NotNull CallableDescriptor owner,
158 @Nullable KotlinType receiverParameterType
159 ) {
160 return receiverParameterType == null
161 ? null
162 : new ReceiverParameterDescriptorImpl(owner, new ExtensionReceiver(owner, receiverParameterType));
163 }
164 }