001    /*
002     * Copyright 2010-2013 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.jet.lang.descriptors.impl;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.descriptors.*;
022    import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
023    import org.jetbrains.jet.lang.resolve.DescriptorUtils;
024    import org.jetbrains.jet.lang.resolve.name.Name;
025    
026    import java.util.Collections;
027    import java.util.List;
028    import java.util.Set;
029    
030    import static org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER;
031    
032    public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements ConstructorDescriptor {
033    
034        private final boolean isPrimary;
035    
036        private static final Name NAME = Name.special("<init>");
037    
038        public ConstructorDescriptorImpl(@NotNull ClassDescriptor containingDeclaration, @NotNull List<AnnotationDescriptor> annotations, boolean isPrimary) {
039            this(containingDeclaration, annotations, isPrimary, Kind.DECLARATION);
040        }
041    
042        public ConstructorDescriptorImpl(@NotNull ClassDescriptor containingDeclaration, @NotNull List<AnnotationDescriptor> annotations, boolean isPrimary, Kind kind) {
043            super(containingDeclaration, annotations, NAME, kind);
044            this.isPrimary = isPrimary;
045        }
046    
047        public ConstructorDescriptorImpl(@NotNull ClassDescriptor containingDeclaration, @NotNull ConstructorDescriptor original, @NotNull List<AnnotationDescriptor> annotations, boolean isPrimary) {
048            super(containingDeclaration, original, annotations, NAME, Kind.DECLARATION);
049            this.isPrimary = isPrimary;
050        }
051    
052        public ConstructorDescriptorImpl initialize(@NotNull List<TypeParameterDescriptor> typeParameters, @NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters, Visibility visibility) {
053            return initialize(typeParameters, unsubstitutedValueParameters, visibility, false);
054        }
055    
056        public ConstructorDescriptorImpl initialize(@NotNull List<TypeParameterDescriptor> typeParameters, @NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters, Visibility visibility, boolean isStatic) {
057            super.initialize(null, isStatic ? NO_RECEIVER_PARAMETER : getExpectedThisObject(getContainingDeclaration()), typeParameters, unsubstitutedValueParameters, null, Modality.FINAL, visibility);
058            return this;
059        }
060    
061        @Nullable
062        private static ReceiverParameterDescriptor getExpectedThisObject(@NotNull ClassDescriptor descriptor) {
063            DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
064            return DescriptorUtils.getExpectedThisObjectIfNeeded(containingDeclaration);
065        }
066    
067        @NotNull
068        @Override
069        public ClassDescriptor getContainingDeclaration() {
070            return (ClassDescriptor) super.getContainingDeclaration();
071        }
072    
073        @NotNull
074        @Override
075        public ConstructorDescriptor getOriginal() {
076            return (ConstructorDescriptor) super.getOriginal();
077        }
078    
079        @Override
080        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
081            return visitor.visitConstructorDescriptor(this, data);
082        }
083    
084        @Override
085        public boolean isPrimary() {
086            return isPrimary;
087        }
088    
089        @NotNull
090        @Override
091        public Set<? extends FunctionDescriptor> getOverriddenDescriptors() {
092            return Collections.emptySet();
093        }
094    
095        @Override
096        public void addOverriddenDescriptor(@NotNull CallableMemberDescriptor overriddenFunction) {
097            throw new UnsupportedOperationException("Constructors cannot override anything");
098        }
099    
100        @Override
101        protected FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal, Kind kind) {
102            if (kind != Kind.DECLARATION) {
103                throw new IllegalStateException("Attempt at creating a constructor that is not a declaration: \n" +
104                                                "copy from: " + this + "\n" +
105                                                "newOwner: " + newOwner + "\n" +
106                                                "kind: " + kind);
107            }
108            return new ConstructorDescriptorImpl(
109                    (ClassDescriptor) newOwner,
110                    this,
111                    Collections.<AnnotationDescriptor>emptyList(), // TODO
112                    isPrimary);
113        }
114    
115        @NotNull
116        @Override
117        public ConstructorDescriptor copy(DeclarationDescriptor newOwner, Modality modality, Visibility visibility, Kind kind, boolean copyOverrides) {
118            throw new UnsupportedOperationException("Constructors should not be copied for overriding");
119        }
120    }