or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

builder.mdconstructors.mddata-classes.mdexperimental.mdindex.mdlogging.mdproperty-access.mdutilities.md

data-classes.mddocs/

0

# Data Class Annotations

1

2

Comprehensive annotations for creating data classes with minimal boilerplate. These annotations generate common methods like getters, setters, constructors, toString, equals, and hashCode, making data classes concise and maintainable.

3

4

## Capabilities

5

6

### @Data Annotation

7

8

Generates a complete data class implementation including getters, setters, constructors, toString, equals, and hashCode methods.

9

10

```java { .api }

11

/**

12

* Generates getters for all fields, setters for non-final fields, a constructor for required fields,

13

* toString, equals, and hashCode methods.

14

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

15

*/

16

@Target(ElementType.TYPE)

17

@interface Data {

18

/**

19

* If specified, generates a private constructor and a static factory method with this name

20

* @return Name of static constructor method (blank = normal public constructor)

21

*/

22

String staticConstructor() default "";

23

}

24

```

25

26

**Usage Examples:**

27

28

```java

29

import lombok.Data;

30

import lombok.NonNull;

31

32

@Data

33

public class Person {

34

@NonNull

35

private String name;

36

private int age;

37

private String email;

38

}

39

40

// Generated methods:

41

// - String getName()

42

// - void setName(String name)

43

// - int getAge()

44

// - void setAge(int age)

45

// - String getEmail()

46

// - void setEmail(String email)

47

// - Person(String name) // constructor for @NonNull fields

48

// - String toString()

49

// - boolean equals(Object other)

50

// - int hashCode()

51

52

// Usage

53

Person person = new Person("John");

54

person.setAge(30);

55

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

56

```

57

58

Static constructor example:

59

```java

60

@Data(staticConstructor = "of")

61

public class Point {

62

private final int x;

63

private final int y;

64

}

65

66

// Usage

67

Point point = Point.of(10, 20); // static factory method

68

```

69

70

### @Value Annotation

71

72

Creates immutable data classes where all fields are final and private by default.

73

74

```java { .api }

75

/**

76

* Generates an immutable data class with all fields final and private, getters, constructor,

77

* toString, equals, and hashCode methods.

78

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

79

*/

80

@Target(ElementType.TYPE)

81

@interface Value {

82

/**

83

* If specified, generates a private constructor and a static factory method with this name

84

* @return Name of static constructor method (blank = normal public constructor)

85

*/

86

String staticConstructor() default "";

87

}

88

```

89

90

**Usage Examples:**

91

92

```java

93

import lombok.Value;

94

95

@Value

96

public class ImmutablePoint {

97

int x;

98

int y;

99

String label;

100

}

101

102

// Generated methods:

103

// - int getX()

104

// - int getY()

105

// - String getLabel()

106

// - ImmutablePoint(int x, int y, String label) // all-args constructor

107

// - String toString()

108

// - boolean equals(Object other)

109

// - int hashCode()

110

111

// Usage

112

ImmutablePoint point = new ImmutablePoint(10, 20, "origin");

113

int x = point.getX(); // getter access only, no setters

114

```

115

116

### @ToString Annotation

117

118

Generates toString() method with customizable field inclusion and formatting.

119

120

```java { .api }

121

/**

122

* Generates toString() method that prints field values

123

*/

124

@Target(ElementType.TYPE)

125

@interface ToString {

126

/**

127

* Include field names in the output

128

* @return Whether to include field names (default: true)

129

*/

130

boolean includeFieldNames() default true;

131

132

/**

133

* Fields to exclude from toString output

134

* @return Array of field names to exclude

135

*/

136

String[] exclude() default {};

137

138

/**

139

* Fields to explicitly include (mutually exclusive with exclude)

140

* @return Array of field names to include

141

*/

142

String[] of() default {};

143

144

/**

145

* Call superclass toString() method

146

* @return Whether to call super.toString() (default: false)

147

*/

148

boolean callSuper() default false;

149

150

/**

151

* Use getter methods instead of field access

152

* @return Whether to use getters (default: false)

153

*/

154

boolean doNotUseGetters() default false;

155

156

/**

157

* Only include fields/methods explicitly marked with @ToString.Include

158

* @return Whether to only use explicitly included items (default: false)

159

*/

160

boolean onlyExplicitlyIncluded() default false;

161

}

162

163

/**

164

* Excludes field from toString output

165

*/

166

@Target(ElementType.FIELD)

167

@interface ToString.Exclude {}

168

169

/**

170

* Explicitly includes field or method in toString output

171

*/

172

@Target({ElementType.FIELD, ElementType.METHOD})

173

@interface ToString.Include {

174

/**

175

* Rank for ordering in toString output

176

* @return Ordering rank (default: 0)

177

*/

178

int rank() default 0;

179

180

/**

181

* Name to use in toString output instead of field/method name

182

* @return Custom name for output

183

*/

184

String name() default "";

185

}

186

```

187

188

**Usage Examples:**

189

190

```java

191

import lombok.ToString;

192

193

@ToString

194

public class User {

195

private String username;

196

private String password;

197

private String email;

198

}

199

// Output: User(username=john, password=secret123, email=john@example.com)

200

201

@ToString(includeFieldNames = false)

202

public class Coordinates {

203

private double latitude;

204

private double longitude;

205

}

206

// Output: Coordinates(40.7128, -74.0060)

207

208

@ToString(exclude = "password")

209

public class SecureUser {

210

private String username;

211

private String password;

212

private String email;

213

}

214

// Output: SecureUser(username=john, email=john@example.com)

215

216

@ToString(onlyExplicitlyIncluded = true)

217

public class Product {

218

@ToString.Include

219

private String name;

220

@ToString.Include(rank = 1)

221

private double price;

222

private String internalCode; // excluded

223

}

224

// Output: Product(name=Laptop, price=999.99)

225

```

226

227

### @EqualsAndHashCode Annotation

228

229

Generates equals() and hashCode() methods with field-based comparison and customizable inclusion rules.

230

231

```java { .api }

232

/**

233

* Generates equals() and hashCode() methods based on field values

234

*/

235

@Target(ElementType.TYPE)

236

@interface EqualsAndHashCode {

237

/**

238

* Fields to exclude from equals/hashCode calculation

239

* @return Array of field names to exclude

240

*/

241

String[] exclude() default {};

242

243

/**

244

* Fields to explicitly include (mutually exclusive with exclude)

245

* @return Array of field names to include

246

*/

247

String[] of() default {};

248

249

/**

250

* Call superclass equals/hashCode methods

251

* @return Whether to call super methods (default: false)

252

*/

253

boolean callSuper() default false;

254

255

/**

256

* Use getter methods instead of field access

257

* @return Whether to use getters (default: false)

258

*/

259

boolean doNotUseGetters() default false;

260

261

/**

262

* Caching strategy for hashCode

263

* @return Caching strategy (default: NEVER)

264

*/

265

CacheStrategy cacheStrategy() default CacheStrategy.NEVER;

266

267

/**

268

* Only include fields/methods explicitly marked with @EqualsAndHashCode.Include

269

* @return Whether to only use explicitly included items (default: false)

270

*/

271

boolean onlyExplicitlyIncluded() default false;

272

273

/**

274

* Annotations to add to generated equals method parameter

275

* @return Array of annotations

276

*/

277

AnyAnnotation[] onParam() default {};

278

}

279

280

/**

281

* Excludes field from equals/hashCode calculation

282

*/

283

@Target(ElementType.FIELD)

284

@interface EqualsAndHashCode.Exclude {}

285

286

/**

287

* Explicitly includes field or method in equals/hashCode calculation

288

*/

289

@Target({ElementType.FIELD, ElementType.METHOD})

290

@interface EqualsAndHashCode.Include {

291

/**

292

* Rank for prioritizing fields in hashCode calculation

293

* @return Priority rank (default: 0)

294

*/

295

int rank() default 0;

296

297

/**

298

* Custom name for the field (used in debugging)

299

* @return Custom field name

300

*/

301

String replaces() default "";

302

}

303

304

/**

305

* Caching strategy for hashCode method

306

*/

307

enum CacheStrategy {

308

/** Never cache hashCode result */

309

NEVER,

310

/** Cache hashCode result lazily after first calculation */

311

LAZY

312

}

313

```

314

315

**Usage Examples:**

316

317

```java

318

import lombok.EqualsAndHashCode;

319

320

@EqualsAndHashCode

321

public class Product {

322

private String name;

323

private double price;

324

private String category;

325

}

326

// equals() compares all fields, hashCode() includes all fields

327

328

@EqualsAndHashCode(exclude = "lastModified")

329

public class Document {

330

private String title;

331

private String content;

332

private Date lastModified; // excluded from equality

333

}

334

335

@EqualsAndHashCode(of = "id")

336

public class Entity {

337

private Long id; // only field used for equality

338

private String name; // ignored

339

private Date created; // ignored

340

}

341

342

@EqualsAndHashCode(callSuper = true)

343

public class Employee extends Person {

344

private String employeeId;

345

private String department;

346

}

347

// Calls Person.equals() and Person.hashCode() as part of comparison

348

349

@EqualsAndHashCode(cacheStrategy = EqualsAndHashCode.CacheStrategy.LAZY)

350

public class ExpensiveHashCode {

351

private String[] largeArray;

352

private Map<String, Object> complexMap;

353

}

354

// hashCode() result is cached after first calculation

355

```

356

357

## Type Definitions

358

359

```java { .api }

360

/**

361

* Placeholder for annotation arrays in onMethod/onParam/onConstructor parameters

362

*/

363

@interface AnyAnnotation {}

364

```