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
017package org.jetbrains.jet.lang.resolve.java.scope;
018
019import org.jetbrains.annotations.NotNull;
020import org.jetbrains.annotations.Nullable;
021import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
022import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor;
023import org.jetbrains.jet.lang.resolve.java.JavaSemanticServices;
024import org.jetbrains.jet.lang.resolve.java.provider.ClassPsiDeclarationProvider;
025
026import java.util.Collection;
027
028public final class JavaClassNonStaticMembersScope extends JavaClassMembersScope {
029
030    private Collection<ConstructorDescriptor> constructors = null;
031    private ConstructorDescriptor primaryConstructor = null;
032    @NotNull
033    private final ClassDescriptor descriptor;
034
035    public JavaClassNonStaticMembersScope(
036            @NotNull ClassDescriptor descriptor,
037            @NotNull ClassPsiDeclarationProvider psiDeclarationProvider,
038            @NotNull JavaSemanticServices semanticServices
039    ) {
040        super(descriptor, psiDeclarationProvider, semanticServices);
041        this.descriptor = descriptor;
042    }
043
044
045    @NotNull
046    public Collection<ConstructorDescriptor> getConstructors() {
047        initConstructorsIfNeeded();
048        return constructors;
049    }
050
051    @Nullable
052    public ConstructorDescriptor getPrimaryConstructor() {
053        initConstructorsIfNeeded();
054        return primaryConstructor;
055    }
056
057    private void initConstructorsIfNeeded() {
058        if (constructors == null) {
059            constructors = getResolver().resolveConstructors(declarationProvider, descriptor);
060
061            for (ConstructorDescriptor constructor : constructors) {
062                if (constructor.isPrimary()) {
063                    if (primaryConstructor != null) {
064                        throw new IllegalStateException(
065                                "Class has more than one primary constructor: " + primaryConstructor + "\n" + constructor);
066                    }
067                    primaryConstructor = constructor;
068                }
069            }
070        }
071    }
072}