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.ClassConstructorDescriptorImpl;
024    import org.jetbrains.kotlin.name.Name;
025    import org.jetbrains.kotlin.types.KotlinType;
026    
027    import java.util.List;
028    
029    public class JavaClassConstructorDescriptor extends ClassConstructorDescriptorImpl implements JavaCallableMemberDescriptor {
030        private Boolean hasStableParameterNames = null;
031        private Boolean hasSynthesizedParameterNames = null;
032    
033        protected JavaClassConstructorDescriptor(
034                @NotNull ClassDescriptor containingDeclaration,
035                @Nullable JavaClassConstructorDescriptor 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 JavaClassConstructorDescriptor createJavaConstructor(
046                @NotNull ClassDescriptor containingDeclaration,
047                @NotNull Annotations annotations,
048                boolean isPrimary,
049                @NotNull SourceElement source
050        ) {
051            return new JavaClassConstructorDescriptor(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 JavaClassConstructorDescriptor createSubstitutedCopy(
077                @NotNull DeclarationDescriptor newOwner,
078                @Nullable FunctionDescriptor original,
079                @NotNull Kind kind,
080                @Nullable Name newName,
081                @NotNull Annotations annotations,
082                @NotNull SourceElement source
083        ) {
084            if (kind != Kind.DECLARATION && kind != Kind.SYNTHESIZED) {
085                throw new IllegalStateException(
086                        "Attempt at creating a constructor that is not a declaration: \n" +
087                        "copy from: " + this + "\n" +
088                        "newOwner: " + newOwner + "\n" +
089                        "kind: " + kind
090                );
091            }
092    
093            assert newName == null : "Attempt to rename constructor: " + this;
094    
095            JavaClassConstructorDescriptor result =
096                    createDescriptor((ClassDescriptor) newOwner, (JavaClassConstructorDescriptor) original, kind, source, annotations);
097            result.setHasStableParameterNames(hasStableParameterNames());
098            result.setHasSynthesizedParameterNames(hasSynthesizedParameterNames());
099            return result;
100        }
101    
102        @NotNull
103        protected JavaClassConstructorDescriptor createDescriptor(
104                @NotNull ClassDescriptor newOwner,
105                @Nullable JavaClassConstructorDescriptor original,
106                @NotNull Kind kind,
107                @NotNull SourceElement sourceElement,
108                @NotNull Annotations annotations
109        ) {
110            return new JavaClassConstructorDescriptor(
111                    newOwner, original, annotations, isPrimary, kind,
112                    sourceElement
113            );
114        }
115    
116        @Override
117        @NotNull
118        public JavaClassConstructorDescriptor enhance(
119                @Nullable KotlinType enhancedReceiverType,
120                @NotNull List<KotlinType> enhancedValueParametersTypes,
121                @NotNull KotlinType enhancedReturnType
122        ) {
123            JavaClassConstructorDescriptor enhanced = createSubstitutedCopy(
124                    getContainingDeclaration(), /* original = */ null, getKind(), null, getAnnotations(), getSource());
125            // We do not use doSubstitute here as in JavaMethodDescriptor.enhance because type parameters of constructor belongs to class
126            enhanced.initialize(
127                    enhancedReceiverType,
128                    getDispatchReceiverParameter(),
129                    getTypeParameters(),
130                    UtilKt.copyValueParameters(enhancedValueParametersTypes, getValueParameters(), enhanced),
131                    enhancedReturnType,
132                    getModality(),
133                    getVisibility()
134            );
135    
136            return enhanced;
137        }
138    
139    }