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 = new JavaConstructorDescriptor(
095                    (ClassDescriptor) newOwner, this, getAnnotations(), isPrimary, kind, getSourceToUseForCopy(preserveSource, original)
096            );
097            result.setHasStableParameterNames(hasStableParameterNames());
098            result.setHasSynthesizedParameterNames(hasSynthesizedParameterNames());
099            return result;
100        }
101    
102        @Override
103        @NotNull
104        public JavaConstructorDescriptor enhance(
105                @Nullable KotlinType enhancedReceiverType,
106                @NotNull List<KotlinType> enhancedValueParametersTypes,
107                @NotNull KotlinType enhancedReturnType
108        ) {
109            JavaConstructorDescriptor enhanced = createSubstitutedCopy(getContainingDeclaration(), getOriginal(), getKind(), null, false);
110            // We do not use doSubstitute here as in JavaMethodDescriptor.enhance because type parameters of constructor belongs to class
111            enhanced.initialize(
112                    enhancedReceiverType,
113                    getDispatchReceiverParameter(),
114                    getTypeParameters(),
115                    UtilKt.copyValueParameters(enhancedValueParametersTypes, getValueParameters(), enhanced),
116                    enhancedReturnType,
117                    getModality(),
118                    getVisibility()
119            );
120    
121            return enhanced;
122        }
123    
124    }