From 26bf0dca13f6a29be298b21ae9a7522679e0509e Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 17 Mar 2016 16:16:40 +0300 Subject: [PATCH] Remove interface JavaTypeSubstitutor, use implementation After 151e55b JavaTypeSubstitutor is only used from inside PSI-based implementation of Java structure --- .../java/structure/impl/JavaClassImpl.java | 5 +- .../impl/JavaClassifierTypeImpl.java | 14 +++-- .../impl/JavaTypeSubstitutorImpl.java | 15 +++-- .../kotlin/load/java/structure/JavaClass.java | 3 - .../java/structure/JavaClassifierType.java | 3 - .../java/structure/JavaTypeSubstitutor.java | 59 ------------------- .../structure/reflect/ReflectJavaClass.kt | 6 +- .../reflect/ReflectJavaClassifierType.kt | 7 ++- .../java/AbstractJavaTypeSubstitutorTest.java | 6 +- 9 files changed, 25 insertions(+), 93 deletions(-) delete mode 100644 core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaTypeSubstitutor.java diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassImpl.java index a437bd57a2d..dbc96af65ad 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassImpl.java @@ -176,14 +176,13 @@ public class JavaClassImpl extends JavaClassifierImpl 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 substMap = new HashMap(); for (Map.Entry entry : substitutor.getSubstitutionMap().entrySet()) { PsiTypeParameter key = ((JavaTypeParameterImpl) entry.getKey()).getPsi(); diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassifierTypeImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassifierTypeImpl.java index 429a7a378c4..7f8255fa7f1 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassifierTypeImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaClassifierTypeImpl.java @@ -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 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 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 implement ? getTypeParameters(((JavaClassImpl) classifier).getPsi()) : Collections.emptyList(); - JavaTypeSubstitutor substitutor = getSubstitutor(); + JavaTypeSubstitutorImpl substitutor = getSubstitutor(); List result = new ArrayList(); for (PsiTypeParameter typeParameter : parameters) { diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaTypeSubstitutorImpl.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaTypeSubstitutorImpl.java index eb2c3ce8714..b365e059429 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaTypeSubstitutorImpl.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/structure/impl/JavaTypeSubstitutorImpl.java @@ -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 substitutionMap; public JavaTypeSubstitutorImpl(@NotNull Map 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 substMap = new HashMap(); - 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 substMap) { + private void processClass( + @NotNull JavaClass javaClass, @NotNull JavaTypeSubstitutorImpl substitutor, @NotNull Map substMap + ) { List 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 getSubstitutionMap() { return substitutionMap; diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaClass.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaClass.java index 8244591b770..c56ca54176e 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaClass.java +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaClass.java @@ -56,9 +56,6 @@ public interface JavaClass extends JavaClassifier, JavaTypeParameterListOwner, J @NotNull OriginKind getOriginKind(); - @NotNull - JavaType createImmediateType(@NotNull JavaTypeSubstitutor substitutor); - enum OriginKind { COMPILED, SOURCE, diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaClassifierType.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaClassifierType.java index 02711b38a9b..a5c9a613d66 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaClassifierType.java +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaClassifierType.java @@ -27,9 +27,6 @@ public interface JavaClassifierType extends JavaType, JavaAnnotationOwner { @Nullable JavaClassifier getClassifier(); - @NotNull - JavaTypeSubstitutor getSubstitutor(); - @NotNull @ReadOnly Collection getSupertypes(); diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaTypeSubstitutor.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaTypeSubstitutor.java deleted file mode 100644 index 8b41527bc3d..00000000000 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/structure/JavaTypeSubstitutor.java +++ /dev/null @@ -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 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 getSubstitutionMap(); -} diff --git a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaClass.kt b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaClass.kt index df8db001d05..14cab082c9d 100644 --- a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaClass.kt +++ b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaClass.kt @@ -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) } diff --git a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaClassifierType.kt b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaClassifierType.kt index d917df10929..1c9d29b618d 100644 --- a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaClassifierType.kt +++ b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/structure/reflect/ReflectJavaClassifierType.kt @@ -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 = throw UnsupportedOperationException() override fun getPresentableText(): String = type.toString() diff --git a/idea/tests/org/jetbrains/kotlin/load/java/AbstractJavaTypeSubstitutorTest.java b/idea/tests/org/jetbrains/kotlin/load/java/AbstractJavaTypeSubstitutorTest.java index 2064c8e348c..61772d8b05b 100644 --- a/idea/tests/org/jetbrains/kotlin/load/java/AbstractJavaTypeSubstitutorTest.java +++ b/idea/tests/org/jetbrains/kotlin/load/java/AbstractJavaTypeSubstitutorTest.java @@ -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);