| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'package:analysis_server/src/protocol_server.dart' as protocol; | 5 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
| 6 import 'package:analysis_server/src/services/search/search_engine.dart'; | 6 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 7 import 'package:analyzer/dart/element/element.dart'; | 7 import 'package:analyzer/dart/element/element.dart'; |
| 8 | 8 |
| 9 class ImplementedComputer { | 9 class ImplementedComputer { |
| 10 final SearchEngine searchEngine; | 10 final SearchEngine searchEngine; |
| 11 final CompilationUnitElement unitElement; | 11 final CompilationUnitElement unitElement; |
| 12 | 12 |
| 13 List<protocol.ImplementedClass> classes = <protocol.ImplementedClass>[]; | 13 List<protocol.ImplementedClass> classes = <protocol.ImplementedClass>[]; |
| 14 List<protocol.ImplementedMember> members = <protocol.ImplementedMember>[]; | 14 List<protocol.ImplementedMember> members = <protocol.ImplementedMember>[]; |
| 15 | 15 |
| 16 Set<ClassElement> subtypes; | 16 Set<String> subtypeMembers; |
| 17 | 17 |
| 18 ImplementedComputer(this.searchEngine, this.unitElement); | 18 ImplementedComputer(this.searchEngine, this.unitElement); |
| 19 | 19 |
| 20 compute() async { | 20 compute() async { |
| 21 for (ClassElement type in unitElement.types) { | 21 for (ClassElement type in unitElement.types) { |
| 22 // always include Object and its members | 22 // Always include Object and its members. |
| 23 if (type.supertype == null) { | 23 if (type.supertype == null) { |
| 24 _addImplementedClass(type); | 24 _addImplementedClass(type); |
| 25 type.accessors.forEach(_addImplementedMember); | 25 type.accessors.forEach(_addImplementedMember); |
| 26 type.fields.forEach(_addImplementedMember); | 26 type.fields.forEach(_addImplementedMember); |
| 27 type.methods.forEach(_addImplementedMember); | 27 type.methods.forEach(_addImplementedMember); |
| 28 continue; | 28 continue; |
| 29 } | 29 } |
| 30 // analyze ancestors | 30 |
| 31 subtypes = await searchEngine.searchAllSubtypes(type); | 31 // Analyze subtypes. |
| 32 if (subtypes.isNotEmpty) { | 32 subtypeMembers = await searchEngine.membersOfSubtypes(type); |
| 33 if (subtypeMembers != null) { |
| 33 _addImplementedClass(type); | 34 _addImplementedClass(type); |
| 35 type.accessors.forEach(_addMemberIfImplemented); |
| 36 type.fields.forEach(_addMemberIfImplemented); |
| 37 type.methods.forEach(_addMemberIfImplemented); |
| 34 } | 38 } |
| 35 type.accessors.forEach(_addMemberIfImplemented); | |
| 36 type.fields.forEach(_addMemberIfImplemented); | |
| 37 type.methods.forEach(_addMemberIfImplemented); | |
| 38 } | 39 } |
| 39 } | 40 } |
| 40 | 41 |
| 41 void _addImplementedClass(ClassElement type) { | 42 void _addImplementedClass(ClassElement type) { |
| 42 int offset = type.nameOffset; | 43 int offset = type.nameOffset; |
| 43 int length = type.nameLength; | 44 int length = type.nameLength; |
| 44 classes.add(new protocol.ImplementedClass(offset, length)); | 45 classes.add(new protocol.ImplementedClass(offset, length)); |
| 45 } | 46 } |
| 46 | 47 |
| 47 void _addImplementedMember(Element member) { | 48 void _addImplementedMember(Element member) { |
| 48 int offset = member.nameOffset; | 49 int offset = member.nameOffset; |
| 49 int length = member.nameLength; | 50 int length = member.nameLength; |
| 50 members.add(new protocol.ImplementedMember(offset, length)); | 51 members.add(new protocol.ImplementedMember(offset, length)); |
| 51 } | 52 } |
| 52 | 53 |
| 53 void _addMemberIfImplemented(Element element) { | 54 void _addMemberIfImplemented(Element element) { |
| 54 if (element.isSynthetic || _isStatic(element)) { | 55 if (element.isSynthetic || _isStatic(element)) { |
| 55 return; | 56 return; |
| 56 } | 57 } |
| 57 if (_hasOverride(element)) { | 58 if (_hasOverride(element)) { |
| 58 _addImplementedMember(element); | 59 _addImplementedMember(element); |
| 59 } | 60 } |
| 60 } | 61 } |
| 61 | 62 |
| 62 bool _hasOverride(Element element) { | 63 bool _hasOverride(Element element) { |
| 63 String name = element.displayName; | 64 String name = element.displayName; |
| 64 LibraryElement library = element.library; | 65 return subtypeMembers.contains(name); |
| 65 for (ClassElement subtype in subtypes) { | |
| 66 ClassMemberElement subElement = subtype.getMethod(name); | |
| 67 if (subElement == null) { | |
| 68 subElement = subtype.getField(name); | |
| 69 } | |
| 70 if (subElement != null && | |
| 71 !subElement.isStatic && | |
| 72 subElement.isAccessibleIn(library)) { | |
| 73 return true; | |
| 74 } | |
| 75 } | |
| 76 return false; | |
| 77 } | 66 } |
| 78 | 67 |
| 79 /** | 68 /** |
| 80 * Return `true` if the given [element] is a static element. | 69 * Return `true` if the given [element] is a static element. |
| 81 */ | 70 */ |
| 82 static bool _isStatic(Element element) { | 71 static bool _isStatic(Element element) { |
| 83 if (element is ExecutableElement) { | 72 if (element is ExecutableElement) { |
| 84 return element.isStatic; | 73 return element.isStatic; |
| 85 } else if (element is PropertyInducingElement) { | 74 } else if (element is PropertyInducingElement) { |
| 86 return element.isStatic; | 75 return element.isStatic; |
| 87 } | 76 } |
| 88 return false; | 77 return false; |
| 89 } | 78 } |
| 90 } | 79 } |
| OLD | NEW |