|
| 1 | +# JAVA CLIENT - AI AGENT INSTRUCTIONS |
| 2 | + |
| 3 | +## ⚠️ CRITICAL: CHECK YOUR REPOSITORY FIRST |
| 4 | + |
| 5 | +Before making ANY changes, verify you're in the correct repository: |
| 6 | + |
| 7 | +```bash |
| 8 | +git remote -v |
| 9 | +``` |
| 10 | + |
| 11 | +- ✅ **CORRECT**: `origin .../algolia/api-clients-automation.git` → You may proceed |
| 12 | +- ❌ **WRONG**: `origin .../algolia/algoliasearch-client-java.git` → STOP! This is the PUBLIC repository |
| 13 | + |
| 14 | +**If you're in `algoliasearch-client-java`**: Do NOT make changes here. All changes must go through `api-clients-automation`. PRs and commits made directly to the public repo will be discarded on next release. |
| 15 | + |
| 16 | +## ⚠️ BEFORE ANY EDIT: Check If File Is Generated |
| 17 | + |
| 18 | +Before editing ANY file, verify it's hand-written by checking `config/generation.config.mjs`: |
| 19 | + |
| 20 | +```javascript |
| 21 | +// In generation.config.mjs - patterns WITHOUT '!' are GENERATED (do not edit) |
| 22 | +'clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/api/**', // Generated |
| 23 | +'clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/model/**', // Generated |
| 24 | +// Everything else is hand-written by default (note the '!' pattern for Java) |
| 25 | +``` |
| 26 | + |
| 27 | +**Hand-written (safe to edit):** |
| 28 | + |
| 29 | +- `src/main/java/com/algolia/config/**` - Configuration classes |
| 30 | +- `src/main/java/com/algolia/exceptions/**` - Exception hierarchy |
| 31 | +- `src/main/java/com/algolia/internal/**` - HTTP requester, serialization |
| 32 | +- `src/main/java/com/algolia/utils/**` - Utility classes |
| 33 | +- `ApiClient.java` - Core client implementation |
| 34 | + |
| 35 | +**Generated (DO NOT EDIT):** |
| 36 | + |
| 37 | +- `src/main/java/com/algolia/api/**` - API client interfaces |
| 38 | +- `src/main/java/com/algolia/model/**` - API models |
| 39 | +- `BuildConfig.java` - Version info |
| 40 | + |
| 41 | +## Language Conventions |
| 42 | + |
| 43 | +### Naming |
| 44 | + |
| 45 | +- **Files**: `PascalCase.java` matching class name |
| 46 | +- **Variables/Methods**: `camelCase` |
| 47 | +- **Classes/Interfaces**: `PascalCase` |
| 48 | +- **Constants**: `UPPER_SNAKE_CASE` |
| 49 | +- **Packages**: `lowercase` |
| 50 | + |
| 51 | +### Formatting |
| 52 | + |
| 53 | +- Google Java Format via Prettier plugin |
| 54 | +- 140 char line width for Java |
| 55 | +- Run: `yarn cli format java clients/algoliasearch-client-java` |
| 56 | + |
| 57 | +### Java Patterns |
| 58 | + |
| 59 | +- Java 8+ compatibility required |
| 60 | +- Use `Optional<T>` for nullable returns |
| 61 | +- Prefer immutable objects where possible |
| 62 | +- Use builder pattern for complex objects |
| 63 | + |
| 64 | +### Dependencies |
| 65 | + |
| 66 | +- **HTTP**: OkHttp 4.x |
| 67 | +- **JSON**: Jackson |
| 68 | +- **Build**: Gradle |
| 69 | +- **Annotations**: `javax.annotation.Nonnull`, `@Nullable` |
| 70 | + |
| 71 | +## Client Patterns |
| 72 | + |
| 73 | +### HTTP Requester Architecture |
| 74 | + |
| 75 | +```java |
| 76 | +// Core HTTP in internal/HttpRequester.java |
| 77 | +public final class HttpRequester implements Requester { |
| 78 | + |
| 79 | + private final OkHttpClient httpClient; |
| 80 | + private final JsonSerializer serializer; |
| 81 | + |
| 82 | + // Uses OkHttp interceptors for: |
| 83 | + // - Header injection (HeaderInterceptor) |
| 84 | + // - Gzip compression (GzipRequestInterceptor) |
| 85 | + // - Logging (LogInterceptor) |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Retry Strategy |
| 90 | + |
| 91 | +- Implemented via `RetryStrategy` class |
| 92 | +- Host states tracked in `StatefulHost` |
| 93 | +- Configurable timeouts per call type (read/write) |
| 94 | +- Network errors trigger retry, 4xx errors do not |
| 95 | + |
| 96 | +### Async Support |
| 97 | + |
| 98 | +```java |
| 99 | +// Sync version |
| 100 | +SearchResponse response = client.search(params); |
| 101 | + |
| 102 | +// Async version (returns CompletableFuture) |
| 103 | +CompletableFuture<SearchResponse> future = client.searchAsync(params); |
| 104 | +``` |
| 105 | + |
| 106 | +### Exception Hierarchy |
| 107 | + |
| 108 | +```java |
| 109 | +// com.algolia.exceptions |
| 110 | +AlgoliaException // Base (RuntimeException) |
| 111 | +├── AlgoliaApiException // API error (4xx, 5xx) |
| 112 | +├── AlgoliaClientException // Client-side error |
| 113 | +├── AlgoliaRetryException // All retries exhausted |
| 114 | +└── AlgoliaRuntimeException // Unexpected runtime error |
| 115 | +``` |
| 116 | + |
| 117 | +## Common Gotchas |
| 118 | + |
| 119 | +### Resource Cleanup |
| 120 | + |
| 121 | +```java |
| 122 | +// Always close the client when done |
| 123 | +SearchClient client = new SearchClient("APP_ID", "API_KEY"); |
| 124 | +try { |
| 125 | + // use client |
| 126 | +} finally { |
| 127 | + client.close(); |
| 128 | +} |
| 129 | + |
| 130 | +// Or use try-with-resources |
| 131 | +try (SearchClient client = new SearchClient("APP_ID", "API_KEY")) { |
| 132 | + // use client |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +### Thread Safety |
| 137 | + |
| 138 | +- Client instances are thread-safe |
| 139 | +- Share single client across threads |
| 140 | +- Don't create new client per request |
| 141 | + |
| 142 | +### Type References for Generics |
| 143 | + |
| 144 | +```java |
| 145 | +// Use TypeReference for generic types |
| 146 | +List<Hit> hits = client.search(params, new TypeReference<List<Hit>>() {}); |
| 147 | +``` |
| 148 | + |
| 149 | +### Null Safety |
| 150 | + |
| 151 | +```java |
| 152 | +// Use Optional for nullable returns |
| 153 | +Optional<String> value = config.getOptionalParam(); |
| 154 | + |
| 155 | +// Check with @Nonnull/@Nullable annotations |
| 156 | +public void method(@Nonnull String required, @Nullable String optional) |
| 157 | +``` |
| 158 | + |
| 159 | +### Gradle vs Maven |
| 160 | + |
| 161 | +- Project uses Gradle |
| 162 | +- Check `build.gradle` for dependencies |
| 163 | +- Use `./gradlew` wrapper for builds |
| 164 | + |
| 165 | +## Build & Test Commands |
| 166 | + |
| 167 | +```bash |
| 168 | +# From repo root (api-clients-automation) |
| 169 | +yarn cli build clients java # Build Java client |
| 170 | +yarn cli cts generate java # Generate CTS tests |
| 171 | +yarn cli cts run java # Run CTS tests |
| 172 | +yarn cli playground java search # Interactive playground |
| 173 | +yarn cli format java clients/algoliasearch-client-java |
| 174 | + |
| 175 | +# From client directory |
| 176 | +cd clients/algoliasearch-client-java |
| 177 | +./gradlew build # Build with Gradle |
| 178 | +./gradlew test # Run tests |
| 179 | +./gradlew spotlessApply # Apply formatting |
| 180 | +``` |
0 commit comments