Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
InstanceRefToString.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, the Dart project authors.
3 *
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package org.dartlang.vm.service;
15
16import org.dartlang.vm.service.consumer.GetInstanceConsumer;
17import org.dartlang.vm.service.element.BoundField;
18import org.dartlang.vm.service.element.ClassRef;
19import org.dartlang.vm.service.element.Instance;
20import org.dartlang.vm.service.element.InstanceKind;
21import org.dartlang.vm.service.element.InstanceRef;
22import org.dartlang.vm.service.element.Isolate;
23import org.dartlang.vm.service.element.RPCError;
24
25/**
26 * Utility class for converting {@link InstanceRef} to a human readable string.
27 */
28public class InstanceRefToString {
29 private Isolate isolate;
30 private final VmService service;
31 private final OpLatch masterLatch;
32
33 /**
34 * Construct a new instance for converting one or more {@link InstanceRef} to human readable
35 * strings. Specify an {@link OpLatch} so that this class can update the expiration time for any
36 * waiting thread as it makes {@link VmService} class to obtain details about each
37 * {@link InstanceRef}.
38 */
39 public InstanceRefToString(Isolate isolate, VmService service, OpLatch latch) {
40 this.isolate = isolate;
41 this.service = service;
42 this.masterLatch = latch;
43 }
44
45 /**
46 * Return a human readable string for the given {@link InstanceRef}.
47 */
48 public String toString(InstanceRef ref) {
49 StringBuilder result = new StringBuilder();
50 printInstance(result, ref, 4);
51 return result.toString();
52 }
53
54 /**
55 * Request the instance information from the {@link VmService}.
56 *
57 * @param ref the instance reference (not {@code null})
58 * @return the instance or {@code null} if there was a problem.
59 */
60 private Instance getInstance(InstanceRef ref) {
61
62 // Request master latch extend its timeout because we are making another call to VmService
63 masterLatch.opWorking();
64
65 final ResultLatch<Instance> instLatch = new ResultLatch<Instance>();
66 service.getInstance(isolate.getId(), ref.getId(), new GetInstanceConsumer() {
67 @Override
68 public void onError(RPCError error) {
69 instLatch.setValue(null);
70 }
71
72 @Override
73 public void received(Instance instance) {
74 instLatch.setValue(instance);
75 }
76 });
77 return instLatch.getValue();
78 }
79
80 /**
81 * Convert the given {@link InstanceRef} into a human readable string.
82 *
83 * @param result the buffer to which the human readable string is added
84 * @param ref the instance to be converted (not {@code null})
85 * @param maxDepth the maximum number of recursions this method can make on itself to determine
86 * human readable strings for child objects
87 */
88 private void printInstance(StringBuilder result, InstanceRef ref, int maxDepth) {
89 if (ref == null) {
90 result.append("-- no value --");
91 return;
92 }
93 InstanceKind kind = ref.getKind();
94 if (kind == null) {
95 result.append("-- unknown instance kind --");
96 return;
97 }
98 switch (kind) {
99 case Bool:
100 case Double:
101 case Float32x4:
102 case Float64x2:
103 case Int:
104 case Int32x4:
105 case Null:
106 case StackTrace:
107 result.append(ref.getValueAsString());
108 return;
109 case String:
110 result.append("'");
111 // Should escape chars such as newline before printing
112 result.append(ref.getValueAsString());
113 if (ref.getValueAsStringIsTruncated()) {
114 result.append("...");
115 }
116 result.append("'");
117 return;
118 case List:
119 printList(result, ref, maxDepth);
120 return;
121 case PlainInstance:
122 printPlainInstance(result, ref, maxDepth);
123 return;
124 case BoundedType:
125 case Closure:
126 case Float32List:
127 case Float32x4List:
128 case Float64List:
129 case Float64x2List:
130 case Int16List:
131 case Int32List:
132 case Int32x4List:
133 case Int64List:
134 case Int8List:
135 case Map:
136 case MirrorReference:
137 case RegExp:
138 case Type:
139 case TypeParameter:
140 case TypeRef:
141 case Uint16List:
142 case Uint32List:
143 case Uint64List:
144 case Uint8ClampedList:
145 case Uint8List:
146 case WeakProperty:
147 }
148 result.append("a " + kind);
149 }
150
151 /**
152 * Convert the given list into a human readable string.
153 *
154 * @param result the buffer to which the human readable string is added
155 * @param ref an instance reference of type "List" (not {@code null})
156 * @param maxDepth the maximum number of recursions this method can make on itself to determine
157 * human readable strings for child objects
158 */
159 private void printList(StringBuilder result, InstanceRef ref, int maxDepth) {
160 if (maxDepth == 0) {
161 result.append("a List");
162 return;
163 }
164 result.append("[");
165 Instance list = getInstance(ref);
166 if (list == null) {
167 result.append("?error?]");
168 return;
169 }
170 int count = 0;
171 for (InstanceRef elem : list.getElements()) {
172 if (count > 10) {
173 result.append(", ...");
174 break;
175 }
176 if (count > 0) {
177 result.append(", ");
178 }
179 ++count;
180 printInstance(result, elem, maxDepth - 1);
181 }
182 result.append("]");
183 }
184
185 /**
186 * Convert the given instance into a human readable string.
187 *
188 * @param result the buffer to which the human readable string is added
189 * @param ref an instance reference of type "PlainInstance" (not {@code null})
190 * @param maxDepth the maximum number of recursions this method can make on itself to determine
191 * human readable strings for child objects
192 */
193 private void printPlainInstance(StringBuilder result, InstanceRef ref, int maxDepth) {
194 ClassRef classRef = ref.getClassRef();
195 String className = classRef.getName();
196 if (maxDepth == 0) {
197 result.append("a " + className);
198 return;
199 }
200 result.append(className);
201 result.append("(");
202 Instance inst = getInstance(ref);
203 boolean first = true;
204 for (BoundField field : inst.getFields()) {
205 if (first) {
206 first = false;
207 } else {
208 result.append(", ");
209 }
210 printInstance(result, field.getValue(), maxDepth - 1);
211 }
212 result.append(")");
213 }
214}
int count
InstanceRefToString(Isolate isolate, VmService service, OpLatch latch)
VkInstance instance
Definition main.cc:48
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
SK_API sk_sp< SkSurface > Null(int width, int height)