Skip to content

Commit 5e35df0

Browse files
committed
GROOVY-11893: Standardise hooks for Groovy pretty printing
1 parent 8d4cc0a commit 5e35df0

9 files changed

Lines changed: 385 additions & 64 deletions

File tree

src/main/java/groovy/lang/IncorrectClosureArgumentsException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public IncorrectClosureArgumentsException(Closure closure, Object arguments, Cla
3838
"Incorrect arguments to closure: "
3939
+ closure
4040
+ ". Expected: "
41-
+ FormatHelper.toString(expected)
41+
+ FormatHelper.toArrayString(expected)
4242
+ ", actual: "
43-
+ FormatHelper.toString(arguments));
43+
+ FormatHelper.toString(arguments)); // arguments is Object, not array
4444
this.closure = closure;
4545
this.arguments = arguments;
4646
this.expected = expected;

src/main/java/groovy/lang/MetaClassImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,11 +3344,11 @@ protected static String createErrorMessageForAmbiguity(String theClassName, Stri
33443344
StringBuilder msg = new StringBuilder("Ambiguous method overloading for method ");
33453345
msg.append(theClassName).append("#").append(name)
33463346
.append(".\nCannot resolve which method to invoke for ")
3347-
.append(FormatHelper.toString(arguments))
3347+
.append(FormatHelper.toArrayString(arguments))
33483348
.append(" due to overlapping prototypes between:");
33493349
for (final Object match : matches) {
33503350
CachedClass[] types = ((ParameterTypes) match).getParameterTypes();
3351-
msg.append("\n\t").append(FormatHelper.toString(types));
3351+
msg.append("\n\t").append(FormatHelper.toArrayString(types));
33523352
}
33533353
return msg.toString();
33543354
}

src/main/java/groovy/lang/MetaMethod.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ public void checkParameters(Class[] arguments) {
9494
"Parameters to method: "
9595
+ getName()
9696
+ " do not match types: "
97-
+ FormatHelper.toString(getParameterTypes())
97+
+ FormatHelper.toArrayString(getParameterTypes())
9898
+ " for arguments: "
99-
+ FormatHelper.toString(arguments));
99+
+ FormatHelper.toArrayString(arguments));
100100
}
101101
}
102102

@@ -143,7 +143,7 @@ public String toString() {
143143
+ "[name: "
144144
+ getName()
145145
+ " params: "
146-
+ FormatHelper.toString(getParameterTypes())
146+
+ FormatHelper.toArrayString(getParameterTypes())
147147
+ " returns: "
148148
+ getReturnType()
149149
+ " owner: "

src/main/java/org/codehaus/groovy/reflection/CachedConstructor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private static GroovyRuntimeException createException(String init, Constructor c
112112
init
113113
+ constructor
114114
+ " with arguments: "
115-
+ FormatHelper.toString(argumentArray)
115+
+ FormatHelper.toArrayString(argumentArray)
116116
+ " reason: "
117117
+ e,
118118
setReason ? e : null);

src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4429,6 +4429,142 @@ public static <T, U, K> Map<K, List<U>> groupByMany(
44294429
return DefaultGroovyMethods.groupByMany(Arrays.asList(self), valueFn, keyFn);
44304430
}
44314431

4432+
//--------------------------------------------------------------------------
4433+
// groovyToString
4434+
4435+
/**
4436+
* Returns Groovy's list-like string representation for an Object array.
4437+
* This is used by Groovy's formatting infrastructure (e.g., GString interpolation,
4438+
* {@code println}, assert messages). By default, it delegates to
4439+
* {@link FormatHelper#toArrayString(Object[])}.
4440+
* <p>
4441+
* If disabled, e.g. via {@code -Dgroovy.extension.disable=groovyToString(Object[])},
4442+
* the JDK's default array {@code toString()} is used instead.
4443+
* Alternatively, you have the option to provide a replacement extension method in an extension module
4444+
* to customize how arrays are displayed throughout Groovy.
4445+
*
4446+
* @param self the array to format
4447+
* @return the string representation
4448+
* @since 6.0.0
4449+
*/
4450+
public static String groovyToString(Object[] self) {
4451+
return FormatHelper.toArrayString(self);
4452+
}
4453+
4454+
/**
4455+
* Returns Groovy's list-like string representation for a boolean array.
4456+
* <p>
4457+
* If disabled, e.g. via {@code -Dgroovy.extension.disable=groovyToString(boolean[])},
4458+
* the JDK's default array {@code toString()} is used instead.
4459+
*
4460+
* @param self the array to format
4461+
* @return the string representation
4462+
* @since 6.0.0
4463+
*/
4464+
public static String groovyToString(boolean[] self) {
4465+
return FormatHelper.toListString(DefaultTypeTransformation.primitiveArrayToList(self));
4466+
}
4467+
4468+
/**
4469+
* Returns Groovy's list-like string representation for a byte array.
4470+
* <p>
4471+
* If disabled, e.g. via {@code -Dgroovy.extension.disable=groovyToString(byte[])},
4472+
* the JDK's default array {@code toString()} is used instead.
4473+
*
4474+
* @param self the array to format
4475+
* @return the string representation
4476+
* @since 6.0.0
4477+
*/
4478+
public static String groovyToString(byte[] self) {
4479+
return FormatHelper.toListString(DefaultTypeTransformation.primitiveArrayToList(self));
4480+
}
4481+
4482+
/**
4483+
* Returns Groovy's string representation for a char array.
4484+
* By default, a char array is rendered as a String (e.g. {@code 'abc'.chars}
4485+
* displays as {@code abc}).
4486+
* <p>
4487+
* If disabled, e.g. via {@code -Dgroovy.extension.disable=groovyToString(char[])},
4488+
* the JDK's default array {@code toString()} is used instead.
4489+
*
4490+
* @param self the array to format
4491+
* @return the string representation
4492+
* @since 6.0.0
4493+
*/
4494+
public static String groovyToString(char[] self) {
4495+
return new String(self);
4496+
}
4497+
4498+
/**
4499+
* Returns Groovy's list-like string representation for a short array.
4500+
* <p>
4501+
* If disabled, e.g. via {@code -Dgroovy.extension.disable=groovyToString(short[])},
4502+
* the JDK's default array {@code toString()} is used instead.
4503+
*
4504+
* @param self the array to format
4505+
* @return the string representation
4506+
* @since 6.0.0
4507+
*/
4508+
public static String groovyToString(short[] self) {
4509+
return FormatHelper.toListString(DefaultTypeTransformation.primitiveArrayToList(self));
4510+
}
4511+
4512+
/**
4513+
* Returns Groovy's list-like string representation for an int array.
4514+
* <p>
4515+
* If disabled, e.g. via {@code -Dgroovy.extension.disable=groovyToString(int[])},
4516+
* the JDK's default array {@code toString()} is used instead.
4517+
*
4518+
* @param self the array to format
4519+
* @return the string representation
4520+
* @since 6.0.0
4521+
*/
4522+
public static String groovyToString(int[] self) {
4523+
return FormatHelper.toListString(DefaultTypeTransformation.primitiveArrayToList(self));
4524+
}
4525+
4526+
/**
4527+
* Returns Groovy's list-like string representation for a long array.
4528+
* <p>
4529+
* If disabled, e.g. via {@code -Dgroovy.extension.disable=groovyToString(long[])},
4530+
* the JDK's default array {@code toString()} is used instead.
4531+
*
4532+
* @param self the array to format
4533+
* @return the string representation
4534+
* @since 6.0.0
4535+
*/
4536+
public static String groovyToString(long[] self) {
4537+
return FormatHelper.toListString(DefaultTypeTransformation.primitiveArrayToList(self));
4538+
}
4539+
4540+
/**
4541+
* Returns Groovy's list-like string representation for a float array.
4542+
* <p>
4543+
* If disabled, e.g. via {@code -Dgroovy.extension.disable=groovyToString(float[])},
4544+
* the JDK's default array {@code toString()} is used instead.
4545+
*
4546+
* @param self the array to format
4547+
* @return the string representation
4548+
* @since 6.0.0
4549+
*/
4550+
public static String groovyToString(float[] self) {
4551+
return FormatHelper.toListString(DefaultTypeTransformation.primitiveArrayToList(self));
4552+
}
4553+
4554+
/**
4555+
* Returns Groovy's list-like string representation for a double array.
4556+
* <p>
4557+
* If disabled, e.g. via {@code -Dgroovy.extension.disable=groovyToString(double[])},
4558+
* the JDK's default array {@code toString()} is used instead.
4559+
*
4560+
* @param self the array to format
4561+
* @return the string representation
4562+
* @since 6.0.0
4563+
*/
4564+
public static String groovyToString(double[] self) {
4565+
return FormatHelper.toListString(DefaultTypeTransformation.primitiveArrayToList(self));
4566+
}
4567+
44324568
//--------------------------------------------------------------------------
44334569
// head
44344570

src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8139,6 +8139,101 @@ public static <G, K, V> Map<G, List<Map.Entry<K, V>>> groupEntriesBy(Map<K, V> s
81398139
return answer;
81408140
}
81418141

8142+
//--------------------------------------------------------------------------
8143+
// groovyToString
8144+
8145+
/**
8146+
* Returns Groovy's default string representation for a Map.
8147+
* This is used by Groovy's formatting infrastructure (e.g., GString interpolation,
8148+
* {@code println}, assert messages). By default, it delegates to
8149+
* {@link FormatHelper#toMapString(Map)}.
8150+
* <p>
8151+
* <pre class="groovyTestCase">
8152+
* assert [a:1, b:2].groovyToString() == '[a:1, b:2]'
8153+
* </pre>
8154+
* <p>
8155+
* If disabled, e.g. via {@code -Dgroovy.extension.disable=groovyToString(Map)},
8156+
* the normal map {@code toString()} is used instead.
8157+
* Alternatively, you have the option to provide a replacement extension method in an extension module
8158+
* to customize how maps are displayed throughout Groovy.
8159+
*
8160+
* @param self the Map to format
8161+
* @return the string representation
8162+
* @since 6.0.0
8163+
*/
8164+
public static String groovyToString(Map self) {
8165+
return FormatHelper.toMapString(self);
8166+
}
8167+
8168+
/**
8169+
* Returns Groovy's default string representation for a Range.
8170+
* By default, it delegates to {@link Range#toString()},
8171+
* producing the compact {@code from..to} notation.
8172+
* <p>
8173+
* <pre class="groovyTestCase">
8174+
* assert (1..4).groovyToString() == '1..4'
8175+
* </pre>
8176+
* <p>
8177+
* This method exists to stop the {@code groovyToString(Collection)} variant
8178+
* from overriding the built-in {@code Range#toString}.
8179+
* Since a range is a list, you can use {@code range.toListString()}
8180+
* to print it using normal list formatting.
8181+
*
8182+
* @param self the Range to format
8183+
* @return the string representation
8184+
* @since 6.0.0
8185+
*/
8186+
public static String groovyToString(Range self) {
8187+
return self.toString();
8188+
}
8189+
8190+
/**
8191+
* Returns Groovy's default string representation for a Collection.
8192+
* This is used by Groovy's formatting infrastructure (e.g., GString interpolation,
8193+
* {@code println}, assert messages). By default, it delegates to
8194+
* {@link FormatHelper#toListString(Collection)}.
8195+
* <p>
8196+
* <pre class="groovyTestCase">
8197+
* assert [1, 2, 3].groovyToString() == '[1, 2, 3]'
8198+
* </pre>
8199+
* <p>
8200+
* If disabled, e.g. via {@code -Dgroovy.extension.disable=groovyToString(Collection)},
8201+
* the normal list {@code toString()} is used instead.
8202+
* Alternatively, you have the option to provide a replacement extension method in an extension module
8203+
* to customize how collections are displayed throughout Groovy.
8204+
*
8205+
* @param self the Collection to format
8206+
* @return the string representation
8207+
* @since 6.0.0
8208+
*/
8209+
public static String groovyToString(Collection self) {
8210+
return FormatHelper.toListString(self);
8211+
}
8212+
8213+
/**
8214+
* Returns Groovy's default string representation for an XML Element.
8215+
* This is used by Groovy's formatting infrastructure. By default, it
8216+
* serializes the element using {@code groovy.xml.XmlUtil.serialize(Element)}.
8217+
* <p>
8218+
* If disabled, e.g. via {@code -Dgroovy.extension.disable=groovyToString(Element)},
8219+
* the element's default {@code toString()} is used instead.
8220+
* Alternatively, you have the option to provide a replacement extension method in an extension module
8221+
* to customize how elements are displayed throughout Groovy.
8222+
*
8223+
* @param self the Element to format
8224+
* @return the serialized XML string
8225+
* @since 6.0.0
8226+
*/
8227+
public static String groovyToString(org.w3c.dom.Element self) {
8228+
try {
8229+
java.lang.reflect.Method serialize = Class.forName("groovy.xml.XmlUtil")
8230+
.getMethod("serialize", org.w3c.dom.Element.class);
8231+
return (String) serialize.invoke(null, self);
8232+
} catch (Exception e) {
8233+
return self.toString();
8234+
}
8235+
}
8236+
81428237
//--------------------------------------------------------------------------
81438238
// hasProperty
81448239

0 commit comments

Comments
 (0)