or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

bean-utilities.mdcore-utilities.mdindex.mdproxy-enhancement.mdreflection-utilities.mdtransformation-utilities.mdutility-classes.md

reflection-utilities.mddocs/

0

# Reflection Utilities

1

2

Fast alternatives to Java reflection for method and constructor invocation, plus method delegation utilities. These utilities provide significant performance improvements over standard Java reflection while maintaining similar functionality.

3

4

## Capabilities

5

6

### FastClass

7

8

Faster alternative to Java reflection for method and constructor invocation.

9

10

```java { .api }

11

/**

12

* Faster alternative to Java reflection for class operations

13

*/

14

public abstract class FastClass {

15

/**

16

* Create FastClass for a type

17

* @param type Class to wrap

18

* @return FastClass instance

19

*/

20

public static FastClass create(Class type);

21

22

/**

23

* Create FastClass with specific ClassLoader

24

* @param loader ClassLoader to use

25

* @param type Class to wrap

26

* @return FastClass instance

27

*/

28

public static FastClass create(ClassLoader loader, Class type);

29

30

/**

31

* Invoke method by name and parameter types

32

* @param name Method name

33

* @param parameterTypes Parameter types

34

* @param obj Target object

35

* @param args Method arguments

36

* @return Method result

37

* @throws InvocationTargetException If method throws exception

38

*/

39

public abstract Object invoke(String name, Class[] parameterTypes, Object obj, Object[] args)

40

throws InvocationTargetException;

41

42

/**

43

* Create new instance with default constructor

44

* @return New instance

45

* @throws InvocationTargetException If constructor throws exception

46

*/

47

public abstract Object newInstance() throws InvocationTargetException;

48

49

/**

50

* Create new instance with specific constructor

51

* @param parameterTypes Constructor parameter types

52

* @param args Constructor arguments

53

* @return New instance

54

* @throws InvocationTargetException If constructor throws exception

55

*/

56

public abstract Object newInstance(Class[] parameterTypes, Object[] args)

57

throws InvocationTargetException;

58

59

/**

60

* Get FastMethod wrapper for a Method

61

* @param method Method to wrap

62

* @return FastMethod instance

63

*/

64

public abstract FastMethod getMethod(Method method);

65

66

/**

67

* Get FastConstructor wrapper for a Constructor

68

* @param constructor Constructor to wrap

69

* @return FastConstructor instance

70

*/

71

public abstract FastConstructor getConstructor(Constructor constructor);

72

73

/**

74

* Get FastMethod by signature

75

* @param name Method name

76

* @param parameterTypes Parameter types

77

* @return FastMethod instance

78

*/

79

public abstract FastMethod getMethod(String name, Class[] parameterTypes);

80

81

/**

82

* Get FastConstructor by parameter types

83

* @param parameterTypes Parameter types

84

* @return FastConstructor instance

85

*/

86

public abstract FastConstructor getConstructor(Class[] parameterTypes);

87

88

/**

89

* Invoke method by index (fastest)

90

* @param index Method index

91

* @param obj Target object

92

* @param args Method arguments

93

* @return Method result

94

* @throws InvocationTargetException If method throws exception

95

*/

96

public Object invoke(int index, Object obj, Object[] args) throws InvocationTargetException;

97

98

/**

99

* Create instance by constructor index

100

* @param index Constructor index

101

* @param args Constructor arguments

102

* @return New instance

103

* @throws InvocationTargetException If constructor throws exception

104

*/

105

public Object newInstance(int index, Object[] args) throws InvocationTargetException;

106

107

/**

108

* Get method index for name and parameters

109

* @param name Method name

110

* @param parameterTypes Parameter types

111

* @return Method index

112

*/

113

public int getIndex(String name, Class[] parameterTypes);

114

115

/**

116

* Get method index for signature

117

* @param signature Method signature

118

* @return Method index

119

*/

120

public int getIndex(Signature signature);

121

122

/**

123

* Get maximum method index

124

* @return Maximum index

125

*/

126

public int getMaxIndex();

127

128

/**

129

* Get wrapped Java class

130

* @return Java class

131

*/

132

public Class getJavaClass();

133

134

/**

135

* Get class name

136

* @return Class name

137

*/

138

public String getName();

139

140

/**

141

* String representation

142

* @return String

143

*/

144

public String toString();

145

}

146

```

147

148

**Usage Examples:**

149

150

```java

151

// Create FastClass for better performance than reflection

152

FastClass fastClass = FastClass.create(MyService.class);

153

154

// Invoke methods by name (faster than Method.invoke)

155

MyService service = new MyService();

156

Object result = fastClass.invoke("processData",

157

new Class[]{String.class, Integer.class},

158

service,

159

new Object[]{"hello", 42});

160

161

// Create instances (faster than Constructor.newInstance)

162

Object newInstance = fastClass.newInstance();

163

Object instanceWithArgs = fastClass.newInstance(

164

new Class[]{String.class},

165

new Object[]{"constructor arg"});

166

167

// Get method wrappers for repeated use

168

FastMethod fastMethod = fastClass.getMethod("processData",

169

new Class[]{String.class, Integer.class});

170

Object result2 = fastMethod.invoke(service, new Object[]{"world", 99});

171

172

// Use indexes for maximum performance

173

int methodIndex = fastClass.getIndex("processData",

174

new Class[]{String.class, Integer.class});

175

Object result3 = fastClass.invoke(methodIndex, service, new Object[]{"fast", 123});

176

177

// Performance comparison

178

long start = System.nanoTime();

179

for (int i = 0; i < 100000; i++) {

180

// FastClass approach

181

fastClass.invoke(methodIndex, service, new Object[]{"test", i});

182

}

183

long fastTime = System.nanoTime() - start;

184

185

start = System.nanoTime();

186

Method method = MyService.class.getMethod("processData", String.class, Integer.class);

187

for (int i = 0; i < 100000; i++) {

188

// Standard reflection

189

method.invoke(service, "test", i);

190

}

191

long reflectionTime = System.nanoTime() - start;

192

// FastClass is typically 10-50x faster

193

```

194

195

### FastMethod

196

197

Fast method invocation wrapper.

198

199

```java { .api }

200

/**

201

* Fast method invocation wrapper

202

*/

203

public class FastMethod {

204

/**

205

* Invoke the method

206

* @param obj Target object

207

* @param args Method arguments

208

* @return Method result

209

* @throws InvocationTargetException If method throws exception

210

*/

211

public Object invoke(Object obj, Object[] args) throws InvocationTargetException;

212

213

/**

214

* Get the wrapped Java method

215

* @return Method instance

216

*/

217

public Method getJavaMethod();

218

219

/**

220

* Get parameter types

221

* @return Parameter types

222

*/

223

public Class[] getParameterTypes();

224

225

/**

226

* Get return type

227

* @return Return type

228

*/

229

public Class getReturnType();

230

231

/**

232

* Get method index in FastClass

233

* @return Method index

234

*/

235

public int getIndex();

236

237

/**

238

* Get method name

239

* @return Method name

240

*/

241

public String getName();

242

243

/**

244

* Get method signature

245

* @return Signature

246

*/

247

public Signature getSignature();

248

249

/**

250

* String representation

251

* @return String

252

*/

253

public String toString();

254

}

255

```

256

257

### FastConstructor

258

259

Fast constructor invocation wrapper.

260

261

```java { .api }

262

/**

263

* Fast constructor invocation wrapper

264

*/

265

public class FastConstructor {

266

/**

267

* Create new instance with no arguments

268

* @return New instance

269

* @throws InvocationTargetException If constructor throws exception

270

*/

271

public Object newInstance() throws InvocationTargetException;

272

273

/**

274

* Create new instance with arguments

275

* @param args Constructor arguments

276

* @return New instance

277

* @throws InvocationTargetException If constructor throws exception

278

*/

279

public Object newInstance(Object[] args) throws InvocationTargetException;

280

281

/**

282

* Get the wrapped Java constructor

283

* @return Constructor instance

284

*/

285

public Constructor getJavaConstructor();

286

287

/**

288

* Get parameter types

289

* @return Parameter types

290

*/

291

public Class[] getParameterTypes();

292

293

/**

294

* Get constructor index in FastClass

295

* @return Constructor index

296

*/

297

public int getIndex();

298

299

/**

300

* String representation

301

* @return String

302

*/

303

public String toString();

304

}

305

```

306

307

### MethodDelegate

308

309

Creates optimized delegates for method calls, providing better performance than reflection or interface implementations.

310

311

```java { .api }

312

/**

313

* Creates optimized method delegates

314

*/

315

public abstract class MethodDelegate {

316

/**

317

* Delegate for static methods only

318

*/

319

public static final MethodDelegate STATIC_ONLY;

320

321

/**

322

* Create method delegate

323

* @param target Target object

324

* @param methodName Method name

325

* @param iface Interface that delegate should implement

326

* @return MethodDelegate instance

327

*/

328

public static MethodDelegate create(Object target, String methodName, Class iface);

329

330

/**

331

* Create new delegate instance

332

* @param target New target object

333

* @param methodName Method name

334

* @return New delegate instance

335

*/

336

public abstract Object newInstance(Object target, String methodName);

337

}

338

```

339

340

**Usage Examples:**

341

342

```java

343

// Define interface for delegate

344

interface StringProcessor {

345

String process(String input);

346

}

347

348

// Create object with method matching interface

349

class TextUtils {

350

public String toUpperCase(String text) {

351

return text.toUpperCase();

352

}

353

354

public String toLowerCase(String text) {

355

return text.toLowerCase();

356

}

357

}

358

359

// Create method delegate

360

TextUtils utils = new TextUtils();

361

StringProcessor upperDelegate = (StringProcessor) MethodDelegate.create(

362

utils, "toUpperCase", StringProcessor.class);

363

364

// Use delegate (much faster than reflection)

365

String result = upperDelegate.process("hello"); // "HELLO"

366

367

// Create delegate for different method

368

StringProcessor lowerDelegate = (StringProcessor) MethodDelegate.create(

369

utils, "toLowerCase", StringProcessor.class);

370

String result2 = lowerDelegate.process("WORLD"); // "world"

371

372

// Create new delegate for different target object

373

TextUtils utils2 = new TextUtils();

374

Object newDelegate = upperDelegate.newInstance(utils2, "toUpperCase");

375

StringProcessor typedDelegate = (StringProcessor) newDelegate;

376

377

// Performance comparison

378

long start = System.nanoTime();

379

for (int i = 0; i < 100000; i++) {

380

upperDelegate.process("test" + i);

381

}

382

long delegateTime = System.nanoTime() - start;

383

384

start = System.nanoTime();

385

Method method = TextUtils.class.getMethod("toUpperCase", String.class);

386

for (int i = 0; i < 100000; i++) {

387

method.invoke(utils, "test" + i);

388

}

389

long reflectionTime = System.nanoTime() - start;

390

// Delegate is typically much faster than reflection

391

```

392

393

### MulticastDelegate

394

395

Multicasts method calls to multiple delegate objects.

396

397

```java { .api }

398

/**

399

* Multicasts method calls to multiple delegates

400

*/

401

public abstract class MulticastDelegate {

402

/**

403

* Create multicast delegate for interface

404

* @param iface Interface to implement

405

* @return MulticastDelegate instance

406

*/

407

public static MulticastDelegate create(Class iface);

408

409

/**

410

* Add delegate to multicast

411

* @param delegate Delegate to add

412

* @return New multicast delegate including added delegate

413

*/

414

public abstract MulticastDelegate add(Object delegate);

415

416

/**

417

* Remove delegate from multicast

418

* @param delegate Delegate to remove

419

* @return New multicast delegate excluding removed delegate

420

*/

421

public abstract MulticastDelegate remove(Object delegate);

422

423

/**

424

* Create new multicast delegate instance

425

* @return New instance

426

*/

427

public abstract MulticastDelegate newInstance();

428

}

429

```

430

431

**Usage Examples:**

432

433

```java

434

// Define interface for multicast

435

interface EventListener {

436

void onEvent(String event);

437

}

438

439

// Create individual listeners

440

EventListener listener1 = event -> System.out.println("Listener 1: " + event);

441

EventListener listener2 = event -> System.out.println("Listener 2: " + event);

442

EventListener listener3 = event -> System.out.println("Listener 3: " + event);

443

444

// Create multicast delegate

445

MulticastDelegate multicast = MulticastDelegate.create(EventListener.class);

446

447

// Add listeners

448

multicast = multicast.add(listener1);

449

multicast = multicast.add(listener2);

450

multicast = multicast.add(listener3);

451

452

// Cast to interface and use

453

EventListener broadcaster = (EventListener) multicast;

454

broadcaster.onEvent("Hello");

455

// Prints:

456

// Listener 1: Hello

457

// Listener 2: Hello

458

// Listener 3: Hello

459

460

// Remove a listener

461

multicast = multicast.remove(listener2);

462

EventListener broadcaster2 = (EventListener) multicast;

463

broadcaster2.onEvent("World");

464

// Prints:

465

// Listener 1: World

466

// Listener 3: World

467

468

// Create new instance for different delegate set

469

MulticastDelegate multicast2 = multicast.newInstance();

470

multicast2 = multicast2.add(listener2);

471

```

472

473

### ConstructorDelegate

474

475

Fast constructor delegation for object creation.

476

477

```java { .api }

478

/**

479

* Fast constructor delegation

480

*/

481

public abstract class ConstructorDelegate {

482

/**

483

* Create constructor delegate

484

* @param targetClass Class to create instances of

485

* @param iface Interface that delegate should implement

486

* @return ConstructorDelegate instance

487

*/

488

public static ConstructorDelegate create(Class targetClass, Class iface);

489

490

/**

491

* Create new instance using constructor

492

* @param args Constructor arguments

493

* @return New instance

494

*/

495

public abstract Object newInstance(Object[] args);

496

}

497

```

498

499

**Usage Examples:**

500

501

```java

502

// Define interface for constructor delegate

503

interface PersonFactory {

504

Person create(String name, int age);

505

}

506

507

// Create constructor delegate

508

ConstructorDelegate delegate = ConstructorDelegate.create(Person.class, PersonFactory.class);

509

PersonFactory factory = (PersonFactory) delegate;

510

511

// Use factory to create instances (faster than reflection)

512

Person person1 = factory.create("Alice", 30);

513

Person person2 = factory.create("Bob", 25);

514

515

// Performance comparison with reflection

516

Constructor<Person> constructor = Person.class.getConstructor(String.class, Integer.TYPE);

517

518

long start = System.nanoTime();

519

for (int i = 0; i < 100000; i++) {

520

factory.create("Test" + i, i);

521

}

522

long delegateTime = System.nanoTime() - start;

523

524

start = System.nanoTime();

525

for (int i = 0; i < 100000; i++) {

526

constructor.newInstance("Test" + i, i);

527

}

528

long reflectionTime = System.nanoTime() - start;

529

// Constructor delegate is typically faster

530

```

531

532

### FastMember

533

534

Base class for FastMethod and FastConstructor providing common functionality.

535

536

```java { .api }

537

/**

538

* Base class for fast member access

539

*/

540

public abstract class FastMember {

541

/**

542

* Get member index

543

* @return Member index

544

*/

545

public abstract int getIndex();

546

547

/**

548

* Get member name

549

* @return Member name

550

*/

551

public abstract String getName();

552

553

/**

554

* Get parameter types

555

* @return Parameter types

556

*/

557

public abstract Class[] getParameterTypes();

558

}

559

```

560

561

## Type Definitions

562

563

### InvocationTargetException

564

565

```java { .api }

566

/**

567

* Exception wrapping exceptions thrown by invoked methods/constructors

568

*/

569

public class InvocationTargetException extends Exception {

570

/**

571

* Get the target exception

572

* @return Target exception

573

*/

574

public Throwable getTargetException();

575

576

/**

577

* Get the cause

578

* @return Cause exception

579

*/

580

public Throwable getCause();

581

}

582

```

583

584

### Signature

585

586

```java { .api }

587

/**

588

* Represents a method signature

589

*/

590

public class Signature {

591

/**

592

* Get method name

593

* @return Method name

594

*/

595

public String getName();

596

597

/**

598

* Get descriptor

599

* @return Method descriptor

600

*/

601

public String getDescriptor();

602

603

/**

604

* String representation

605

* @return String

606

*/

607

public String toString();

608

}

609

```