Use TypeResolver instead of analyzing artificial type cast expression

This commit is contained in:
Valentin Kipyatkov
2015-09-16 22:37:10 +03:00
parent 63947904b5
commit c12f44fa22
@@ -180,16 +180,16 @@ object ReplaceWithAnnotationAnalyzer {
val explicitImportsScope = buildExplicitImportsScope(annotation, resolutionFacade, module) val explicitImportsScope = buildExplicitImportsScope(annotation, resolutionFacade, module)
val scope = getResolutionScope(symbolDescriptor, symbolDescriptor, listOf(explicitImportsScope)) val scope = getResolutionScope(symbolDescriptor, symbolDescriptor, listOf(explicitImportsScope))
val dummyExpression = psiFactory.createExpressionByPattern("x as $0", typeReference) as JetBinaryExpressionWithTypeRHS val typeResolver = resolutionFacade.getFrontendService(TypeResolver::class.java)
val bindingTrace = BindingTraceContext()
val bindingContext = analyzeInContext(dummyExpression, module, scope, resolutionFacade) typeResolver.resolvePossiblyBareType(TypeResolutionContext(scope, bindingTrace, false, true), typeReference)
val typesToQualify = ArrayList<Pair<JetNameReferenceExpression, FqName>>() val typesToQualify = ArrayList<Pair<JetNameReferenceExpression, FqName>>()
dummyExpression.right!!.forEachDescendantOfType<JetNameReferenceExpression> { expression -> typeReference.forEachDescendantOfType<JetNameReferenceExpression> { expression ->
val parentType = expression.parent as? JetUserType ?: return@forEachDescendantOfType val parentType = expression.parent as? JetUserType ?: return@forEachDescendantOfType
if (parentType.qualifier != null) return@forEachDescendantOfType if (parentType.qualifier != null) return@forEachDescendantOfType
val targetClass = bindingContext[BindingContext.REFERENCE_TARGET, expression] as? ClassDescriptor ?: return@forEachDescendantOfType val targetClass = bindingTrace.bindingContext[BindingContext.REFERENCE_TARGET, expression] as? ClassDescriptor ?: return@forEachDescendantOfType
val fqName = targetClass.fqNameUnsafe val fqName = targetClass.fqNameUnsafe
if (fqName.isSafe) { if (fqName.isSafe) {
typesToQualify.add(expression to fqName.toSafe()) typesToQualify.add(expression to fqName.toSafe())
@@ -200,7 +200,7 @@ object ReplaceWithAnnotationAnalyzer {
nameExpression.mainReference.bindToFqName(fqName, JetSimpleNameReference.ShorteningMode.NO_SHORTENING) nameExpression.mainReference.bindToFqName(fqName, JetSimpleNameReference.ShorteningMode.NO_SHORTENING)
} }
return dummyExpression.right!!.typeElement as JetUserType return typeReference.typeElement as JetUserType
} }
private fun buildExplicitImportsScope(annotation: ReplaceWith, resolutionFacade: ResolutionFacade, module: ModuleDescriptor): ExplicitImportsScope { private fun buildExplicitImportsScope(annotation: ReplaceWith, resolutionFacade: ResolutionFacade, module: ModuleDescriptor): ExplicitImportsScope {