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