Skip to content

Commit

Permalink
Truncate strings in toString method that are longer than a defined …
Browse files Browse the repository at this point in the history
…length
  • Loading branch information
arouel committed Jun 3, 2020
1 parent 7538110 commit 275e5f0
Show file tree
Hide file tree
Showing 5 changed files with 26 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 100;

/**
* 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 @@ -4380,6 +4380,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)]([value] != null && [value].length() > [a.limitStringLengthInToString] ? [value].substring(0, [a.limitStringLengthInToString]) + "..." : [value])[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 100;

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

public enum ImplementationVisibility {
Expand Down

0 comments on commit 275e5f0

Please sign in to comment.