Coerce an object to a string. @param ctx the context in which this conversion is taking place @param obj the object to convert @return the String value of the object
(final ELContext ctx, final Object obj)
| 489 | * @return the String value of the object |
| 490 | */ |
| 491 | public static String coerceToString(final ELContext ctx, final Object obj) { |
| 492 | |
| 493 | if (ctx != null) { |
| 494 | boolean originalIsPropertyResolved = ctx.isPropertyResolved(); |
| 495 | try { |
| 496 | String result = ctx.getELResolver().convertToType(ctx, obj, String.class); |
| 497 | if (ctx.isPropertyResolved()) { |
| 498 | return result; |
| 499 | } |
| 500 | } finally { |
| 501 | ctx.setPropertyResolved(originalIsPropertyResolved); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if (obj == null) { |
| 506 | return ""; |
| 507 | } else if (obj instanceof String) { |
| 508 | return (String) obj; |
| 509 | } else if (obj instanceof Enum<?>) { |
| 510 | return ((Enum<?>) obj).name(); |
| 511 | } else { |
| 512 | try { |
| 513 | return obj.toString(); |
| 514 | } catch (ELException e) { |
| 515 | // Unlikely but you never know |
| 516 | throw e; |
| 517 | } catch (Throwable t) { |
| 518 | ExceptionUtils.handleThrowable(t); |
| 519 | throw new ELException(t); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Coerces an object to the specified target type. |
no test coverage detected