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.structure.impl;
018    
019    import com.intellij.psi.PsiClass;
020    import com.intellij.psi.PsiClassType;
021    import com.intellij.psi.PsiType;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.annotations.Nullable;
024    import org.jetbrains.jet.lang.resolve.java.structure.JavaClassifier;
025    import org.jetbrains.jet.lang.resolve.java.structure.JavaClassifierType;
026    import org.jetbrains.jet.lang.resolve.java.structure.JavaType;
027    import org.jetbrains.jet.lang.resolve.java.structure.JavaTypeSubstitutor;
028    
029    import java.util.ArrayList;
030    import java.util.Collection;
031    import java.util.Collections;
032    import java.util.List;
033    
034    import static org.jetbrains.jet.lang.resolve.java.structure.impl.JavaElementCollectionFromPsiArrayUtil.types;
035    
036    public class JavaClassifierTypeImpl extends JavaTypeImpl<PsiClassType> implements JavaClassifierType {
037        private static class ResolutionResult {
038            private final JavaClassifier classifier;
039            private final JavaTypeSubstitutor substitutor;
040    
041            private ResolutionResult(@Nullable JavaClassifier classifier, @NotNull JavaTypeSubstitutor substitutor) {
042                this.classifier = classifier;
043                this.substitutor = substitutor;
044            }
045        }
046    
047        private ResolutionResult resolutionResult;
048    
049        public JavaClassifierTypeImpl(@NotNull PsiClassType psiClassType) {
050            super(psiClassType);
051        }
052    
053        @Override
054        @Nullable
055        public JavaClassifier getClassifier() {
056            resolve();
057            return resolutionResult.classifier;
058        }
059    
060        @Override
061        @NotNull
062        public JavaTypeSubstitutor getSubstitutor() {
063            resolve();
064            return resolutionResult.substitutor;
065        }
066    
067        private void resolve() {
068            if (resolutionResult == null) {
069                PsiClassType.ClassResolveResult result = getPsi().resolveGenerics();
070                PsiClass psiClass = result.getElement();
071                resolutionResult = new ResolutionResult(
072                        psiClass == null ? null : JavaClassifierImpl.create(psiClass),
073                        new JavaTypeSubstitutorImpl(result.getSubstitutor())
074                );
075            }
076        }
077    
078        @Override
079        @NotNull
080        public Collection<JavaClassifierType> getSupertypes() {
081            PsiType[] psiTypes = getPsi().getSuperTypes();
082            if (psiTypes.length == 0) return Collections.emptyList();
083            List<JavaClassifierType> result = new ArrayList<JavaClassifierType>(psiTypes.length);
084            for (PsiType psiType : psiTypes) {
085                if (!(psiType instanceof PsiClassType)) {
086                    throw new IllegalStateException("Supertype should be a class: " + psiType + ", type: " + getPsi());
087                }
088                result.add(new JavaClassifierTypeImpl((PsiClassType) psiType));
089            }
090            return result;
091        }
092    
093        @Override
094        @NotNull
095        public String getPresentableText() {
096            return getPsi().getPresentableText();
097        }
098    
099        @Override
100        public boolean isRaw() {
101            return getPsi().isRaw();
102        }
103    
104        @Override
105        @NotNull
106        public Collection<JavaType> getTypeArguments() {
107            return types(getPsi().getParameters());
108        }
109    }