or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

builder.mdclassfile.mdcollections.mdfunctional.mdindex.mdsignatures.mdstream.md

classfile.mddocs/

0

# Class File Processing

1

2

Complete Java class file parsing, manipulation, and generation capabilities with full support for all JVM bytecode features including modules, records, sealed classes, and type annotations. This module provides comprehensive access to Java bytecode structure while maintaining type safety and performance.

3

4

## Capabilities

5

6

### ClassFile Operations

7

8

Core class file parsing and writing functionality.

9

10

```java { .api }

11

/**

12

* Represents a complete Java class file with all its components

13

*/

14

public class ClassFile extends ElementInfo {

15

public final int minor_version;

16

public final int major_version;

17

public final ConstantPool constant_pool;

18

public final String this_class;

19

public final String super_class;

20

public final String[] interfaces;

21

public final FieldInfo[] fields;

22

public final MethodInfo[] methods;

23

24

/**

25

* Parse a class file from input stream

26

* @param in DataInput to read from

27

* @return Parsed ClassFile instance

28

* @throws IOException if parsing fails

29

*/

30

public static ClassFile parseClassFile(DataInput in) throws IOException;

31

32

/**

33

* Write class file to output stream

34

* @param out DataOutput to write to

35

* @throws IOException if writing fails

36

*/

37

public void write(DataOutput out) throws IOException;

38

39

/**

40

* Write class file to output stream

41

* @param out OutputStream to write to

42

* @throws IOException if writing fails

43

*/

44

public void write(OutputStream out) throws IOException;

45

46

/**

47

* Write class file to byte array

48

* @return Byte array containing class file data

49

* @throws IOException if writing fails

50

*/

51

public byte[] write() throws IOException;

52

}

53

```

54

55

**Usage Examples:**

56

57

```java

58

import aQute.bnd.classfile.ClassFile;

59

import java.io.*;

60

61

// Parse class file from file

62

try (FileInputStream fis = new FileInputStream("Example.class");

63

DataInputStream dis = new DataInputStream(fis)) {

64

ClassFile classFile = ClassFile.parseClassFile(dis);

65

System.out.println("Class name: " + classFile.this_class);

66

System.out.println("Super class: " + classFile.super_class);

67

System.out.println("Methods: " + classFile.methods.length);

68

}

69

70

// Write modified class file

71

byte[] bytecode = classFile.write();

72

Files.write(Paths.get("Modified.class"), bytecode);

73

```

74

75

### Constant Pool Operations

76

77

Access and manipulation of Java class file constant pools.

78

79

```java { .api }

80

/**

81

* Represents and manipulates Java class file constant pools

82

*/

83

public class ConstantPool {

84

// Constant pool entry type constants

85

public static final int CONSTANT_Utf8 = 1;

86

public static final int CONSTANT_Integer = 3;

87

public static final int CONSTANT_Float = 4;

88

public static final int CONSTANT_Long = 5;

89

public static final int CONSTANT_Double = 6;

90

public static final int CONSTANT_Class = 7;

91

public static final int CONSTANT_String = 8;

92

public static final int CONSTANT_Fieldref = 9;

93

public static final int CONSTANT_Methodref = 10;

94

public static final int CONSTANT_InterfaceMethodref = 11;

95

public static final int CONSTANT_NameAndType = 12;

96

public static final int CONSTANT_MethodHandle = 15;

97

public static final int CONSTANT_MethodType = 16;

98

public static final int CONSTANT_Dynamic = 17;

99

public static final int CONSTANT_InvokeDynamic = 18;

100

public static final int CONSTANT_Module = 19;

101

public static final int CONSTANT_Package = 20;

102

103

/**

104

* Get the size of the constant pool

105

* @return Number of entries in constant pool

106

*/

107

public int size();

108

109

/**

110

* Get constant pool entry at specified index

111

* @param index Index of entry to retrieve

112

* @return Constant pool entry

113

*/

114

public <T> T entry(int index);

115

116

/**

117

* Get entry type tag at specified index

118

* @param index Index of entry

119

* @return Type tag constant

120

*/

121

public int tag(int index);

122

123

/**

124

* Get UTF-8 string from constant pool

125

* @param utf8_index Index of UTF-8 entry

126

* @return String value

127

*/

128

public String utf8(int utf8_index);

129

130

/**

131

* Get class name from constant pool

132

* @param class_info_index Index of class info entry

133

* @return Class name string

134

*/

135

public String className(int class_info_index);

136

137

/**

138

* Get module name from constant pool

139

* @param module_info_index Index of module info entry

140

* @return Module name string

141

*/

142

public String moduleName(int module_info_index);

143

144

/**

145

* Get package name from constant pool

146

* @param package_info_index Index of package info entry

147

* @return Package name string

148

*/

149

public String packageName(int package_info_index);

150

151

/**

152

* Read constant pool from input stream

153

* @param in DataInput to read from

154

* @return Constant pool instance

155

* @throws IOException if reading fails

156

*/

157

public static ConstantPool read(DataInput in) throws IOException;

158

159

/**

160

* Write constant pool to output stream

161

* @param out DataOutput to write to

162

* @throws IOException if writing fails

163

*/

164

public void write(DataOutput out) throws IOException;

165

}

166

```

167

168

### Class File Building

169

170

Programmatic construction of class files using builder pattern.

171

172

```java { .api }

173

/**

174

* Builder for constructing ClassFile objects programmatically

175

*/

176

public class ClassFileBuilder {

177

/**

178

* Set minor version number

179

* @param minor_version Minor version

180

* @return This builder instance

181

*/

182

public ClassFileBuilder minor_version(int minor_version);

183

184

/**

185

* Set major version number

186

* @param major_version Major version

187

* @return This builder instance

188

*/

189

public ClassFileBuilder major_version(int major_version);

190

191

/**

192

* Set access flags

193

* @param access Access flags

194

* @return This builder instance

195

*/

196

public ClassFileBuilder access(int access);

197

198

/**

199

* Set this class name

200

* @param this_class Class name

201

* @return This builder instance

202

*/

203

public ClassFileBuilder this_class(String this_class);

204

205

/**

206

* Set super class name

207

* @param super_class Super class name

208

* @return This builder instance

209

*/

210

public ClassFileBuilder super_class(String super_class);

211

212

/**

213

* Set interface names

214

* @param interfaces Array of interface names

215

* @return This builder instance

216

*/

217

public ClassFileBuilder interfaces(String... interfaces);

218

219

/**

220

* Set field information

221

* @param fields Array of field info

222

* @return This builder instance

223

*/

224

public ClassFileBuilder fields(FieldInfo... fields);

225

226

/**

227

* Set method information

228

* @param methods Array of method info

229

* @return This builder instance

230

*/

231

public ClassFileBuilder methods(MethodInfo... methods);

232

233

/**

234

* Set attributes

235

* @param attributes Array of attributes

236

* @return This builder instance

237

*/

238

public ClassFileBuilder attributes(Attribute... attributes);

239

240

/**

241

* Build the final ClassFile object

242

* @return Constructed ClassFile instance

243

*/

244

public ClassFile build();

245

}

246

247

/**

248

* Mutable constant pool for building class files

249

*/

250

public class MutableConstantPool extends ConstantPool {

251

/**

252

* Add UTF-8 string to constant pool

253

* @param string String to add

254

* @return Index of added entry

255

*/

256

public int utf8Info(String string);

257

258

/**

259

* Add class info to constant pool

260

* @param class_name Class name to add

261

* @return Index of added entry

262

*/

263

public int classInfo(String class_name);

264

265

/**

266

* Add string info to constant pool

267

* @param string String value to add

268

* @return Index of added entry

269

*/

270

public int stringInfo(String string);

271

}

272

```

273

274

**Usage Examples:**

275

276

```java

277

import aQute.bnd.classfile.builder.ClassFileBuilder;

278

import aQute.bnd.classfile.builder.MutableConstantPool;

279

280

// Build a simple class file

281

MutableConstantPool cp = new MutableConstantPool();

282

ClassFile newClass = new ClassFileBuilder(cp)

283

.major_version(61) // Java 17

284

.access(ClassFile.ACC_PUBLIC)

285

.this_class("com/example/GeneratedClass")

286

.super_class("java/lang/Object")

287

.build();

288

289

byte[] bytecode = newClass.write();

290

```

291

292

### Field and Method Information

293

294

Access to field and method metadata within class files.

295

296

```java { .api }

297

/**

298

* Base class for field and method information

299

*/

300

public abstract class MemberInfo extends ElementInfo {

301

public final String name;

302

public final String descriptor;

303

304

protected MemberInfo(int access, String name, String descriptor, Attribute[] attributes);

305

}

306

307

/**

308

* Represents field information in a class file

309

*/

310

public class FieldInfo extends MemberInfo {

311

public FieldInfo(int access, String name, String descriptor, Attribute[] attributes);

312

}

313

314

/**

315

* Represents method information in a class file

316

*/

317

public class MethodInfo extends MemberInfo {

318

public MethodInfo(int access, String name, String descriptor, Attribute[] attributes);

319

}

320

321

/**

322

* Base class for class file elements with access flags and attributes

323

*/

324

public abstract class ElementInfo {

325

public final int access;

326

public final Attribute[] attributes;

327

328

protected ElementInfo(int access, Attribute[] attributes);

329

330

/**

331

* Get attribute of specified type

332

* @param type Attribute class type

333

* @return Optional containing attribute if found

334

*/

335

public <T> Optional<T> getAttribute(Class<T> type);

336

}

337

```

338

339

### Attributes

340

341

Comprehensive support for all Java class file attributes.

342

343

```java { .api }

344

/**

345

* Interface for class file attributes

346

*/

347

public interface Attribute {

348

/**

349

* Get attribute name

350

* @return Attribute name string

351

*/

352

String name();

353

354

/**

355

* Write attribute to output stream

356

* @param out DataOutput to write to

357

* @param constant_pool Constant pool for resolving references

358

* @throws IOException if writing fails

359

*/

360

void write(DataOutput out, ConstantPool constant_pool) throws IOException;

361

362

/**

363

* Get attribute length in bytes

364

* @return Attribute length

365

*/

366

int attribute_length();

367

368

/**

369

* Read attributes from input stream

370

* @param in DataInput to read from

371

* @param constant_pool Constant pool for resolving references

372

* @return Array of attributes

373

* @throws IOException if reading fails

374

*/

375

static Attribute[] readAttributes(DataInput in, ConstantPool constant_pool) throws IOException;

376

}

377

378

// Common attribute implementations

379

public class CodeAttribute implements Attribute { /* Method bytecode */ }

380

public class ConstantValueAttribute implements Attribute { /* Constant field values */ }

381

public class SignatureAttribute implements Attribute { /* Generic type signatures */ }

382

public class SourceFileAttribute implements Attribute { /* Source file name */ }

383

public class ModuleAttribute implements Attribute { /* Module information */ }

384

public class RecordAttribute implements Attribute { /* Record component information */ }

385

public class PermittedSubclassesAttribute implements Attribute { /* Sealed class subclasses */ }

386

```

387

388

## Constants

389

390

```java { .api }

391

public class ClassFile {

392

// Class file version constants

393

public static final int MAJOR_VERSION = 61; // Current major version

394

395

// Access flag constants

396

public static final int ACC_PUBLIC = 0x0001;

397

public static final int ACC_PRIVATE = 0x0002;

398

public static final int ACC_PROTECTED = 0x0004;

399

public static final int ACC_STATIC = 0x0008;

400

public static final int ACC_FINAL = 0x0010;

401

public static final int ACC_SUPER = 0x0020;

402

public static final int ACC_VOLATILE = 0x0040;

403

public static final int ACC_TRANSIENT = 0x0080;

404

public static final int ACC_NATIVE = 0x0100;

405

public static final int ACC_INTERFACE = 0x0200;

406

public static final int ACC_ABSTRACT = 0x0400;

407

public static final int ACC_STRICT = 0x0800;

408

public static final int ACC_SYNTHETIC = 0x1000;

409

public static final int ACC_ANNOTATION = 0x2000;

410

public static final int ACC_ENUM = 0x4000;

411

public static final int ACC_MODULE = 0x8000;

412

}

413

```