Moved alternative signature parsing logic from JDR to separate class.

This commit is contained in:
Evgeny Gerashchenko
2012-06-05 13:21:32 +04:00
parent 968d159f86
commit 247e3978d9
2 changed files with 99 additions and 64 deletions
@@ -0,0 +1,92 @@
/*
* Copyright 2010-2012 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.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptorImpl;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.JetTypeImpl;
import org.jetbrains.jet.lang.types.TypeProjection;
import org.jetbrains.jet.lang.types.TypeUtils;
import java.util.ArrayList;
import java.util.List;
/**
* @author Evgeny Gerashchenko
* @since 6/5/12
*/
class AlternativeSignatureParsing {
static JetType computeAlternativeTypeFromAnnotation(JetTypeElement alternativeTypeElement, final JetType autoType) {
return alternativeTypeElement.accept(new JetVisitor<JetType, Void>() {
@Override
public JetType visitNullableType(JetNullableType nullableType, Void data) {
return TypeUtils.makeNullable(computeAlternativeTypeFromAnnotation(nullableType.getInnerType(), autoType));
}
@Override
public JetType visitFunctionType(JetFunctionType type, Void data) {
return autoType; //TODO
}
@Override
public JetType visitTupleType(JetTupleType type, Void data) {
return autoType; //TODO
}
@Override
public JetType visitUserType(JetUserType type, Void data) {
List<TypeProjection> arguments = autoType.getArguments();
List<TypeProjection> altArguments = new ArrayList<TypeProjection>();
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());
}
@Override
public JetType visitSelfType(JetSelfType type, Void data) {
throw new UnsupportedOperationException("Self-types are not supported yet");
}
}, null);
}
static JavaDescriptorResolver.ValueParameterDescriptors computeAlternativeValueParameters(JavaDescriptorResolver.ValueParameterDescriptors valueParameterDescriptors,
JetNamedFunction altFunDeclaration) {
List<ValueParameterDescriptor> parameterDescriptors = valueParameterDescriptors.descriptors;
List<ValueParameterDescriptor> altParamDescriptors = new ArrayList<ValueParameterDescriptor>();
for (int i = 0, size = parameterDescriptors.size(); i < size; i++) {
ValueParameterDescriptor pd = parameterDescriptors.get(i);
JetTypeElement alternativeTypeElement = altFunDeclaration.getValueParameters().get(i).getTypeReference().getTypeElement();
JetType alternativeType = computeAlternativeTypeFromAnnotation(alternativeTypeElement, pd.getType());
// TODO vararg
altParamDescriptors.add(new ValueParameterDescriptorImpl(pd.getContainingDeclaration(), pd.getIndex(), pd.getAnnotations(),
pd.getName(), pd.isVar(), alternativeType, pd.declaresDefaultValue(),
pd.getVarargElementType()));
}
JetType altReceiverType = null;
if (valueParameterDescriptors.receiverType != null) {
altReceiverType = computeAlternativeTypeFromAnnotation(altFunDeclaration.getReceiverTypeRef().getTypeElement(), valueParameterDescriptors.receiverType);
}
return new JavaDescriptorResolver.ValueParameterDescriptors(altReceiverType, altParamDescriptors);
}
}
@@ -1029,11 +1029,11 @@ public class JavaDescriptorResolver implements DependencyClassByQualifiedNameRes
return psiClassFinder.findPsiClass(packageFQN.child(Name.identifier(JvmAbi.PACKAGE_CLASS)), PsiClassFinder.RuntimeClassesHandleMode.IGNORE);
}
private static class ValueParameterDescriptors {
private final JetType receiverType;
private final List<ValueParameterDescriptor> descriptors;
static class ValueParameterDescriptors {
final JetType receiverType;
final List<ValueParameterDescriptor> descriptors;
private ValueParameterDescriptors(@Nullable JetType receiverType, List<ValueParameterDescriptor> descriptors) {
ValueParameterDescriptors(@Nullable JetType receiverType, List<ValueParameterDescriptor> descriptors) {
this.receiverType = receiverType;
this.descriptors = descriptors;
}
@@ -1570,64 +1570,6 @@ public class JavaDescriptorResolver implements DependencyClassByQualifiedNameRes
return new ValueParameterDescriptors(receiverType, result);
}
private static JetType computeAlternativeTypeFromAnnotation(JetTypeElement alternativeTypeElement, final JetType autoType) {
return alternativeTypeElement.accept(new JetVisitor<JetType, Void>() {
@Override
public JetType visitNullableType(JetNullableType nullableType, Void data) {
return TypeUtils.makeNullable(computeAlternativeTypeFromAnnotation(nullableType.getInnerType(), autoType));
}
@Override
public JetType visitFunctionType(JetFunctionType type, Void data) {
return autoType; //TODO
}
@Override
public JetType visitTupleType(JetTupleType type, Void data) {
return autoType; //TODO
}
@Override
public JetType visitUserType(JetUserType type, Void data) {
List<TypeProjection> arguments = autoType.getArguments();
List<TypeProjection> altArguments = new ArrayList<TypeProjection>();
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());
}
@Override
public JetType visitSelfType(JetSelfType type, Void data) {
throw new UnsupportedOperationException("Self-types are not supported yet");
}
}, null);
}
private static ValueParameterDescriptors computeAlternativeValueParameters(ValueParameterDescriptors valueParameterDescriptors,
JetNamedFunction altFunDeclaration) {
List<ValueParameterDescriptor> parameterDescriptors = valueParameterDescriptors.descriptors;
List<ValueParameterDescriptor> altParamDescriptors = new ArrayList<ValueParameterDescriptor>();
for (int i = 0, size = parameterDescriptors.size(); i < size; i++) {
ValueParameterDescriptor pd = parameterDescriptors.get(i);
JetTypeElement alternativeTypeElement = altFunDeclaration.getValueParameters().get(i).getTypeReference().getTypeElement();
JetType alternativeType = computeAlternativeTypeFromAnnotation(alternativeTypeElement, pd.getType());
// TODO vararg
altParamDescriptors.add(new ValueParameterDescriptorImpl(pd.getContainingDeclaration(), pd.getIndex(), pd.getAnnotations(),
pd.getName(), pd.isVar(), alternativeType, pd.declaresDefaultValue(),
pd.getVarargElementType()));
}
JetType altReceiverType = null;
if (valueParameterDescriptors.receiverType != null) {
altReceiverType = computeAlternativeTypeFromAnnotation(altFunDeclaration.getReceiverTypeRef().getTypeElement(), valueParameterDescriptors.receiverType);
}
return new ValueParameterDescriptors(altReceiverType, altParamDescriptors);
}
@Nullable
private FunctionDescriptorImpl resolveMethodToFunctionDescriptor(
@NotNull final PsiClass psiClass, final PsiMethodWrapper method,
@@ -1674,10 +1616,11 @@ public class JavaDescriptorResolver implements DependencyClassByQualifiedNameRes
String signature = method.getSignatureAnnotation().signature();
if (!signature.isEmpty()) {
JetNamedFunction altFunDeclaration = JetPsiFactory.createFunction(project, signature);
valueParameterDescriptors = computeAlternativeValueParameters(valueParameterDescriptors, altFunDeclaration);
valueParameterDescriptors = AlternativeSignatureParsing
.computeAlternativeValueParameters(valueParameterDescriptors, altFunDeclaration);
JetTypeReference returnTypeRef = altFunDeclaration.getReturnTypeRef();
if (returnTypeRef != null) {
returnType = computeAlternativeTypeFromAnnotation(returnTypeRef.getTypeElement(), returnType);
returnType = AlternativeSignatureParsing.computeAlternativeTypeFromAnnotation(returnTypeRef.getTypeElement(), returnType);
}
}