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

Side by Side Diff: pkg/analysis_server/lib/src/computer/element.dart

Issue 356563006: Element.fromEngine() constructor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Tweaks for review comments. Created 6 years, 6 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
« no previous file with comments | « no previous file | pkg/analysis_server/test/computer/element_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library computer.element; 5 library computer.element;
6 6
7 import 'package:analysis_server/src/constants.dart'; 7 import 'package:analysis_server/src/constants.dart';
8 import 'package:analyzer/src/generated/element.dart' as engine;
9 import 'package:analyzer/src/generated/utilities_dart.dart' as engine;
8 10
9 11
10 /** 12 /**
11 * Information about an element. 13 * Information about an element.
12 */ 14 */
13 class Element { 15 class Element {
14 static const List<Element> EMPTY_ARRAY = const <Element>[]; 16 static const List<Element> EMPTY_ARRAY = const <Element>[];
15 17
16 static const int _FLAG_ABSTRACT = 0x01; 18 static const int FLAG_ABSTRACT = 0x01;
17 static const int _FLAG_CONST = 0x02; 19 static const int FLAG_CONST = 0x02;
18 static const int _FLAG_DEPRECATED = 0x20; 20 static const int FLAG_DEPRECATED = 0x20;
19 static const int _FLAG_FINAL = 0x04; 21 static const int FLAG_FINAL = 0x04;
20 static const int _FLAG_PRIVATE = 0x10; 22 static const int FLAG_PRIVATE = 0x10;
21 static const int _FLAG_STATIC = 0x08; 23 static const int FLAG_STATIC = 0x08;
22 24
23 final bool isAbstract; 25 final bool isAbstract;
24 final bool isConst; 26 final bool isConst;
25 final bool isDeprecated; 27 final bool isDeprecated;
26 final bool isFinal; 28 final bool isFinal;
27 final bool isPrivate; 29 final bool isPrivate;
28 final bool isStatic; 30 final bool isStatic;
29 31
30 /** 32 /**
31 * The kind of the element. 33 * The kind of the element.
(...skipping 26 matching lines...) Expand all
58 * The return type of the element. 60 * The return type of the element.
59 * If the element is not a method or function then `null`. 61 * If the element is not a method or function then `null`.
60 * If the element does not have a declared return type, then an empty string. 62 * If the element does not have a declared return type, then an empty string.
61 */ 63 */
62 final String returnType; 64 final String returnType;
63 65
64 Element(this.kind, this.name, this.offset, this.length, this.isPrivate, 66 Element(this.kind, this.name, this.offset, this.length, this.isPrivate,
65 this.isDeprecated, {this.parameters, this.returnType, this.isAbstract: fal se, 67 this.isDeprecated, {this.parameters, this.returnType, this.isAbstract: fal se,
66 this.isConst: false, this.isFinal: false, this.isStatic: false}); 68 this.isConst: false, this.isFinal: false, this.isStatic: false});
67 69
70 factory Element.fromEngine(engine.Element element) {
71 String name = element.displayName;
72 int nameLength = name != null ? name.length : 0;
73 String elementParameters = _getParametersString(element);
74 String elementReturnType = _getReturnTypeString(element);
75 return new Element(ElementKind.valueOfEngine(element.kind), name,
76 element.nameOffset, nameLength, element.isPrivate, element.isDeprecated,
77 parameters: elementParameters, returnType: elementReturnType, isAbstract :
78 _isAbstract(element), isConst: _isConst(element), isFinal: _isFinal(elem ent),
79 isStatic: _isStatic(element));
80 }
81
68 factory Element.fromJson(Map<String, Object> map) { 82 factory Element.fromJson(Map<String, Object> map) {
69 ElementKind kind = ElementKind.valueOf(map[KIND]); 83 ElementKind kind = ElementKind.valueOf(map[KIND]);
70 int flags = map[FLAGS]; 84 int flags = map[FLAGS];
71 return new Element(kind, map[NAME], map[OFFSET], map[LENGTH], _hasFlag( 85 return new Element(kind, map[NAME], map[OFFSET], map[LENGTH], _hasFlag(
72 flags, _FLAG_PRIVATE), _hasFlag(flags, _FLAG_DEPRECATED), parameters: 86 flags, FLAG_PRIVATE), _hasFlag(flags, FLAG_DEPRECATED), parameters:
73 map[PARAMETERS], returnType: map[RETURN_TYPE], isAbstract: _hasFlag(flag s, 87 map[PARAMETERS], returnType: map[RETURN_TYPE], isAbstract: _hasFlag(flag s,
74 _FLAG_ABSTRACT), isConst: _hasFlag(flags, _FLAG_CONST), isFinal: _hasFla g(flags, 88 FLAG_ABSTRACT), isConst: _hasFlag(flags, FLAG_CONST), isFinal: _hasFlag( flags,
75 _FLAG_FINAL), isStatic: _hasFlag(flags, _FLAG_STATIC)); 89 FLAG_FINAL), isStatic: _hasFlag(flags, FLAG_STATIC));
76 } 90 }
77 91
78 int get flags { 92 int get flags {
79 int flags = 0; 93 int flags = 0;
80 if (isAbstract) flags |= _FLAG_ABSTRACT; 94 if (isAbstract) flags |= FLAG_ABSTRACT;
81 if (isConst) flags |= _FLAG_CONST; 95 if (isConst) flags |= FLAG_CONST;
82 if (isFinal) flags |= _FLAG_FINAL; 96 if (isFinal) flags |= FLAG_FINAL;
83 if (isStatic) flags |= _FLAG_STATIC; 97 if (isStatic) flags |= FLAG_STATIC;
84 if (isPrivate) flags |= _FLAG_PRIVATE; 98 if (isPrivate) flags |= FLAG_PRIVATE;
85 if (isDeprecated) flags |= _FLAG_DEPRECATED; 99 if (isDeprecated) flags |= FLAG_DEPRECATED;
86 return flags; 100 return flags;
87 } 101 }
88 102
89 Map<String, Object> toJson() { 103 Map<String, Object> toJson() {
90 Map<String, Object> json = { 104 Map<String, Object> json = {
91 KIND: kind.name, 105 KIND: kind.name,
92 NAME: name, 106 NAME: name,
93 OFFSET: offset, 107 OFFSET: offset,
94 LENGTH: length, 108 LENGTH: length,
95 FLAGS: flags 109 FLAGS: flags
96 }; 110 };
97 if (parameters != null) { 111 if (parameters != null) {
98 json[PARAMETERS] = parameters; 112 json[PARAMETERS] = parameters;
99 } 113 }
100 if (returnType != null) { 114 if (returnType != null) {
101 json[RETURN_TYPE] = returnType; 115 json[RETURN_TYPE] = returnType;
102 } 116 }
103 return json; 117 return json;
104 } 118 }
105 119
120 static String _getParametersString(engine.Element element) {
121 // TODO(scheglov) expose the corresponding feature from ExecutableElement
122 if (element is engine.ExecutableElement) {
123 var sb = new StringBuffer();
124 String closeOptionalString = '';
125 for (var parameter in element.parameters) {
126 if (sb.isNotEmpty) {
127 sb.write(', ');
128 }
129 if (closeOptionalString.isEmpty) {
130 if (parameter.kind == engine.ParameterKind.NAMED) {
131 sb.write('{');
132 closeOptionalString = '}';
133 }
134 if (parameter.kind == engine.ParameterKind.POSITIONAL) {
135 sb.write('[');
136 closeOptionalString = ']';
137 }
138 }
139 sb.write(parameter.toString());
140 }
141 sb.write(closeOptionalString);
142 return '(' + sb.toString() + ')';
143 } else {
144 return null;
145 }
146 }
147
148 static String _getReturnTypeString(engine.Element element) {
149 if ((element is engine.ExecutableElement)) {
150 return element.returnType.toString();
151 } else {
152 return null;
153 }
154 }
155
106 static bool _hasFlag(int flags, int flag) => (flags & flag) != 0; 156 static bool _hasFlag(int flags, int flag) => (flags & flag) != 0;
157
158 static bool _isAbstract(engine.Element element) {
159 // TODO(scheglov) add isAbstract to Element API
160 if (element is engine.ClassElement) {
161 return element.isAbstract;
162 }
163 if (element is engine.MethodElement) {
164 return element.isAbstract;
165 }
166 if (element is engine.PropertyAccessorElement) {
167 return element.isAbstract;
168 }
169 return false;
170 }
171
172 static bool _isConst(engine.Element element) {
173 // TODO(scheglov) add isConst to Element API
174 if (element is engine.ConstructorElement) {
175 return element.isConst;
176 }
177 if (element is engine.VariableElement) {
178 return element.isConst;
179 }
180 return false;
181 }
182
183 static bool _isFinal(engine.Element element) {
184 // TODO(scheglov) add isFinal to Element API
185 if (element is engine.VariableElement) {
186 return element.isFinal;
187 }
188 return false;
189 }
190
191 static bool _isStatic(engine.Element element) {
192 // TODO(scheglov) add isStatic to Element API
193 if (element is engine.ExecutableElement) {
194 return element.isStatic;
195 }
196 if (element is engine.PropertyInducingElement) {
197 return element.isStatic;
198 }
199 return false;
200 }
107 } 201 }
108 202
109 203
110 /** 204 /**
111 * An enumeration of the kinds of elements. 205 * An enumeration of the kinds of elements.
112 */ 206 */
113 class ElementKind { 207 class ElementKind {
114 static const CLASS = const ElementKind('CLASS'); 208 static const CLASS = const ElementKind('CLASS');
115 static const CLASS_TYPE_ALIAS = const ElementKind('CLASS_TYPE_ALIAS'); 209 static const CLASS_TYPE_ALIAS = const ElementKind('CLASS_TYPE_ALIAS');
116 static const COMPILATION_UNIT = const ElementKind('COMPILATION_UNIT'); 210 static const COMPILATION_UNIT = const ElementKind('COMPILATION_UNIT');
117 static const CONSTRUCTOR = const ElementKind('CONSTRUCTOR'); 211 static const CONSTRUCTOR = const ElementKind('CONSTRUCTOR');
118 static const FIELD = const ElementKind('FIELD'); 212 static const FIELD = const ElementKind('FIELD');
119 static const FUNCTION = const ElementKind('FUNCTION'); 213 static const FUNCTION = const ElementKind('FUNCTION');
120 static const FUNCTION_TYPE_ALIAS = const ElementKind('FUNCTION_TYPE_ALIAS'); 214 static const FUNCTION_TYPE_ALIAS = const ElementKind('FUNCTION_TYPE_ALIAS');
121 static const GETTER = const ElementKind('GETTER'); 215 static const GETTER = const ElementKind('GETTER');
122 static const LIBRARY = const ElementKind('LIBRARY'); 216 static const LIBRARY = const ElementKind('LIBRARY');
123 static const METHOD = const ElementKind('METHOD'); 217 static const METHOD = const ElementKind('METHOD');
124 static const SETTER = const ElementKind('SETTER'); 218 static const SETTER = const ElementKind('SETTER');
125 static const TOP_LEVEL_VARIABLE = const ElementKind('TOP_LEVEL_VARIABLE'); 219 static const TOP_LEVEL_VARIABLE = const ElementKind('TOP_LEVEL_VARIABLE');
126 static const UNIT_TEST_CASE = const ElementKind('UNIT_TEST_CASE'); 220 static const UNIT_TEST_CASE = const ElementKind('UNIT_TEST_CASE');
127 static const UNIT_TEST_GROUP = const ElementKind('UNIT_TEST_GROUP'); 221 static const UNIT_TEST_GROUP = const ElementKind('UNIT_TEST_GROUP');
128 static const UNKNOWN = const ElementKind('UNKNOWN'); 222 static const UNKNOWN = const ElementKind('UNKNOWN');
129 223
130 final String name; 224 final String name;
131 225
132 const ElementKind(this.name); 226 const ElementKind(this.name);
133 227
228 @override
229 String toString() => name;
230
134 static ElementKind valueOf(String name) { 231 static ElementKind valueOf(String name) {
135 if (CLASS.name == name) return CLASS; 232 if (CLASS.name == name) return CLASS;
136 if (CLASS_TYPE_ALIAS.name == name) return CLASS_TYPE_ALIAS; 233 if (CLASS_TYPE_ALIAS.name == name) return CLASS_TYPE_ALIAS;
137 if (COMPILATION_UNIT.name == name) return COMPILATION_UNIT; 234 if (COMPILATION_UNIT.name == name) return COMPILATION_UNIT;
138 if (CONSTRUCTOR.name == name) return CONSTRUCTOR; 235 if (CONSTRUCTOR.name == name) return CONSTRUCTOR;
139 if (FIELD.name == name) return FIELD; 236 if (FIELD.name == name) return FIELD;
140 if (FUNCTION.name == name) return FUNCTION; 237 if (FUNCTION.name == name) return FUNCTION;
141 if (FUNCTION_TYPE_ALIAS.name == name) return FUNCTION_TYPE_ALIAS; 238 if (FUNCTION_TYPE_ALIAS.name == name) return FUNCTION_TYPE_ALIAS;
142 if (GETTER.name == name) return GETTER; 239 if (GETTER.name == name) return GETTER;
143 if (LIBRARY.name == name) return LIBRARY; 240 if (LIBRARY.name == name) return LIBRARY;
144 if (METHOD.name == name) return METHOD; 241 if (METHOD.name == name) return METHOD;
145 if (SETTER.name == name) return SETTER; 242 if (SETTER.name == name) return SETTER;
146 if (TOP_LEVEL_VARIABLE.name == name) return TOP_LEVEL_VARIABLE; 243 if (TOP_LEVEL_VARIABLE.name == name) return TOP_LEVEL_VARIABLE;
147 if (UNIT_TEST_CASE.name == name) return UNIT_TEST_CASE; 244 if (UNIT_TEST_CASE.name == name) return UNIT_TEST_CASE;
148 if (UNIT_TEST_GROUP.name == name) return UNIT_TEST_GROUP; 245 if (UNIT_TEST_GROUP.name == name) return UNIT_TEST_GROUP;
149 if (UNKNOWN.name == name) return UNKNOWN; 246 if (UNKNOWN.name == name) return UNKNOWN;
150 throw new ArgumentError('Unknown ElementKind: $name'); 247 throw new ArgumentError('Unknown ElementKind: $name');
151 } 248 }
249
250 static ElementKind valueOfEngine(engine.ElementKind kind) {
251 if (kind == engine.ElementKind.CLASS) {
252 return CLASS;
253 }
254 if (kind == engine.ElementKind.COMPILATION_UNIT) {
255 return COMPILATION_UNIT;
256 }
257 if (kind == engine.ElementKind.CONSTRUCTOR) {
258 return CONSTRUCTOR;
259 }
260 if (kind == engine.ElementKind.FIELD) {
261 return FIELD;
262 }
263 if (kind == engine.ElementKind.FUNCTION) {
264 return FUNCTION;
265 }
266 if (kind == engine.ElementKind.FUNCTION_TYPE_ALIAS) {
267 return FUNCTION_TYPE_ALIAS;
268 }
269 if (kind == engine.ElementKind.GETTER) {
270 return GETTER;
271 }
272 if (kind == engine.ElementKind.LIBRARY) {
273 return LIBRARY;
274 }
275 if (kind == engine.ElementKind.METHOD) {
276 return METHOD;
277 }
278 if (kind == engine.ElementKind.SETTER) {
279 return SETTER;
280 }
281 if (kind == engine.ElementKind.TOP_LEVEL_VARIABLE) {
282 return TOP_LEVEL_VARIABLE;
283 }
284 return UNKNOWN;
285 }
152 } 286 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/test/computer/element_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698