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    public class JavaClassifierTypeImpl extends JavaTypeImpl<PsiClassType> implements JavaClassifierType {
027        private static class ResolutionResult {
028            private final JavaClassifier classifier;
029            private final JavaTypeSubstitutor substitutor;
030            private final boolean isRaw;
031    
032            private ResolutionResult(@Nullable JavaClassifier classifier, @NotNull JavaTypeSubstitutor substitutor, boolean isRaw) {
033                this.classifier = classifier;
034                this.substitutor = substitutor;
035                this.isRaw = isRaw;
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                        PsiClassType.isRaw(result));
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            resolve();
108            return resolutionResult.isRaw;
109        }
110    
111        @Override
112        @NotNull
113        public List<JavaType> getTypeArguments() {
114            JavaClassifier classifier = getClassifier();
115    
116            // parameters including ones from outer class
117            Iterable<PsiTypeParameter> parameters = classifier instanceof JavaClassImpl
118                                                    ? getTypeParameters(((JavaClassImpl) classifier).getPsi())
119                                                    : Collections.<PsiTypeParameter>emptyList();
120    
121            JavaTypeSubstitutor substitutor = getSubstitutor();
122    
123            List<JavaType> result = new ArrayList<JavaType>();
124            for (PsiTypeParameter typeParameter : parameters) {
125                result.add(substitutor.substitute(new JavaTypeParameterImpl(typeParameter)));
126            }
127    
128            return result;
129        }
130    
131        // Copy-pasted from PsiUtil.typeParametersIterable
132        // The only change is using `Collections.addAll(result, typeParameters)` instead of reversing type parameters of `currentOwner`
133        // Result differs in cases like:
134        // class Outer<H1> {
135        //   class Inner<H2, H3> {}
136        // }
137        //
138        // PsiUtil.typeParametersIterable returns H3, H2, H1
139        // But we would like to have H2, H3, H1 as such order is consistent with our type representation
140        @NotNull
141        public static List<PsiTypeParameter> getTypeParameters(@NotNull PsiClass owner) {
142            List<PsiTypeParameter> result = null;
143    
144            PsiTypeParameterListOwner currentOwner = owner;
145            while (currentOwner != null) {
146                PsiTypeParameter[] typeParameters = currentOwner.getTypeParameters();
147                if (typeParameters.length > 0) {
148                    if (result == null) result = new ArrayList<PsiTypeParameter>(typeParameters.length);
149                    Collections.addAll(result, typeParameters);
150                }
151    
152                if (currentOwner.hasModifierProperty(PsiModifier.STATIC)) break;
153                currentOwner = currentOwner.getContainingClass();
154            }
155    
156            if (result == null) return Collections.emptyList();
157            return result;
158        }
159    }