Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(529)

Side by Side Diff: pkg/docgen/lib/src/models/class.dart

Issue 242363004: pkg/docgen: moved model classes into minilibs (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: cl nits Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
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.
4
5 library docgen.models.clazz;
6
7 import '../exports/dart2js_mirrors.dart' as dart2js_mirrors;
8 import '../exports/mirrors_util.dart' as dart2js_util;
9 import '../exports/source_mirrors.dart';
10
11 import '../library_helpers.dart';
12
13 import 'dummy_mirror.dart';
14 import 'generic.dart';
15 import 'indexable.dart';
16 import 'library.dart';
17 import 'method.dart';
18 import 'model_helpers.dart';
19 import 'owned_indexable.dart';
20 import 'variable.dart';
21
22 /// A class containing contents of a Dart class.
23 class Class extends OwnedIndexable<dart2js_mirrors.Dart2JsInterfaceTypeMirror>
24 implements Comparable<Class> {
25
26 /// List of the names of interfaces that this class implements.
27 List<Class> interfaces = [];
28
29 /// Names of classes that extends or implements this class.
30 Set<Class> subclasses = new Set<Class>();
31
32 /// Top-level variables in the class.
33 Map<String, Variable> variables;
34
35 /// Inherited variables in the class.
36 final Map<String, Variable> inheritedVariables = {};
37
38 /// Methods in the class.
39 Map<String, Method> methods;
40
41 final Map<String, Method> inheritedMethods = new Map<String, Method>();
42
43 /// Generic infomation about the class.
44 final Map<String, Generic> generics;
45
46 Class superclass;
47 bool get isAbstract => mirror.isAbstract;
48
49 /// Make sure that we don't check for inherited comments more than once.
50 bool _commentsEnsured = false;
51
52 /// Returns the [Class] for the given [mirror] if it has already been created,
53 /// else creates it.
54 factory Class(ClassMirror mirror, Library owner) {
55 var clazz = getDocgenObject(mirror, owner);
56 if (clazz is DummyMirror) {
57 clazz = new Class._(mirror, owner);
58 }
59 return clazz;
60 }
61
62 /// Called when we are constructing a superclass or interface class, but it
63 /// is not known if it belongs to the same owner as the original class. In
64 /// this case, we create an object whose owner is what the original mirror
65 /// says it is.
66 factory Class._possiblyDifferentOwner(ClassMirror mirror,
67 Library originalOwner) {
68 if (mirror.owner is LibraryMirror) {
69 var realOwner = getDocgenObject(mirror.owner);
70 if (realOwner is Library) {
71 return new Class(mirror, realOwner);
72 } else {
73 return new Class(mirror, originalOwner);
74 }
75 } else {
76 return new Class(mirror, originalOwner);
77 }
78 }
79
80 Class._(ClassSourceMirror classMirror, Indexable owner)
81 : generics = createGenerics(classMirror),
82 super(classMirror, owner) {
83
84 // The reason we do this madness is the superclass and interface owners may
85 // not be this class's owner!! Example: BaseClient in http pkg.
86 var superinterfaces = classMirror.superinterfaces.map(
87 (interface) => new Class._possiblyDifferentOwner(interface, owner));
88 this.superclass = classMirror.superclass == null? null :
89 new Class._possiblyDifferentOwner(classMirror.superclass, owner);
90
91 interfaces = superinterfaces.toList();
92 variables = createVariables(
93 dart2js_util.variablesOf(classMirror.declarations), this);
94 methods = createMethods(classMirror.declarations.values.where(
95 (mirror) => mirror is MethodMirror), this);
96
97 // Tell superclass that you are a subclass, unless you are not
98 // visible or an intermediary mixin class.
99 if (!classMirror.isNameSynthetic && isVisible && superclass != null) {
100 superclass.addSubclass(this);
101 }
102
103 if (this.superclass != null) addInherited(superclass);
104 interfaces.forEach((interface) => addInherited(interface));
105 }
106
107 String _lookupInClassAndSuperclasses(String name) {
108 var lookupFunc = determineLookupFunc(name);
109 var classScope = this;
110 while (classScope != null) {
111 var classFunc = lookupFunc(classScope.mirror, name);
112 if (classFunc != null) {
113 return packagePrefix + getDocgenObject(classFunc, owner).docName;
114 }
115 classScope = classScope.superclass;
116 }
117 return null;
118 }
119
120 /// Look for the specified name starting with the current member, and
121 /// progressively working outward to the current library scope.
122 String findElementInScope(String name) {
123 var lookupFunc = determineLookupFunc(name);
124 var result = _lookupInClassAndSuperclasses(name);
125 if (result != null) {
126 return result;
127 }
128 result = owner.findElementInScope(name);
129 return result == null ? super.findElementInScope(name) : result;
130 }
131
132 String get typeName => 'class';
133
134 /// Add all inherited variables and methods from the provided superclass.
135 /// If [_includePrivate] is true, it also adds the variables and methods from
136 /// the superclass.
137 void addInherited(Class superclass) {
138 inheritedVariables.addAll(superclass.inheritedVariables);
139 inheritedVariables.addAll(_allButStatics(superclass.variables));
140 addInheritedMethod(superclass, this);
141 }
142
143 /** [newParent] refers to the actual class is currently using these methods.
144 * which may be different because with the mirror system, we only point to the
145 * original canonical superclasse's method.
146 */
147 void addInheritedMethod(Class parent, Class newParent) {
148 parent.inheritedMethods.forEach((name, method) {
149 if (!method.mirror.isConstructor) {
150 inheritedMethods[name] = new Method(method.mirror, newParent, method);
151 }
152 });
153 _allButStatics(parent.methods).forEach((name, method) {
154 if (!method.mirror.isConstructor) {
155 inheritedMethods[name] = new Method(method.mirror, newParent, method);
156 }
157 });
158 }
159
160 /// Remove statics from the map of inherited items before adding them.
161 Map _allButStatics(Map items) {
162 var result = {};
163 items.forEach((name, item) {
164 if (!item.isStatic) {
165 result[name] = item;
166 }
167 });
168 return result;
169 }
170
171 /// Add the subclass to the class.
172 ///
173 /// If [this] is private (or an intermediary mixin class), it will add the
174 /// subclass to the list of subclasses in the superclasses.
175 void addSubclass(Class subclass) {
176 if (docName == 'dart-core.Object') return;
177
178 if (!includePrivateMembers && isPrivate || mirror.isNameSynthetic) {
179 if (superclass != null) superclass.addSubclass(subclass);
180 interfaces.forEach((interface) {
181 interface.addSubclass(subclass);
182 });
183 } else {
184 subclasses.add(subclass);
185 }
186 }
187
188 /// Check if this [Class] is an error or exception.
189 bool isError() {
190 if (qualifiedName == 'dart-core.Error' ||
191 qualifiedName == 'dart-core.Exception')
192 return true;
193 for (var interface in interfaces) {
194 if (interface.isError()) return true;
195 }
196 if (superclass == null) return false;
197 return superclass.isError();
198 }
199
200 /// Makes sure that all methods with inherited equivalents have comments.
201 void ensureComments() {
202 if (_commentsEnsured) return;
203 _commentsEnsured = true;
204 if (superclass != null) superclass.ensureComments();
205 inheritedMethods.forEach((qualifiedName, inheritedMethod) {
206 var method = methods[qualifiedName];
207 if (method != null) {
208 // if we have overwritten this method in this class, we still provide
209 // the opportunity to inherit the comments.
210 method.ensureCommentFor(inheritedMethod);
211 }
212 });
213 // we need to populate the comments for all methods. so that the subclasses
214 // can get for their inherited versions the comments.
215 methods.forEach((qualifiedName, method) {
216 if (!method.mirror.isConstructor) method.ensureCommentFor(method);
217 });
218 }
219
220 /// If a class extends a private superclass, find the closest public
221 /// superclass of the private superclass.
222 String validSuperclass() {
223 if (superclass == null) return 'dart-core.Object';
224 if (superclass.isVisible) return superclass.qualifiedName;
225 return superclass.validSuperclass();
226 }
227
228 /// Generates a map describing the [Class] object.
229 Map toMap() => {
230 'name': name,
231 'qualifiedName': qualifiedName,
232 'comment': comment,
233 'isAbstract' : isAbstract,
234 'superclass': validSuperclass(),
235 'implements': interfaces.where((i) => i.isVisible)
236 .map((e) => e.qualifiedName).toList(),
237 'subclass': (subclasses.toList()..sort())
238 .map((x) => x.qualifiedName).toList(),
239 'variables': recurseMap(variables),
240 'inheritedVariables': recurseMap(inheritedVariables),
241 'methods': expandMethodMap(methods),
242 'inheritedMethods': expandMethodMap(inheritedMethods),
243 'annotations': annotations.map((a) => a.toMap()).toList(),
244 'generics': recurseMap(generics)
245 };
246
247 int compareTo(Class other) => name.compareTo(other.name);
248
249 bool isValidMirror(DeclarationMirror mirror) => mirror is ClassMirror;
250 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698