Skip to content

Commit

Permalink
Merge pull request #207 from dart-lang/class-static-methods
Browse files Browse the repository at this point in the history
display static methods on a class
  • Loading branch information
sethladd committed Feb 17, 2015
2 parents 548d703 + 4dc89cf commit 0799309
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
35 changes: 29 additions & 6 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,9 @@ class Class extends ModelElement {
ElementType _supertype;
List<ElementType> _interfaces;
List<Constructor> _constructors;
List<Method> _methods;
List<Method> _allMethods;
List<Method> _staticMethods;
List<Method> _instanceMethods;
List<Field> _fields;
List<Field> _staticFields;
List<Field> _instanceFields;
Expand Down Expand Up @@ -592,10 +594,10 @@ class Class extends ModelElement {
return _constructors;
}

List<Method> get methods {
if (_methods != null) return _methods;
List<Method> get _methods {
if (_allMethods != null) return _allMethods;

_methods = _cls.methods
_allMethods = _cls.methods
.where(isPublic)
.map((e) {
var mSource =
Expand All @@ -604,10 +606,28 @@ class Class extends ModelElement {
})
.toList(growable:false);

return _methods;
return _allMethods;
}

bool get hasMethods => methods.isNotEmpty;
List<Method> get staticMethods {
if (_staticMethods != null) return _staticMethods;

_staticMethods = _methods.where((m) => m.isStatic).toList(growable:false);

return _staticMethods;
}

List<Method> get instanceMethods {
if (_instanceMethods != null) return _instanceMethods;

_instanceMethods = _methods.where((m) => !m.isStatic).toList(growable:false);

return _instanceMethods;
}

bool get hasInstanceMethods => instanceMethods.isNotEmpty;

bool get hasStaticMethods => staticMethods.isNotEmpty;

bool get isErrorOrException => _isClassErrorOrException(element);

Expand Down Expand Up @@ -739,6 +759,9 @@ class Method extends ModelElement {
return null;
}

@override
bool get isStatic => _method.isStatic;

String get linkedReturnType => type.createLinkedReturnTypeName();

@override
Expand Down
31 changes: 25 additions & 6 deletions templates/new/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ <h2>Static Properties</h2>
</section>
{{/class.hasStaticProperties}}

{{#class.hasMethods}}
<section id="methods-summary" class="summary">
<h2>Methods</h2>
{{#class.hasStaticMethods}}
<section id="static-methods-summary" class="summary">
<h2>Static Methods</h2>
<dl class="withreturntypes">
{{#class.methods}}
{{#class.staticMethods}}
<dt>
<code>
<span class="returntype">{{{ linkedReturnType }}}</span>
Expand All @@ -131,10 +131,29 @@ <h2>Methods</h2>
<dd>
{{#oneLiner}}{{ documentation }}{{/oneLiner}}
</dd>
{{/class.methods}}
{{/class.staticMethods}}
</dl>
</section>
{{/class.hasMethods}}
{{/class.hasStaticMethods}}

{{#class.hasInstanceMethods}}
<section id="instance-methods-summary" class="summary">
<h2>Instance Methods</h2>
<dl class="withreturntypes">
{{#class.instanceMethods}}
<dt>
<code>
<span class="returntype">{{{ linkedReturnType }}}</span>
<span class="name">{{ name }}</span>({{{ linkedParams }})
</code>
</dt>
<dd>
{{#oneLiner}}{{ documentation }}{{/oneLiner}}
</dd>
{{/class.instanceMethods}}
</dl>
</section>
{{/class.hasInstanceMethods}}

</div>

Expand Down
8 changes: 8 additions & 0 deletions test/fake_package/lib/fake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ class LongFirstLine extends Object with MixMeIn implements Interface, AnotherInt
static int get staticGetter => 11111;

static void set staticOnlySetter(bool thing) {}

/// Just a static method with no parameters.
///
/// Returns an int.
static int staticMethodNoParams() => 42;

/// A static method that takes a single dynamic thing, and returns void.
static void staticMethodReturnsVoid(dynamicThing) {}
}

/// My bad!
Expand Down
14 changes: 7 additions & 7 deletions test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void main() {
});

test('get methods', () {
expect(B.methods, hasLength(1));
expect(B.instanceMethods, hasLength(1));
});
});

Expand All @@ -160,9 +160,9 @@ void main() {

group('Method', () {
var c = l.getTypes()[1];
var m = c.methods[0];
var m2 = lib2.getTypes()[1].methods[0];
var m3 = l.getTypes()[0].methods[0];
var m = c.instanceMethods[0];
var m2 = lib2.getTypes()[1].instanceMethods[0];
var m3 = l.getTypes()[0].instanceMethods[0];

test('overriden method', () {
expect(m.getOverriddenElement().runtimeType.toString(), 'Method');
Expand Down Expand Up @@ -275,9 +275,9 @@ void main() {
setUp(() {
c = l.getTypes()[0]; // A

m1 = c.methods[0]; // m1
printMsg = c.methods[1]; // printMsg
isGreaterThan = c.methods[2]; // isGreaterThan
m1 = c.instanceMethods[0]; // m1
printMsg = c.instanceMethods[1]; // printMsg
isGreaterThan = c.instanceMethods[2]; // isGreaterThan

p1 = isGreaterThan.parameters[1]; // {int check:5}
p2 = printMsg.parameters[1]; // [bool linebreak]
Expand Down

0 comments on commit 0799309

Please sign in to comment.