-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add Compile-time annotation-processor tool to clientcore #43457
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4683e22
Add compile time codegen tool
samvaity 149a750
update naming and groupid
samvaity ddf15fb
update for unknown words
samvaity a6ed313
skip for readme
samvaity cdea14e
add readme note
samvaity daaf8d1
review comments
samvaity 270b2f0
update for path builder tests
samvaity 89e7c8e
Merge branch 'main' into compile-time-codegen
samvaity 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 hidden or 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 hidden or 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 hidden or 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,14 @@ | ||
| <!-- Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the MIT License. --> | ||
| <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</groupId> | ||
| <artifactId>clientcore-tools-service</artifactId> | ||
samvaity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <packaging>pom</packaging> | ||
| <version>1.0.0</version> <!-- Need not change for every release--> | ||
| <modules> | ||
| <module>sdk-codegen-tool</module> | ||
| </modules> | ||
| </project> | ||
This file contains hidden or 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,135 @@ | ||
| # Codegen Compile time generator Plugin | ||
samvaity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| A Java annotation processor for generating HTTP service implementations based on annotated interfaces. | ||
samvaity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Usage | ||
|
|
||
| 1. Add the plugin dependency: | ||
| ```xml | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>io.clientcore.tools</groupId> | ||
| <artifactId>sdk-codegen-tool</artifactId> | ||
samvaity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <version>1.0.0.beta.1</version> <!-- {x-version-update;io.clientcore.tools:sdk-codegen-tool;external_dependency} --> | ||
| <scope>provided</scope> | ||
samvaity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </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 OpenAIServiceVersion serviceVersion; | ||
|
|
||
| private String apiVersion; | ||
|
|
||
| public OpenAIClientServiceImpl(HttpPipeline defaultPipeline, ObjectSerializer serializer, | ||
| String endpoint, OpenAIServiceVersion 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 OpenAIServiceVersion 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 hidden or 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>sdk-codegen-tool</artifactId> | ||
| <version>1.0.0-beta.1</version> <!-- {x-version-update;io.clientcore.tools:sdk-codegen-tool;external_dependency} --> | ||
|
|
||
| <name>Clientcore codegen tool</name> | ||
| <description>A Java annotation processor tool for generating HTTP service implementations</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.
Uh oh!
There was an error while loading. Please reload this page.