Extract interface out of JavaTypeSubstitutor

This commit is contained in:
Alexander Udalov
2013-08-19 19:29:40 +04:00
parent 2eb5754289
commit d14a7282ba
4 changed files with 102 additions and 70 deletions
@@ -224,7 +224,7 @@ class PropagationHeuristics {
JavaType value = entry.getValue();
erasedMap.put(entry.getKey(), value == null ? null : erasure(value));
}
return JavaTypeSubstitutor.create(erasedMap);
return JavaTypeSubstitutorImpl.create(erasedMap);
}
private static boolean canHaveSuperMethod(@NotNull JavaMethod method) {
@@ -72,7 +72,7 @@ public class JavaClassifierTypeImpl extends JavaTypeImpl implements JavaClassifi
PsiClass psiClass = result.getElement();
resolutionResult = new ResolutionResult(
psiClass == null ? null : JavaClassifierImpl.create(psiClass),
new JavaTypeSubstitutor(result.getSubstitutor())
new JavaTypeSubstitutorImpl(result.getSubstitutor())
);
}
}
@@ -17,83 +17,20 @@
package org.jetbrains.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiSubstitutor;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiTypeParameter;
import com.intellij.psi.impl.PsiSubstitutorImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
public class JavaTypeSubstitutor {
public static final JavaTypeSubstitutor EMPTY = new JavaTypeSubstitutor(PsiSubstitutor.EMPTY);
private final PsiSubstitutor psiSubstitutor;
private Map<JavaTypeParameter, JavaType> substitutionMap;
public JavaTypeSubstitutor(@NotNull PsiSubstitutor psiSubstitutor) {
this.psiSubstitutor = psiSubstitutor;
}
public JavaTypeSubstitutor(@NotNull PsiSubstitutor psiSubstitutor, @NotNull Map<JavaTypeParameter, JavaType> substitutionMap) {
this(psiSubstitutor);
this.substitutionMap = substitutionMap;
}
public interface JavaTypeSubstitutor {
JavaTypeSubstitutor EMPTY = new JavaTypeSubstitutorImpl(PsiSubstitutor.EMPTY);
@NotNull
public PsiSubstitutor getPsi() {
return psiSubstitutor;
}
@NotNull
public static JavaTypeSubstitutor create(@NotNull Map<JavaTypeParameter, JavaType> substitutionMap) {
Map<PsiTypeParameter, PsiType> psiMap = new HashMap<PsiTypeParameter, PsiType>();
for (Map.Entry<JavaTypeParameter, JavaType> entry : substitutionMap.entrySet()) {
JavaType value = entry.getValue();
psiMap.put(entry.getKey().getPsi(), value == null ? null : value.getPsi());
}
PsiSubstitutor psiSubstitutor = PsiSubstitutorImpl.createSubstitutor(psiMap);
return new JavaTypeSubstitutor(psiSubstitutor, substitutionMap);
}
@NotNull
public JavaType substitute(@NotNull JavaType type) {
return JavaTypeImpl.create(getPsi().substitute(type.getPsi()));
}
JavaType substitute(@NotNull JavaType type);
@Nullable
public JavaType substitute(@NotNull JavaTypeParameter typeParameter) {
PsiType psiType = getPsi().substitute(typeParameter.getPsi());
return psiType == null ? null : JavaTypeImpl.create(psiType);
}
JavaType substitute(@NotNull JavaTypeParameter typeParameter);
@NotNull
public Map<JavaTypeParameter, JavaType> getSubstitutionMap() {
if (substitutionMap == null) {
Map<PsiTypeParameter, PsiType> psiMap = psiSubstitutor.getSubstitutionMap();
substitutionMap = new HashMap<JavaTypeParameter, JavaType>();
for (Map.Entry<PsiTypeParameter, PsiType> entry : psiMap.entrySet()) {
PsiType value = entry.getValue();
substitutionMap.put(new JavaTypeParameterImpl(entry.getKey()), value == null ? null : JavaTypeImpl.create(value));
}
}
return substitutionMap;
}
@Override
public int hashCode() {
return getPsi().hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof JavaTypeSubstitutor && getPsi().equals(((JavaTypeSubstitutor) obj).getPsi());
}
@Override
public String toString() {
return getClass().getSimpleName() + ": " + getPsi();
}
Map<JavaTypeParameter, JavaType> getSubstitutionMap();
}
@@ -0,0 +1,95 @@
/*
* Copyright 2010-2013 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.jet.lang.resolve.java.structure;
import com.intellij.psi.PsiSubstitutor;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiTypeParameter;
import com.intellij.psi.impl.PsiSubstitutorImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Map;
public class JavaTypeSubstitutorImpl implements JavaTypeSubstitutor {
private final PsiSubstitutor psiSubstitutor;
private Map<JavaTypeParameter, JavaType> substitutionMap;
public JavaTypeSubstitutorImpl(@NotNull PsiSubstitutor psiSubstitutor) {
this.psiSubstitutor = psiSubstitutor;
}
public JavaTypeSubstitutorImpl(@NotNull PsiSubstitutor psiSubstitutor, @NotNull Map<JavaTypeParameter, JavaType> substitutionMap) {
this(psiSubstitutor);
this.substitutionMap = substitutionMap;
}
@NotNull
public static JavaTypeSubstitutor create(@NotNull Map<JavaTypeParameter, JavaType> substitutionMap) {
Map<PsiTypeParameter, PsiType> psiMap = new HashMap<PsiTypeParameter, PsiType>();
for (Map.Entry<JavaTypeParameter, JavaType> entry : substitutionMap.entrySet()) {
JavaType value = entry.getValue();
psiMap.put(entry.getKey().getPsi(), value == null ? null : value.getPsi());
}
PsiSubstitutor psiSubstitutor = PsiSubstitutorImpl.createSubstitutor(psiMap);
return new JavaTypeSubstitutorImpl(psiSubstitutor, substitutionMap);
}
@Override
@NotNull
public JavaType substitute(@NotNull JavaType type) {
return JavaTypeImpl.create(psiSubstitutor.substitute(type.getPsi()));
}
@Override
@Nullable
public JavaType substitute(@NotNull JavaTypeParameter typeParameter) {
PsiType psiType = psiSubstitutor.substitute(typeParameter.getPsi());
return psiType == null ? null : JavaTypeImpl.create(psiType);
}
@Override
@NotNull
public Map<JavaTypeParameter, JavaType> getSubstitutionMap() {
if (substitutionMap == null) {
Map<PsiTypeParameter, PsiType> psiMap = psiSubstitutor.getSubstitutionMap();
substitutionMap = new HashMap<JavaTypeParameter, JavaType>();
for (Map.Entry<PsiTypeParameter, PsiType> entry : psiMap.entrySet()) {
PsiType value = entry.getValue();
substitutionMap.put(new JavaTypeParameterImpl(entry.getKey()), value == null ? null : JavaTypeImpl.create(value));
}
}
return substitutionMap;
}
@Override
public int hashCode() {
return psiSubstitutor.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof JavaTypeSubstitutorImpl && psiSubstitutor.equals(((JavaTypeSubstitutorImpl) obj).psiSubstitutor);
}
@Override
public String toString() {
return getClass().getSimpleName() + ": " + psiSubstitutor;
}
}