Remove interface JavaTypeSubstitutor, use implementation

After 151e55b JavaTypeSubstitutor is only used from inside PSI-based
implementation of Java structure
This commit is contained in:
Alexander Udalov
2016-03-17 16:16:40 +03:00
parent e42cb2af40
commit 26bf0dca13
9 changed files with 25 additions and 93 deletions
@@ -176,14 +176,13 @@ public class JavaClassImpl extends JavaClassifierImpl<PsiClass> implements JavaC
}
@NotNull
@Override
public JavaType createImmediateType(@NotNull JavaTypeSubstitutor substitutor) {
public JavaType createImmediateType(@NotNull JavaTypeSubstitutorImpl substitutor) {
return new JavaClassifierTypeImpl(
JavaPsiFacade.getElementFactory(getPsi().getProject()).createType(getPsi(), createPsiSubstitutor(substitutor)));
}
@NotNull
private static PsiSubstitutor createPsiSubstitutor(@NotNull JavaTypeSubstitutor substitutor) {
private static PsiSubstitutor createPsiSubstitutor(@NotNull JavaTypeSubstitutorImpl substitutor) {
Map<PsiTypeParameter, PsiType> substMap = new HashMap<PsiTypeParameter, PsiType>();
for (Map.Entry<JavaTypeParameter, JavaType> entry : substitutor.getSubstitutionMap().entrySet()) {
PsiTypeParameter key = ((JavaTypeParameterImpl) entry.getKey()).getPsi();
@@ -19,17 +19,20 @@ package org.jetbrains.kotlin.load.java.structure.impl;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.load.java.structure.*;
import org.jetbrains.kotlin.load.java.structure.JavaClassifier;
import org.jetbrains.kotlin.load.java.structure.JavaClassifierType;
import org.jetbrains.kotlin.load.java.structure.JavaType;
import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter;
import java.util.*;
public class JavaClassifierTypeImpl extends JavaTypeImpl<PsiClassType> implements JavaClassifierType {
private static class ResolutionResult {
private final JavaClassifier classifier;
private final JavaTypeSubstitutor substitutor;
private final JavaTypeSubstitutorImpl substitutor;
private final boolean isRaw;
private ResolutionResult(@Nullable JavaClassifier classifier, @NotNull JavaTypeSubstitutor substitutor, boolean isRaw) {
private ResolutionResult(@Nullable JavaClassifier classifier, @NotNull JavaTypeSubstitutorImpl substitutor, boolean isRaw) {
this.classifier = classifier;
this.substitutor = substitutor;
this.isRaw = isRaw;
@@ -49,9 +52,8 @@ public class JavaClassifierTypeImpl extends JavaTypeImpl<PsiClassType> implement
return resolutionResult.classifier;
}
@Override
@NotNull
public JavaTypeSubstitutor getSubstitutor() {
public JavaTypeSubstitutorImpl getSubstitutor() {
resolve();
return resolutionResult.substitutor;
}
@@ -118,7 +120,7 @@ public class JavaClassifierTypeImpl extends JavaTypeImpl<PsiClassType> implement
? getTypeParameters(((JavaClassImpl) classifier).getPsi())
: Collections.<PsiTypeParameter>emptyList();
JavaTypeSubstitutor substitutor = getSubstitutor();
JavaTypeSubstitutorImpl substitutor = getSubstitutor();
List<JavaType> result = new ArrayList<JavaType>();
for (PsiTypeParameter typeParameter : parameters) {
@@ -25,7 +25,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JavaTypeSubstitutorImpl implements JavaTypeSubstitutor {
public class JavaTypeSubstitutorImpl {
private final Map<JavaTypeParameter, JavaType> substitutionMap;
public JavaTypeSubstitutorImpl(@NotNull Map<JavaTypeParameter, JavaType> substitutionMap) {
@@ -33,7 +33,6 @@ public class JavaTypeSubstitutorImpl implements JavaTypeSubstitutor {
}
@NotNull
@Override
public JavaType substitute(@NotNull JavaType type) {
JavaType substitutedType = substituteInternal(type);
return substitutedType != null ? substitutedType : correctSubstitutionForRawType(type);
@@ -63,10 +62,10 @@ public class JavaTypeSubstitutorImpl implements JavaTypeSubstitutor {
if (classifier instanceof JavaTypeParameter) {
return substitute((JavaTypeParameter) classifier);
}
else if (classifier instanceof JavaClass) {
JavaClass javaClass = (JavaClass) classifier;
else if (classifier instanceof JavaClassImpl) {
JavaClassImpl javaClass = (JavaClassImpl) classifier;
Map<JavaTypeParameter, JavaType> substMap = new HashMap<JavaTypeParameter, JavaType>();
processClass(javaClass, classifierType.getSubstitutor(), substMap);
processClass(javaClass, ((JavaClassifierTypeImpl) classifierType).getSubstitutor(), substMap);
return javaClass.createImmediateType(new JavaTypeSubstitutorImpl(substMap));
}
@@ -90,7 +89,9 @@ public class JavaTypeSubstitutorImpl implements JavaTypeSubstitutor {
return type;
}
private void processClass(@NotNull JavaClass javaClass, @NotNull JavaTypeSubstitutor substitutor, @NotNull Map<JavaTypeParameter, JavaType> substMap) {
private void processClass(
@NotNull JavaClass javaClass, @NotNull JavaTypeSubstitutorImpl substitutor, @NotNull Map<JavaTypeParameter, JavaType> substMap
) {
List<JavaTypeParameter> typeParameters = javaClass.getTypeParameters();
for (JavaTypeParameter typeParameter : typeParameters) {
JavaType substitutedParam = substitutor.substitute(typeParameter);
@@ -152,7 +153,6 @@ public class JavaTypeSubstitutorImpl implements JavaTypeSubstitutor {
return typeParameter.getTypeProvider().createJavaLangObjectType();
}
@Override
@Nullable
public JavaType substitute(@NotNull JavaTypeParameter typeParameter) {
if (substitutionMap.containsKey(typeParameter)) {
@@ -162,7 +162,6 @@ public class JavaTypeSubstitutorImpl implements JavaTypeSubstitutor {
return typeParameter.getType();
}
@Override
@NotNull
public Map<JavaTypeParameter, JavaType> getSubstitutionMap() {
return substitutionMap;
@@ -56,9 +56,6 @@ public interface JavaClass extends JavaClassifier, JavaTypeParameterListOwner, J
@NotNull
OriginKind getOriginKind();
@NotNull
JavaType createImmediateType(@NotNull JavaTypeSubstitutor substitutor);
enum OriginKind {
COMPILED,
SOURCE,
@@ -27,9 +27,6 @@ public interface JavaClassifierType extends JavaType, JavaAnnotationOwner {
@Nullable
JavaClassifier getClassifier();
@NotNull
JavaTypeSubstitutor getSubstitutor();
@NotNull
@ReadOnly
Collection<JavaClassifierType> getSupertypes();
@@ -1,59 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.load.java.structure;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.Map;
public interface JavaTypeSubstitutor {
JavaTypeSubstitutor EMPTY = new JavaTypeSubstitutor() {
@NotNull
@Override
public JavaType substitute(@NotNull JavaType type) {
return type;
}
@Nullable
@Override
public JavaType substitute(@NotNull JavaTypeParameter typeParameter) {
return typeParameter.getType();
}
@NotNull
@Override
public Map<JavaTypeParameter, JavaType> getSubstitutionMap() {
return Collections.emptyMap();
}
@Override
public String toString() {
return "Empty JavaTypeSubstitutor";
}
};
@NotNull
JavaType substitute(@NotNull JavaType type);
@Nullable
JavaType substitute(@NotNull JavaTypeParameter typeParameter);
@NotNull
Map<JavaTypeParameter, JavaType> getSubstitutionMap();
}
@@ -18,11 +18,9 @@ package org.jetbrains.kotlin.load.java.structure.reflect
import org.jetbrains.kotlin.load.java.structure.JavaClass
import org.jetbrains.kotlin.load.java.structure.JavaClassifierType
import org.jetbrains.kotlin.load.java.structure.JavaType
import org.jetbrains.kotlin.load.java.structure.JavaTypeSubstitutor
import org.jetbrains.kotlin.name.Name
import java.lang.reflect.Method
import java.util.Arrays
import java.util.*
class ReflectJavaClass(
private val klass: Class<*>
@@ -88,8 +86,6 @@ class ReflectJavaClass(
// TODO: drop OriginKind
override fun getOriginKind() = JavaClass.OriginKind.COMPILED
override fun createImmediateType(substitutor: JavaTypeSubstitutor): JavaType = throw UnsupportedOperationException()
override fun getName(): Name = Name.identifier(klass.simpleName)
override fun getTypeParameters() = klass.typeParameters.map { ReflectJavaTypeParameter(it) }
@@ -16,7 +16,10 @@
package org.jetbrains.kotlin.load.java.structure.reflect
import org.jetbrains.kotlin.load.java.structure.*
import org.jetbrains.kotlin.load.java.structure.JavaAnnotation
import org.jetbrains.kotlin.load.java.structure.JavaClassifier
import org.jetbrains.kotlin.load.java.structure.JavaClassifierType
import org.jetbrains.kotlin.load.java.structure.JavaType
import org.jetbrains.kotlin.name.FqName
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
@@ -36,8 +39,6 @@ class ReflectJavaClassifierType(public override val type: Type) : ReflectJavaTyp
override fun getClassifier(): JavaClassifier = classifier
override fun getSubstitutor(): JavaTypeSubstitutor = throw UnsupportedOperationException()
override fun getSupertypes(): Collection<JavaClassifierType> = throw UnsupportedOperationException()
override fun getPresentableText(): String = type.toString()
@@ -23,9 +23,9 @@ import com.intellij.testFramework.LightProjectDescriptor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase;
import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor;
import org.jetbrains.kotlin.load.java.structure.JavaClassifierType;
import org.jetbrains.kotlin.load.java.structure.JavaType;
import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter;
import org.jetbrains.kotlin.load.java.structure.impl.JavaClassifierTypeImpl;
import org.jetbrains.kotlin.load.java.structure.impl.JavaTypeImpl;
import org.jetbrains.kotlin.load.java.structure.impl.JavaTypeParameterImpl;
@@ -68,7 +68,7 @@ public abstract class AbstractJavaTypeSubstitutorTest extends KotlinLightCodeIns
private static void doTest(@NotNull PsiClassType type, @NotNull PsiTypeParameter typeParameter) {
PsiType expectedType = type.resolveGenerics().getSubstitutor().substitute(typeParameter);
JavaClassifierType javaClassifierType = (JavaClassifierType) JavaTypeImpl.create(type);
JavaClassifierTypeImpl javaClassifierType = (JavaClassifierTypeImpl) JavaTypeImpl.create(type);
JavaTypeParameter javaTypeToSubstitute = new JavaTypeParameterImpl(typeParameter);
JavaType actualType = javaClassifierType.getSubstitutor().substitute(javaTypeToSubstitute);
@@ -83,7 +83,7 @@ public abstract class AbstractJavaTypeSubstitutorTest extends KotlinLightCodeIns
private static void doTest(@NotNull PsiClassType type, @NotNull PsiType psiTypeToSubstitute) {
PsiType expectedType = type.resolveGenerics().getSubstitutor().substitute(psiTypeToSubstitute);
JavaClassifierType javaClassifierType = (JavaClassifierType) JavaTypeImpl.create(type);
JavaClassifierTypeImpl javaClassifierType = (JavaClassifierTypeImpl) JavaTypeImpl.create(type);
JavaType javaTypeToSubstitute = JavaTypeImpl.create(psiTypeToSubstitute);
JavaType actualType = javaClassifierType.getSubstitutor().substitute(javaTypeToSubstitute);