Implement JavaTypeSubstitutor without PSI

This commit is contained in:
zarechenskiy
2014-06-24 15:24:22 +04:00
committed by Alexander Udalov
parent 97b7768890
commit 0355b1bd53
54 changed files with 1000 additions and 62 deletions
@@ -16,10 +16,8 @@
package org.jetbrains.jet.lang.resolve.java.structure.impl;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiCompiledElement;
import com.intellij.psi.PsiTypeParameter;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiSubstitutorImpl;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -30,7 +28,9 @@ import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.jetbrains.jet.lang.resolve.java.structure.impl.JavaElementCollectionFromPsiArrayUtil.*;
@@ -181,4 +181,27 @@ public class JavaClassImpl extends JavaClassifierImpl<PsiClass> implements JavaC
return OriginKind.SOURCE;
}
}
@NotNull
@Override
public JavaType createImmediateType(@NotNull JavaTypeSubstitutor substitutor) {
return new JavaClassifierTypeImpl(
JavaPsiFacade.getElementFactory(getPsi().getProject()).createType(getPsi(), createPsiSubstitutor(substitutor)));
}
@NotNull
private static PsiSubstitutor createPsiSubstitutor(@NotNull JavaTypeSubstitutor substitutor) {
Map<PsiTypeParameter, PsiType> substMap = new HashMap<PsiTypeParameter, PsiType>();
for (Map.Entry<JavaTypeParameter, JavaType> entry : substitutor.getSubstitutionMap().entrySet()) {
PsiTypeParameter key = ((JavaTypeParameterImpl) entry.getKey()).getPsi();
if (entry.getValue() == null) {
substMap.put(key, null);
}
else {
substMap.put(key, ((JavaTypeImpl) entry.getValue()).getPsi());
}
}
return PsiSubstitutorImpl.createSubstitutor(substMap);
}
}
@@ -16,20 +16,12 @@
package org.jetbrains.jet.lang.resolve.java.structure.impl;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiType;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.structure.JavaClassifier;
import org.jetbrains.jet.lang.resolve.java.structure.JavaClassifierType;
import org.jetbrains.jet.lang.resolve.java.structure.JavaType;
import org.jetbrains.jet.lang.resolve.java.structure.JavaTypeSubstitutor;
import org.jetbrains.jet.lang.resolve.java.structure.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.*;
import static org.jetbrains.jet.lang.resolve.java.structure.impl.JavaElementCollectionFromPsiArrayUtil.types;
@@ -68,13 +60,25 @@ public class JavaClassifierTypeImpl extends JavaTypeImpl<PsiClassType> implement
if (resolutionResult == null) {
PsiClassType.ClassResolveResult result = getPsi().resolveGenerics();
PsiClass psiClass = result.getElement();
PsiSubstitutor substitutor = result.getSubstitutor();
resolutionResult = new ResolutionResult(
psiClass == null ? null : JavaClassifierImpl.create(psiClass),
new JavaTypeSubstitutorImpl(result.getSubstitutor())
new JavaTypeSubstitutorImpl(convertSubstitutionMap(substitutor.getSubstitutionMap()))
);
}
}
@NotNull
private Map<JavaTypeParameter, JavaType> convertSubstitutionMap(@NotNull Map<PsiTypeParameter, PsiType> psiMap) {
Map<JavaTypeParameter, JavaType> 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
@NotNull
public Collection<JavaClassifierType> getSupertypes() {
@@ -39,31 +39,31 @@ public abstract class JavaTypeImpl<Psi extends PsiType> implements JavaType {
return psiType.accept(new PsiTypeVisitor<JavaTypeImpl<?>>() {
@Nullable
@Override
public JavaTypeImpl<?> visitType(PsiType type) {
public JavaTypeImpl<?> visitType(@NotNull PsiType type) {
throw new UnsupportedOperationException("Unsupported PsiType: " + type);
}
@Nullable
@Override
public JavaTypeImpl<?> visitPrimitiveType(PsiPrimitiveType primitiveType) {
public JavaTypeImpl<?> visitPrimitiveType(@NotNull PsiPrimitiveType primitiveType) {
return new JavaPrimitiveTypeImpl(primitiveType);
}
@Nullable
@Override
public JavaTypeImpl<?> visitArrayType(PsiArrayType arrayType) {
public JavaTypeImpl<?> visitArrayType(@NotNull PsiArrayType arrayType) {
return new JavaArrayTypeImpl(arrayType);
}
@Nullable
@Override
public JavaTypeImpl<?> visitClassType(PsiClassType classType) {
public JavaTypeImpl<?> visitClassType(@NotNull PsiClassType classType) {
return new JavaClassifierTypeImpl(classType);
}
@Nullable
@Override
public JavaTypeImpl<?> visitWildcardType(PsiWildcardType wildcardType) {
public JavaTypeImpl<?> visitWildcardType(@NotNull PsiWildcardType wildcardType) {
return new JavaWildcardTypeImpl(wildcardType);
}
});
@@ -18,10 +18,12 @@ package org.jetbrains.jet.lang.resolve.java.structure.impl;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiWildcardType;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.java.structure.JavaType;
import org.jetbrains.jet.lang.resolve.java.structure.JavaTypeProvider;
import org.jetbrains.jet.lang.resolve.java.structure.JavaWildcardType;
public class JavaTypeProviderImpl implements JavaTypeProvider {
private final PsiManager manager;
@@ -35,4 +37,22 @@ public class JavaTypeProviderImpl implements JavaTypeProvider {
public JavaType createJavaLangObjectType() {
return JavaTypeImpl.create(PsiType.getJavaLangObject(manager, GlobalSearchScope.allScope(manager.getProject())));
}
@NotNull
@Override
public JavaWildcardType createUpperBoundWildcard(@NotNull JavaType bound) {
return new JavaWildcardTypeImpl(PsiWildcardType.createExtends(manager, ((JavaTypeImpl) bound).getPsi()));
}
@NotNull
@Override
public JavaWildcardType createLowerBoundWildcard(@NotNull JavaType bound) {
return new JavaWildcardTypeImpl(PsiWildcardType.createSuper(manager, ((JavaTypeImpl) bound).getPsi()));
}
@NotNull
@Override
public JavaWildcardType createUnboundedWildcard() {
return new JavaWildcardTypeImpl(PsiWildcardType.createUnbounded(manager));
}
}
@@ -16,83 +16,170 @@
package org.jetbrains.jet.lang.resolve.java.structure.impl;
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 org.jetbrains.jet.lang.resolve.java.structure.JavaType;
import org.jetbrains.jet.lang.resolve.java.structure.JavaTypeParameter;
import org.jetbrains.jet.lang.resolve.java.structure.JavaTypeSubstitutor;
import org.jetbrains.jet.lang.resolve.java.structure.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JavaTypeSubstitutorImpl implements JavaTypeSubstitutor {
private final PsiSubstitutor psiSubstitutor;
private Map<JavaTypeParameter, JavaType> substitutionMap;
private final Map<JavaTypeParameter, JavaType> substitutionMap;
public JavaTypeSubstitutorImpl(@NotNull PsiSubstitutor psiSubstitutor) {
this.psiSubstitutor = psiSubstitutor;
}
public JavaTypeSubstitutorImpl(@NotNull PsiSubstitutor psiSubstitutor, @NotNull Map<JavaTypeParameter, JavaType> substitutionMap) {
this(psiSubstitutor);
public JavaTypeSubstitutorImpl(@NotNull Map<JavaTypeParameter, JavaType> substitutionMap) {
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()) {
JavaTypeImpl value = ((JavaTypeImpl) entry.getValue());
psiMap.put(((JavaTypeParameterImpl) entry.getKey()).getPsi(), value == null ? null : value.getPsi());
}
PsiSubstitutor psiSubstitutor = PsiSubstitutorImpl.createSubstitutor(psiMap);
return new JavaTypeSubstitutorImpl(psiSubstitutor, substitutionMap);
@Override
public JavaType substitute(@NotNull JavaType type) {
JavaType substitutedType = substituteInternal(type);
return substitutedType != null ? substitutedType : correctSubstitutionForRawType(type);
}
@Override
@NotNull
public JavaType substitute(@NotNull JavaType type) {
return JavaTypeImpl.create(psiSubstitutor.substitute(((JavaTypeImpl) type).getPsi()));
// In case of raw type we get substition map like T -> null,
// in this case we should substitute upper bound of T or,
// if it does not exist, return java.lang.Object
private JavaType correctSubstitutionForRawType(@NotNull JavaType original) {
if (original instanceof JavaClassifierType) {
JavaClassifier classifier = ((JavaClassifierType) original).getClassifier();
if (classifier instanceof JavaTypeParameter) {
return rawTypeForTypeParameter((JavaTypeParameter) classifier);
}
}
return original;
}
@Nullable
private JavaType substituteInternal(@NotNull JavaType type) {
if (type instanceof JavaClassifierType) {
JavaClassifierType classifierType = (JavaClassifierType) type;
JavaClassifier classifier = classifierType.getClassifier();
if (classifier instanceof JavaTypeParameter) {
return substitute((JavaTypeParameter) classifier);
}
else if (classifier instanceof JavaClass) {
JavaClass javaClass = (JavaClass) classifier;
Map<JavaTypeParameter, JavaType> substMap = new HashMap<JavaTypeParameter, JavaType>();
processClass(javaClass, classifierType.getSubstitutor(), substMap);
return javaClass.createImmediateType(new JavaTypeSubstitutorImpl(substMap));
}
return type;
}
else if (type instanceof JavaPrimitiveType) {
return type;
}
else if (type instanceof JavaArrayType) {
JavaType componentType = ((JavaArrayType) type).getComponentType();
JavaType substitutedComponentType = substitute(componentType);
if (substitutedComponentType == componentType) return type;
return substitutedComponentType.createArrayType();
}
else if (type instanceof JavaWildcardType) {
return substituteWildcardType((JavaWildcardType) type);
}
return type;
}
private void processClass(@NotNull JavaClass javaClass, @NotNull JavaTypeSubstitutor substitutor, @NotNull Map<JavaTypeParameter, JavaType> substMap) {
List<JavaTypeParameter> typeParameters = javaClass.getTypeParameters();
for (JavaTypeParameter typeParameter : typeParameters) {
JavaType substitutedParam = substitutor.substitute(typeParameter);
if (substitutedParam == null) {
substMap.put(typeParameter, null);
}
else {
substMap.put(typeParameter, substituteInternal(substitutedParam));
}
}
if (javaClass.isStatic()) {
return;
}
JavaClass outerClass = javaClass.getOuterClass();
if (outerClass != null) {
processClass(outerClass, substitutor, substMap);
}
}
@Nullable
private JavaType substituteWildcardType(@NotNull JavaWildcardType wildcardType) {
JavaType bound = wildcardType.getBound();
if (bound == null) {
return wildcardType;
}
JavaType newBound = substituteInternal(bound);
if (newBound == null) {
// This can be in case of substitution wildcard to raw type
return null;
}
return rebound(wildcardType, newBound);
}
@NotNull
private static JavaWildcardType rebound(@NotNull JavaWildcardType type, @NotNull JavaType newBound) {
if (type.getTypeProvider().createJavaLangObjectType().equals(newBound)) {
return type.getTypeProvider().createUnboundedWildcard();
}
if (type.isExtends()) {
return type.getTypeProvider().createUpperBoundWildcard(newBound);
}
else {
return type.getTypeProvider().createLowerBoundWildcard(newBound);
}
}
@NotNull
private JavaType rawTypeForTypeParameter(@NotNull JavaTypeParameter typeParameter) {
Collection<JavaClassifierType> bounds = typeParameter.getUpperBounds();
if (!bounds.isEmpty()) {
return substitute(bounds.iterator().next());
}
return typeParameter.getTypeProvider().createJavaLangObjectType();
}
@Override
@Nullable
public JavaType substitute(@NotNull JavaTypeParameter typeParameter) {
PsiType psiType = psiSubstitutor.substitute(((JavaTypeParameterImpl) typeParameter).getPsi());
return psiType == null ? null : JavaTypeImpl.create(psiType);
if (substitutionMap.containsKey(typeParameter)) {
return substitutionMap.get(typeParameter);
}
return typeParameter.getType();
}
@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();
return substitutionMap.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof JavaTypeSubstitutorImpl && psiSubstitutor.equals(((JavaTypeSubstitutorImpl) obj).psiSubstitutor);
return obj instanceof JavaTypeSubstitutorImpl && substitutionMap.equals(((JavaTypeSubstitutorImpl) obj).substitutionMap);
}
@Override
public String toString() {
return getClass().getSimpleName() + ": " + psiSubstitutor;
return getClass().getSimpleName() + ": " + substitutionMap;
}
}
}
@@ -62,6 +62,9 @@ public interface JavaClass extends JavaClassifier, JavaTypeParameterListOwner, J
@NotNull
OriginKind getOriginKind();
@NotNull
JavaType createImmediateType(@NotNull JavaTypeSubstitutor substitutor);
enum OriginKind {
COMPILED,
SOURCE,
@@ -21,4 +21,13 @@ import org.jetbrains.annotations.NotNull;
public interface JavaTypeProvider {
@NotNull
JavaType createJavaLangObjectType();
@NotNull
JavaWildcardType createUpperBoundWildcard(@NotNull JavaType bound);
@NotNull
JavaWildcardType createLowerBoundWildcard(@NotNull JavaType bound);
@NotNull
JavaWildcardType createUnboundedWildcard();
}
@@ -115,6 +115,7 @@ import org.jetbrains.jet.plugin.structureView.AbstractKotlinFileStructureTest
import org.jetbrains.jet.j2k.test.AbstractJavaToKotlinConverterTest
import org.jetbrains.jet.jps.build.AbstractIncrementalJpsTest
import org.jetbrains.jet.asJava.AbstractKotlinLightClassTest
import org.jetbrains.jet.lang.resolve.java.AbstractJavaTypeSubstitutorTest
fun main(args: Array<String>) {
System.setProperty("java.awt.headless", "true")
@@ -271,6 +272,11 @@ fun main(args: Array<String>) {
}
testGroup("idea/tests", "idea/testData") {
testClass(javaClass<AbstractJavaTypeSubstitutorTest>()) {
model("typeSubstitution", extension = "java")
}
testClass(javaClass<AbstractAdditionalLazyResolveDescriptorRendererTest>()) {
model("resolve/additionalLazyResolve", testMethod = "doTest")
}
@@ -0,0 +1,8 @@
interface arrayType {
interface SuperArray<T> {
T[] typeForSubstitute();
}
interface MidArray extends SuperArray<Integer> {
}
}
@@ -0,0 +1,8 @@
interface classType {
interface SuperClass<T> {
List<Integer> typeForSubstitute();
}
interface MidClass extends SuperClass {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface classWithWildcard {
interface SuperList<T> {
List<T> typeForSubstitute();
}
interface MidList<U> extends SuperList<List<? extends U>> {
}
}
@@ -0,0 +1,8 @@
interface genericArray {
interface SuperGenericArray<T> {
T typeForSubstitute();
}
interface MidGenericArray<T> extends SuperGenericArray<T[]> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface innerParameter {
interface SuperInnerParam<T> {
<T> T typeForSubstitute();
}
interface MidInnerParam<U> extends SuperInnerParam<U> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface intersectionType {
interface SuperIntersection<T> {
<R extends Enum<R> & List<T>> R typeForSubstitute();
}
interface MidIntersection<U> extends SuperIntersection<U> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface intersectionTypeAsTypeParameter {
interface Super<K> {
<T extends Enum<T> & List<K>> Map<? extends T, K> typeForSubstitute();
}
interface Sub<U> extends Super<U> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface intersectionTypeInEnum {
interface Super<T> {
<R extends Enum<R> & List<T>> Enum<R> typeForSubstitute();
}
interface Sub<U> extends Super<U> {
}
}
@@ -0,0 +1,8 @@
interface intersectionTypeInInterfaceDeclaration {
interface SuperIntersection<T extends Object & Cloneable> {
T typeForSubstitute();
}
interface MidIntersection<U extends Object & Cloneable> extends SuperIntersection<U> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface intersectionTypeInTypeVariableClass {
interface Super<T> {
<R extends Class<R> & List<T>> TypeVariable<R> typeForSubstitute();
}
interface Sub<U> extends Super<U> {
}
}
@@ -0,0 +1,10 @@
import java.util.Map;
interface mapEntry {
interface Super<T, E> {
Map.Entry<T, E> typeForSubstitute();
}
interface Mid<E> extends Super<E, Integer> {
}
}
@@ -0,0 +1,8 @@
interface primitiveType {
interface SuperPrimitive<T> {
int typeForSubstitute();
}
interface MidPrimitive extends SuperPrimitive<Integer> {
}
}
@@ -0,0 +1,8 @@
interface rawArrayType {
interface SuperArray<T> {
T[] typeForSubstitute();
}
interface MidArray extends SuperArray {
}
}
@@ -0,0 +1,8 @@
interface rawArrayTypeParameterWithBound {
interface Super<T extends Cloneable> {
T[] typeForSubstitute();
}
interface Sub extends Super {
}
}
@@ -0,0 +1,8 @@
interface rawEnum {
interface Super<T extends Enum<T>> {
Enum<T> typeForSubstitute();
}
interface Sub extends Super {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface rawExtendsWildcard {
interface SuperRawWild<T> {
List<? extends T> typeForSubstitute();
}
interface MidRawWildcard extends SuperRawWild {
}
}
@@ -0,0 +1,8 @@
interface rawIntersectionType {
interface Super<T extends Integer & Cloneable> {
T typeForSubstitute();
}
interface Sub extends Super {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface rawSuperWildcard {
interface SuperRawWild<T> {
List<? super T> typeForSubstitute();
}
interface MidRawWildcard extends SuperRawWild {
}
}
@@ -0,0 +1,8 @@
interface rawType {
interface Super<T> {
T typeForSubstitute();
}
interface MidRaw extends Super {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface rawTypeInDeclaration {
interface Super<T> {
List typeForSubstitute();
}
interface Sub<U> extends Super<U> {
}
}
@@ -0,0 +1,8 @@
interface rawTypeWithBound {
interface Super<T extends Integer> {
T typeForSubstitute();
}
interface MidRaw extends Super {
}
}
@@ -0,0 +1,8 @@
interface rawTypeWithSelfReferenceBound {
interface Super<T extends Super<T>> {
T typeForSubstitute();
}
interface MidRaw extends Super {
}
}
@@ -0,0 +1,10 @@
import java.lang.reflect.TypeVariable;
interface rawWildcardInTypeVariableClass {
interface Super<T> {
TypeVariable<? super T> typeForSubstitute();
}
interface Sub extends Super {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface rawWildcardWithBound {
interface Super<T extends Cloneable> {
List<? extends T> typeForSubstitute();
}
interface Sub extends Super {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface selfReference {
interface SuperSelfRef<T extends SuperSelfRef<T>> {
public List<T> typeForSubstitute();
}
interface MidSelfRef extends SuperSelfRef<MidSelfRef> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface twoParameters {
interface SuperTwoParams<T, U> {
Map<T, List<U>> typeForSubstitute();
}
interface MidTwoParams<E> extends SuperTwoParams<Integer, E> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface twoParametersInSubClass {
interface SuperTwoParams<T> {
List<T> typeForSubstitute();
}
interface MidTwoParams<T, S> extends SuperTwoParams<S> {
}
}
@@ -0,0 +1,10 @@
import java.lang.reflect.TypeVariable;
interface typeVariableClass {
interface Super<T> {
TypeVariable<? super T> typeForSubstitute();
}
interface Mid<E> extends Super<E> {
}
}
@@ -0,0 +1,10 @@
import java.lang.reflect.TypeVariable;
interface typeVariableRaw {
interface Super<T extends Class> {
TypeVariable<T> typeForSubstitute();
}
interface Mid extends Super {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface unboundedWildcard {
interface Super<T> {
List<?> typeForSubstitute();
}
interface Sub extends Super<Integer> {
}
}
@@ -0,0 +1,8 @@
import java.util.*;
interface unboundedWildcardToTypeParameter {
interface SupList<K> extends List<K> {
@Override
boolean retainAll(Collection<K> c); // error, check that we do not fall
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface varargArray {
interface Super<T> {
void typeForSubstitute(T... a);
}
interface Sub<Integer> extends Super<Integer[]> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface varargArrayTypeParameter {
interface Super<T> {
void typeForSubstitute(T... a);
}
interface Sub<E> extends Super<E[]> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface varargClass {
interface Super<T> {
void typeForSubstitute(List<T>... a);
}
interface Sub extends Super<Integer> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface varargClassWithWildcard {
interface Super<T> {
void typeForSubstitute(List<? extends T>... a);
}
interface Sub<E> extends Super<List<? extends E>> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface varargRawType {
interface Super<T> {
void typeForSubstitute(T... a);
}
interface Sub extends Super {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface varargRawTypeWithBound {
interface Super<T extends Integer> {
void typeForSubstitute(T... a);
}
interface Sub<E> extends Super {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface varargToClass {
interface Super<T> {
void typeForSubstitute(T... a);
}
interface Sub extends Super<Integer> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface varargToClassWithWildcard {
interface Super<T> {
void typeForSubstitute(T... a);
}
interface Sub<E> extends Super<List<? extends E>> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface wildcardExtends {
interface SuperWildcardExtends<T> {
List<? extends T> typeForSubstitute();
}
interface MidWildcardExtends extends SuperWildcardExtends<Integer> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface wildcardExtendsObject {
interface SuperWildcardExtendsObject<T> {
List<? extends T> typeForSubstitute();
}
interface MidWildcardExtendsObject extends SuperWildcardExtendsObject<Object> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface wildcardExtendsTypeParameter {
interface SuperWildcard<T, U> {
Map<? extends T, ? extends U> typeForSubstitute();
}
interface MidWildcard<E, K> extends SuperWildcard<E, K> {
}
}
@@ -0,0 +1,10 @@
import java.util.*;
interface wildcardSuper {
interface SuperWildcardSuper<T> {
List<? extends T> typeForSubstitute();
}
interface MidWildcardSuper extends SuperWildcardSuper<Integer> {
}
}
@@ -0,0 +1,8 @@
import java.util.*;
interface wildcardToWildcard {
interface SupList<K> extends List<K> {
@Override
boolean addAll(Collection<? extends K> c);
}
}
@@ -0,0 +1,115 @@
/*
* Copyright 2010-2014 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;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.testFramework.LightProjectDescriptor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.java.structure.JavaClassifierType;
import org.jetbrains.jet.lang.resolve.java.structure.JavaType;
import org.jetbrains.jet.lang.resolve.java.structure.JavaTypeParameter;
import org.jetbrains.jet.lang.resolve.java.structure.impl.JavaTypeImpl;
import org.jetbrains.jet.lang.resolve.java.structure.impl.JavaTypeParameterImpl;
import org.jetbrains.jet.plugin.JetLightCodeInsightFixtureTestCase;
import org.jetbrains.jet.plugin.JetLightProjectDescriptor;
public abstract class AbstractJavaTypeSubstitutorTest extends JetLightCodeInsightFixtureTestCase {
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JetLightProjectDescriptor.INSTANCE;
}
public void doTest(@NotNull String testFile) {
PsiFile psiFile = myFixture.configureByFile(testFile);
Project project = myFixture.getProject();
String javaClassName = psiFile.getName().substring(0, psiFile.getName().length() - ".java".length());
PsiClass psiClass = JavaPsiFacade.getInstance(project).findClass(javaClassName, GlobalSearchScope.allScope(project));
assert psiClass != null : "Wrong path to test file: " + testFile;
assert psiClass.getInnerClasses().length > 0;
for (PsiClass innerInterface : psiClass.getInnerClasses()) {
PsiMethod method = getMethodWithTestData(innerInterface);
PsiClassType[] superTypes = innerInterface.getSuperTypes();
for (PsiClassType superType : superTypes) {
if (method.getReturnType() != null) {
doTest(superType, method.getReturnType());
}
if (method.getTypeParameters().length > 0) {
doTest(superType, method.getTypeParameters()[0]);
}
PsiParameter[] parameters = method.getParameterList().getParameters();
if (parameters.length > 0) {
doTest(superType, parameters[0].getType());
}
}
}
}
private static void doTest(@NotNull PsiClassType type, @NotNull PsiTypeParameter typeParameter) {
PsiType expectedType = type.resolveGenerics().getSubstitutor().substitute(typeParameter);
JavaClassifierType javaClassifierType = (JavaClassifierType) JavaTypeImpl.create(type);
JavaTypeParameter javaTypeToSubstitute = new JavaTypeParameterImpl(typeParameter);
JavaType actualType = javaClassifierType.getSubstitutor().substitute(javaTypeToSubstitute);
if (actualType == null) {
assertEquals(expectedType, null);
}
else {
assertEquals(expectedType, ((JavaTypeImpl) actualType).getPsi());
}
}
private static void doTest(@NotNull PsiClassType type, @NotNull PsiType psiTypeToSubstitute) {
PsiType expectedType = type.resolveGenerics().getSubstitutor().substitute(psiTypeToSubstitute);
JavaClassifierType javaClassifierType = (JavaClassifierType) JavaTypeImpl.create(type);
JavaType javaTypeToSubstitute = JavaTypeImpl.create(psiTypeToSubstitute);
JavaType actualType = javaClassifierType.getSubstitutor().substitute(javaTypeToSubstitute);
if (expectedType instanceof PsiEllipsisType) {
PsiEllipsisType ellipsisType = (PsiEllipsisType) expectedType;
assertEquals(ellipsisType.toArrayType(), ((JavaTypeImpl) actualType).getPsi());
}
else {
assertEquals(expectedType, ((JavaTypeImpl) actualType).getPsi());
}
}
@NotNull
private static PsiMethod getMethodWithTestData(@NotNull PsiClass psiClass) {
String substituteParameterName = "typeForSubstitute";
PsiMethod[] methods = psiClass.findMethodsByName(substituteParameterName, false);
if (methods.length == 0) {
methods = psiClass.findMethodsByName(substituteParameterName, true);
}
if (methods.length == 0) {
methods = psiClass.getMethods();
}
assert methods.length > 0 : "Wrong parameters for test: method typeForSubstitute not found";
return methods[0];
}
}
@@ -0,0 +1,259 @@
/*
* Copyright 2010-2014 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;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.File;
import java.util.regex.Pattern;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import org.jetbrains.jet.lang.resolve.java.AbstractJavaTypeSubstitutorTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("idea/testData/typeSubstitution")
public class JavaTypeSubstitutorTestGenerated extends AbstractJavaTypeSubstitutorTest {
public void testAllFilesPresentInTypeSubstitution() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/typeSubstitution"), Pattern.compile("^(.+)\\.java$"), true);
}
@TestMetadata("arrayType.java")
public void testArrayType() throws Exception {
doTest("idea/testData/typeSubstitution/arrayType.java");
}
@TestMetadata("classType.java")
public void testClassType() throws Exception {
doTest("idea/testData/typeSubstitution/classType.java");
}
@TestMetadata("classWithWildcard.java")
public void testClassWithWildcard() throws Exception {
doTest("idea/testData/typeSubstitution/classWithWildcard.java");
}
@TestMetadata("genericArray.java")
public void testGenericArray() throws Exception {
doTest("idea/testData/typeSubstitution/genericArray.java");
}
@TestMetadata("innerParameter.java")
public void testInnerParameter() throws Exception {
doTest("idea/testData/typeSubstitution/innerParameter.java");
}
@TestMetadata("intersectionType.java")
public void testIntersectionType() throws Exception {
doTest("idea/testData/typeSubstitution/intersectionType.java");
}
@TestMetadata("intersectionTypeAsTypeParameter.java")
public void testIntersectionTypeAsTypeParameter() throws Exception {
doTest("idea/testData/typeSubstitution/intersectionTypeAsTypeParameter.java");
}
@TestMetadata("intersectionTypeInEnum.java")
public void testIntersectionTypeInEnum() throws Exception {
doTest("idea/testData/typeSubstitution/intersectionTypeInEnum.java");
}
@TestMetadata("intersectionTypeInInterfaceDeclaration.java")
public void testIntersectionTypeInInterfaceDeclaration() throws Exception {
doTest("idea/testData/typeSubstitution/intersectionTypeInInterfaceDeclaration.java");
}
@TestMetadata("intersectionTypeInTypeVariableClass.java")
public void testIntersectionTypeInTypeVariableClass() throws Exception {
doTest("idea/testData/typeSubstitution/intersectionTypeInTypeVariableClass.java");
}
@TestMetadata("mapEntry.java")
public void testMapEntry() throws Exception {
doTest("idea/testData/typeSubstitution/mapEntry.java");
}
@TestMetadata("primitiveType.java")
public void testPrimitiveType() throws Exception {
doTest("idea/testData/typeSubstitution/primitiveType.java");
}
@TestMetadata("rawArrayType.java")
public void testRawArrayType() throws Exception {
doTest("idea/testData/typeSubstitution/rawArrayType.java");
}
@TestMetadata("rawArrayTypeParameterWithBound.java")
public void testRawArrayTypeParameterWithBound() throws Exception {
doTest("idea/testData/typeSubstitution/rawArrayTypeParameterWithBound.java");
}
@TestMetadata("rawEnum.java")
public void testRawEnum() throws Exception {
doTest("idea/testData/typeSubstitution/rawEnum.java");
}
@TestMetadata("rawExtendsWildcard.java")
public void testRawExtendsWildcard() throws Exception {
doTest("idea/testData/typeSubstitution/rawExtendsWildcard.java");
}
@TestMetadata("rawIntersectionType.java")
public void testRawIntersectionType() throws Exception {
doTest("idea/testData/typeSubstitution/rawIntersectionType.java");
}
@TestMetadata("rawSuperWildcard.java")
public void testRawSuperWildcard() throws Exception {
doTest("idea/testData/typeSubstitution/rawSuperWildcard.java");
}
@TestMetadata("rawType.java")
public void testRawType() throws Exception {
doTest("idea/testData/typeSubstitution/rawType.java");
}
@TestMetadata("rawTypeInDeclaration.java")
public void testRawTypeInDeclaration() throws Exception {
doTest("idea/testData/typeSubstitution/rawTypeInDeclaration.java");
}
@TestMetadata("rawTypeWithBound.java")
public void testRawTypeWithBound() throws Exception {
doTest("idea/testData/typeSubstitution/rawTypeWithBound.java");
}
@TestMetadata("rawTypeWithSelfReferenceBound.java")
public void testRawTypeWithSelfReferenceBound() throws Exception {
doTest("idea/testData/typeSubstitution/rawTypeWithSelfReferenceBound.java");
}
@TestMetadata("rawWildcardInTypeVariableClass.java")
public void testRawWildcardInTypeVariableClass() throws Exception {
doTest("idea/testData/typeSubstitution/rawWildcardInTypeVariableClass.java");
}
@TestMetadata("rawWildcardWithBound.java")
public void testRawWildcardWithBound() throws Exception {
doTest("idea/testData/typeSubstitution/rawWildcardWithBound.java");
}
@TestMetadata("selfReference.java")
public void testSelfReference() throws Exception {
doTest("idea/testData/typeSubstitution/selfReference.java");
}
@TestMetadata("twoParameters.java")
public void testTwoParameters() throws Exception {
doTest("idea/testData/typeSubstitution/twoParameters.java");
}
@TestMetadata("twoParametersInSubClass.java")
public void testTwoParametersInSubClass() throws Exception {
doTest("idea/testData/typeSubstitution/twoParametersInSubClass.java");
}
@TestMetadata("typeVariableClass.java")
public void testTypeVariableClass() throws Exception {
doTest("idea/testData/typeSubstitution/typeVariableClass.java");
}
@TestMetadata("typeVariableRaw.java")
public void testTypeVariableRaw() throws Exception {
doTest("idea/testData/typeSubstitution/typeVariableRaw.java");
}
@TestMetadata("unboundedWildcard.java")
public void testUnboundedWildcard() throws Exception {
doTest("idea/testData/typeSubstitution/unboundedWildcard.java");
}
@TestMetadata("unboundedWildcardToTypeParameter.java")
public void testUnboundedWildcardToTypeParameter() throws Exception {
doTest("idea/testData/typeSubstitution/unboundedWildcardToTypeParameter.java");
}
@TestMetadata("varargArray.java")
public void testVarargArray() throws Exception {
doTest("idea/testData/typeSubstitution/varargArray.java");
}
@TestMetadata("varargArrayTypeParameter.java")
public void testVarargArrayTypeParameter() throws Exception {
doTest("idea/testData/typeSubstitution/varargArrayTypeParameter.java");
}
@TestMetadata("varargClass.java")
public void testVarargClass() throws Exception {
doTest("idea/testData/typeSubstitution/varargClass.java");
}
@TestMetadata("varargClassWithWildcard.java")
public void testVarargClassWithWildcard() throws Exception {
doTest("idea/testData/typeSubstitution/varargClassWithWildcard.java");
}
@TestMetadata("varargRawType.java")
public void testVarargRawType() throws Exception {
doTest("idea/testData/typeSubstitution/varargRawType.java");
}
@TestMetadata("varargRawTypeWithBound.java")
public void testVarargRawTypeWithBound() throws Exception {
doTest("idea/testData/typeSubstitution/varargRawTypeWithBound.java");
}
@TestMetadata("varargToClass.java")
public void testVarargToClass() throws Exception {
doTest("idea/testData/typeSubstitution/varargToClass.java");
}
@TestMetadata("varargToClassWithWildcard.java")
public void testVarargToClassWithWildcard() throws Exception {
doTest("idea/testData/typeSubstitution/varargToClassWithWildcard.java");
}
@TestMetadata("wildcardExtends.java")
public void testWildcardExtends() throws Exception {
doTest("idea/testData/typeSubstitution/wildcardExtends.java");
}
@TestMetadata("wildcardExtendsObject.java")
public void testWildcardExtendsObject() throws Exception {
doTest("idea/testData/typeSubstitution/wildcardExtendsObject.java");
}
@TestMetadata("wildcardExtendsTypeParameter.java")
public void testWildcardExtendsTypeParameter() throws Exception {
doTest("idea/testData/typeSubstitution/wildcardExtendsTypeParameter.java");
}
@TestMetadata("wildcardSuper.java")
public void testWildcardSuper() throws Exception {
doTest("idea/testData/typeSubstitution/wildcardSuper.java");
}
@TestMetadata("wildcardToWildcard.java")
public void testWildcardToWildcard() throws Exception {
doTest("idea/testData/typeSubstitution/wildcardToWildcard.java");
}
}