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 static class DefaultConstructorDescriptor extends ConstructorDescriptorImpl {
039 public DefaultConstructorDescriptor(@NotNull ClassDescriptor containingClass) {
040 super(containingClass, Collections.<AnnotationDescriptor>emptyList(), true);
041 initialize(Collections.<TypeParameterDescriptor>emptyList(), Collections.<ValueParameterDescriptor>emptyList(),
042 getDefaultConstructorVisibility(containingClass));
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 = new PropertySetterDescriptorImpl(
057 propertyDescriptor, Collections.<AnnotationDescriptor>emptyList(), propertyDescriptor.getModality(),
058 propertyDescriptor.getVisibility(),
059 !isDefault, isDefault, CallableMemberDescriptor.Kind.DECLARATION);
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(
072 propertyDescriptor, Collections.<AnnotationDescriptor>emptyList(), propertyDescriptor.getModality(),
073 propertyDescriptor.getVisibility(),
074 !isDefault, isDefault, CallableMemberDescriptor.Kind.DECLARATION);
075 }
076
077 @NotNull
078 public static ConstructorDescriptorImpl createPrimaryConstructorForObject(@NotNull ClassDescriptor containingClass) {
079 return new DefaultConstructorDescriptor(containingClass);
080 }
081
082 public static boolean isDefaultPrimaryConstructor(@NotNull ConstructorDescriptor constructor) {
083 return constructor instanceof DefaultConstructorDescriptor;
084 }
085
086 @NotNull
087 public static SimpleFunctionDescriptor createEnumClassObjectValuesMethod(
088 @NotNull ClassDescriptor classObject,
089 @NotNull JetType returnType
090 ) {
091 SimpleFunctionDescriptorImpl values =
092 new SimpleFunctionDescriptorImpl(classObject, Collections.<AnnotationDescriptor>emptyList(), VALUES_METHOD_NAME,
093 CallableMemberDescriptor.Kind.SYNTHESIZED);
094 return values.initialize(null, classObject.getThisAsReceiverParameter(), Collections.<TypeParameterDescriptor>emptyList(),
095 Collections.<ValueParameterDescriptor>emptyList(),
096 returnType, Modality.FINAL,
097 Visibilities.PUBLIC);
098 }
099
100 @NotNull
101 public static SimpleFunctionDescriptor createEnumClassObjectValueOfMethod(
102 @NotNull ClassDescriptor classObject,
103 @NotNull JetType returnType
104 ) {
105 SimpleFunctionDescriptorImpl values =
106 new SimpleFunctionDescriptorImpl(classObject, Collections.<AnnotationDescriptor>emptyList(), VALUE_OF_METHOD_NAME,
107 CallableMemberDescriptor.Kind.SYNTHESIZED);
108 ValueParameterDescriptor parameterDescriptor = new ValueParameterDescriptorImpl(
109 values,
110 0,
111 Collections.<AnnotationDescriptor>emptyList(),
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 }