diff --git a/lib/src/model.dart b/lib/src/model.dart index 9af5842c63..6b1426610a 100644 --- a/lib/src/model.dart +++ b/lib/src/model.dart @@ -378,7 +378,6 @@ class Package { } class Library extends ModelElement { - List _variables; Package package; List _classes; @@ -507,6 +506,9 @@ class Class extends ModelElement { List _interfaces; List _constructors; List _methods; + List _fields; + List _staticFields; + List _instanceFields; ClassElement get _cls => (element as ClassElement); @@ -514,20 +516,22 @@ class Class extends ModelElement { : 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; @@ -545,17 +549,33 @@ class Class extends ModelElement { bool get hasInterfaces => interfaces.isNotEmpty; - List _getAllfields() { - List elements = _cls.fields.toList() - ..removeWhere(isPrivate); - return elements.map((e) => new Field(e, library)).toList(); + List get _allFields { + if (_fields != null) return _fields; + + _fields = _cls + .fields + .where(isPublic) + .map((e) => new Field(e, library)) + .toList(growable: false); + + return _fields; + } + + List get staticProperties { + if (_staticFields != null) return _staticFields; + _staticFields = _allFields.where((f) => f.isStatic).toList(growable:false); + return _staticFields; + } + + List get instanceProperties { + if (_instanceFields != null) return _instanceFields; + _instanceFields = _allFields.where((f) => !f.isStatic).toList(growable:false); + return _instanceFields; } - List getStaticFields() => - _getAllfields()..removeWhere((f) => !f.isStatic); + bool get hasStaticProperties => staticProperties.isNotEmpty; - List getInstanceFields() => - _getAllfields()..removeWhere((f) => f.isStatic); + bool get hasInstanceProperties => instanceProperties.isNotEmpty; List get constructors { if (_constructors != null) return _constructors; @@ -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'; diff --git a/templates/new/class.html b/templates/new/class.html index aaac872cbe..b3ab156a34 100644 --- a/templates/new/class.html +++ b/templates/new/class.html @@ -69,6 +69,30 @@

Constructors

+ {{#class.hasInstanceProperties}} +
+

Properties

+ +
+ {{#class.instanceProperties}} +
+ + {{{ linkedReturnType }}} + {{ name }} + +
+
+
+ read + write +
+ {{#oneLiner}}{{ documentation }}{{/oneLiner}} +
+ {{/class.instanceProperties}} +
+
+ {{/class.hasInstanceProperties}} + {{#class.hasMethods}}

Methods

diff --git a/test/fake_package/lib/fake.dart b/test/fake_package/lib/fake.dart index f72dc3b16f..a3a069eb51 100644 --- a/test/fake_package/lib/fake.dart +++ b/test/fake_package/lib/fake.dart @@ -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(); @@ -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! diff --git a/test/model_test.dart b/test/model_test.dart index 298346c4c7..a202ec12a4 100644 --- a/test/model_test.dart +++ b/test/model_test.dart @@ -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', () { @@ -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); @@ -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);