Simplify TypeVariableResolver
This commit is contained in:
committed by
Pavel V. Talanov
parent
cbdc157743
commit
155f3b3b96
+54
-2
@@ -16,10 +16,62 @@
|
||||
|
||||
package org.jetbrains.jet.lang.resolve.java;
|
||||
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
|
||||
|
||||
public interface TypeVariableResolver {
|
||||
import java.util.List;
|
||||
|
||||
public class TypeVariableResolver {
|
||||
@NotNull
|
||||
TypeParameterDescriptor getTypeVariable(@NotNull String name);
|
||||
protected final List<TypeParameterDescriptor> typeParameters;
|
||||
@NotNull
|
||||
protected final DeclarationDescriptor owner;
|
||||
@NotNull
|
||||
protected final String context;
|
||||
|
||||
public TypeVariableResolver(
|
||||
@NotNull List<TypeParameterDescriptor> typeParameters,
|
||||
@NotNull DeclarationDescriptor owner,
|
||||
@NotNull String context
|
||||
) {
|
||||
this.typeParameters = typeParameters;
|
||||
this.owner = owner;
|
||||
this.context = context;
|
||||
|
||||
assert ContainerUtil.and(typeParameters, new Condition<TypeParameterDescriptor>() {
|
||||
@Override
|
||||
public boolean value(TypeParameterDescriptor descriptor) {
|
||||
return descriptor.getContainingDeclaration() == TypeVariableResolver.this.owner;
|
||||
}
|
||||
}) : "Type parameters should be parameters of owner: " + owner + "; " + typeParameters;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public TypeParameterDescriptor getTypeVariable(@NotNull String name) {
|
||||
return getTypeVariable(name, typeParameters, owner);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private TypeParameterDescriptor getTypeVariable(
|
||||
@NotNull String name,
|
||||
@NotNull List<TypeParameterDescriptor> typeParameters,
|
||||
@NotNull DeclarationDescriptor owner
|
||||
) {
|
||||
for (TypeParameterDescriptor typeParameter : typeParameters) {
|
||||
if (typeParameter.getName().asString().equals(name)) {
|
||||
return typeParameter;
|
||||
}
|
||||
}
|
||||
|
||||
DeclarationDescriptor container = owner.getContainingDeclaration();
|
||||
if (container instanceof ClassDescriptor) {
|
||||
return getTypeVariable(name, ((ClassDescriptor) container).getTypeConstructor().getParameters(), container);
|
||||
}
|
||||
|
||||
throw new IllegalStateException("Type parameter not found by name '" + name + "' in " + context);
|
||||
}
|
||||
}
|
||||
|
||||
-78
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.ClassOrNamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TypeVariableResolverFromTypeDescriptors implements TypeVariableResolver {
|
||||
|
||||
@NotNull
|
||||
private final List<TypeParameterDescriptor> typeParameters;
|
||||
@NotNull
|
||||
private final DeclarationDescriptor typeParametersOwner;
|
||||
@NotNull
|
||||
private final String context;
|
||||
|
||||
public TypeVariableResolverFromTypeDescriptors(
|
||||
@NotNull List<TypeParameterDescriptor> typeParameters,
|
||||
@NotNull DeclarationDescriptor owner,
|
||||
@NotNull String context) {
|
||||
this.typeParameters = typeParameters;
|
||||
this.typeParametersOwner = owner;
|
||||
this.context = context;
|
||||
|
||||
for (TypeParameterDescriptor typeParameter : typeParameters) {
|
||||
if (typeParameter.getContainingDeclaration() != owner) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public TypeParameterDescriptor getTypeVariable(@NotNull String name) {
|
||||
return getTypeVariable(name, typeParameters, typeParametersOwner, context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static TypeParameterDescriptor getTypeVariable(
|
||||
@NotNull String name,
|
||||
@NotNull List<TypeParameterDescriptor> typeParameters,
|
||||
@NotNull DeclarationDescriptor owner,
|
||||
@NotNull String context) {
|
||||
for (TypeParameterDescriptor typeParameter : typeParameters) {
|
||||
if (typeParameter.getName().asString().equals(name)) {
|
||||
return typeParameter;
|
||||
}
|
||||
}
|
||||
|
||||
DeclarationDescriptor containingDeclaration = owner.getContainingDeclaration();
|
||||
if (containingDeclaration != null) {
|
||||
return getTypeVariable(
|
||||
name,
|
||||
TypeVariableResolvers.getTypeParameterDescriptors((ClassOrNamespaceDescriptor) containingDeclaration),
|
||||
containingDeclaration,
|
||||
context);
|
||||
}
|
||||
throw new RuntimeException("type parameter not found by name '" + name + "' in " + context);
|
||||
}
|
||||
}
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TypeVariableResolvers {
|
||||
|
||||
@NotNull
|
||||
public static List<TypeParameterDescriptor> getTypeParameterDescriptors(@NotNull ClassOrNamespaceDescriptor clazz) {
|
||||
if (clazz instanceof ClassDescriptor) {
|
||||
return ((ClassDescriptor) clazz).getTypeConstructor().getParameters();
|
||||
}
|
||||
else {
|
||||
return new ArrayList<TypeParameterDescriptor>(0);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static TypeVariableResolver classTypeVariableResolver(@NotNull ClassOrNamespaceDescriptor clazz, @NotNull String context) {
|
||||
return typeVariableResolverFromTypeParameters(getTypeParameterDescriptors(clazz), clazz, context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static TypeVariableResolver typeVariableResolverFromTypeParameters(
|
||||
@NotNull List<TypeParameterDescriptor> typeParameters, @NotNull DeclarationDescriptor owner, @NotNull String context) {
|
||||
|
||||
return new TypeVariableResolverFromTypeDescriptors(typeParameters, owner, context);
|
||||
}
|
||||
|
||||
}
|
||||
+10
-8
@@ -75,11 +75,11 @@ public final class JavaConstructorResolver {
|
||||
|
||||
PsiClass psiClass = classData.getPsiClass();
|
||||
|
||||
TypeVariableResolver resolverForTypeParameters = TypeVariableResolvers.classTypeVariableResolver(
|
||||
containingClass, "class " + psiClass.getQualifiedName());
|
||||
|
||||
List<TypeParameterDescriptor> typeParameters = containingClass.getTypeConstructor().getParameters();
|
||||
|
||||
TypeVariableResolver typeVariableResolver =
|
||||
new TypeVariableResolver(typeParameters, containingClass, "class " + psiClass.getQualifiedName());
|
||||
|
||||
PsiMethod[] psiConstructors = psiClass.getConstructors();
|
||||
|
||||
boolean isStatic = psiClass.hasModifierProperty(PsiModifier.STATIC);
|
||||
@@ -128,7 +128,7 @@ public final class JavaConstructorResolver {
|
||||
JetType varargElementType = null;
|
||||
if (i == methods.length - 1 && (returnType instanceof PsiArrayType)) {
|
||||
varargElementType = typeTransformer
|
||||
.transformToType(((PsiArrayType) returnType).getComponentType(), resolverForTypeParameters);
|
||||
.transformToType(((PsiArrayType) returnType).getComponentType(), typeVariableResolver);
|
||||
}
|
||||
|
||||
assert returnType != null;
|
||||
@@ -137,7 +137,7 @@ public final class JavaConstructorResolver {
|
||||
i,
|
||||
Collections.<AnnotationDescriptor>emptyList(),
|
||||
Name.identifier(method.getName()),
|
||||
typeTransformer.transformToType(returnType, resolverForTypeParameters),
|
||||
typeTransformer.transformToType(returnType, typeVariableResolver),
|
||||
annotationMethod.getDefaultValue() != null,
|
||||
varargElementType));
|
||||
}
|
||||
@@ -184,10 +184,12 @@ public final class JavaConstructorResolver {
|
||||
Collections.<AnnotationDescriptor>emptyList(), // TODO
|
||||
false);
|
||||
|
||||
String context = "constructor of class " + psiClass.getQualifiedName();
|
||||
List<TypeParameterDescriptor> typeParameters = classDescriptor.getTypeConstructor().getParameters();
|
||||
|
||||
JavaDescriptorResolver.ValueParameterDescriptors valueParameterDescriptors = valueParameterResolver.resolveParameterDescriptors(
|
||||
constructorDescriptor, constructor.getParameters(),
|
||||
TypeVariableResolvers.classTypeVariableResolver(classDescriptor, context));
|
||||
new TypeVariableResolver(typeParameters, classDescriptor, "constructor of class " + psiClass.getQualifiedName())
|
||||
);
|
||||
|
||||
if (valueParameterDescriptors.getReceiverType() != null) {
|
||||
throw new IllegalStateException();
|
||||
@@ -204,7 +206,7 @@ public final class JavaConstructorResolver {
|
||||
Collections.singletonList(alternativeMethodSignatureData.getError()));
|
||||
}
|
||||
|
||||
constructorDescriptor.initialize(classDescriptor.getTypeConstructor().getParameters(),
|
||||
constructorDescriptor.initialize(typeParameters,
|
||||
valueParameterDescriptors.getDescriptors(),
|
||||
DescriptorResolverUtils.resolveVisibility(psiConstructor),
|
||||
aStatic);
|
||||
|
||||
+3
-8
@@ -128,15 +128,10 @@ public final class JavaFunctionResolver {
|
||||
CallableMemberDescriptor.Kind.DECLARATION
|
||||
);
|
||||
|
||||
String context = "method " + method.getName() + " in class " + psiClass.getQualifiedName();
|
||||
List<TypeParameterDescriptor> methodTypeParameters = signatureResolver.resolveMethodTypeParameters(method, functionDescriptorImpl);
|
||||
|
||||
List<TypeParameterDescriptor> methodTypeParameters =
|
||||
signatureResolver.resolveMethodTypeParameters(method,
|
||||
functionDescriptorImpl);
|
||||
|
||||
TypeVariableResolver methodTypeVariableResolver = TypeVariableResolvers.typeVariableResolverFromTypeParameters(methodTypeParameters,
|
||||
functionDescriptorImpl,
|
||||
context);
|
||||
TypeVariableResolver methodTypeVariableResolver = new TypeVariableResolver(
|
||||
methodTypeParameters, functionDescriptorImpl, "method " + method.getName() + " in class " + psiClass.getQualifiedName());
|
||||
|
||||
JavaDescriptorResolver.ValueParameterDescriptors valueParameterDescriptors = parameterResolver
|
||||
.resolveParameterDescriptors(functionDescriptorImpl, method.getParameters(), methodTypeVariableResolver);
|
||||
|
||||
+3
-2
@@ -158,8 +158,9 @@ public final class JavaPropertyResolver {
|
||||
|
||||
propertyDescriptor.initialize(null, null);
|
||||
|
||||
TypeVariableResolver typeVariableResolverForPropertyInternals = TypeVariableResolvers.typeVariableResolverFromTypeParameters(
|
||||
Collections.<TypeParameterDescriptor>emptyList(), propertyDescriptor, "property " + propertyName + " in " + context);
|
||||
TypeVariableResolver typeVariableResolverForPropertyInternals =
|
||||
new TypeVariableResolver(Collections.<TypeParameterDescriptor>emptyList(), propertyDescriptor,
|
||||
"property " + propertyName + " in " + context);
|
||||
|
||||
JetType propertyType = getPropertyType(field, typeVariableResolverForPropertyInternals);
|
||||
|
||||
|
||||
+2
-4
@@ -30,7 +30,6 @@ import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
|
||||
import org.jetbrains.jet.lang.resolve.java.JavaSemanticServices;
|
||||
import org.jetbrains.jet.lang.resolve.java.TypeUsage;
|
||||
import org.jetbrains.jet.lang.resolve.java.TypeVariableResolver;
|
||||
import org.jetbrains.jet.lang.resolve.java.TypeVariableResolvers;
|
||||
import org.jetbrains.jet.lang.resolve.java.wrapper.PsiMethodWrapper;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.lang.types.Variance;
|
||||
@@ -137,9 +136,8 @@ public final class JavaSignatureResolver {
|
||||
|
||||
for (TypeParameterDescriptorInitialization psiTypeParameter : typeParametersInitialization) {
|
||||
prevTypeParameters.add(psiTypeParameter.descriptor);
|
||||
initializeTypeParameter(psiTypeParameter,
|
||||
TypeVariableResolvers
|
||||
.typeVariableResolverFromTypeParameters(typeParameters, typeParametersOwner, context));
|
||||
|
||||
initializeTypeParameter(psiTypeParameter, new TypeVariableResolver(typeParameters, typeParametersOwner, context));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -67,8 +67,7 @@ public final class JavaSupertypeResolver {
|
||||
|
||||
String context = "class " + psiClass.getQualifiedName();
|
||||
|
||||
TypeVariableResolver typeVariableResolverForSupertypes =
|
||||
TypeVariableResolvers.typeVariableResolverFromTypeParameters(typeParameters, classDescriptor, context);
|
||||
TypeVariableResolver typeVariableResolverForSupertypes = new TypeVariableResolver(typeParameters, classDescriptor, context);
|
||||
transformSupertypeList(result, psiClass.getExtendsListTypes(), typeVariableResolverForSupertypes);
|
||||
transformSupertypeList(result, psiClass.getImplementsListTypes(), typeVariableResolverForSupertypes);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user