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

property-access.mddocs/

0

# Property Access

1

2

Automatic generation of getter and setter methods with configurable access levels, lazy evaluation support, and annotation forwarding capabilities.

3

4

## Capabilities

5

6

### @Getter Annotation

7

8

Generates getter methods for fields with configurable access levels and lazy evaluation support.

9

10

```java { .api }

11

/**

12

* Put on any field to make lombok build a standard getter.

13

* This annotation can also be applied to a class, in which case it'll be as if all non-static fields

14

* that don't already have a @Getter annotation have the annotation.

15

*/

16

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

17

@Retention(RetentionPolicy.SOURCE)

18

public @interface Getter {

19

/**

20

* If you want your getter to be non-public, you can specify an alternate access level here.

21

*

22

* @return The getter method will be generated with this access modifier.

23

*/

24

AccessLevel value() default AccessLevel.PUBLIC;

25

26

/**

27

* Any annotations listed here are put on the generated method.

28

* The syntax for this feature depends on JDK version.

29

* up to JDK7: @Getter(onMethod=@__({@AnnotationsGoHere}))

30

* from JDK8: @Getter(onMethod_={@AnnotationsGohere})

31

*

32

* @return List of annotations to apply to the generated getter method.

33

*/

34

AnyAnnotation[] onMethod() default {};

35

36

/**

37

* If true, the generated getter will calculate the value once, the first time the getter is called,

38

* and cache the result for subsequent calls.

39

*

40

* @return Whether to generate a lazy getter.

41

*/

42

boolean lazy() default false;

43

}

44

```

45

46

**Usage Examples:**

47

48

```java

49

import lombok.Getter;

50

import lombok.AccessLevel;

51

52

public class Example {

53

@Getter

54

private String name;

55

56

@Getter(AccessLevel.PROTECTED)

57

private int age;

58

59

@Getter(lazy = true)

60

private final String expensiveValue = computeExpensiveValue();

61

62

private String computeExpensiveValue() {

63

// Expensive computation

64

return "expensive result";

65

}

66

}

67

68

// Generated methods:

69

// public String getName() { return this.name; }

70

// protected int getAge() { return this.age; }

71

// public String getExpensiveValue() { /* lazy computation */ }

72

```

73

74

**Class-Level Usage:**

75

76

```java

77

@Getter

78

public class Person {

79

private String firstName;

80

private String lastName;

81

82

@Getter(AccessLevel.PRIVATE)

83

private String ssn; // Override class-level setting

84

}

85

86

// Generated:

87

// public String getFirstName() { return this.firstName; }

88

// public String getLastName() { return this.lastName; }

89

// private String getSsn() { return this.ssn; }

90

```

91

92

### @Setter Annotation

93

94

Generates setter methods for fields with configurable access levels and annotation forwarding.

95

96

```java { .api }

97

/**

98

* Put on any field to make lombok build a standard setter.

99

* This annotation can also be applied to a class, in which case it'll be as if all non-static,

100

* non-final fields that don't already have a @Setter annotation have the annotation.

101

*/

102

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

103

@Retention(RetentionPolicy.SOURCE)

104

public @interface Setter {

105

/**

106

* If you want your setter to be non-public, you can specify an alternate access level here.

107

*

108

* @return The setter method will be generated with this access modifier.

109

*/

110

AccessLevel value() default AccessLevel.PUBLIC;

111

112

/**

113

* Any annotations listed here are put on the generated method.

114

* The syntax for this feature depends on JDK version.

115

* up to JDK7: @Setter(onMethod=@__({@AnnotationsGoHere}))

116

* from JDK8: @Setter(onMethod_={@AnnotationsGohere})

117

*

118

* @return List of annotations to apply to the generated setter method.

119

*/

120

AnyAnnotation[] onMethod() default {};

121

122

/**

123

* Any annotations listed here are put on the generated method's parameter.

124

* The syntax for this feature depends on JDK version.

125

* up to JDK7: @Setter(onParam=@__({@AnnotationsGoHere}))

126

* from JDK8: @Setter(onParam_={@AnnotationsGohere})

127

*

128

* @return List of annotations to apply to the generated setter method's parameter.

129

*/

130

AnyAnnotation[] onParam() default {};

131

}

132

```

133

134

**Usage Examples:**

135

136

```java

137

import lombok.Setter;

138

import lombok.AccessLevel;

139

import lombok.NonNull;

140

141

public class Example {

142

@Setter

143

private String name;

144

145

@Setter(AccessLevel.PACKAGE)

146

private int age;

147

148

@Setter

149

@NonNull

150

private String email; // Will generate null check in setter

151

}

152

153

// Generated methods:

154

// public void setName(String name) { this.name = name; }

155

// void setAge(int age) { this.age = age; } // package-private

156

// public void setEmail(@NonNull String email) {

157

// if (email == null) throw new NullPointerException("email is marked non-null but is null");

158

// this.email = email;

159

// }

160

```

161

162

**Class-Level Usage:**

163

164

```java

165

@Setter

166

public class Configuration {

167

private String host;

168

private int port;

169

private final String version = "1.0"; // No setter generated (final field)

170

171

@Setter(AccessLevel.PRIVATE)

172

private String apiKey; // Override class-level setting

173

}

174

```

175

176

### @With Annotation

177

178

Generates wither methods that return new instances with modified field values for immutable objects.

179

180

```java { .api }

181

/**

182

* Generates wither methods that return a clone of this object except with one field changed.

183

* Particularly useful for immutable data structures.

184

*/

185

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

186

@Retention(RetentionPolicy.SOURCE)

187

public @interface With {

188

/**

189

* If you want your wither to be non-public, you can specify an alternate access level here.

190

*

191

* @return The wither method will be generated with this access modifier.

192

*/

193

AccessLevel value() default AccessLevel.PUBLIC;

194

195

/**

196

* Any annotations listed here are put on the generated method.

197

*

198

* @return List of annotations to apply to the generated wither method.

199

*/

200

AnyAnnotation[] onMethod() default {};

201

202

/**

203

* Any annotations listed here are put on the generated method's parameter.

204

*

205

* @return List of annotations to apply to the generated wither method's parameter.

206

*/

207

AnyAnnotation[] onParam() default {};

208

}

209

```

210

211

**Usage Examples:**

212

213

```java

214

import lombok.Value;

215

import lombok.With;

216

217

@Value

218

public class Person {

219

@With String name;

220

@With int age;

221

String email;

222

}

223

224

// Generated methods:

225

// public Person withName(String name) {

226

// return this.name == name ? this : new Person(name, this.age, this.email);

227

// }

228

// public Person withAge(int age) {

229

// return this.age == age ? this : new Person(this.name, age, this.email);

230

// }

231

232

// Usage

233

Person original = new Person("John", 30, "john@example.com");

234

Person older = original.withAge(31);

235

Person renamed = original.withName("Johnny");

236

```

237

238

**Class-Level Usage:**

239

240

```java

241

@Value

242

@With

243

public class ImmutablePoint {

244

int x;

245

int y;

246

String label;

247

}

248

249

// All fields get wither methods

250

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

251

ImmutablePoint moved = point.withX(15).withY(25);

252

```

253

254

## Advanced Features

255

256

### Lazy Getters

257

258

Lazy getters compute and cache values on first access:

259

260

```java

261

public class DataProcessor {

262

@Getter(lazy = true)

263

private final List<String> processedData = loadAndProcessData();

264

265

private List<String> loadAndProcessData() {

266

// Expensive operation that runs only once

267

return Arrays.asList("processed", "data");

268

}

269

}

270

271

// Generated lazy getter with double-checked locking pattern

272

```

273

274

### Annotation Forwarding

275

276

Forward annotations to generated methods:

277

278

```java

279

import javax.annotation.Nullable;

280

import javax.validation.constraints.NotNull;

281

282

public class User {

283

@Getter(onMethod_ = {@Override, @Nullable})

284

private String nickname;

285

286

@Setter(onParam_ = @NotNull)

287

private String email;

288

}

289

290

// Generated:

291

// @Override @Nullable public String getNickname() { return this.nickname; }

292

// public void setEmail(@NotNull String email) { this.email = email; }

293

```

294

295

### Combining Property Annotations

296

297

```java

298

import lombok.*;

299

300

@Getter

301

@Setter

302

public class Product {

303

private String name;

304

305

@Getter(AccessLevel.NONE) // Override class-level @Getter

306

@Setter(AccessLevel.PROTECTED) // Override class-level @Setter

307

private double price;

308

309

@With // Add wither in addition to getter/setter

310

private String category;

311

}

312

```

313

314

## Configuration Options

315

316

Property access annotations respect these lombok configuration settings:

317

318

- `lombok.getter.flagUsage`: Control usage warnings for @Getter

319

- `lombok.setter.flagUsage`: Control usage warnings for @Setter

320

- `lombok.accessors.fluent`: Generate fluent accessor methods

321

- `lombok.accessors.chain`: Make setters return 'this'

322

- `lombok.accessors.prefix`: Strip prefixes from field names