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

property-access.mddocs/

0

# Property Access Generation

1

2

Automatic getter and setter generation with access level control, lazy loading, and method annotation support. These annotations eliminate the boilerplate code for field accessors while providing fine-grained control over visibility and behavior.

3

4

## Capabilities

5

6

### @Getter Annotation

7

8

Generates getter methods for fields with customizable access levels, lazy loading support, and annotation forwarding.

9

10

```java { .api }

11

/**

12

* Generates getter methods for fields. Can be applied to fields or classes.

13

* When applied to a class, generates getters for all non-static fields without existing getters.

14

*/

15

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

16

@interface Getter {

17

/**

18

* Access level for the generated getter method

19

* @return The getter method will be generated with this access modifier (default: PUBLIC)

20

*/

21

AccessLevel value() default AccessLevel.PUBLIC;

22

23

/**

24

* Annotations to apply to the generated getter method

25

* Syntax depends on JDK version:

26

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

27

* - JDK8+: @Getter(onMethod_={@AnnotationsGoHere})

28

* @return Array of annotations for the generated method

29

*/

30

AnyAnnotation[] onMethod() default {};

31

32

/**

33

* Enables lazy initialization for expensive field computations

34

* Field must be final and have an initializer expression

35

* @return Whether to use lazy evaluation (default: false)

36

*/

37

boolean lazy() default false;

38

}

39

```

40

41

**Usage Examples:**

42

43

```java

44

import lombok.Getter;

45

import lombok.AccessLevel;

46

47

public class BasicExample {

48

@Getter

49

private String name;

50

51

@Getter

52

private int age;

53

54

@Getter(AccessLevel.PROTECTED)

55

private String email;

56

}

57

58

// Generated methods:

59

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

60

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

61

// protected String getEmail() { return this.email; }

62

```

63

64

Class-level getter:

65

```java

66

@Getter

67

public class User {

68

private String username;

69

private String email;

70

private Date lastLogin;

71

}

72

73

// Generates getters for all fields:

74

// public String getUsername() { return this.username; }

75

// public String getEmail() { return this.email; }

76

// public Date getLastLogin() { return this.lastLogin; }

77

```

78

79

Lazy getter example:

80

```java

81

public class ExpensiveComputation {

82

@Getter(lazy = true)

83

private final String expensiveValue = calculateExpensiveValue();

84

85

private String calculateExpensiveValue() {

86

// Expensive computation here

87

return "computed value";

88

}

89

}

90

91

// Generated:

92

// public String getExpensiveValue() {

93

// if (this.expensiveValue == null) {

94

// synchronized(this.$expensiveValue$lock) {

95

// if (this.expensiveValue == null) {

96

// this.expensiveValue = calculateExpensiveValue();

97

// }

98

// }

99

// }

100

// return this.expensiveValue;

101

// }

102

```

103

104

Method annotation example:

105

```java

106

public class AnnotatedGetter {

107

@Getter(onMethod_ = {@Deprecated, @JsonProperty("user_name")})

108

private String userName;

109

}

110

111

// Generated:

112

// @Deprecated

113

// @JsonProperty("user_name")

114

// public String getUserName() { return this.userName; }

115

```

116

117

### @Setter Annotation

118

119

Generates setter methods for fields with access control, parameter annotation support, and method annotation support.

120

121

```java { .api }

122

/**

123

* Generates setter methods for fields. Can be applied to fields or classes.

124

* When applied to a class, generates setters for all non-final, non-static fields without existing setters.

125

*/

126

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

127

@interface Setter {

128

/**

129

* Access level for the generated setter method

130

* @return The setter method will be generated with this access modifier (default: PUBLIC)

131

*/

132

AccessLevel value() default AccessLevel.PUBLIC;

133

134

/**

135

* Annotations to apply to the generated setter method

136

* Syntax depends on JDK version:

137

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

138

* - JDK8+: @Setter(onMethod_={@AnnotationsGoHere})

139

* @return Array of annotations for the generated method

140

*/

141

AnyAnnotation[] onMethod() default {};

142

143

/**

144

* Annotations to apply to the generated setter method parameter

145

* Syntax depends on JDK version:

146

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

147

* - JDK8+: @Setter(onParam_={@AnnotationsGoHere})

148

* @return Array of annotations for the generated parameter

149

*/

150

AnyAnnotation[] onParam() default {};

151

}

152

```

153

154

**Usage Examples:**

155

156

```java

157

import lombok.Setter;

158

import lombok.AccessLevel;

159

import lombok.NonNull;

160

161

public class BasicSetterExample {

162

@Setter

163

private String name;

164

165

@Setter(AccessLevel.PROTECTED)

166

private int age;

167

168

@Setter(AccessLevel.PACKAGE)

169

private String email;

170

}

171

172

// Generated methods:

173

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

174

// protected void setAge(int age) { this.age = age; }

175

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

176

```

177

178

Class-level setter:

179

```java

180

@Setter

181

public class Product {

182

private String name;

183

private double price;

184

private final String id; // No setter generated for final fields

185

}

186

187

// Generates setters for non-final fields:

188

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

189

// public void setPrice(double price) { this.price = price; }

190

```

191

192

Parameter and method annotations:

193

```java

194

public class AnnotatedSetter {

195

@Setter(onParam_ = @NonNull, onMethod_ = @Deprecated)

196

private String description;

197

198

@Setter(onParam_ = {@NotNull, @Valid})

199

private User user;

200

}

201

202

// Generated:

203

// @Deprecated

204

// public void setDescription(@NonNull String description) {

205

// this.description = description;

206

// }

207

//

208

// public void setUser(@NotNull @Valid User user) {

209

// this.user = user;

210

// }

211

```

212

213

### Combined Usage

214

215

Using both @Getter and @Setter together:

216

217

```java

218

import lombok.Getter;

219

import lombok.Setter;

220

import lombok.AccessLevel;

221

222

public class Person {

223

@Getter @Setter

224

private String name;

225

226

@Getter @Setter(AccessLevel.PROTECTED)

227

private int age;

228

229

@Getter(AccessLevel.PACKAGE)

230

private final String id;

231

232

@Setter(onParam_ = @NonNull)

233

private String email;

234

235

public Person(String id) {

236

this.id = id;

237

}

238

}

239

240

// Usage:

241

Person person = new Person("P001");

242

person.setName("John Doe"); // public setter

243

person.setAge(30); // protected setter (compile error outside package)

244

String name = person.getName(); // public getter

245

int age = person.getAge(); // public getter

246

String id = person.getId(); // package-private getter

247

person.setEmail("john@example.com"); // setter with @NonNull parameter

248

```

249

250

### Advanced Access Control

251

252

```java

253

import lombok.Getter;

254

import lombok.Setter;

255

import lombok.AccessLevel;

256

257

public class AccessControlExample {

258

@Getter

259

@Setter(AccessLevel.NONE) // No setter generated

260

private String readOnlyField;

261

262

@Getter(AccessLevel.PROTECTED)

263

@Setter(AccessLevel.PRIVATE)

264

private String restrictedField;

265

266

@Getter(AccessLevel.PUBLIC)

267

@Setter(AccessLevel.MODULE) // Java 9+ module access

268

private String moduleField;

269

270

// Manual setter with validation

271

public void setRestrictedField(String value) {

272

if (value != null && value.length() > 0) {

273

this.restrictedField = value.trim();

274

}

275

}

276

}

277

```

278

279

### Integration with Null Safety

280

281

```java

282

import lombok.Getter;

283

import lombok.Setter;

284

import lombok.NonNull;

285

286

public class NullSafetyExample {

287

@Getter

288

@Setter

289

@NonNull

290

private String requiredField;

291

292

@Getter

293

@Setter(onParam_ = @Nullable)

294

private String optionalField;

295

296

@Getter(lazy = true)

297

@NonNull

298

private final String computedField = computeValue();

299

300

private String computeValue() {

301

return "computed";

302

}

303

}

304

305

// Generated setter includes null check:

306

// public void setRequiredField(@NonNull String requiredField) {

307

// if (requiredField == null) {

308

// throw new NullPointerException("requiredField is marked non-null but is null");

309

// }

310

// this.requiredField = requiredField;

311

// }

312

```

313

314

## Type Definitions

315

316

```java { .api }

317

/**

318

* Access levels for generated methods

319

*/

320

public enum AccessLevel {

321

/** Generate public method */

322

PUBLIC,

323

/** Generate module-private method (Java 9+) */

324

MODULE,

325

/** Generate protected method */

326

PROTECTED,

327

/** Generate package-private method */

328

PACKAGE,

329

/** Generate private method */

330

PRIVATE,

331

/** Do not generate method */

332

NONE

333

}

334

335

/**

336

* Placeholder for annotation arrays in onMethod/onParam parameters

337

*/

338

@interface AnyAnnotation {}

339

```