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
031 private ResolutionResult(@Nullable JavaClassifier classifier, @NotNull JavaTypeSubstitutor substitutor) {
032 this.classifier = classifier;
033 this.substitutor = substitutor;
034 }
035 }
036
037 private ResolutionResult resolutionResult;
038
039 public JavaClassifierTypeImpl(@NotNull PsiClassType psiClassType) {
040 super(psiClassType);
041 }
042
043 @Override
044 @Nullable
045 public JavaClassifier getClassifier() {
046 resolve();
047 return resolutionResult.classifier;
048 }
049
050 @Override
051 @NotNull
052 public JavaTypeSubstitutor getSubstitutor() {
053 resolve();
054 return resolutionResult.substitutor;
055 }
056
057 private void resolve() {
058 if (resolutionResult == null) {
059 PsiClassType.ClassResolveResult result = getPsi().resolveGenerics();
060 PsiClass psiClass = result.getElement();
061 PsiSubstitutor substitutor = result.getSubstitutor();
062 resolutionResult = new ResolutionResult(
063 psiClass == null ? null : JavaClassifierImpl.create(psiClass),
064 new JavaTypeSubstitutorImpl(convertSubstitutionMap(substitutor.getSubstitutionMap()))
065 );
066 }
067 }
068
069 @NotNull
070 private static Map<JavaTypeParameter, JavaType> convertSubstitutionMap(@NotNull Map<PsiTypeParameter, PsiType> psiMap) {
071 if (psiMap.isEmpty()) return Collections.emptyMap();
072
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 JavaClassifier classifier = getClassifier();
112
113 // parameters including ones from outer class
114 Iterable<PsiTypeParameter> parameters = classifier instanceof JavaClassImpl
115 ? getTypeParameters(((JavaClassImpl) classifier).getPsi())
116 : Collections.<PsiTypeParameter>emptyList();
117
118 JavaTypeSubstitutor substitutor = getSubstitutor();
119
120 List<JavaType> result = new ArrayList<JavaType>();
121 for (PsiTypeParameter typeParameter : parameters) {
122 result.add(substitutor.substitute(new JavaTypeParameterImpl(typeParameter)));
123 }
124
125 return result;
126 }
127
128 // Copy-pasted from PsiUtil.typeParametersIterable
129 // The only change is using `Collections.addAll(result, typeParameters)` instead of reversing type parameters of `currentOwner`
130 // Result differs in cases like:
131 // class Outer<H1> {
132 // class Inner<H2, H3> {}
133 // }
134 //
135 // PsiUtil.typeParametersIterable returns H3, H2, H1
136 // But we would like to have H2, H3, H1 as such order is consistent with our type representation
137 @NotNull
138 public static List<PsiTypeParameter> getTypeParameters(@NotNull PsiClass owner) {
139 List<PsiTypeParameter> result = null;
140
141 PsiTypeParameterListOwner currentOwner = owner;
142 while (currentOwner != null) {
143 PsiTypeParameter[] typeParameters = currentOwner.getTypeParameters();
144 if (typeParameters.length > 0) {
145 if (result == null) result = new ArrayList<PsiTypeParameter>(typeParameters.length);
146 Collections.addAll(result, typeParameters);
147 }
148
149 if (currentOwner.hasModifierProperty(PsiModifier.STATIC)) break;
150 currentOwner = currentOwner.getContainingClass();
151 }
152
153 if (result == null) return Collections.emptyList();
154 return result;
155 }
156 }