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.AnnotationDescriptor;
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 DescriptorFactory() {
039 }
040
041 @NotNull
042 public static PropertySetterDescriptorImpl createDefaultSetter(@NotNull PropertyDescriptor propertyDescriptor) {
043 return createSetter(propertyDescriptor, true);
044 }
045
046 @NotNull
047 public static PropertySetterDescriptorImpl createSetter(@NotNull PropertyDescriptor propertyDescriptor, boolean isDefault) {
048 PropertySetterDescriptorImpl setterDescriptor = new PropertySetterDescriptorImpl(
049 propertyDescriptor, Collections.<AnnotationDescriptor>emptyList(), propertyDescriptor.getModality(),
050 propertyDescriptor.getVisibility(),
051 !isDefault, isDefault, CallableMemberDescriptor.Kind.DECLARATION);
052 setterDescriptor.initializeDefault();
053 return setterDescriptor;
054 }
055
056 @NotNull
057 public static PropertyGetterDescriptorImpl createDefaultGetter(@NotNull PropertyDescriptor propertyDescriptor) {
058 return createGetter(propertyDescriptor, true);
059 }
060
061 @NotNull
062 public static PropertyGetterDescriptorImpl createGetter(@NotNull PropertyDescriptor propertyDescriptor, boolean isDefault) {
063 return new PropertyGetterDescriptorImpl(
064 propertyDescriptor, Collections.<AnnotationDescriptor>emptyList(), propertyDescriptor.getModality(),
065 propertyDescriptor.getVisibility(),
066 !isDefault, isDefault, CallableMemberDescriptor.Kind.DECLARATION);
067 }
068
069 @NotNull
070 public static ConstructorDescriptorImpl createPrimaryConstructorForObject(@NotNull ClassDescriptor containingClass) {
071 ConstructorDescriptorImpl constructorDescriptor =
072 new ConstructorDescriptorImpl(containingClass, Collections.<AnnotationDescriptor>emptyList(), true);
073 constructorDescriptor.initialize(Collections.<TypeParameterDescriptor>emptyList(),
074 Collections.<ValueParameterDescriptor>emptyList(),
075 getDefaultConstructorVisibility(containingClass));
076 return constructorDescriptor;
077 }
078
079 @NotNull
080 public static SimpleFunctionDescriptor createEnumClassObjectValuesMethod(
081 @NotNull ClassDescriptor classObject,
082 @NotNull JetType returnType
083 ) {
084 SimpleFunctionDescriptorImpl values =
085 new SimpleFunctionDescriptorImpl(classObject, Collections.<AnnotationDescriptor>emptyList(), VALUES_METHOD_NAME,
086 CallableMemberDescriptor.Kind.SYNTHESIZED);
087 return values.initialize(null, classObject.getThisAsReceiverParameter(), Collections.<TypeParameterDescriptor>emptyList(),
088 Collections.<ValueParameterDescriptor>emptyList(),
089 returnType, Modality.FINAL,
090 Visibilities.PUBLIC, false);
091 }
092
093 @NotNull
094 public static SimpleFunctionDescriptor createEnumClassObjectValueOfMethod(
095 @NotNull ClassDescriptor classObject,
096 @NotNull JetType returnType
097 ) {
098 SimpleFunctionDescriptorImpl values =
099 new SimpleFunctionDescriptorImpl(classObject, Collections.<AnnotationDescriptor>emptyList(), VALUE_OF_METHOD_NAME,
100 CallableMemberDescriptor.Kind.SYNTHESIZED);
101 ValueParameterDescriptor parameterDescriptor = new ValueParameterDescriptorImpl(
102 values,
103 0,
104 Collections.<AnnotationDescriptor>emptyList(),
105 Name.identifier("value"),
106 KotlinBuiltIns.getInstance().getStringType(),
107 false,
108 null);
109 return values.initialize(null, classObject.getThisAsReceiverParameter(),
110 Collections.<TypeParameterDescriptor>emptyList(),
111 Collections.singletonList(parameterDescriptor),
112 returnType, Modality.FINAL,
113 Visibilities.PUBLIC, false);
114 }
115
116 @Nullable
117 public static ReceiverParameterDescriptor createReceiverParameterForCallable(
118 @NotNull CallableDescriptor owner,
119 @Nullable JetType receiverParameterType
120 ) {
121 return receiverParameterType == null
122 ? NO_RECEIVER_PARAMETER
123 : new ReceiverParameterDescriptorImpl(owner, receiverParameterType, new ExtensionReceiver(owner, receiverParameterType));
124 }
125 }