Index: sky/examples/fn/lib/reflect.dart |
diff --git a/sky/examples/fn/lib/reflect.dart b/sky/examples/fn/lib/reflect.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..66b2efefc2ea698c86a7c7cd98819750f054157c |
--- /dev/null |
+++ b/sky/examples/fn/lib/reflect.dart |
@@ -0,0 +1,41 @@ |
+library reflect; |
+ |
+import 'dart:mirrors'; |
+import 'dart:collection'; |
+ |
+HashMap<ClassMirror, List> _fieldCache = new HashMap<ClassMirror, List>(); |
+ |
+List<Symbol> _getPublicFields(ClassMirror mirror) { |
+ var fields = _fieldCache[mirror]; |
+ if (fields == null) { |
+ fields = new List<Symbol>(); |
+ _fieldCache[mirror] = fields; |
+ |
+ while (mirror != null) { |
+ var decls = mirror.declarations; |
+ fields.addAll(decls.keys.where((symbol) { |
+ var mirror = decls[symbol]; |
+ if (mirror is! VariableMirror) { |
+ return false; |
+ } |
+ |
+ var vMirror = mirror as VariableMirror; |
+ return !vMirror.isPrivate && !vMirror.isStatic && !vMirror.isFinal; |
+ })); |
+ |
+ mirror = mirror.superclass; |
+ } |
+ } |
+ |
+ return fields; |
+} |
+ |
+void copyPublicFields(Object source, Object target) { |
+ assert(source.runtimeType == target.runtimeType); |
+ |
+ var sourceMirror = reflect(source); |
+ var targetMirror = reflect(target); |
+ for (var symbol in _getPublicFields(sourceMirror.type)) { |
+ targetMirror.setField(symbol, sourceMirror.getField(symbol).reflectee); |
+ } |
+} |