diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/AlternativeSignatureParsing.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/AlternativeSignatureParsing.java index 07eba765388..697c5281daa 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/AlternativeSignatureParsing.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/AlternativeSignatureParsing.java @@ -16,6 +16,7 @@ package org.jetbrains.jet.lang.resolve.java; +import com.intellij.openapi.util.Ref; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.*; @@ -35,11 +36,19 @@ import java.util.List; * @since 6/5/12 */ class AlternativeSignatureParsing { - static JetType computeAlternativeTypeFromAnnotation(JetTypeElement alternativeTypeElement, final JetType autoType) { - return alternativeTypeElement.accept(new JetVisitor() { + static JetType computeAlternativeTypeFromAnnotation(JetTypeElement alternativeTypeElement, final JetType autoType) + throws AlternativeSignatureMismatchException { + final Ref exception = new Ref(); + JetType result = alternativeTypeElement.accept(new JetVisitor() { @Override 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 @@ -57,17 +66,24 @@ class AlternativeSignatureParsing { return visitCommonType(type); } - public JetType visitCommonType(JetTypeElement type) { - List arguments = autoType.getArguments(); - List altArguments = new ArrayList(); - for (int i = 0, size = arguments.size(); i < size; i++) { - JetTypeElement argumentAlternativeTypeElement = type.getTypeArgumentsAsTypes().get(i).getTypeElement(); - TypeProjection argument = arguments.get(i); - JetType alternativeType = computeAlternativeTypeFromAnnotation(argumentAlternativeTypeElement, argument.getType()); - altArguments.add(new TypeProjection(argument.getProjectionKind(), alternativeType)); + private JetType visitCommonType(JetTypeElement type) { + try { + List arguments = autoType.getArguments(); + List altArguments = new ArrayList(); + for (int i = 0, size = arguments.size(); i < size; i++) { + JetTypeElement argumentAlternativeTypeElement = type.getTypeArgumentsAsTypes().get(i).getTypeElement(); + TypeProjection argument = arguments.get(i); + 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 @@ -75,10 +91,16 @@ class AlternativeSignatureParsing { throw new UnsupportedOperationException("Self-types are not supported yet"); } }, null); + //noinspection ThrowableResultOfMethodCallIgnored + if (exception.get() != null) { + throw exception.get(); + } + return result; } - static JavaDescriptorResolver.ValueParameterDescriptors computeAlternativeValueParameters(JavaDescriptorResolver.ValueParameterDescriptors valueParameterDescriptors, - JetNamedFunction altFunDeclaration) { + static JavaDescriptorResolver.ValueParameterDescriptors computeAlternativeValueParameters( + JavaDescriptorResolver.ValueParameterDescriptors valueParameterDescriptors, + JetNamedFunction altFunDeclaration) throws AlternativeSignatureMismatchException { List parameterDescriptors = valueParameterDescriptors.descriptors; List altParamDescriptors = new ArrayList(); for (int i = 0, size = parameterDescriptors.size(); i < size; i++) { @@ -101,13 +123,14 @@ class AlternativeSignatureParsing { } JetType altReceiverType = 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); } static List computeAlternativeTypeParameters(List typeParameterDescriptors, - JetNamedFunction altFunDeclaration) { + JetNamedFunction altFunDeclaration) throws AlternativeSignatureMismatchException { List altParamDescriptors = new ArrayList(); for (int i = 0, size = typeParameterDescriptors.size(); i < size; i++) { TypeParameterDescriptor pd = typeParameterDescriptors.get(i); diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java index 75de4e21231..24c2d115398 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaDescriptorResolver.java @@ -1654,15 +1654,20 @@ public class JavaDescriptorResolver implements DependencyClassByQualifiedNameRes throw new AlternativeSignatureMismatchException(errorText); } else { - valueParameterDescriptors = AlternativeSignatureParsing + ValueParameterDescriptors altValueParameters = AlternativeSignatureParsing .computeAlternativeValueParameters(valueParameterDescriptors, altFunDeclaration); JetTypeReference returnTypeRef = altFunDeclaration.getReturnTypeRef(); - if (returnTypeRef != null) { - returnType = AlternativeSignatureParsing.computeAlternativeTypeFromAnnotation(returnTypeRef.getTypeElement(), - returnType); - } - methodTypeParameters = AlternativeSignatureParsing.computeAlternativeTypeParameters(methodTypeParameters, - altFunDeclaration); + JetType altReturnType = returnTypeRef != null + ? AlternativeSignatureParsing.computeAlternativeTypeFromAnnotation( + returnTypeRef.getTypeElement(), returnType) + : returnType; + List altTypeParameters = + AlternativeSignatureParsing.computeAlternativeTypeParameters(methodTypeParameters, + altFunDeclaration); + // if no exceptions were thrown, save alternative data + valueParameterDescriptors = altValueParameters; + returnType = altReturnType; + methodTypeParameters = altTypeParameters; } } catch (AlternativeSignatureMismatchException e) {