Skip to content

Commit 970f239

Browse files
authored
Add diagnostic/logging messages for annotation processor (Azure#45116)
1 parent c4e3c61 commit 970f239

File tree

6 files changed

+66
-35
lines changed

6 files changed

+66
-35
lines changed

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/implementation/SimpleXmlSerializableService.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ static SimpleXmlSerializableService getNewInstance(HttpPipeline pipeline) {
6767
/**
6868
* Retrieves an invalid XML payload.
6969
* @param contentType The content type of the invalid XML payload.
70-
*
71-
* @param contentType The content type of the XML payload.
7270
* @return The retrieved invalid XML payload.
7371
*/
7472
@HttpRequestInformation(method = HttpMethod.GET, path = "getInvalidXml", expectedStatusCodes = { 200 })

sdk/clientcore/annotation-processor/src/main/java/io/clientcore/annotation/processor/AnnotationProcessor.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import javax.lang.model.element.ExecutableElement;
3737
import javax.lang.model.element.TypeElement;
3838
import javax.lang.model.element.VariableElement;
39+
import javax.tools.Diagnostic;
3940

4041
/**
4142
* Annotation processor that generates client code based on annotated interfaces.
@@ -56,18 +57,28 @@ public SourceVersion getSupportedSourceVersion() {
5657

5758
@Override
5859
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
59-
// We iterate through each interface annotated with @ServiceInterface separately.
60-
// This outer for-loop is not strictly necessary, as we only have one annotation that we care about
61-
// (@ServiceInterface), but we'll leave it here for now
62-
annotations.stream()
63-
.map(roundEnv::getElementsAnnotatedWith)
64-
.flatMap(Set::stream)
60+
// Gather all elements to process
61+
Set<? extends Element> elementsToProcess = annotations.stream()
62+
.flatMap(annotation -> roundEnv.getElementsAnnotatedWith(annotation).stream())
6563
.filter(element -> element.getKind().isInterface())
66-
.forEach(element -> {
67-
if (element.getAnnotation(ServiceInterface.class) != null) {
68-
this.processServiceInterface(element);
69-
}
70-
});
64+
.collect(Collectors.toSet());
65+
66+
if (elementsToProcess.isEmpty()) {
67+
// No interfaces to process in this round; skip logging
68+
return false;
69+
}
70+
71+
processingEnv.getMessager()
72+
.printMessage(Diagnostic.Kind.NOTE,
73+
"[Clientcore SDK AnnotationProcessor] Starting annotation processing for service interfaces.");
74+
75+
for (Element element : elementsToProcess) {
76+
this.processServiceInterface(element);
77+
}
78+
79+
processingEnv.getMessager()
80+
.printMessage(Diagnostic.Kind.NOTE,
81+
"[Clientcore SDK AnnotationProcessor] Completed annotation processing.");
7182

7283
return true;
7384
}
@@ -76,7 +87,9 @@ private void processServiceInterface(Element serviceInterface) {
7687
if (serviceInterface == null || serviceInterface.getKind() != ElementKind.INTERFACE) {
7788
throw new IllegalArgumentException("Invalid service interface provided.");
7889
}
79-
90+
processingEnv.getMessager()
91+
.printMessage(Diagnostic.Kind.NOTE,
92+
"Generating client implementation for: " + serviceInterface.asType().toString());
8093
TemplateInput templateInput = new TemplateInput();
8194

8295
// Determine the fully qualified name (FQN) and package name
@@ -124,8 +137,6 @@ private void processServiceInterface(Element serviceInterface) {
124137

125138
// Process the template
126139
TemplateProcessor.getInstance().process(templateInput, processingEnv);
127-
128-
// Additional formatting or logging if necessary
129140
}
130141

131142
private void addImports(TemplateInput templateInput) {
@@ -177,13 +188,13 @@ private HttpRequestContext createHttpRequestContext(ExecutableElement requestMet
177188
} else if (headerParam != null) {
178189
// Only add header param if the key is not already present (e.g., set by static header params)
179190
String key = headerParam.value();
180-
if (method.getHeaders() == null || !method.getHeaders().containsKey(key)) {
191+
if (!method.getHeaders().containsKey(key)) {
181192
method.addHeader(headerParam.value(), param.getSimpleName().toString());
182193
}
183194
} else if (queryParam != null) {
184195
// Only add query param if the key is not already present (e.g., set by static query params)
185196
String key = queryParam.value();
186-
if (method.getQueryParams() == null || !method.getQueryParams().containsKey(key)) {
197+
if (!method.getQueryParams().containsKey(key)) {
187198
method.addQueryParam(key, param.getSimpleName().toString(), queryParam.multipleQueryParams(),
188199
!queryParam.encoded());
189200
}
@@ -205,7 +216,7 @@ private HttpRequestContext createHttpRequestContext(ExecutableElement requestMet
205216
return method;
206217
}
207218

208-
private static String getHost(HttpRequestContext method) {
219+
private String getHost(HttpRequestContext method) {
209220
String path = method.getPath();
210221
// Set the path after host, concatenating the path segment in the host.
211222
if (path != null && !path.isEmpty() && !"/".equals(path)) {

sdk/clientcore/annotation-processor/src/main/java/io/clientcore/annotation/processor/templating/JavaParserTemplateProcessor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import javax.annotation.processing.ProcessingEnvironment;
5050
import javax.lang.model.type.TypeKind;
5151
import javax.lang.model.type.TypeMirror;
52+
import javax.tools.Diagnostic;
5253

5354
import static io.clientcore.annotation.processor.utils.ResponseHandler.generateResponseHandling;
5455

@@ -102,8 +103,11 @@ public void process(TemplateInput templateInput, ProcessingEnvironment processin
102103
addCopyrightComments();
103104
setPackageDeclaration(packageName);
104105
createClass(serviceInterfaceImplShortName, serviceInterfaceShortName, templateInput, processingEnv);
105-
106+
processingEnv.getMessager()
107+
.printMessage(Diagnostic.Kind.NOTE, "Writing generated source file for: " + serviceInterfaceImplShortName);
106108
writeFile(packageName, serviceInterfaceImplShortName, processingEnv);
109+
processingEnv.getMessager()
110+
.printMessage(Diagnostic.Kind.NOTE, "Completed code generation for: " + serviceInterfaceImplShortName);
107111
}
108112

109113
void addImports(TemplateInput templateInput) {
@@ -260,7 +264,9 @@ private void writeFile(String packageName, String serviceInterfaceImplShortName,
260264
fileWriter.write(compilationUnit.toString());
261265
fileWriter.flush();
262266
} catch (IOException e) {
263-
e.printStackTrace();
267+
processingEnv.getMessager()
268+
.printMessage(Diagnostic.Kind.ERROR, "Failed to write generated source file for "
269+
+ serviceInterfaceImplShortName + ": " + e.getMessage());
264270
}
265271
}
266272

sdk/clientcore/annotation-processor/src/main/java/io/clientcore/annotation/processor/utils/RequestBodyHandler.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public final class RequestBodyHandler {
3030
* @param body The BlockStmt to which the statements are added.
3131
* @param requestBody The request body context containing parameter type and content type.
3232
* @param processingEnv The processing environment providing utility methods for operating on program elements and types.
33-
* @return true if serialization format is set and used in the request body, false otherwise.
33+
* @return true if a serialization format is set and used in the request body, false otherwise.
3434
*/
3535
public static boolean configureRequestBody(BlockStmt body, HttpRequestContext.Body requestBody,
3636
ProcessingEnvironment processingEnv) {
@@ -83,17 +83,6 @@ public static void addBinaryDataRequestBody(BlockStmt body, String parameterName
8383
+ "httpRequest.setBody(binaryData); }"));
8484
}
8585

86-
/**
87-
* Adds a JSON request body to the HTTP request.
88-
*
89-
* @param body The block statement to which the request body is added.
90-
* @param parameterName The name of the parameter.
91-
*/
92-
public static void addJsonRequestBody(BlockStmt body, String parameterName) {
93-
body.addStatement(StaticJavaParser.parseStatement(
94-
String.format("httpRequest.setBody(BinaryData.fromObject(%s, jsonSerializer));", parameterName)));
95-
}
96-
9786
/**
9887
* Checks if the given parameter type is a byte array.
9988
*

sdk/clientcore/annotation-processor/src/main/java/io/clientcore/annotation/processor/utils/ResponseHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public final class ResponseHandler {
3434
* @param body the method builder to append generated code.
3535
* @param returnType the return type of the method.
3636
* @param method whether request options are used.
37-
* @param serializationFormatSet indicates if serialization format is set.
37+
* @param serializationFormatSet indicates if a serialization format is set.
3838
*/
3939
public static void generateResponseHandling(BlockStmt body, TypeMirror returnType, HttpRequestContext method,
4040
boolean serializationFormatSet) {

sdk/clientcore/annotation-processor/src/test/java/io/clientcore/annotation/processor/mocks/MockProcessingEnvironment.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
import javax.annotation.processing.Messager;
77
import javax.annotation.processing.ProcessingEnvironment;
88
import javax.lang.model.SourceVersion;
9+
import javax.lang.model.element.AnnotationMirror;
10+
import javax.lang.model.element.AnnotationValue;
11+
import javax.lang.model.element.Element;
912
import javax.lang.model.util.Elements;
1013
import javax.lang.model.util.Types;
1114
import java.util.Collections;
1215
import java.util.Locale;
1316
import java.util.Map;
17+
import javax.tools.Diagnostic;
1418

1519
/**
1620
* Mock implementation of {@link ProcessingEnvironment}.
@@ -41,7 +45,7 @@ public Map<String, String> getOptions() {
4145

4246
@Override
4347
public Messager getMessager() {
44-
return null;
48+
return new MockMessager();
4549
}
4650

4751
@Override
@@ -78,4 +82,27 @@ public SourceVersion getSourceVersion() {
7882
public Locale getLocale() {
7983
return null;
8084
}
85+
86+
private static class MockMessager implements Messager {
87+
@Override
88+
public void printMessage(Diagnostic.Kind kind, CharSequence msg) {
89+
// No-op for mock
90+
}
91+
92+
@Override
93+
public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e) {
94+
// No-op for mock
95+
}
96+
97+
@Override
98+
public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a) {
99+
// No-op for mock
100+
}
101+
102+
@Override
103+
public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a,
104+
AnnotationValue v) {
105+
// No-op for mock
106+
}
107+
}
81108
}

0 commit comments

Comments
 (0)