New posts

This commit is contained in:
2026-11-06 21:43:25 +05:30
parent dfd3a830cd
commit d7d5689f05
40 changed files with 1359 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
# annotation-processor
Companion code for the ankurm.com post *"Custom Annotations and Annotation Processors in Java:
Generate Code at Compile Time."* A module of `java-core-examples`; itself a small two-module Maven build.
## Versions this was built and tested against
| Component | Version |
|---|---|
| JDK | 25.0.5+8 (Temurin, LTS) |
| Maven | 3.9.11 |
| maven-compiler-plugin | 3.16.0 |
| maven-surefire-plugin | 3.6.1 |
| JUnit Jupiter | 6.1.3 |
## Run it
```bash
export JAVA_HOME=/path/to/jdk-25
mvn -B clean test # from this directory: builds the processor, then the demo that uses it
scripts/run-all.sh # also regenerates every file in output/
```
## Files
| Path | What it is |
|---|---|
| `processor/.../GenerateBuilder.java` | The annotation (SOURCE retention, TYPE target, two members with defaults) |
| `processor/.../BuilderProcessor.java` | The `AbstractProcessor` that writes `<Type>Builder` through `Filer`, reports problems through `Messager` |
| `processor/src/main/resources/META-INF/services/javax.annotation.processing.Processor` | Registration |
| `processor/src/test/.../Compilation.java`, `ProcessorCompileTest.java` | In-memory `javax.tools.JavaCompiler` harness and its tests (writes `output/09`) |
| `demo/.../Person.java`, `Pizza.java` | Consumers of `@GenerateBuilder` (a record and a class) |
| `demo/.../annotations/*` | SOURCE / CLASS / RUNTIME annotations, `@Inherited`, `@Repeatable`, members and defaults |
| `demo/.../RetentionDemo.java`, `RuntimeValidatorDemo.java` | What reflection sees; a reflection-based validator |
| `demo/src/test/...` | Runtime tests of the generated builders and of the reflection behaviour |
| `bad/Shape.java` | Deliberately invalid input for the compile-error transcript |
| `output/01..10` | Captured runs; the post quotes these verbatim |
`demo/pom.xml` has two throwaway profiles (`classpath-only`, `classpath-proc-full`) used only to capture
`output/08`.