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

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: Created 6 years, 5 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
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 if (element is engine.ExecutableElement) {
122 var sb = new StringBuffer();
Brian Wilkerson 2014/06/25 13:50:08 This duplicates code in engine.ExecutableElement.
scheglov 2014/06/25 17:20:46 I've added TODO.
123 String closeOptionalString = '';
124 for (var parameter in element.parameters) {
125 if (sb.isNotEmpty) {
126 sb.write(', ');
127 }
128 if (closeOptionalString.isEmpty) {
129 if (parameter.kind == engine.ParameterKind.NAMED) {
130 sb.write('{');
131 closeOptionalString = '}';
132 }
133 if (parameter.kind == engine.ParameterKind.POSITIONAL) {
134 sb.write('[');
135 closeOptionalString = ']';
136 }
137 }
138 sb.write(parameter.toString());
139 }
140 sb.write(closeOptionalString);
141 return '(' + sb.toString() + ')';
142 } else {
143 return null;
144 }
145 }
146
147 static String _getReturnTypeString(engine.Element element) {
148 if ((element is engine.ExecutableElement)) {
149 return element.returnType.toString();
150 } else {
151 return null;
152 }
153 }
154
106 static bool _hasFlag(int flags, int flag) => (flags & flag) != 0; 155 static bool _hasFlag(int flags, int flag) => (flags & flag) != 0;
156
157 static bool _isAbstract(engine.Element element) {
158 if (element is engine.ClassElement) {
Brian Wilkerson 2014/06/25 13:50:08 Code with lots of 'is' expressions is always suspe
scheglov 2014/06/25 17:20:46 I've added TODO.
159 return element.isAbstract;
160 }
161 if (element is engine.MethodElement) {
162 return element.isAbstract;
163 }
164 if (element is engine.PropertyAccessorElement) {
165 return element.isAbstract;
166 }
167 return false;
168 }
169
170 static bool _isConst(engine.Element element) {
171 if (element is engine.ConstructorElement) {
172 return element.isConst;
173 }
174 if (element is engine.VariableElement) {
175 return element.isConst;
176 }
177 return false;
178 }
179
180 static bool _isFinal(engine.Element element) {
181 if (element is engine.VariableElement) {
182 return element.isFinal;
183 }
184 return false;
185 }
186
187 static bool _isStatic(engine.Element element) {
188 if (element is engine.ExecutableElement) {
189 return element.isStatic;
190 }
191 if (element is engine.PropertyInducingElement) {
192 return element.isStatic;
193 }
194 return false;
195 }
107 } 196 }
108 197
109 198
110 /** 199 /**
111 * An enumeration of the kinds of elements. 200 * An enumeration of the kinds of elements.
112 */ 201 */
113 class ElementKind { 202 class ElementKind {
114 static const CLASS = const ElementKind('CLASS'); 203 static const CLASS = const ElementKind('CLASS');
115 static const CLASS_TYPE_ALIAS = const ElementKind('CLASS_TYPE_ALIAS'); 204 static const CLASS_TYPE_ALIAS = const ElementKind('CLASS_TYPE_ALIAS');
116 static const COMPILATION_UNIT = const ElementKind('COMPILATION_UNIT'); 205 static const COMPILATION_UNIT = const ElementKind('COMPILATION_UNIT');
117 static const CONSTRUCTOR = const ElementKind('CONSTRUCTOR'); 206 static const CONSTRUCTOR = const ElementKind('CONSTRUCTOR');
118 static const FIELD = const ElementKind('FIELD'); 207 static const FIELD = const ElementKind('FIELD');
119 static const FUNCTION = const ElementKind('FUNCTION'); 208 static const FUNCTION = const ElementKind('FUNCTION');
120 static const FUNCTION_TYPE_ALIAS = const ElementKind('FUNCTION_TYPE_ALIAS'); 209 static const FUNCTION_TYPE_ALIAS = const ElementKind('FUNCTION_TYPE_ALIAS');
121 static const GETTER = const ElementKind('GETTER'); 210 static const GETTER = const ElementKind('GETTER');
122 static const LIBRARY = const ElementKind('LIBRARY'); 211 static const LIBRARY = const ElementKind('LIBRARY');
123 static const METHOD = const ElementKind('METHOD'); 212 static const METHOD = const ElementKind('METHOD');
124 static const SETTER = const ElementKind('SETTER'); 213 static const SETTER = const ElementKind('SETTER');
125 static const TOP_LEVEL_VARIABLE = const ElementKind('TOP_LEVEL_VARIABLE'); 214 static const TOP_LEVEL_VARIABLE = const ElementKind('TOP_LEVEL_VARIABLE');
126 static const UNIT_TEST_CASE = const ElementKind('UNIT_TEST_CASE'); 215 static const UNIT_TEST_CASE = const ElementKind('UNIT_TEST_CASE');
127 static const UNIT_TEST_GROUP = const ElementKind('UNIT_TEST_GROUP'); 216 static const UNIT_TEST_GROUP = const ElementKind('UNIT_TEST_GROUP');
128 static const UNKNOWN = const ElementKind('UNKNOWN'); 217 static const UNKNOWN = const ElementKind('UNKNOWN');
129 218
130 final String name; 219 final String name;
131 220
132 const ElementKind(this.name); 221 const ElementKind(this.name);
133 222
223 @override
224 String toString() => name;
225
134 static ElementKind valueOf(String name) { 226 static ElementKind valueOf(String name) {
135 if (CLASS.name == name) return CLASS; 227 if (CLASS.name == name) return CLASS;
136 if (CLASS_TYPE_ALIAS.name == name) return CLASS_TYPE_ALIAS; 228 if (CLASS_TYPE_ALIAS.name == name) return CLASS_TYPE_ALIAS;
137 if (COMPILATION_UNIT.name == name) return COMPILATION_UNIT; 229 if (COMPILATION_UNIT.name == name) return COMPILATION_UNIT;
138 if (CONSTRUCTOR.name == name) return CONSTRUCTOR; 230 if (CONSTRUCTOR.name == name) return CONSTRUCTOR;
139 if (FIELD.name == name) return FIELD; 231 if (FIELD.name == name) return FIELD;
140 if (FUNCTION.name == name) return FUNCTION; 232 if (FUNCTION.name == name) return FUNCTION;
141 if (FUNCTION_TYPE_ALIAS.name == name) return FUNCTION_TYPE_ALIAS; 233 if (FUNCTION_TYPE_ALIAS.name == name) return FUNCTION_TYPE_ALIAS;
142 if (GETTER.name == name) return GETTER; 234 if (GETTER.name == name) return GETTER;
143 if (LIBRARY.name == name) return LIBRARY; 235 if (LIBRARY.name == name) return LIBRARY;
144 if (METHOD.name == name) return METHOD; 236 if (METHOD.name == name) return METHOD;
145 if (SETTER.name == name) return SETTER; 237 if (SETTER.name == name) return SETTER;
146 if (TOP_LEVEL_VARIABLE.name == name) return TOP_LEVEL_VARIABLE; 238 if (TOP_LEVEL_VARIABLE.name == name) return TOP_LEVEL_VARIABLE;
147 if (UNIT_TEST_CASE.name == name) return UNIT_TEST_CASE; 239 if (UNIT_TEST_CASE.name == name) return UNIT_TEST_CASE;
148 if (UNIT_TEST_GROUP.name == name) return UNIT_TEST_GROUP; 240 if (UNIT_TEST_GROUP.name == name) return UNIT_TEST_GROUP;
149 if (UNKNOWN.name == name) return UNKNOWN; 241 if (UNKNOWN.name == name) return UNKNOWN;
150 throw new ArgumentError('Unknown ElementKind: $name'); 242 throw new ArgumentError('Unknown ElementKind: $name');
151 } 243 }
244
245 static ElementKind valueOfEngine(engine.ElementKind kind) {
Brian Wilkerson 2014/06/25 13:50:08 What's the value of having two implementations of
scheglov 2014/06/25 17:20:46 Well, there isn't. But ElementKind here is the one
246 if (kind == engine.ElementKind.CLASS) {
247 return CLASS;
248 }
249 if (kind == engine.ElementKind.COMPILATION_UNIT) {
250 return COMPILATION_UNIT;
251 }
252 if (kind == engine.ElementKind.CONSTRUCTOR) {
253 return CONSTRUCTOR;
254 }
255 if (kind == engine.ElementKind.FIELD) {
256 return FIELD;
257 }
258 if (kind == engine.ElementKind.FUNCTION) {
259 return FUNCTION;
260 }
261 if (kind == engine.ElementKind.FUNCTION_TYPE_ALIAS) {
262 return FUNCTION_TYPE_ALIAS;
263 }
264 if (kind == engine.ElementKind.GETTER) {
265 return GETTER;
266 }
267 if (kind == engine.ElementKind.LIBRARY) {
268 return LIBRARY;
269 }
270 if (kind == engine.ElementKind.METHOD) {
271 return METHOD;
272 }
273 if (kind == engine.ElementKind.SETTER) {
274 return SETTER;
275 }
276 if (kind == engine.ElementKind.TOP_LEVEL_VARIABLE) {
277 return TOP_LEVEL_VARIABLE;
278 }
279 return UNKNOWN;
280 }
152 } 281 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698