@@ -34,8 +34,8 @@ Revivable reviveInstance(DartObject object, [LibraryElement origin]) {
34
34
accessor: '${element .enclosingElement .name }.${element .name }' ,
35
35
);
36
36
}
37
- final clazz = element as ClassElement ;
38
37
// Enums are not included in .definingCompilationUnit.types.
38
+ final clazz = element as ClassElement ;
39
39
if (clazz.isEnum) {
40
40
for (final e in clazz.fields.where (
41
41
(f) => f.isPublic && f.isConst && f.computeConstantValue () == object)) {
@@ -45,39 +45,55 @@ Revivable reviveInstance(DartObject object, [LibraryElement origin]) {
45
45
);
46
46
}
47
47
}
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
+
48
58
for (final e in origin.definingCompilationUnit.types
49
59
.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 ._(
53
62
source: url.removeFragment (),
54
63
accessor: '${clazz .name }.${e .name }' ,
55
64
);
65
+ if (tryResult (result)) {
66
+ return result;
67
+ }
56
68
}
57
69
final i = (object as DartObjectImpl ).getInvocation ();
58
- if (i != null &&
59
- i.constructor.isPublic &&
60
- i.constructor.enclosingElement.isPublic) {
70
+ if (i != null ) {
61
71
url = Uri .parse (urlOfElement (i.constructor.enclosingElement));
62
- return Revivable ._(
72
+ final result = Revivable ._(
63
73
source: url,
64
74
accessor: i.constructor.name,
65
75
namedArguments: i.namedArguments,
66
76
positionalArguments: i.positionalArguments,
67
77
);
78
+ if (tryResult (result)) {
79
+ return result;
80
+ }
68
81
}
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
+ }
79
94
}
80
- return null ;
95
+ // We could try and return the "best" result more intelligently.
96
+ return allResults.first;
81
97
}
82
98
83
99
/// Decoded "instructions" for re-creating a const [DartObject] at runtime.
@@ -113,5 +129,18 @@ class Revivable {
113
129
///
114
130
/// Builds tools may use this to fail when the symbol is expected to be
115
131
/// 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
+ }
117
146
}
0 commit comments