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