@@ -34,8 +34,8 @@ Revivable reviveInstance(DartObject object, [LibraryElement origin]) {
3434 accessor: '${element .enclosingElement .name }.${element .name }' ,
3535 );
3636 }
37- final clazz = element as ClassElement ;
3837 // Enums are not included in .definingCompilationUnit.types.
38+ final clazz = element as ClassElement ;
3939 if (clazz.isEnum) {
4040 for (final e in clazz.fields.where (
4141 (f) => f.isPublic && f.isConst && f.computeConstantValue () == object)) {
@@ -45,39 +45,55 @@ Revivable reviveInstance(DartObject object, [LibraryElement origin]) {
4545 );
4646 }
4747 }
48+
49+ // We try and return a public accessor/constructor if available.
50+ final allResults = < Revivable > [];
51+
52+ /// Returns whether [result] is an acceptable result to immediately return.
53+ bool tryResult (Revivable result) {
54+ allResults.add (result);
55+ return ! result.isPrivate;
56+ }
57+
4858 for (final e in origin.definingCompilationUnit.types
4959 .expand ((t) => t.fields)
50- .where ((f) =>
51- f.isPublic && f.isConst && f.computeConstantValue () == object)) {
52- return Revivable ._(
60+ .where ((f) => f.isConst && f.computeConstantValue () == object)) {
61+ final result = Revivable ._(
5362 source: url.removeFragment (),
5463 accessor: '${clazz .name }.${e .name }' ,
5564 );
65+ if (tryResult (result)) {
66+ return result;
67+ }
5668 }
5769 final i = (object as DartObjectImpl ).getInvocation ();
58- if (i != null &&
59- i.constructor.isPublic &&
60- i.constructor.enclosingElement.isPublic) {
70+ if (i != null ) {
6171 url = Uri .parse (urlOfElement (i.constructor.enclosingElement));
62- return Revivable ._(
72+ final result = Revivable ._(
6373 source: url,
6474 accessor: i.constructor.name,
6575 namedArguments: i.namedArguments,
6676 positionalArguments: i.positionalArguments,
6777 );
78+ if (tryResult (result)) {
79+ return result;
80+ }
6881 }
69- if (origin == null ) {
70- return null ;
71- }
72- for (final e in origin.definingCompilationUnit.topLevelVariables.where (
73- (f) => f.isPublic && f.isConst && f.computeConstantValue () == object,
74- )) {
75- return Revivable ._(
76- source: Uri .parse (urlOfElement (origin)).replace (fragment: '' ),
77- accessor: e.name,
78- );
82+ if (origin != null ) {
83+ for (final e in origin.definingCompilationUnit.topLevelVariables.where (
84+ (f) => f.isConst && f.computeConstantValue () == object,
85+ )) {
86+ final result = Revivable ._(
87+ source: Uri .parse (urlOfElement (origin)).replace (fragment: '' ),
88+ accessor: e.name,
89+ );
90+ if (tryResult (result)) {
91+ return result;
92+ }
93+ }
7994 }
80- return null ;
95+ // We could try and return the "best" result more intelligently.
96+ return allResults.first;
8197}
8298
8399/// Decoded "instructions" for re-creating a const [DartObject] at runtime.
@@ -113,5 +129,18 @@ class Revivable {
113129 ///
114130 /// Builds tools may use this to fail when the symbol is expected to be
115131 /// importable (i.e. isn't used with `part of` ).
116- bool get isPrivate => accessor.startsWith ('_' );
132+ bool get isPrivate {
133+ return source.fragment.startsWith ('_' ) || accessor.startsWith ('_' );
134+ }
135+
136+ @override
137+ String toString () {
138+ if (source.fragment.isNotEmpty) {
139+ if (accessor.isEmpty) {
140+ return 'const $source ' ;
141+ }
142+ return 'const $source .$accessor ' ;
143+ }
144+ return '$source ::$accessor ' ;
145+ }
117146}
0 commit comments