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.ConstructorDescriptorImpl;
024 import org.jetbrains.jet.lang.descriptors.impl.PropertyGetterDescriptorImpl;
025 import org.jetbrains.jet.lang.descriptors.impl.PropertySetterDescriptorImpl;
026 import org.jetbrains.jet.lang.descriptors.impl.ReceiverParameterDescriptorImpl;
027 import org.jetbrains.jet.lang.resolve.scopes.receivers.ExtensionReceiver;
028 import org.jetbrains.jet.lang.types.JetType;
029
030 import java.util.Collections;
031
032 import static org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER;
033 import static org.jetbrains.jet.lang.resolve.DescriptorUtils.getDefaultConstructorVisibility;
034
035 public class DescriptorFactory {
036 private static class DefaultConstructorDescriptor extends ConstructorDescriptorImpl {
037 public DefaultConstructorDescriptor(@NotNull ClassDescriptor containingClass, @NotNull SourceElement source) {
038 super(containingClass, null, Annotations.EMPTY, true, Kind.DECLARATION, source);
039 initialize(Collections.<TypeParameterDescriptor>emptyList(), Collections.<ValueParameterDescriptor>emptyList(),
040 getDefaultConstructorVisibility(containingClass), true);
041 }
042 }
043
044 private DescriptorFactory() {
045 }
046
047 @NotNull
048 public static PropertySetterDescriptorImpl createDefaultSetter(@NotNull PropertyDescriptor propertyDescriptor) {
049 return createSetter(propertyDescriptor, true);
050 }
051
052 @NotNull
053 public static PropertySetterDescriptorImpl createSetter(@NotNull PropertyDescriptor propertyDescriptor, boolean isDefault) {
054 PropertySetterDescriptorImpl setterDescriptor =
055 new PropertySetterDescriptorImpl(propertyDescriptor, Annotations.EMPTY, propertyDescriptor.getModality(),
056 propertyDescriptor.getVisibility(), !isDefault, isDefault,
057 CallableMemberDescriptor.Kind.DECLARATION, null, SourceElement.NO_SOURCE);
058 setterDescriptor.initializeDefault();
059 return setterDescriptor;
060 }
061
062 @NotNull
063 public static PropertyGetterDescriptorImpl createDefaultGetter(@NotNull PropertyDescriptor propertyDescriptor) {
064 return createGetter(propertyDescriptor, true);
065 }
066
067 @NotNull
068 public static PropertyGetterDescriptorImpl createGetter(@NotNull PropertyDescriptor propertyDescriptor, boolean isDefault) {
069 return new PropertyGetterDescriptorImpl(propertyDescriptor, Annotations.EMPTY, propertyDescriptor.getModality(),
070 propertyDescriptor.getVisibility(), !isDefault, isDefault,
071 CallableMemberDescriptor.Kind.DECLARATION, null, SourceElement.NO_SOURCE);
072 }
073
074 @NotNull
075 public static ConstructorDescriptorImpl createPrimaryConstructorForObject(
076 @NotNull ClassDescriptor containingClass,
077 @NotNull SourceElement source
078 ) {
079 return new DefaultConstructorDescriptor(containingClass, source);
080 }
081
082 public static boolean isDefaultPrimaryConstructor(@NotNull ConstructorDescriptor constructor) {
083 return constructor instanceof DefaultConstructorDescriptor;
084 }
085
086 @Nullable
087 public static ReceiverParameterDescriptor createReceiverParameterForCallable(
088 @NotNull CallableDescriptor owner,
089 @Nullable JetType receiverParameterType
090 ) {
091 return receiverParameterType == null
092 ? NO_RECEIVER_PARAMETER
093 : new ReceiverParameterDescriptorImpl(owner, receiverParameterType, new ExtensionReceiver(owner, receiverParameterType));
094 }
095 }