Skip to content

Commit

Permalink
Truncate strings or values of type variables in toString method tha…
Browse files Browse the repository at this point in the history
…t are longer than a defined length
  • Loading branch information
arouel committed Jun 4, 2020
1 parent 7538110 commit 6cb6e80
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
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

0 comments on commit 6cb6e80

Please sign in to comment.