Returns the name of the type of instances of class c. This function accepts any class, not just those of legal Starlark values, and may be used for reporting error messages involving arbitrary Java classes, for example at the interface between Starlark and Java.
(Class<?> c)
| 335 | * between Starlark and Java. |
| 336 | */ |
| 337 | public static String classType(Class<?> c) { |
| 338 | // Check for "direct hits" first to avoid needing to scan for annotations. |
| 339 | if (c.equals(String.class)) { |
| 340 | return "string"; |
| 341 | } else if (StarlarkInt.class.isAssignableFrom(c)) { |
| 342 | return "int"; |
| 343 | } else if (c.equals(Boolean.class)) { |
| 344 | return "bool"; |
| 345 | } else if (c.equals(StarlarkFloat.class)) { |
| 346 | return "float"; |
| 347 | } |
| 348 | |
| 349 | // Shortcut for the most common types. |
| 350 | // These cases can be handled by `getStarlarkBuiltin` |
| 351 | // but `getStarlarkBuiltin` is quite expensive. |
| 352 | if (StarlarkList.class.isAssignableFrom(c)) { |
| 353 | return "list"; |
| 354 | } else if (Tuple.class.isAssignableFrom(c)) { |
| 355 | return "tuple"; |
| 356 | } else if (c.equals(Dict.class)) { |
| 357 | return "dict"; |
| 358 | } else if (c.equals(NoneType.class)) { |
| 359 | return "NoneType"; |
| 360 | } else if (c.equals(StarlarkFunction.class)) { |
| 361 | return "function"; |
| 362 | } else if (c.equals(RangeList.class)) { |
| 363 | return "range"; |
| 364 | } else if (c.equals(UnboundMarker.class)) { |
| 365 | return "unbound"; |
| 366 | } |
| 367 | |
| 368 | // Abstract types, often used as parameter types. |
| 369 | // Note == not isAssignableFrom: we don't want any |
| 370 | // concrete types to inherit these names. |
| 371 | if (c == StarlarkIterable.class) { |
| 372 | return "iterable"; |
| 373 | } else if (c == Sequence.class) { |
| 374 | return "sequence"; |
| 375 | } else if (c == StarlarkCallable.class) { |
| 376 | return "callable"; |
| 377 | } else if (c == Structure.class) { |
| 378 | return "structure"; |
| 379 | } |
| 380 | |
| 381 | StarlarkBuiltin module = StarlarkAnnotations.getStarlarkBuiltin(c); |
| 382 | if (module != null) { |
| 383 | return module.name(); |
| 384 | } |
| 385 | |
| 386 | if (c.equals(Object.class)) { |
| 387 | // "unknown" is another unfortunate choice. |
| 388 | // Object.class does mean "unknown" when talking about the type parameter |
| 389 | // of a collection (List<Object>), but it also means "any" when used |
| 390 | // as an argument to Sequence.cast, and more generally it means "value". |
| 391 | return "unknown"; |
| 392 | |
| 393 | } else if (List.class.isAssignableFrom(c)) { |
| 394 | // Any class of java.util.List that isn't a Sequence. |
no test coverage detected