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.*;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.annotations.Nullable;
022    import org.jetbrains.jet.lang.resolve.java.structure.*;
023    
024    import java.util.*;
025    
026    import static org.jetbrains.jet.lang.resolve.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 Map<JavaTypeParameter, JavaType> convertSubstitutionMap(@NotNull Map<PsiTypeParameter, PsiType> psiMap) {
073            Map<JavaTypeParameter, JavaType> substitutionMap = new HashMap<JavaTypeParameter, JavaType>();
074            for (Map.Entry<PsiTypeParameter, PsiType> entry : psiMap.entrySet()) {
075                PsiType value = entry.getValue();
076                substitutionMap.put(new JavaTypeParameterImpl(entry.getKey()), value == null ? null : JavaTypeImpl.create(value));
077            }
078    
079            return substitutionMap;
080        }
081    
082        @Override
083        @NotNull
084        public Collection<JavaClassifierType> getSupertypes() {
085            PsiType[] psiTypes = getPsi().getSuperTypes();
086            if (psiTypes.length == 0) return Collections.emptyList();
087            List<JavaClassifierType> result = new ArrayList<JavaClassifierType>(psiTypes.length);
088            for (PsiType psiType : psiTypes) {
089                if (!(psiType instanceof PsiClassType)) {
090                    throw new IllegalStateException("Supertype should be a class: " + psiType + ", type: " + getPsi());
091                }
092                result.add(new JavaClassifierTypeImpl((PsiClassType) psiType));
093            }
094            return result;
095        }
096    
097        @Override
098        @NotNull
099        public String getPresentableText() {
100            return getPsi().getPresentableText();
101        }
102    
103        @Override
104        public boolean isRaw() {
105            return getPsi().isRaw();
106        }
107    
108        @Override
109        @NotNull
110        public List<JavaType> getTypeArguments() {
111            return types(getPsi().getParameters());
112        }
113    }