How lenni0451/reflect works
Project Type and Purpose
• This is a Java library (“Reflect”) offering a unified API to perform advanced reflection tasks and bypass the usual JVM restrictions (e.g., inaccessible members, module boundaries).
• It provides builders for bytecode generation (via ASM), dynamic proxies, local variable capture, stream-style reflection queries, and utility helpers. There is also a dedicated j9 submodule for Java 9+ module support.
File Structure Analysis
• Top─level: Gradle build files, src/ main module, j9/ Java 9 compatibility module, config/, templates, and tests.
• src/main/java/net/lenni0451/reflect: core reflection APIs (Classes, Fields, Methods, Modules, etc.).
• Subpackages:
• bytecode → bytecode builders & wrappers (ASM-based implementations)
• proxy → proxy generation abstractions and runtime
• localcapture → capturing local stack frames & values
• stream → fluent reflection-based streams
• accessor, wrapper, exceptions, utils
• j9/src/main/java/...: mirrors core logic but with module‐safe implementations.
Key Architecture Insights from README
• Primary concern is modularity: separate concerns (core reflection vs. code-gen vs. proxy vs. Java 9 module hacks).
Main Components
a. Core Module
• Entry point classes (net.lenni0451.reflect.*)
• Basic operations: find classes, fields, methods, manipulate objects
b. Bytecode Engine
• High-level builders (bytecode.builder)
• ASM implementations (bytecode.impl.asm)
• Wrappers (bytecode.wrapper)
c. Proxy Module
• Public API (reflect.proxy.*)
• Internal builders & runtime (.impl, .internal)
d. LocalCapture Module
• And inspect local variables at runtime
e. Stream API
• Fluent reflection streams (constructor, field, method streams)
f. Utilities & Exceptions
• Helpers (ObjectPrinter, FieldInitializer)
• Custom exceptions
g. j9 Compatibility Module
• Overrides or extensions for Java 9 module access
h. Build & Distribution
• Gradle build scripts, templates (Mustache), generated UnsafeAccess code
• Publication to Maven repositories
Component Relationships and Data Flows
• Core → Proxy Module (to build dynamic proxies)
• Core → LocalCapture (to snapshot local stack frames)
• Bytecode Engine uses ASM library (external)
• Proxy Module depends on Bytecode Engine for instrumentation
• j9 Module plugs into Reflect Core and Proxy to adjust access for modules
• Templates feed into codegen at build time (UnsafeAccess.mustache → generated Java)
• Build subsystem packages everything into a JAR and publishes to Maven
Architectural Patterns & Principles
• Modular layered design: core API layer, codegen layer, runtime helpers
• Builder pattern in bytecode and proxy generation
• Abstraction over ASM and Unsafe primitives
• Extension point via j9 submodule