aboutsummaryrefslogtreecommitdiff
path: root/value/src/main/java/com/google/auto/value/extension/toprettystring/ToPrettyString.java
blob: cd2762a25bcd48d1e3256420da2bc640aa789232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.auto.value.extension.toprettystring;

import static java.lang.annotation.ElementType.METHOD;

import java.lang.annotation.Documented;
import java.lang.annotation.Target;
import java.util.Collection;

/**
 * Annotates instance methods that return an easy-to-read {@link String} representing the instance.
 * When the method is {@code abstract} and enclosed in an {@link com.google.auto.value.AutoValue}
 * class, an implementation of the method will be automatically generated.
 *
 * <p>When generating an implementation of an {@code @ToPrettyString} method, each property of the
 * {@code @AutoValue} type is individually printed in an easy-to-read format. If the type of the
 * property itself has a {@code @ToPrettyString} method, that method will be called in assistance of
 * computing the pretty string. Non-{@code @AutoValue} classes can contribute a pretty string
 * representation by annotating a method with {@code @ToPrettyString}.
 *
 * <p>{@link Collection} and {@link Collection}-like types have special representations in generated
 * pretty strings.
 *
 * <p>If no {@code @ToPrettyString} method is found on a type and the type is not one with a built
 * in rendering, the {@link Object#toString()} value will be used instead.
 *
 * <p>{@code @ToPrettyString} is valid on overridden {@code toString()} and other methods alike.
 *
 * <h3>Example</h3>
 *
 * <pre>
 *   {@code @AutoValue}
 *   abstract class Pretty {
 *     abstract {@code List<String>} property();
 *
 *     {@code @ToPrettyString}
 *     abstract String toPrettyString();
 *   }
 *
 *   System.out.println(new AutoValue_Pretty(List.of("abc", "def", "has\nnewline)).toPrettyString())
 *   // Pretty{
 *   //   property = [
 *   //     abc,
 *   //     def,
 *   //     has
 *   //     newline,
 *   //   ]
 *   // }
 *   }</pre>
 */
@Documented
@Target(METHOD)
public @interface ToPrettyString {}