MCPcopy
hub / github.com/bazelbuild/bazel / repr

Method repr

src/main/java/net/starlark/java/eval/Printer.java:160–223  ·  view source on GitHub ↗

Appends the StarlarkValue.repr (quoted) representation of a value to the printer's buffer. The quoted form is often a Starlark expression that evaluates to the value. Implementations of StarlarkValue may define their own behavior of repr. Cyclic values are rendered as {@code

(Object o)

Source from the content-addressed store, hash-verified

158 * TODO(adonovan): disallow that.
159 */
160 public Printer repr(Object o) {
161 // atomic values (leaves of the object graph)
162 if (o == null) {
163 // Java null is not a valid Starlark value, but sometimes printers are used on non-Starlark
164 // values such as Locations or Nodes.
165 return this.append("null");
166
167 } else if (o instanceof String) {
168 appendQuoted((String) o);
169 return this;
170
171 } else if (o instanceof StarlarkInt) {
172 ((StarlarkInt) o).repr(this);
173 return this;
174
175 } else if (o instanceof Boolean) {
176 this.append(((boolean) o) ? "True" : "False");
177 return this;
178
179 } else if (o instanceof Integer) { // a non-Starlark value
180 this.buffer.append((int) o);
181 return this;
182
183 } else if (o instanceof Class) { // a non-Starlark value
184 this.append(Starlark.classType((Class<?>) o));
185 return this;
186 }
187
188 // compound values (may form cycles in the object graph)
189
190 if (!push(o)) {
191 return this.append("..."); // elided cycle
192 }
193 try {
194 if (o instanceof StarlarkValue) {
195 ((StarlarkValue) o).repr(this);
196
197 // -- non-Starlark values --
198
199 } else if (o instanceof Map) {
200 Map<?, ?> dict = (Map<?, ?>) o;
201 this.printList(dict.entrySet(), "{", ", ", "}");
202
203 } else if (o instanceof List) {
204 this.printList((List) o, "[", ", ", "]");
205
206 } else if (o instanceof Map.Entry) {
207 Map.Entry<?, ?> entry = (Map.Entry<?, ?>) o;
208 this.repr(entry.getKey());
209 this.append(": ");
210 this.repr(entry.getValue());
211
212 } else {
213 // All other non-Starlark Java values (e.g. Node, Location).
214 // Starlark code cannot access values of o that would reach here,
215 // and native code is already trusted to be deterministic.
216 this.append(o.toString());
217 }

Callers 2

printListMethod · 0.95
strMethod · 0.95

Calls 11

appendMethod · 0.95
appendQuotedMethod · 0.95
classTypeMethod · 0.95
pushMethod · 0.95
printListMethod · 0.95
popMethod · 0.95
reprMethod · 0.65
getValueMethod · 0.65
entrySetMethod · 0.45
getKeyMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected