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.resolve.java.descriptor;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.descriptors.ClassKind;
022    import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor;
023    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
024    import org.jetbrains.jet.lang.descriptors.impl.MutableClassDescriptorLite;
025    import org.jetbrains.jet.lang.resolve.java.scope.JavaClassNonStaticMembersScope;
026    import org.jetbrains.jet.lang.resolve.name.Name;
027    import org.jetbrains.jet.lang.types.JetType;
028    
029    import java.util.Collection;
030    
031    /**
032     * @see org.jetbrains.jet.lang.resolve.lazy.descriptors.LazyClassDescriptor
033     */
034    public class ClassDescriptorFromJvmBytecode extends MutableClassDescriptorLite {
035        private JetType functionTypeForSamInterface;
036        private JavaClassNonStaticMembersScope scopeForConstructorResolve;
037        private ConstructorDescriptor primaryConstructor;
038        private Collection<ConstructorDescriptor> constructors;
039    
040        public ClassDescriptorFromJvmBytecode(
041                @NotNull DeclarationDescriptor containingDeclaration,
042                @NotNull Name name,
043                @NotNull ClassKind kind,
044                boolean isInner
045        ) {
046            super(containingDeclaration, name, kind, isInner);
047        }
048    
049        @NotNull
050        @Override
051        public Collection<ConstructorDescriptor> getConstructors() {
052            assert scopeForConstructorResolve != null;
053            if (constructors == null) {
054                constructors = scopeForConstructorResolve.getConstructors();
055            }
056            return constructors;
057        }
058    
059        @Nullable
060        @Override
061        public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
062            if (primaryConstructor == null) {
063                for (ConstructorDescriptor constructor : getConstructors()) {
064                    if (constructor.isPrimary()) {
065                        if (primaryConstructor != null) {
066                            throw new IllegalStateException(
067                                    "Class has more than one primary constructor: " + primaryConstructor + "\n" + constructor);
068                        }
069                        primaryConstructor = constructor;
070                    }
071                }
072            }
073            return primaryConstructor;
074        }
075    
076        public void setScopeForConstructorResolve(@NotNull JavaClassNonStaticMembersScope scopeForConstructorResolve) {
077            this.scopeForConstructorResolve = scopeForConstructorResolve;
078        }
079    
080        @Nullable
081        public JetType getFunctionTypeForSamInterface() {
082            return functionTypeForSamInterface;
083        }
084    
085        public void setFunctionTypeForSamInterface(@NotNull JetType functionTypeForSamInterface) {
086            this.functionTypeForSamInterface = functionTypeForSamInterface;
087        }
088    }