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

constructors.mddocs/

0

# Constructor Generation

1

2

Automatic constructor generation with various configurations for different use cases including dependency injection, immutable objects, and flexible initialization patterns.

3

4

## Capabilities

5

6

### @NoArgsConstructor Annotation

7

8

Generates a no-argument constructor, useful for frameworks that require default constructors like JPA, Jackson, or Spring.

9

10

```java { .api }

11

/**

12

* Generates a no-args constructor.

13

* Will generate an error if such a constructor cannot be written due to final fields.

14

*/

15

@Target(ElementType.TYPE)

16

@interface NoArgsConstructor {

17

/**

18

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

19

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

20

*/

21

String staticName() default "";

22

23

/**

24

* Access level for the generated constructor

25

* @return Access level (default: PUBLIC)

26

*/

27

AccessLevel access() default AccessLevel.PUBLIC;

28

29

/**

30

* Force generation even if final fields exist (initializes with default values)

31

* @return Whether to force generation (default: false)

32

*/

33

boolean force() default false;

34

35

/**

36

* Annotations to add to the generated constructor

37

* @return Array of annotations to apply

38

*/

39

AnyAnnotation[] onConstructor() default {};

40

}

41

```

42

43

**Usage Examples:**

44

45

```java

46

import lombok.NoArgsConstructor;

47

import lombok.AccessLevel;

48

49

@NoArgsConstructor

50

public class User {

51

private String name;

52

private int age;

53

}

54

// Generates: public User() {}

55

56

@NoArgsConstructor(access = AccessLevel.PROTECTED)

57

public class BaseEntity {

58

private Long id;

59

}

60

// Generates: protected BaseEntity() {}

61

62

@NoArgsConstructor(staticName = "create")

63

public class Product {

64

private String name;

65

private double price;

66

}

67

// Generates: private Product() {}

68

// public static Product create() { return new Product(); }

69

70

@NoArgsConstructor(force = true)

71

public class ImmutableUser {

72

private final String name = "default";

73

private final int age = 0;

74

}

75

// Force generation despite final fields

76

```

77

78

### @AllArgsConstructor Annotation

79

80

Generates a constructor with parameters for all fields in the class, useful for creating fully initialized objects.

81

82

```java { .api }

83

/**

84

* Generates an all-args constructor requiring one argument for every field

85

*/

86

@Target(ElementType.TYPE)

87

@interface AllArgsConstructor {

88

/**

89

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

90

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

91

*/

92

String staticName() default "";

93

94

/**

95

* Access level for the generated constructor

96

* @return Access level (default: PUBLIC)

97

*/

98

AccessLevel access() default AccessLevel.PUBLIC;

99

100

/**

101

* Annotations to add to the generated constructor

102

* @return Array of annotations to apply

103

*/

104

AnyAnnotation[] onConstructor() default {};

105

}

106

```

107

108

**Usage Examples:**

109

110

```java

111

import lombok.AllArgsConstructor;

112

import lombok.AccessLevel;

113

114

@AllArgsConstructor

115

public class Point {

116

private final int x;

117

private final int y;

118

private String label;

119

}

120

// Generates: public Point(int x, int y, String label) { ... }

121

122

@AllArgsConstructor(access = AccessLevel.PRIVATE)

123

public class Singleton {

124

private static final Singleton INSTANCE = new Singleton("default");

125

private String value;

126

}

127

128

@AllArgsConstructor(staticName = "of")

129

public class Coordinate {

130

private double latitude;

131

private double longitude;

132

}

133

// Generates: private Coordinate(double latitude, double longitude) { ... }

134

// public static Coordinate of(double latitude, double longitude) { ... }

135

136

// Usage

137

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

138

Coordinate coord = Coordinate.of(40.7128, -74.0060);

139

```

140

141

### @RequiredArgsConstructor Annotation

142

143

Generates a constructor with parameters for "required" fields - final fields and fields marked with @NonNull.

144

145

```java { .api }

146

/**

147

* Generates a constructor with required arguments.

148

* Required arguments are final fields and fields with constraints such as @NonNull

149

*/

150

@Target(ElementType.TYPE)

151

@interface RequiredArgsConstructor {

152

/**

153

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

154

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

155

*/

156

String staticName() default "";

157

158

/**

159

* Access level for the generated constructor

160

* @return Access level (default: PUBLIC)

161

*/

162

AccessLevel access() default AccessLevel.PUBLIC;

163

164

/**

165

* Annotations to add to the generated constructor

166

* @return Array of annotations to apply

167

*/

168

AnyAnnotation[] onConstructor() default {};

169

}

170

```

171

172

**Usage Examples:**

173

174

```java

175

import lombok.RequiredArgsConstructor;

176

import lombok.NonNull;

177

178

@RequiredArgsConstructor

179

public class Service {

180

@NonNull

181

private final UserRepository userRepository;

182

@NonNull

183

private final EmailService emailService;

184

private String configValue; // optional, not included in constructor

185

}

186

// Generates: public Service(UserRepository userRepository, EmailService emailService) { ... }

187

188

@RequiredArgsConstructor

189

public class Person {

190

@NonNull

191

private String name;

192

private final String id;

193

private int age; // not required - not final, not @NonNull

194

private String email; // not required - not final, not @NonNull

195

}

196

// Generates: public Person(String name, String id) { ... }

197

198

@RequiredArgsConstructor(staticName = "create")

199

public class DatabaseConnection {

200

@NonNull

201

private final String url;

202

@NonNull

203

private final String username;

204

private String password; // optional

205

}

206

// Generates: private DatabaseConnection(String url, String username) { ... }

207

// public static DatabaseConnection create(String url, String username) { ... }

208

209

// Usage

210

Service service = new Service(userRepo, emailService);

211

Person person = new Person("John", "12345");

212

DatabaseConnection db = DatabaseConnection.create("jdbc:...", "admin");

213

```

214

215

### Multiple Constructor Annotations

216

217

You can combine constructor annotations to generate multiple constructors for different use cases.

218

219

**Usage Examples:**

220

221

```java

222

import lombok.*;

223

224

@NoArgsConstructor

225

@AllArgsConstructor

226

@RequiredArgsConstructor

227

public class FlexibleUser {

228

@NonNull

229

private String username;

230

private String email;

231

private int age;

232

private boolean active = true;

233

}

234

235

// Generates three constructors:

236

// 1. public FlexibleUser() {} // @NoArgsConstructor

237

// 2. public FlexibleUser(String username) { ... } // @RequiredArgsConstructor

238

// 3. public FlexibleUser(String username, String email, int age, boolean active) { ... } // @AllArgsConstructor

239

240

// Usage flexibility

241

FlexibleUser user1 = new FlexibleUser(); // default constructor

242

FlexibleUser user2 = new FlexibleUser("john"); // required args only

243

FlexibleUser user3 = new FlexibleUser("jane", "jane@...", 25, true); // all args

244

```

245

246

### Constructor with Dependency Injection

247

248

Lombok constructors work seamlessly with dependency injection frameworks.

249

250

**Usage Examples:**

251

252

```java

253

import lombok.RequiredArgsConstructor;

254

import org.springframework.stereotype.Service;

255

import org.springframework.beans.factory.annotation.Autowired;

256

257

@Service

258

@RequiredArgsConstructor(onConstructor_ = @Autowired) // Spring annotation on constructor

259

public class UserService {

260

private final UserRepository userRepository;

261

private final EmailService emailService;

262

private final AuditService auditService;

263

}

264

265

// Generates:

266

// @Autowired

267

// public UserService(UserRepository userRepository,

268

// EmailService emailService,

269

// AuditService auditService) { ... }

270

```

271

272

### Constructor with Validation

273

274

Combine with validation annotations for automatic parameter validation.

275

276

**Usage Examples:**

277

278

```java

279

import lombok.AllArgsConstructor;

280

import javax.validation.constraints.NotNull;

281

import javax.validation.constraints.Min;

282

283

@AllArgsConstructor(onConstructor_ = {@javax.validation.Valid})

284

public class Product {

285

@NotNull

286

private String name;

287

288

@Min(0)

289

private double price;

290

291

private String description;

292

}

293

294

// Generated constructor includes @Valid annotation and parameter validation

295

```

296

297

## Type Definitions

298

299

```java { .api }

300

/**

301

* Access levels for generated constructors

302

*/

303

public enum AccessLevel {

304

PUBLIC, MODULE, PROTECTED, PACKAGE, PRIVATE, NONE

305

}

306

307

/**

308

* Placeholder for annotation arrays in onConstructor parameters

309

*/

310

@interface AnyAnnotation {}

311

```