diff --git a/pkg/analysis_server/analyzer_use_new_elements.txt b/pkg/analysis_server/analyzer_use_new_elements.txt index f6f472e599ab..bf827a5e2aa4 100644 --- a/pkg/analysis_server/analyzer_use_new_elements.txt +++ b/pkg/analysis_server/analyzer_use_new_elements.txt @@ -285,6 +285,7 @@ lib/src/services/correction/dart/create_method_or_function.dart lib/src/services/correction/dart/create_no_such_method.dart lib/src/services/correction/dart/create_parameter.dart lib/src/services/correction/dart/data_driven.dart +lib/src/services/correction/dart/encapsulate_field.dart lib/src/services/correction/dart/exchange_operands.dart lib/src/services/correction/dart/extend_class_for_mixin.dart lib/src/services/correction/dart/flutter_move_down.dart diff --git a/pkg/analysis_server/lib/src/services/correction/dart/encapsulate_field.dart b/pkg/analysis_server/lib/src/services/correction/dart/encapsulate_field.dart index 78fd48658b47..eb9c8271ac90 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/encapsulate_field.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/encapsulate_field.dart @@ -5,7 +5,7 @@ import 'package:analysis_server/src/services/correction/assist.dart'; import 'package:analysis_server_plugin/edit/dart/correction_producer.dart'; import 'package:analyzer/dart/ast/ast.dart'; -import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/src/dart/element/inheritance_manager3.dart'; import 'package:analyzer_plugin/utilities/assist/assist.dart'; import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; @@ -50,7 +50,8 @@ class EncapsulateField extends ResolvedCorrectionProducer { } var field = fields.first; var nameToken = field.name; - var fieldElement = field.declaredElement as FieldElement; + var fieldFragment = field.declaredFragment as FieldFragment; + var fieldElement = fieldFragment.element; // should have a public name var name = nameToken.lexeme; if (Identifier.isPrivateName(name)) { @@ -63,15 +64,15 @@ class EncapsulateField extends ResolvedCorrectionProducer { // Should be in a class or mixin. List classMembers; - InterfaceElement parentElement; + InterfaceElement2 parentElement; var parent = fieldDeclaration.parent; switch (parent) { case ClassDeclaration(): classMembers = parent.members; - parentElement = parent.declaredElement!; + parentElement = parent.declaredFragment!.element; case MixinDeclaration(): classMembers = parent.members; - parentElement = parent.declaredElement!; + parentElement = parent.declaredFragment!.element; default: return; } @@ -129,7 +130,7 @@ class EncapsulateField extends ResolvedCorrectionProducer { } // Write getter. - var overriddenGetters = inheritanceManager.getOverridden2( + var overriddenGetters = inheritanceManager.getOverridden4( parentElement, Name(null, name), ); @@ -137,7 +138,7 @@ class EncapsulateField extends ResolvedCorrectionProducer { builder.write(' ${typeCode}get $name => _$name;'); // Write setter. - var overriddenSetters = inheritanceManager.getOverridden2( + var overriddenSetters = inheritanceManager.getOverridden4( parentElement, Name(null, '$name='), ); @@ -152,15 +153,15 @@ class EncapsulateField extends ResolvedCorrectionProducer { void _updateReferencesInConstructor( DartFileEditBuilder builder, ConstructorDeclaration constructor, - FieldElement fieldElement, + FieldElement2 fieldElement, String name, ) { for (var parameter in constructor.parameters.parameters) { var identifier = parameter.name; - var parameterElement = parameter.declaredElement; + var parameterElement = parameter.declaredFragment?.element; if (identifier != null && - parameterElement is FieldFormalParameterElement && - parameterElement.field == fieldElement) { + parameterElement is FieldFormalParameterElement2 && + parameterElement.field2 == fieldElement) { if (parameter.isNamed && parameter is DefaultFormalParameter) { var normalParam = parameter.parameter; if (normalParam is FieldFormalParameter) { @@ -182,7 +183,7 @@ class EncapsulateField extends ResolvedCorrectionProducer { } for (var initializer in constructor.initializers) { if (initializer is ConstructorFieldInitializer && - initializer.fieldName.staticElement == fieldElement) { + initializer.fieldName.element == fieldElement) { builder.addSimpleReplacement( range.node(initializer.fieldName), '_$name', @@ -194,7 +195,7 @@ class EncapsulateField extends ResolvedCorrectionProducer { void _updateReferencesInConstructors( DartFileEditBuilder builder, List classMembers, - FieldElement fieldElement, + FieldElement2 fieldElement, String name, ) { for (var constructor in classMembers) { diff --git a/pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart b/pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart index 56f78b7e0363..3e5e2bd5a3e3 100644 --- a/pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart +++ b/pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart @@ -11,6 +11,7 @@ import 'package:analyzer/src/dart/element/member.dart'; import 'package:analyzer/src/dart/element/type_algebra.dart'; import 'package:analyzer/src/dart/element/type_system.dart'; import 'package:analyzer/src/utilities/extensions/collection.dart'; +import 'package:analyzer/src/utilities/extensions/element.dart'; import 'package:meta/meta.dart'; /// Failure because of there is no most specific signature in [candidates]. @@ -410,6 +411,18 @@ class InheritanceManager3 { return interface.overridden[name]; } + /// Return all members of mixins, superclasses, and interfaces that a member + /// with the given [name], defined in the [element], would override; or `null` + /// if no members would be overridden. + List? getOverridden4( + InterfaceElement2 element, Name name) { + var interface = getInterface2(element); + var fragments = interface.overridden[name]; + return fragments + ?.map((fragment) => fragment.asElement2 as ExecutableElement2) + .toList(); + } + /// Remove interfaces for classes defined in specified libraries. void removeOfLibraries(Set uriSet) { _interfaces.removeWhere((element, _) {