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.builtins.KotlinBuiltIns; 022 import org.jetbrains.kotlin.descriptors.*; 023 import org.jetbrains.kotlin.descriptors.annotations.Annotations; 024 import org.jetbrains.kotlin.descriptors.impl.*; 025 import org.jetbrains.kotlin.name.Name; 026 import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver; 027 import org.jetbrains.kotlin.types.JetType; 028 import org.jetbrains.kotlin.types.Variance; 029 030 import java.util.Collections; 031 032 import static org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER; 033 import static org.jetbrains.kotlin.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)); 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 @NotNull 087 public static SimpleFunctionDescriptor createEnumValuesMethod(@NotNull ClassDescriptor enumClass) { 088 SimpleFunctionDescriptorImpl values = 089 SimpleFunctionDescriptorImpl.create(enumClass, Annotations.EMPTY, DescriptorUtils.ENUM_VALUES, 090 CallableMemberDescriptor.Kind.SYNTHESIZED, enumClass.getSource()); 091 return values.initialize(null, NO_RECEIVER_PARAMETER, Collections.<TypeParameterDescriptor>emptyList(), 092 Collections.<ValueParameterDescriptor>emptyList(), 093 KotlinBuiltIns.getInstance().getArrayType(Variance.INVARIANT, enumClass.getDefaultType()), Modality.FINAL, 094 Visibilities.PUBLIC); 095 } 096 097 @NotNull 098 public static SimpleFunctionDescriptor createEnumValueOfMethod(@NotNull ClassDescriptor enumClass) { 099 SimpleFunctionDescriptorImpl valueOf = 100 SimpleFunctionDescriptorImpl.create(enumClass, Annotations.EMPTY, DescriptorUtils.ENUM_VALUE_OF, 101 CallableMemberDescriptor.Kind.SYNTHESIZED, enumClass.getSource()); 102 ValueParameterDescriptor parameterDescriptor = new ValueParameterDescriptorImpl( 103 valueOf, null, 0, Annotations.EMPTY, Name.identifier("value"), KotlinBuiltIns.getInstance().getStringType(), false, null, 104 enumClass.getSource() 105 ); 106 return valueOf.initialize(null, NO_RECEIVER_PARAMETER, Collections.<TypeParameterDescriptor>emptyList(), 107 Collections.singletonList(parameterDescriptor), enumClass.getDefaultType(), Modality.FINAL, 108 Visibilities.PUBLIC); 109 } 110 111 @Nullable 112 public static ReceiverParameterDescriptor createExtensionReceiverParameterForCallable( 113 @NotNull CallableDescriptor owner, 114 @Nullable JetType receiverParameterType 115 ) { 116 return receiverParameterType == null 117 ? NO_RECEIVER_PARAMETER 118 : new ReceiverParameterDescriptorImpl(owner, receiverParameterType, new ExtensionReceiver(owner, receiverParameterType)); 119 } 120 }