or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

builder-pattern.mdconstructors.mddata-classes.mdexperimental.mdimmutable-patterns.mdindex.mdlogging.mdobject-methods.mdproperty-access.mdtype-inference.mdutilities.md

data-classes.mddocs/

0

# Data Classes

1

2

Comprehensive data class support with automatic generation of getters, setters, constructors, and object methods. The `@Data` and `@Value` annotations provide complete data class functionality with minimal boilerplate.

3

4

## Capabilities

5

6

### @Data Annotation

7

8

Generates a complete mutable data class with getters, setters, constructors, toString, equals, and hashCode methods.

9

10

```java { .api }

11

/**

12

* Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check

13

* all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor

14

* (except that no constructor will be generated if any explicitly written constructors already exist).

15

*

16

* Equivalent to @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode.

17

*/

18

@Target(ElementType.TYPE)

19

@Retention(RetentionPolicy.SOURCE)

20

public @interface Data {

21

/**

22

* If you specify a static constructor name, then the generated constructor will be private, and

23

* instead a static factory method is created that other classes can use to create instances.

24

* We suggest the name: "of", like so:

25

*

26

* public @Data(staticConstructor = "of") class Point { final int x, y; }

27

*

28

* Default: No static constructor, instead the normal constructor is public.

29

*

30

* @return Name of static 'constructor' method to generate (blank = generate a normal constructor).

31

*/

32

String staticConstructor() default "";

33

}

34

```

35

36

**Usage Examples:**

37

38

```java

39

import lombok.Data;

40

41

@Data

42

public class Person {

43

private final String name;

44

private int age;

45

private String email;

46

}

47

48

// Generated methods available:

49

// - String getName()

50

// - int getAge()

51

// - void setAge(int age)

52

// - String getEmail()

53

// - void setEmail(String email)

54

// - Person(String name) // Required args constructor

55

// - String toString()

56

// - boolean equals(Object o)

57

// - int hashCode()

58

59

// Usage

60

Person person = new Person("John Doe");

61

person.setAge(30);

62

person.setEmail("john@example.com");

63

System.out.println(person.toString()); // Person(name=John Doe, age=30, email=john@example.com)

64

```

65

66

**With Static Constructor:**

67

68

```java

69

@Data(staticConstructor = "of")

70

public class Point {

71

private final int x;

72

private final int y;

73

}

74

75

// Usage

76

Point point = Point.of(10, 20);

77

```

78

79

### @Value Annotation

80

81

Generates an immutable value class with all fields final, getters, all-args constructor, toString, equals, and hashCode methods.

82

83

```java { .api }

84

/**

85

* Generates immutable value objects. Makes all fields private and final, generates getters,

86

* all-args constructor, toString, equals, and hashCode methods.

87

*

88

* Equivalent to @Getter @FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE)

89

* @AllArgsConstructor @ToString @EqualsAndHashCode.

90

*/

91

@Target(ElementType.TYPE)

92

@Retention(RetentionPolicy.SOURCE)

93

public @interface Value {

94

/**

95

* If you specify a static constructor name, then the generated constructor will be private, and

96

* instead a static factory method is created that other classes can use to create instances.

97

*

98

* @return Name of static 'constructor' method to generate (blank = generate a normal constructor).

99

*/

100

String staticConstructor() default "";

101

}

102

```

103

104

**Usage Examples:**

105

106

```java

107

import lombok.Value;

108

109

@Value

110

public class Address {

111

String street;

112

String city;

113

String zipCode;

114

}

115

116

// Generated methods available:

117

// - String getStreet()

118

// - String getCity()

119

// - String getZipCode()

120

// - Address(String street, String city, String zipCode) // All args constructor

121

// - String toString()

122

// - boolean equals(Object o)

123

// - int hashCode()

124

125

// Usage

126

Address address = new Address("123 Main St", "Anytown", "12345");

127

System.out.println(address.getStreet()); // 123 Main St

128

// address.street = "new street"; // Compilation error - field is final

129

```

130

131

**With Static Constructor:**

132

133

```java

134

@Value(staticConstructor = "of")

135

public class Coordinates {

136

double latitude;

137

double longitude;

138

}

139

140

// Usage

141

Coordinates coords = Coordinates.of(40.7128, -74.0060);

142

```

143

144

## Advanced Usage

145

146

### Combining with Other Annotations

147

148

```java

149

import lombok.Value;

150

import lombok.Builder;

151

import lombok.extern.slf4j.Slf4j;

152

153

@Value

154

@Builder

155

@Slf4j

156

public class ImmutableUser {

157

String username;

158

String email;

159

int age;

160

161

public void logInfo() {

162

log.info("User: {} ({})", username, email);

163

}

164

}

165

166

// Usage with builder

167

ImmutableUser user = ImmutableUser.builder()

168

.username("johndoe")

169

.email("john@example.com")

170

.age(30)

171

.build();

172

```

173

174

### Inheritance Considerations

175

176

```java

177

@Data

178

public class Animal {

179

private final String species;

180

private String name;

181

}

182

183

@Data(callSuper = true) // Note: This parameter doesn't exist on @Data

184

// Use @ToString(callSuper = true) @EqualsAndHashCode(callSuper = true) instead

185

public class Dog extends Animal {

186

private String breed;

187

188

public Dog(String species, String breed) {

189

super(species);

190

this.breed = breed;

191

}

192

}

193

```

194

195

## Generated Code Behavior

196

197

### @Data Generated Code Pattern

198

199

For a `@Data` class with fields:

200

- **Getters**: Generated for all fields

201

- **Setters**: Generated for all non-final fields

202

- **Constructor**: Required arguments constructor (final fields and @NonNull fields)

203

- **toString()**: Includes all fields

204

- **equals()/hashCode()**: Based on all non-transient fields

205

206

### @Value Generated Code Pattern

207

208

For a `@Value` class with fields:

209

- **Getters**: Generated for all fields

210

- **No Setters**: All fields are final

211

- **Constructor**: All arguments constructor

212

- **toString()**: Includes all fields

213

- **equals()/hashCode()**: Based on all fields

214

215

## Configuration

216

217

Both annotations respect lombok configuration settings:

218

219

- `lombok.data.flagUsage`: Control usage warnings

220

- `lombok.value.flagUsage`: Control usage warnings

221

- `lombok.toString.includeFieldNames`: Default field name inclusion

222

- `lombok.equalsAndHashCode.doNotUseGetters`: Use fields directly