|
26 | 26 |
|
27 | 27 | import java.util.Arrays;
|
28 | 28 |
|
29 |
| -import jdk.graal.compiler.core.common.type.Stamp; |
30 | 29 | import jdk.graal.compiler.nodeinfo.Verbosity;
|
31 |
| -import jdk.graal.compiler.nodes.NodeView; |
32 | 30 | import jdk.graal.compiler.nodes.ValueNode;
|
33 | 31 | import jdk.graal.compiler.nodes.cfg.HIRBlock;
|
34 | 32 | import jdk.graal.compiler.options.Option;
|
@@ -77,72 +75,128 @@ private static String formatAssertionContextArgV(Object... args) {
|
77 | 75 | for (int i = 0; i < args.length; i += 2) {
|
78 | 76 | Object val = args[i + 1];
|
79 | 77 | sb.append(args[i]).append("=");
|
80 |
| - formatObjectContext(sb, val); |
| 78 | + sb.append(decorateObjectErrorContext(val)); |
81 | 79 | if (++entriesDone < entries) {
|
82 | 80 | sb.append(";");
|
83 | 81 | }
|
84 | 82 | }
|
85 | 83 | return sb.toString();
|
86 | 84 | }
|
87 | 85 |
|
88 |
| - private static void formatObjectContext(StringBuilder sb, Object val) { |
| 86 | + /** |
| 87 | + * Returns a string representation of the given object, enhancing it with additional contextual |
| 88 | + * information if such data is available. |
| 89 | + */ |
| 90 | + public static Object decorateObjectErrorContext(Object o) { |
89 | 91 | try {
|
90 |
| - if (val instanceof HIRBlock b) { |
91 |
| - sb.append(b.toString(Verbosity.All)); |
92 |
| - } else if (val instanceof ValueNode v) { |
93 |
| - sb.append(v.toString(Verbosity.All)); |
94 |
| - sb.append("/"); |
95 |
| - Stamp stamp = v.stamp(NodeView.DEFAULT); |
96 |
| - sb.append(stamp).append(" "); |
97 |
| - sb.append(v.getStackKind()); |
98 |
| - sb.append("\\"); |
| 92 | + if (o instanceof HIRBlock b) { |
| 93 | + return b.toString(Verbosity.All); |
| 94 | + } else if (o instanceof ValueNode v) { |
| 95 | + return v.toString(Verbosity.All); |
99 | 96 | } else {
|
100 |
| - sb.append(val); |
| 97 | + return o; |
101 | 98 | }
|
102 | 99 | } catch (Throwable e) {
|
103 |
| - sb.append("Error calling toString on object ").append(e.getMessage()); |
| 100 | + return "Error calling toString on object " + e.getMessage(); |
104 | 101 | }
|
105 | 102 | }
|
106 | 103 |
|
| 104 | + /** |
| 105 | + * Returns an enhanced error message for the supplied string and object. It considers arguments |
| 106 | + * one by one. Each argument is represented by its {@code toString} representation enhanced with |
| 107 | + * {@code decorateObjectErrorContext(arg[i])}. It uses |
| 108 | + * {@link #decorateObjectErrorContext(Object)} for the {@code toString} of the values. |
| 109 | + * |
| 110 | + * For example the following code |
| 111 | + * |
| 112 | + * <pre> |
| 113 | + * LoopBeginNode loopBegin = getLoopBegin(); |
| 114 | + * Assertions.errorMessage("Message", loopBeginNode); |
| 115 | + * </pre> |
| 116 | + * |
| 117 | + * would be formatted to (for an arbitrary loop begin node with {@code id=6}, output is cropped |
| 118 | + * for brevity): |
| 119 | + * |
| 120 | + * {@code [0]=Message;[1]=6|LoopBegin { guestLoopEndsSafepointState...}}. |
| 121 | + * |
| 122 | + */ |
107 | 123 | public static String errorMessage(Object... args) {
|
108 | 124 | StringBuilder sb = new StringBuilder();
|
109 |
| - int entries = args.length; |
| 125 | + final int entries = args.length; |
110 | 126 | int entriesDone = 0;
|
111 | 127 | for (int i = 0; i < args.length; i++) {
|
112 | 128 | Object val = args[i];
|
113 | 129 | sb.append("[").append(i).append("]=");
|
114 |
| - formatObjectContext(sb, val); |
| 130 | + sb.append(decorateObjectErrorContext(val)); |
115 | 131 | if (++entriesDone < entries) {
|
116 | 132 | sb.append(";");
|
117 | 133 | }
|
118 | 134 | }
|
119 | 135 | return sb.toString();
|
120 | 136 | }
|
121 | 137 |
|
| 138 | + /** |
| 139 | + * Returns an enhanced error message for the supplied string and object. It considers arguments |
| 140 | + * pair wise in a key - value fashion. Each pair is then represented in its {@code toString} |
| 141 | + * representation of the form {@code key=decorateObjectErrorContext(value)}. It uses |
| 142 | + * {@link #decorateObjectErrorContext(Object)} for the {@code toString} of the values. |
| 143 | + * |
| 144 | + * For example the following code |
| 145 | + * |
| 146 | + * <pre> |
| 147 | + * LoopBeginNode loopBegin = getLoopBegin(); |
| 148 | + * Assertions.errorMessageContext("Message", loopBeginNode); |
| 149 | + * </pre> |
| 150 | + * |
| 151 | + * would be formatted to (for an arbitrary loop begin node with {@code id=6}, output is cropped |
| 152 | + * for brevity): |
| 153 | + * |
| 154 | + * {@code Message=6|LoopBegin { guestLoopEndsSafepointState=ENABLED, splits=0, |
| 155 | + * cloneFromNodeId=-1}...}. |
| 156 | + * |
| 157 | + */ |
122 | 158 | public static String errorMessageContext(String s1, Object o1) {
|
123 | 159 | return formatAssertionContextArgV(s1, o1);
|
124 | 160 | }
|
125 | 161 |
|
| 162 | + /** |
| 163 | + * See {@link #errorMessageContext(String, Object)}. |
| 164 | + */ |
126 | 165 | public static String errorMessageContext(String s1, Object o1, String s2, Object o2) {
|
127 | 166 | return formatAssertionContextArgV(s1, o1, s2, o2);
|
128 | 167 | }
|
129 | 168 |
|
| 169 | + /** |
| 170 | + * See {@link #errorMessageContext(String, Object)}. |
| 171 | + */ |
130 | 172 | public static String errorMessageContext(String s1, Object o1, String s2, Object o2, String s3, Object o3) {
|
131 | 173 | return formatAssertionContextArgV(s1, o1, s2, o2, s3, o3);
|
132 | 174 | }
|
133 | 175 |
|
| 176 | + /** |
| 177 | + * See {@link #errorMessageContext(String, Object)}. |
| 178 | + */ |
134 | 179 | public static String errorMessageContext(String s1, Object o1, String s2, Object o2, String s3, Object o3, String s4, Object o4) {
|
135 | 180 | return formatAssertionContextArgV(s1, o1, s2, o2, s3, o3, s4, o4);
|
136 | 181 | }
|
137 | 182 |
|
| 183 | + /** |
| 184 | + * See {@link #errorMessageContext(String, Object)}. |
| 185 | + */ |
138 | 186 | public static String errorMessageContext(String s1, Object o1, String s2, Object o2, String s3, Object o3, String s4, Object o4, String s5, Object o5) {
|
139 | 187 | return formatAssertionContextArgV(s1, o1, s2, o2, s3, o3, s4, o4, s5, o5);
|
140 | 188 | }
|
141 | 189 |
|
| 190 | + /** |
| 191 | + * See {@link #errorMessageContext(String, Object)}. |
| 192 | + */ |
142 | 193 | public static String errorMessageContext(String s1, Object o1, String s2, Object o2, String s3, Object o3, String s4, Object o4, String s5, Object o5, String s6, Object o6) {
|
143 | 194 | return formatAssertionContextArgV(s1, o1, s2, o2, s3, o3, s4, o4, s5, o5, s6, o6);
|
144 | 195 | }
|
145 | 196 |
|
| 197 | + /** |
| 198 | + * See {@link #errorMessageContext(String, Object)}. |
| 199 | + */ |
146 | 200 | public static String errorMessageContext(String s1, Object o1, String s2, Object o2, String s3, Object o3, String s4, Object o4, String s5, Object o5, String s6, Object o6, String s7,
|
147 | 201 | Object o7) {
|
148 | 202 | return formatAssertionContextArgV(s1, o1, s2, o2, s3, o3, s4, o4, s5, o5, s6, o6, s7, o7);
|
|
0 commit comments