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.load.java.descriptors;
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.ConstructorDescriptorImpl;
024 import org.jetbrains.kotlin.types.JetType;
025
026 import java.util.List;
027
028 public class JavaConstructorDescriptor extends ConstructorDescriptorImpl implements JavaCallableMemberDescriptor {
029 private Boolean hasStableParameterNames = null;
030 private Boolean hasSynthesizedParameterNames = null;
031
032 protected JavaConstructorDescriptor(
033 @NotNull ClassDescriptor containingDeclaration,
034 @Nullable JavaConstructorDescriptor original,
035 @NotNull Annotations annotations,
036 boolean isPrimary,
037 @NotNull Kind kind,
038 @NotNull SourceElement source
039 ) {
040 super(containingDeclaration, original, annotations, isPrimary, kind, source);
041 }
042
043 @NotNull
044 public static JavaConstructorDescriptor createJavaConstructor(
045 @NotNull ClassDescriptor containingDeclaration,
046 @NotNull Annotations annotations,
047 boolean isPrimary,
048 @NotNull SourceElement source
049 ) {
050 return new JavaConstructorDescriptor(containingDeclaration, null, annotations, isPrimary, Kind.DECLARATION, source);
051 }
052
053 @Override
054 public boolean hasStableParameterNames() {
055 assert hasStableParameterNames != null : "hasStableParameterNames was not set: " + this;
056 return hasStableParameterNames;
057 }
058
059 public void setHasStableParameterNames(boolean hasStableParameterNames) {
060 this.hasStableParameterNames = hasStableParameterNames;
061 }
062
063 @Override
064 public boolean hasSynthesizedParameterNames() {
065 assert hasSynthesizedParameterNames != null : "hasSynthesizedParameterNames was not set: " + this;
066 return hasSynthesizedParameterNames;
067 }
068
069 public void setHasSynthesizedParameterNames(boolean hasSynthesizedParameterNames) {
070 this.hasSynthesizedParameterNames = hasSynthesizedParameterNames;
071 }
072
073 @NotNull
074 @Override
075 protected JavaConstructorDescriptor createSubstitutedCopy(
076 @NotNull DeclarationDescriptor newOwner,
077 @Nullable FunctionDescriptor original,
078 @NotNull Kind kind
079 ) {
080 if (kind != Kind.DECLARATION && kind != Kind.SYNTHESIZED) {
081 throw new IllegalStateException(
082 "Attempt at creating a constructor that is not a declaration: \n" +
083 "copy from: " + this + "\n" +
084 "newOwner: " + newOwner + "\n" +
085 "kind: " + kind
086 );
087 }
088 JavaConstructorDescriptor result = new JavaConstructorDescriptor(
089 (ClassDescriptor) newOwner, this, getAnnotations(), isPrimary, kind, SourceElement.NO_SOURCE
090 );
091 result.setHasStableParameterNames(hasStableParameterNames());
092 result.setHasSynthesizedParameterNames(hasSynthesizedParameterNames());
093 return result;
094 }
095
096 @Override
097 @NotNull
098 public JavaConstructorDescriptor enhance(
099 @Nullable JetType enhancedReceiverType,
100 @NotNull List<JetType> enhancedValueParametersTypes,
101 @NotNull JetType enhancedReturnType
102 ) {
103 JavaConstructorDescriptor enhanced = createSubstitutedCopy(getContainingDeclaration(), getOriginal(), getKind());
104 // We do not use doSubstitute here as in JavaMethodDescriptor.enhance because type parameters of constructor belongs to class
105 enhanced.initialize(
106 enhancedReceiverType,
107 getDispatchReceiverParameter(),
108 getTypeParameters(),
109 DescriptorsPackage.createEnhancedValueParameters(enhancedValueParametersTypes, getValueParameters(), enhanced),
110 enhancedReturnType,
111 getModality(),
112 getVisibility(),
113 false
114 );
115
116 return enhanced;
117 }
118
119 }