Skip to content

Commit

Permalink
Merge pull request #203 from dart-lang/class-properties
Browse files Browse the repository at this point in the history
properties for a class (incl getters and setters)
  • Loading branch information
sethladd committed Feb 16, 2015
2 parents 0bfe56f + 1b43bf0 commit 9186c93
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 17 deletions.
66 changes: 54 additions & 12 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ class Package {
}

class Library extends ModelElement {

List<Variable> _variables;
Package package;
List<Class> _classes;
Expand Down Expand Up @@ -507,27 +506,32 @@ class Class extends ModelElement {
List<ElementType> _interfaces;
List<Constructor> _constructors;
List<Method> _methods;
List<Field> _fields;
List<Field> _staticFields;
List<Field> _instanceFields;

ClassElement get _cls => (element as ClassElement);

Class(ClassElement element, Library library, [String source])
: super(element, library, source) {
var p = library.package;
_type = new ElementType(_cls.type, this);

_mixins = _cls.mixins.map((f) {
var lib = new Library(f.element.library, p);
return new ElementType(f, new ModelElement.from(f.element, lib));
}).toList(growable: false);

if (hasSupertype) {
var lib = new Library(_cls.supertype.element.library, p);
_supertype = new ElementType(
_cls.supertype, new ModelElement.from(_cls.supertype.element, lib));
}

_interfaces = _cls.interfaces.map((f) {
var lib = new Library(f.element.library, p);
return new ElementType(f, new ModelElement.from(f.element, lib));
}).toList(growable: false);

}

bool get isAbstract => _cls.isAbstract;
Expand All @@ -545,17 +549,33 @@ class Class extends ModelElement {

bool get hasInterfaces => interfaces.isNotEmpty;

List<Field> _getAllfields() {
List<FieldElement> elements = _cls.fields.toList()
..removeWhere(isPrivate);
return elements.map((e) => new Field(e, library)).toList();
List<Field> get _allFields {
if (_fields != null) return _fields;

_fields = _cls
.fields
.where(isPublic)
.map((e) => new Field(e, library))
.toList(growable: false);

return _fields;
}

List<Field> get staticProperties {
if (_staticFields != null) return _staticFields;
_staticFields = _allFields.where((f) => f.isStatic).toList(growable:false);
return _staticFields;
}

List<Field> get instanceProperties {
if (_instanceFields != null) return _instanceFields;
_instanceFields = _allFields.where((f) => !f.isStatic).toList(growable:false);
return _instanceFields;
}

List<Field> getStaticFields() =>
_getAllfields()..removeWhere((f) => !f.isStatic);
bool get hasStaticProperties => staticProperties.isNotEmpty;

List<Field> getInstanceFields() =>
_getAllfields()..removeWhere((f) => f.isStatic);
bool get hasInstanceProperties => instanceProperties.isNotEmpty;

List<Constructor> get constructors {
if (_constructors != null) return _constructors;
Expand Down Expand Up @@ -637,14 +657,36 @@ class Field extends ModelElement {

Field(FieldElement element, Library library) : super(element, library) {
var e = _field.type.element;
_type = new ElementType(
_field.type, new ModelElement.from(e, new Library(e.library, package)));

if (hasGetter) {
var t = _field.getter.returnType;
var lib = new Library(t.element.library, package);
_type = new ElementType(t, new ModelElement.from(t.element, lib));
} else {
var s = _field.setter.parameters.first.type;
var lib = new Library(s.element.library, package);
_type = new ElementType(s, new ModelElement.from(s.element, lib));
}
}

bool get isFinal => _field.isFinal;

bool get isConst => _field.isConst;

String get linkedReturnType {
return type.linkedName;
}

String get constantValue {
var v = (_field as ConstFieldElementImpl).node.toSource();
if (v == null) return '';
return v.substring(v.indexOf('= ')+2, v.length);
}

bool get hasGetter => _field.getter != null;

bool get hasSetter => _field.setter != null;

String get _href {
if (element.enclosingElement is ClassElement) {
return '/${library.name}/${element.enclosingElement.name}.html#$name';
Expand Down
24 changes: 24 additions & 0 deletions templates/new/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ <h2>Constructors</h2>

</section>

{{#class.hasInstanceProperties}}
<section class="summary" id="properties">
<h2>Properties</h2>

<dl class="withreturntypes">
{{#class.instanceProperties}}
<dt id="{{htmlId}}">
<code>
<span class="returntype">{{{ linkedReturnType }}}</span>
<span class="name">{{ name }}</span>
</code>
</dt>
<dd>
<div>
<span class="{{#hasGetter}}has-it{{/hasGetter}} getter">read</span>
<span class="{{#hasSetter}}has-it{{/hasSetter}} setter">write</span>
</div>
{{#oneLiner}}{{ documentation }}{{/oneLiner}}
</dd>
{{/class.instanceProperties}}
</dl>
</section>
{{/class.hasInstanceProperties}}

{{#class.hasMethods}}
<section id="methods-summary" class="summary">
<h2>Methods</h2>
Expand Down
12 changes: 12 additions & 0 deletions test/fake_package/lib/fake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ abstract class AnotherInterface {
/// The rest of this is not in the first paragraph.
class LongFirstLine extends Object with MixMeIn implements Interface, AnotherInterface {

/// An instance string property. Readable and writable.
String aStringProperty;

/// A static int property.
static int meaningOfLife = 42;

/// The default constructor.
LongFirstLine();

Expand All @@ -87,6 +93,12 @@ class LongFirstLine extends Object with MixMeIn implements Interface, AnotherInt

/// One dynamic param, two named optionals.
bool optionalParams(first, {second, int third}) => true;

/// Dynamic getter. Readable only.
get dynamicGetter => 'could be anything';

/// Only a setter, with a single param, of type double.
void set onlySetter(double d) {}
}

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

test('get static fields', () {
expect(A.getStaticFields(), hasLength(2));
expect(A.staticProperties, hasLength(2));
});

test('get instance fields', () {
expect(A.getInstanceFields(), hasLength(3));
expect(A.instanceProperties, hasLength(3));
});

test('get methods', () {
Expand Down Expand Up @@ -184,8 +184,8 @@ void main() {

group('Field', () {
var c = l.getTypes()[0];
var f1 = c.getStaticFields()[0];
var f2 = c.getInstanceFields()[0];
var f1 = c.staticProperties[0];
var f2 = c.instanceProperties[0];

test('is const', () {
expect(f1.isConst, true);
Expand Down Expand Up @@ -248,7 +248,7 @@ void main() {
});

group('Type', () {
var f = l.getTypes()[1].getInstanceFields()[0];
var f = l.getTypes()[1].instanceProperties[0];

test('parameterized type', () {
expect(f.type.isParameterizedType, true);
Expand Down

0 comments on commit 9186c93

Please sign in to comment.