Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate strings or values of type variables in toString method that are longer than a defined length #1185

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions value-annotations/src/org/immutables/value/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,12 @@
*/
Class<? extends Annotation>[] allowedClasspathAnnotations() default {};

/**
* Setting to cut strings longer than a defined length when calling the toString method.
* @return string limit
*/
int limitStringLengthInToString() default 1000;

/**
* If implementation visibility is more restrictive than visibility of abstract value type, then
* implementation type will not be exposed as a return type of {@code build()} or {@code of()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3165,6 +3165,14 @@ private int [disambiguateAccessor type 'computeHashCode']() {
[template generateToString Type type]
[if not type.toStringDefined]

static String objectToString(Object input, int limit) {
if (input == null) {
return null;
}
String output = input.toString();
return output.length() > limit ? output.substring(0, limit).concat("…") : output;
}

/**
* Prints the immutable value {@code [type.name]}[if type.equivalenceAttributes] with attribute values[/if].
* @return A string representation of the value
Expand Down Expand Up @@ -4380,6 +4388,6 @@ private static final long serialVersionUID = [literal serialVersion];

[template castObject String typename][if typename ne 'java.lang.Object']([typename]) [/if][/template]

[template maybeMasked Attribute a String value][if a.redactedMask][literal.string a.redactedMask][else][value][/if][/template]
[template maybeMasked Attribute a String value][if a.redactedMask][literal.string a.redactedMask][else if a.limitStringLengthInToString and (a.stringType or a.optionalStringType)]objectToString([value], [a.limitStringLengthInToString])[else if a.limitStringLengthInToString and a.hasTypeVariables]objectToString([value], [a.limitStringLengthInToString])[else][value][/if][/template]

[template hiddenMutableState Type type][if type allowsClasspathAnnotation 'com.google.errorprone.annotations.Immutable']@SuppressWarnings("Immutable")[/if][/template]
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ public Class<? extends Annotation>[] allowedClasspathAnnotations() {
throw new UnsupportedOperationException("Use StyleInfo.allowedClasspathAnnotationsNames() instead");
}

@Value.Parameter
public abstract int limitStringLengthInToString();

static StyleInfo infoFrom(StyleMirror input) {
return ImmutableStyleInfo.of(
input.get(),
Expand Down Expand Up @@ -481,6 +484,7 @@ static StyleInfo infoFrom(StyleMirror input) {
input.addAllBuilder(),
input.getBuilders(),
input.nullableAnnotation(),
ImmutableSet.copyOf(input.allowedClasspathAnnotationsName()));
ImmutableSet.copyOf(input.allowedClasspathAnnotationsName()),
input.limitStringLengthInToString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ public boolean isStringType() {
return String.class.getName().equals(rawTypeName);
}

public boolean isOptionalStringType() {
return isOptionalType() && getGenericArgs().equals("<" + String.class.getName() + ">");
}

public boolean charType() {
return returnType.getKind() == TypeKind.CHAR;
}
Expand Down Expand Up @@ -1857,6 +1861,14 @@ public String atNullableInSupertypeLocal() {
return nullabilityInSupertype != null ? nullabilityInSupertype.asLocalPrefix() : "";
}

public int getLimitStringLengthInToString() {
return protoclass().styles().style().limitStringLengthInToString();
}

public boolean isLimitStringLengthInToString() {
return protoclass().styles().style().limitStringLengthInToString() > 0;
}

enum ToName implements Function<ValueAttribute, String> {
FUNCTION;
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ private ValueMirrors() {}

String nullableAnnotation() default "Nullable";

int limitStringLengthInToString() default 1000;

Class<? extends Annotation>[] allowedClasspathAnnotations() default {};

public enum ImplementationVisibility {
Expand Down