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)
| 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 | } |