|
| 1 | + |
| 2 | +# Immutables Annotation Processor Options Test |
| 3 | + |
| 4 | +This directory contains manual tests for Immutables annotation processor options, specifically: |
| 5 | + |
| 6 | +1. `-Aimmutables.annotations.pick` - Controls which annotation packages are used in generated code |
| 7 | +2. `-Aimmutables.guava.suppress` - Forces JDK-only collections even when Guava is on classpath |
| 8 | + |
| 9 | +## Directory Structure |
| 10 | + |
| 11 | +``` |
| 12 | +immutables-annotation-processor-test/ |
| 13 | +├── lib/ # Downloaded JAR dependencies |
| 14 | +│ ├── value-2.12.0-rc0.jar # Immutables annotation processor |
| 15 | +│ ├── value-annotations-2.12.0-rc0.jar # Immutables annotations |
| 16 | +│ ├── guava-33.0.0-jre.jar # Google Guava |
| 17 | +│ ├── jakarta.annotation-api-3.0.0.jar # Jakarta annotations |
| 18 | +│ └── javax.annotation-api-1.3.2.jar # Javax annotations |
| 19 | +├── src/com/example/ # Sample Java source files |
| 20 | +│ ├── Person.java # Simple immutable for annotations test |
| 21 | +│ └── Team.java # Immutable with collections for Guava test |
| 22 | +├── generated/ # Generated code output (created when tests run) |
| 23 | +├── download-jars.sh # Script to download all JAR dependencies |
| 24 | +├── test-annotations-pick.sh # Test script for -Aimmutables.annotations.pick |
| 25 | +├── test-guava-suppress.sh # Test script for -Aimmutables.guava.suppress |
| 26 | +└── README.md # This file |
| 27 | +``` |
| 28 | + |
| 29 | +## Getting Started |
| 30 | + |
| 31 | +### Download Dependencies |
| 32 | + |
| 33 | +If the `lib/` directory is empty (e.g., after cloning the repository), run the download script to fetch all required JARs from Maven Central: |
| 34 | + |
| 35 | +```bash |
| 36 | +chmod +x download-jars.sh |
| 37 | +./download-jars.sh |
| 38 | +``` |
| 39 | + |
| 40 | +This will download: |
| 41 | +- Immutables 2.12.0-rc0 (annotation processor and annotations) |
| 42 | +- Guava 33.0.0-jre |
| 43 | +- Jakarta Annotation API 3.0.0 |
| 44 | +- Javax Annotation API 1.3.2 |
| 45 | + |
| 46 | +The script is idempotent - it will skip files that already exist, so it's safe to run multiple times. |
| 47 | + |
| 48 | +## Dependencies |
| 49 | + |
| 50 | +- **Immutables 2.12.0-rc0**: The version being tested |
| 51 | +- **Guava 33.0.0-jre**: Latest stable Guava for testing suppression |
| 52 | +- **Jakarta Annotation API 3.0.0**: For testing jakarta annotation generation |
| 53 | +- **Javax Annotation API 1.3.2**: For testing javax annotation generation |
| 54 | + |
| 55 | +## Test 1: Annotations Pick (`-Aimmutables.annotations.pick`) |
| 56 | + |
| 57 | +This option controls which annotation packages Immutables uses in generated code. |
| 58 | + |
| 59 | +### Usage |
| 60 | + |
| 61 | +```bash |
| 62 | +chmod +x test-annotations-pick.sh |
| 63 | +./test-annotations-pick.sh |
| 64 | +``` |
| 65 | + |
| 66 | +### What it tests |
| 67 | + |
| 68 | +The script compiles `Person.java` with different values for `-Aimmutables.annotations.pick`: |
| 69 | + |
| 70 | +1. **`jakarta`** - Uses `jakarta.annotation.Generated` and `jakarta.annotation.ParametersAreNonnullByDefault` |
| 71 | +2. **`javax`** - Uses `javax.annotation.Generated` |
| 72 | +3. **`javax+processing`** - Uses `javax.annotation.processing.Generated` |
| 73 | +4. **`legacy`** - Uses legacy `javax.annotation.*` packages |
| 74 | +5. **`none`** (empty value) - No processor-generated annotations |
| 75 | + |
| 76 | +### Expected output |
| 77 | + |
| 78 | +The script shows: |
| 79 | +- Which annotations are imported in the generated `ImmutablePerson.java` |
| 80 | +- Which annotations are applied to the class |
| 81 | +- Differences between jakarta vs javax vs none |
| 82 | + |
| 83 | +### Generated code locations |
| 84 | + |
| 85 | +After running, inspect the generated code: |
| 86 | +``` |
| 87 | +generated/annotations-pick-jakarta/com/example/ImmutablePerson.java |
| 88 | +generated/annotations-pick-javax/com/example/ImmutablePerson.java |
| 89 | +generated/annotations-pick-javax-processing/com/example/ImmutablePerson.java |
| 90 | +generated/annotations-pick-legacy/com/example/ImmutablePerson.java |
| 91 | +generated/annotations-pick-none/com/example/ImmutablePerson.java |
| 92 | +``` |
| 93 | + |
| 94 | +## Test 2: Guava Suppress (`-Aimmutables.guava.suppress`) |
| 95 | + |
| 96 | +This option forces the processor to ignore Guava even when it's on the classpath. |
| 97 | + |
| 98 | +### Usage |
| 99 | + |
| 100 | +```bash |
| 101 | +chmod +x test-guava-suppress.sh |
| 102 | +./test-guava-suppress.sh |
| 103 | +``` |
| 104 | + |
| 105 | +### What it tests |
| 106 | + |
| 107 | +The script runs three compilation scenarios: |
| 108 | + |
| 109 | +1. **Guava on classpath, NOT suppressed** - Should use `com.google.common.collect.ImmutableList/Set/Map` |
| 110 | +2. **Guava on classpath, BUT suppressed** - Should use JDK `Collections.unmodifiableList/Set/Map` |
| 111 | +3. **Guava NOT on classpath** - Baseline comparison, should match scenario 2 |
| 112 | + |
| 113 | +### Expected output |
| 114 | + |
| 115 | +The script shows: |
| 116 | +- Whether Guava imports are present in generated code |
| 117 | +- What collection types are used in the generated fields |
| 118 | +- A diff command to compare the generated files |
| 119 | + |
| 120 | +### Generated code locations |
| 121 | + |
| 122 | +After running, inspect and compare: |
| 123 | +``` |
| 124 | +generated/guava-not-suppressed/com/example/ImmutableTeam.java # Uses Guava |
| 125 | +generated/guava-suppressed/com/example/ImmutableTeam.java # Uses JDK only |
| 126 | +generated/guava-absent/com/example/ImmutableTeam.java # Uses JDK only |
| 127 | +``` |
| 128 | + |
| 129 | +Compare them: |
| 130 | +```bash |
| 131 | +diff generated/guava-not-suppressed/com/example/ImmutableTeam.java \ |
| 132 | + generated/guava-suppressed/com/example/ImmutableTeam.java |
| 133 | +``` |
| 134 | + |
| 135 | +## Manual Testing |
| 136 | + |
| 137 | +You can also run javac manually to test specific scenarios: |
| 138 | + |
| 139 | +### Test annotations.pick manually |
| 140 | + |
| 141 | +```bash |
| 142 | +javac -cp lib/value-annotations-2.12.0-rc0.jar:lib/jakarta.annotation-api-3.0.0.jar \ |
| 143 | + -processorpath lib/value-2.12.0-rc0.jar \ |
| 144 | + -proc:only \ |
| 145 | + -s generated/manual-test \ |
| 146 | + -Aimmutables.annotations.pick=jakarta \ |
| 147 | + src/com/example/Person.java |
| 148 | +``` |
| 149 | + |
| 150 | +### Test guava.suppress manually |
| 151 | + |
| 152 | +```bash |
| 153 | +javac -cp lib/value-annotations-2.12.0-rc0.jar:lib/guava-33.0.0-jre.jar \ |
| 154 | + -processorpath lib/value-2.12.0-rc0.jar \ |
| 155 | + -proc:only \ |
| 156 | + -s generated/manual-test \ |
| 157 | + -Aimmutables.guava.suppress \ |
| 158 | + src/com/example/Team.java |
| 159 | +``` |
| 160 | + |
| 161 | +## Requirements |
| 162 | + |
| 163 | +- Java 11 or later (for running javac) |
| 164 | +- bash shell |
| 165 | +- curl (for downloading dependencies) |
| 166 | +- Basic Unix tools (grep, diff) |
| 167 | + |
| 168 | +## Documentation References |
| 169 | + |
| 170 | +These options are documented in the Immutables documentation: |
| 171 | + |
| 172 | +- **Annotation Processing Options**: `/newandnice.mdx` section "Annotation Processing Options" |
| 173 | +- **`-Aimmutables.annotations.pick`**: Controls javax vs jakarta vs none for standard annotations |
| 174 | +- **`-Aimmutables.guava.suppress`**: Forces JDK-only collections when Guava is present |
| 175 | + |
| 176 | +## Cleaning Up |
| 177 | + |
| 178 | +To clean all generated code: |
| 179 | + |
| 180 | +```bash |
| 181 | +rm -rf generated/ |
| 182 | +``` |
| 183 | + |
| 184 | +To start fresh (including downloads): |
| 185 | + |
| 186 | +```bash |
| 187 | +rm -rf lib/ generated/ |
| 188 | +``` |
| 189 | + |
| 190 | +Then re-download dependencies by running the test scripts again (or manually with curl). |
0 commit comments