|
1 | 1 | package com.hubspot.jinjava.lib.filter; |
2 | 2 |
|
3 | | -import static com.hubspot.jinjava.util.Logging.ENGINE_LOG; |
4 | | - |
| 3 | +import com.fasterxml.jackson.databind.node.POJONode; |
5 | 4 | import com.hubspot.jinjava.doc.annotations.JinjavaDoc; |
6 | 5 | import com.hubspot.jinjava.doc.annotations.JinjavaParam; |
7 | 6 | import com.hubspot.jinjava.doc.annotations.JinjavaSnippet; |
8 | | -import com.hubspot.jinjava.interpret.DeferredValueException; |
9 | 7 | import com.hubspot.jinjava.interpret.JinjavaInterpreter; |
10 | 8 | import com.hubspot.jinjava.objects.date.PyishDate; |
11 | | -import java.beans.BeanInfo; |
12 | | -import java.beans.IntrospectionException; |
13 | | -import java.beans.Introspector; |
14 | | -import java.beans.PropertyDescriptor; |
15 | | -import java.lang.reflect.InvocationTargetException; |
16 | | -import java.lang.reflect.Method; |
17 | | -import java.util.LinkedList; |
18 | | -import java.util.List; |
19 | 9 | import java.util.Map; |
20 | 10 | import java.util.Objects; |
21 | | -import org.apache.commons.lang3.StringEscapeUtils; |
22 | | -import org.apache.commons.lang3.StringUtils; |
23 | 11 |
|
24 | 12 | @JinjavaDoc( |
25 | 13 | value = "Pretty print a variable. Useful for debugging.", |
@@ -60,50 +48,11 @@ public Object filter(Object var, JinjavaInterpreter interpreter, String... args) |
60 | 48 | ) { |
61 | 49 | varStr = Objects.toString(var); |
62 | 50 | } else { |
63 | | - varStr = objPropsToString(var); |
| 51 | + varStr = new POJONode(var).toPrettyString(); |
64 | 52 | } |
65 | 53 |
|
66 | | - return StringEscapeUtils.escapeHtml4( |
| 54 | + return EscapeFilter.escapeHtmlEntities( |
67 | 55 | "{% raw %}(" + var.getClass().getSimpleName() + ": " + varStr + "){% endraw %}" |
68 | 56 | ); |
69 | 57 | } |
70 | | - |
71 | | - private String objPropsToString(Object var) { |
72 | | - List<String> props = new LinkedList<>(); |
73 | | - |
74 | | - try { |
75 | | - BeanInfo beanInfo = Introspector.getBeanInfo(var.getClass()); |
76 | | - |
77 | | - for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { |
78 | | - try { |
79 | | - if (pd.getPropertyType() != null && pd.getPropertyType().equals(Class.class)) { |
80 | | - continue; |
81 | | - } |
82 | | - |
83 | | - Method readMethod = pd.getReadMethod(); |
84 | | - if ( |
85 | | - readMethod != null && !readMethod.getDeclaringClass().equals(Object.class) |
86 | | - ) { |
87 | | - props.add(pd.getName() + "=" + readMethod.invoke(var)); |
88 | | - } |
89 | | - } catch (Exception e) { |
90 | | - if ( |
91 | | - e instanceof InvocationTargetException && |
92 | | - ( |
93 | | - (InvocationTargetException) e |
94 | | - ).getTargetException() instanceof DeferredValueException |
95 | | - ) { |
96 | | - throw (DeferredValueException) ( |
97 | | - (InvocationTargetException) e |
98 | | - ).getTargetException(); |
99 | | - } |
100 | | - ENGINE_LOG.error("Error reading bean value", e); |
101 | | - } |
102 | | - } |
103 | | - } catch (IntrospectionException e) { |
104 | | - ENGINE_LOG.error("Error inspecting bean", e); |
105 | | - } |
106 | | - |
107 | | - return '{' + StringUtils.join(props, ", ") + '}'; |
108 | | - } |
109 | 58 | } |
0 commit comments