Made methods in AlternativeSignatureParsing throwing AlternativeSignatureMismatchException and added proper handling of them.

This commit is contained in:
Evgeny Gerashchenko
2012-06-08 02:14:43 +04:00
parent 4cbfa9beb6
commit 16565e1101
2 changed files with 52 additions and 24 deletions
@@ -16,6 +16,7 @@
package org.jetbrains.jet.lang.resolve.java; package org.jetbrains.jet.lang.resolve.java;
import com.intellij.openapi.util.Ref;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
@@ -35,11 +36,19 @@ import java.util.List;
* @since 6/5/12 * @since 6/5/12
*/ */
class AlternativeSignatureParsing { class AlternativeSignatureParsing {
static JetType computeAlternativeTypeFromAnnotation(JetTypeElement alternativeTypeElement, final JetType autoType) { static JetType computeAlternativeTypeFromAnnotation(JetTypeElement alternativeTypeElement, final JetType autoType)
return alternativeTypeElement.accept(new JetVisitor<JetType, Void>() { throws AlternativeSignatureMismatchException {
final Ref<AlternativeSignatureMismatchException> exception = new Ref<AlternativeSignatureMismatchException>();
JetType result = alternativeTypeElement.accept(new JetVisitor<JetType, Void>() {
@Override @Override
public JetType visitNullableType(JetNullableType nullableType, Void data) { public JetType visitNullableType(JetNullableType nullableType, Void data) {
return TypeUtils.makeNullable(computeAlternativeTypeFromAnnotation(nullableType.getInnerType(), autoType)); try {
return TypeUtils.makeNullable(computeAlternativeTypeFromAnnotation(nullableType.getInnerType(), autoType));
}
catch (AlternativeSignatureMismatchException e) {
exception.set(e);
return null;
}
} }
@Override @Override
@@ -57,17 +66,24 @@ class AlternativeSignatureParsing {
return visitCommonType(type); return visitCommonType(type);
} }
public JetType visitCommonType(JetTypeElement type) { private JetType visitCommonType(JetTypeElement type) {
List<TypeProjection> arguments = autoType.getArguments(); try {
List<TypeProjection> altArguments = new ArrayList<TypeProjection>(); List<TypeProjection> arguments = autoType.getArguments();
for (int i = 0, size = arguments.size(); i < size; i++) { List<TypeProjection> altArguments = new ArrayList<TypeProjection>();
JetTypeElement argumentAlternativeTypeElement = type.getTypeArgumentsAsTypes().get(i).getTypeElement(); for (int i = 0, size = arguments.size(); i < size; i++) {
TypeProjection argument = arguments.get(i); JetTypeElement argumentAlternativeTypeElement = type.getTypeArgumentsAsTypes().get(i).getTypeElement();
JetType alternativeType = computeAlternativeTypeFromAnnotation(argumentAlternativeTypeElement, argument.getType()); TypeProjection argument = arguments.get(i);
altArguments.add(new TypeProjection(argument.getProjectionKind(), alternativeType)); JetType alternativeType =
computeAlternativeTypeFromAnnotation(argumentAlternativeTypeElement, argument.getType());
altArguments.add(new TypeProjection(argument.getProjectionKind(), alternativeType));
}
return new JetTypeImpl(autoType.getAnnotations(), autoType.getConstructor(), false,
altArguments, autoType.getMemberScope());
}
catch (AlternativeSignatureMismatchException e) {
exception.set(e);
return null;
} }
return new JetTypeImpl(autoType.getAnnotations(), autoType.getConstructor(), false,
altArguments, autoType.getMemberScope());
} }
@Override @Override
@@ -75,10 +91,16 @@ class AlternativeSignatureParsing {
throw new UnsupportedOperationException("Self-types are not supported yet"); throw new UnsupportedOperationException("Self-types are not supported yet");
} }
}, null); }, null);
//noinspection ThrowableResultOfMethodCallIgnored
if (exception.get() != null) {
throw exception.get();
}
return result;
} }
static JavaDescriptorResolver.ValueParameterDescriptors computeAlternativeValueParameters(JavaDescriptorResolver.ValueParameterDescriptors valueParameterDescriptors, static JavaDescriptorResolver.ValueParameterDescriptors computeAlternativeValueParameters(
JetNamedFunction altFunDeclaration) { JavaDescriptorResolver.ValueParameterDescriptors valueParameterDescriptors,
JetNamedFunction altFunDeclaration) throws AlternativeSignatureMismatchException {
List<ValueParameterDescriptor> parameterDescriptors = valueParameterDescriptors.descriptors; List<ValueParameterDescriptor> parameterDescriptors = valueParameterDescriptors.descriptors;
List<ValueParameterDescriptor> altParamDescriptors = new ArrayList<ValueParameterDescriptor>(); List<ValueParameterDescriptor> altParamDescriptors = new ArrayList<ValueParameterDescriptor>();
for (int i = 0, size = parameterDescriptors.size(); i < size; i++) { for (int i = 0, size = parameterDescriptors.size(); i < size; i++) {
@@ -101,13 +123,14 @@ class AlternativeSignatureParsing {
} }
JetType altReceiverType = null; JetType altReceiverType = null;
if (valueParameterDescriptors.receiverType != null) { if (valueParameterDescriptors.receiverType != null) {
altReceiverType = computeAlternativeTypeFromAnnotation(altFunDeclaration.getReceiverTypeRef().getTypeElement(), valueParameterDescriptors.receiverType); altReceiverType = computeAlternativeTypeFromAnnotation(altFunDeclaration.getReceiverTypeRef().getTypeElement(),
valueParameterDescriptors.receiverType);
} }
return new JavaDescriptorResolver.ValueParameterDescriptors(altReceiverType, altParamDescriptors); return new JavaDescriptorResolver.ValueParameterDescriptors(altReceiverType, altParamDescriptors);
} }
static List<TypeParameterDescriptor> computeAlternativeTypeParameters(List<TypeParameterDescriptor> typeParameterDescriptors, static List<TypeParameterDescriptor> computeAlternativeTypeParameters(List<TypeParameterDescriptor> typeParameterDescriptors,
JetNamedFunction altFunDeclaration) { JetNamedFunction altFunDeclaration) throws AlternativeSignatureMismatchException {
List<TypeParameterDescriptor> altParamDescriptors = new ArrayList<TypeParameterDescriptor>(); List<TypeParameterDescriptor> altParamDescriptors = new ArrayList<TypeParameterDescriptor>();
for (int i = 0, size = typeParameterDescriptors.size(); i < size; i++) { for (int i = 0, size = typeParameterDescriptors.size(); i < size; i++) {
TypeParameterDescriptor pd = typeParameterDescriptors.get(i); TypeParameterDescriptor pd = typeParameterDescriptors.get(i);
@@ -1654,15 +1654,20 @@ public class JavaDescriptorResolver implements DependencyClassByQualifiedNameRes
throw new AlternativeSignatureMismatchException(errorText); throw new AlternativeSignatureMismatchException(errorText);
} }
else { else {
valueParameterDescriptors = AlternativeSignatureParsing ValueParameterDescriptors altValueParameters = AlternativeSignatureParsing
.computeAlternativeValueParameters(valueParameterDescriptors, altFunDeclaration); .computeAlternativeValueParameters(valueParameterDescriptors, altFunDeclaration);
JetTypeReference returnTypeRef = altFunDeclaration.getReturnTypeRef(); JetTypeReference returnTypeRef = altFunDeclaration.getReturnTypeRef();
if (returnTypeRef != null) { JetType altReturnType = returnTypeRef != null
returnType = AlternativeSignatureParsing.computeAlternativeTypeFromAnnotation(returnTypeRef.getTypeElement(), ? AlternativeSignatureParsing.computeAlternativeTypeFromAnnotation(
returnType); returnTypeRef.getTypeElement(), returnType)
} : returnType;
methodTypeParameters = AlternativeSignatureParsing.computeAlternativeTypeParameters(methodTypeParameters, List<TypeParameterDescriptor> altTypeParameters =
altFunDeclaration); AlternativeSignatureParsing.computeAlternativeTypeParameters(methodTypeParameters,
altFunDeclaration);
// if no exceptions were thrown, save alternative data
valueParameterDescriptors = altValueParameters;
returnType = altReturnType;
methodTypeParameters = altTypeParameters;
} }
} }
catch (AlternativeSignatureMismatchException e) { catch (AlternativeSignatureMismatchException e) {