-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Compile-time annotation-processor tool to clientcore #43457
Draft
samvaity
wants to merge
5
commits into
Azure:main
Choose a base branch
from
samvaity:compile-time-codegen
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# Client Core Compile-Time Annotation Processor | ||
|
||
The client-core annotation processor for introducing compile-time code generation for libraries based on client core | ||
>Note: This project is for experimentation and exploring new ideas that may or may not make it into a supported GA release. | ||
|
||
## Usage | ||
|
||
1. Add the plugin dependency: | ||
```xml | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.clientcore.tools</groupId> | ||
<artifactId>annotation-processor</artifactId> | ||
<version>1.0.0.beta.1</version> <!-- {x-version-update;io.clientcore.tools:annotation-processor;external_dependency} --> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
``` | ||
1.1. Add the plugin configuration to your `pom.xml`: | ||
```xml | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.11.0</version> <!-- {x-version-update;org.apache.maven.plugins:maven-compiler-plugin;external_dependency} --> | ||
<configuration> | ||
<generatedSourcesDirectory>${project.build.directory}/generated-sources/</generatedSourcesDirectory> | ||
<annotationProcessors> | ||
<annotationProcessor>io.generation.tools.codegen.AnnotationProcessor</annotationProcessor> | ||
</annotationProcessors> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
``` | ||
2. Annotate your interfaces with `@ServiceInterface`, `@HttpRequestInformation` and | ||
`@UnexpectedResponseExceptionDetail` such annotations: | ||
```java | ||
@ServiceInterface(name = "ExampleClient", host = "{endpoint}/example") | ||
public interface ExampleService { | ||
@HttpRequestInformation(method = HttpMethod.GET, path = "/user/{userId}", expectedStatusCodes = { 200 }) | ||
@UnexpectedResponseExceptionDetail(exceptionTypeName = "CLIENT_AUTHENTICATION", statusCode = { 401 }) | ||
@UnexpectedResponseExceptionDetail(exceptionTypeName = "RESOURCE_NOT_FOUND", statusCode = { 404 }) | ||
@UnexpectedResponseExceptionDetail(exceptionTypeName = "RESOURCE_MODIFIED", statusCode = { 409 }) | ||
User getUser(@PathParam("userId") String userId); | ||
} | ||
``` | ||
|
||
3. Build your project and the plugin will generate an implementation of the annotated interface. | ||
The processor would generate an implementation: | ||
```java | ||
public class ExampleServiceImpl implements ExampleService { | ||
private static final ClientLogger LOGGER = new ClientLogger(OpenAIClientServiceImpl.class); | ||
|
||
private final HttpPipeline defaultPipeline; | ||
|
||
private final ObjectSerializer serializer; | ||
|
||
private final String endpoint; | ||
|
||
private final ExampleServiceVersion serviceVersion; | ||
|
||
private String apiVersion; | ||
|
||
public ExampleServiceImpl (HttpPipeline defaultPipeline, ObjectSerializer serializer, | ||
String endpoint, ExampleServiceVersion serviceVersion) { | ||
this.defaultPipeline = defaultPipeline; | ||
this.serializer = serializer; | ||
this.endpoint = endpoint; | ||
this.apiVersion = serviceVersion.getVersion(); | ||
this.serviceVersion = serviceVersion; | ||
} | ||
|
||
public String getEndpoint() { | ||
return endpoint; | ||
} | ||
|
||
public HttpPipeline getPipeline() { | ||
return defaultPipeline; | ||
} | ||
|
||
public ExampleServiceVersion getServiceVersion() { | ||
return serviceVersion; | ||
} | ||
|
||
private final HttpPipeline pipeline; | ||
|
||
public ExampleServiceImpl(HttpPipeline pipeline) { | ||
this.pipeline = pipeline; | ||
} | ||
|
||
public Response<BinaryData> getUser(String userId, Context context) { | ||
return getUser(endpoint, apiVersion, userId, context); | ||
} | ||
|
||
@Override | ||
private Response<BinaryData> getUser(String endpoint, String apiVersion, String userId, Context context) { | ||
HttpPipeline pipeline = this.getPipeline(); | ||
String host = endpoint + "/example/users/" + userId + "?api-version=" + apiVersion; | ||
|
||
// create the request | ||
HttpRequest httpRequest = new HttpRequest(HttpMethod.GET, host); | ||
|
||
// set the headers | ||
HttpHeaders headers = new HttpHeaders(); | ||
httpRequest.setHeaders(headers); | ||
|
||
// add RequestOptions to the request | ||
httpRequest.setRequestOptions(requestOptions); | ||
|
||
// set the body content if present | ||
|
||
// send the request through the pipeline | ||
Response<?> response = pipeline.send(httpRequest); | ||
|
||
final int responseCode = response.getStatusCode(); | ||
boolean expectedResponse = responseCode == 200; | ||
if (!expectedResponse) { | ||
throw new RuntimeException("Unexpected response code: " + responseCode); | ||
} | ||
ResponseBodyMode responseBodyMode = ResponseBodyMode.IGNORE; | ||
if (requestOptions != null) { | ||
responseBodyMode = requestOptions.getResponseBodyMode(); | ||
} | ||
if (responseBodyMode == ResponseBodyMode.DESERIALIZE) { | ||
BinaryData responseBody = response.getBody(); | ||
HttpResponseAccessHelper.setValue((HttpResponse<?>) response, responseBody); | ||
} else { | ||
BinaryData responseBody = response.getBody(); | ||
HttpResponseAccessHelper.setBodyDeserializer((HttpResponse<?>) response, (body) -> responseBody); | ||
} | ||
return (Response<BinaryData>) response; | ||
} | ||
} | ||
``` | ||
This implementation eliminates reflection and integrates directly with your HTTP client infrastructure. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>io.clientcore.tools</groupId> | ||
<artifactId>annotation-processor</artifactId> | ||
<version>1.0.0-beta.1</version> <!-- {x-version-update;io.clientcore.tools:annotation-processor;external_dependency} --> | ||
|
||
<name>Client Core Compile-Time Annotation Processor</name> | ||
<description>The client-core annotation processor for introducing compile-time code generation for libraries based on client core</description> | ||
|
||
<distributionManagement> | ||
<snapshotRepository> | ||
<id>ossrh</id> | ||
<url>https://oss.sonatype.org/content/repositories/snapshots</url> | ||
</snapshotRepository> | ||
<repository> | ||
<id>ossrh</id> | ||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> | ||
</repository> | ||
</distributionManagement> | ||
|
||
<url>https://github.com/azure/azure-sdk-for-java</url> | ||
<organization> | ||
<name>Microsoft Corporation</name> | ||
<url>http://microsoft.com</url> | ||
</organization> | ||
<licenses> | ||
<license> | ||
<name>The MIT License (MIT)</name> | ||
<url>http://opensource.org/licenses/MIT</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
|
||
<developers> | ||
<developer> | ||
<id>microsoft</id> | ||
<name>Microsoft Corporation</name> | ||
</developer> | ||
</developers> | ||
|
||
<issueManagement> | ||
<system>GitHub</system> | ||
<url>https://github.com/Azure/azure-sdk-for-java/issues</url> | ||
</issueManagement> | ||
|
||
<scm> | ||
<url>https://github.com/Azure/azure-sdk-for-java</url> | ||
<connection>scm:git:https://github.com/Azure/azure-sdk-for-java.git</connection> | ||
<developerConnection/> | ||
<tag>HEAD</tag> | ||
</scm> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<packageOutputDirectory>${project.build.directory}</packageOutputDirectory> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.squareup</groupId> | ||
<artifactId>javapoet</artifactId> | ||
<version>1.13.0</version> <!-- {x-version-update;com.squareup:javapoet;external_dependency} --> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.clientcore</groupId> | ||
<artifactId>core</artifactId> | ||
<version>1.0.0-beta.1</version> <!-- {x-version-update;io.clientcore:core;dependency} --> | ||
<scope>compile</scope> | ||
</dependency> | ||
|
||
<!-- Unit Test --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>5.11.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-api;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<version>5.11.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-engine;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
<version>5.11.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-params;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>4.11.0</version> <!-- {x-version-update;org.mockito:mockito-core;external_dependency} --> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
</resource> | ||
</resources> | ||
</build> | ||
</project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a note here, at least temporarily, that "this project is for experimentation and exploring new ideas that may or may not make it into a supported GA release."