Skip to content

sl-inherited-methods #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -901,29 +901,46 @@ class Class extends ModelElement {

bool get hasInstanceMethods => instanceMethods.isNotEmpty;

// TODO: make this method smarter about hierarchies and overrides. Right
// now, we're creating a flat list. We're not paying attention to where
// these methods are actually coming from. This might turn out to be a
// problem if we want to show that info later.
List<Method> get inheritedMethods {
if (_inheritedMethods != null) return _inheritedMethods;

InheritanceManager manager = new InheritanceManager(element.library);
MemberMap cmap = manager.getMapOfMembersInheritedFromClasses(element);
MemberMap imap = manager.getMapOfMembersInheritedFromInterfaces(element);

// remove methods that exist on this class
_methods.forEach((method) {
cmap.remove(method.name);
imap.remove(method.name);
});

_inheritedMethods = [];
var vs = [];
List<ExecutableElement> vs = [];
Set<String> uniqueNames = new Set();

for (var i = 0; i < cmap.size; i++) {
// XXX: if we care about showing a hierarchy with our inherited methods,
// then don't do this
if (uniqueNames.contains(cmap.getKey(i))) continue;

uniqueNames.add(cmap.getKey(i));
vs.add(cmap.getValue(i));
}

for (var i = 0; i < imap.size; i++) {
// XXX: if we care about showing a hierarchy with our inherited methods,
// then don't do this
if (uniqueNames.contains(imap.getKey(i))) continue;

uniqueNames.add(imap.getKey(i));
vs.add(imap.getValue(i));
}

vs = vs.toSet().toList();

for (var value in vs) {
for (ExecutableElement value in vs) {
if (value != null &&
value is MethodElement &&
!value.isPrivate &&
Expand Down Expand Up @@ -1003,23 +1020,39 @@ class Class extends ModelElement {

bool get hasInheritedMethods => inheritedMethods.isNotEmpty;

// TODO: make this method smarter about hierarchies and overrides. Right
// now, we're creating a flat list. We're not paying attention to where
// these methods are actually coming from. This might turn out to be a
// problem if we want to show that info later.
List<Field> get inheritedProperties {
if (_inheritedProperties != null) return _inheritedProperties;

InheritanceManager manager = new InheritanceManager(element.library);
MemberMap cmap = manager.getMapOfMembersInheritedFromClasses(element);
MemberMap imap = manager.getMapOfMembersInheritedFromInterfaces(element);

_inheritedProperties = [];
var vs = [];
List<ExecutableElement> vs = [];
Set<String> uniqueNames = new Set();

for (var i = 0; i < cmap.size; i++) {
// XXX: if we care about showing a hierarchy with our inherited methods,
// then don't do this
if (uniqueNames.contains(cmap.getKey(i))) continue;

uniqueNames.add(cmap.getKey(i));
vs.add(cmap.getValue(i));
}

for (var i = 0; i < imap.size; i++) {
// XXX: if we care about showing a hierarchy with our inherited methods,
// then don't do this
if (uniqueNames.contains(imap.getKey(i))) continue;

uniqueNames.add(imap.getKey(i));
vs.add(imap.getValue(i));
}

vs = vs.toSet().toList();

vs.removeWhere((it) => instanceProperties.any((i) => it.name == i.name));

for (var value in vs) {
Expand Down
35 changes: 34 additions & 1 deletion test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ void main() {

group('Class', () {
List<Class> classes;
Class Apple, B, Cat, Dog, F, DT;
Class Apple, B, Cat, Dog, F, DT, SpecialList;

setUp(() {
classes = exLibrary.classes;
Expand All @@ -264,6 +264,8 @@ void main() {
Dog = classes.firstWhere((c) => c.name == 'Dog');
F = classes.firstWhere((c) => c.name == 'F');
DT = classes.firstWhere((c) => c.name == 'DateTime');
SpecialList =
fakeLibrary.classes.firstWhere((c) => c.name == 'SpecialList');
});

test('we got the classes we expect', () {
Expand Down Expand Up @@ -356,6 +358,37 @@ void main() {
expect(DT.instanceMethods[0].href, isNotNull);
expect(DT.instanceProperties[0].href, isNotNull);
});

test('F has a single instance method', () {
expect(F.instanceMethods, hasLength(1));
expect(F.instanceMethods.first.name, equals('methodWithGenericParam'));
});

test('F has many inherited methods', () {
expect(F.inheritedMethods, hasLength(4));
expect(F.inheritedMethods.map((im) => im.name),
equals(['foo', 'getClassA', 'test', 'testMethod']));
});

test('F has zero instance properties', () {
expect(F.instanceProperties, hasLength(0));
});

test('F has a few inherited properties', () {
expect(F.inheritedProperties, hasLength(2));
expect(F.inheritedProperties.map((ip) => ip.name),
equals(['isImplemented', 'name']));
});

test('SpecialList has zero instance methods', () {
expect(SpecialList.instanceMethods, hasLength(0));
});

test('SpecialList has many inherited methods', () {
expect(SpecialList.inheritedMethods, hasLength(43));
expect(SpecialList.inheritedMethods.first.name, equals('add'));
expect(SpecialList.inheritedMethods[1].name, equals('addAll'));
});
});

group('Enum', () {
Expand Down
15 changes: 15 additions & 0 deletions test_package/lib/fake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ library fake;

import 'dart:async';

import 'dart:collection';

import 'example.dart';

/// Useful for annotations.
Expand Down Expand Up @@ -353,3 +355,16 @@ class WithGetterAndSetter {

/// I have a generic and it extends [Foo2]
class HasGenericWithExtends<T extends Foo2> {}

/// Extends [ListBase]
class SpecialList<E> extends ListBase<E> {
E operator [](int index) {
return null;
}

int get length => 0;

void set length(int length) {}

void operator []=(int index, E value) {}
}