-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from funkyFangs/SC-2-migrate-to-custom-annotati…
…on-processor SC-2: replace Reflections with compile time annotation processor
- Loading branch information
Showing
23 changed files
with
196 additions
and
305 deletions.
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
12 changes: 6 additions & 6 deletions
12
...vc-demo/src/main/java/io/funky/fangs/springdoc/customizer/pets/examples/PetsExamples.java
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
3 changes: 1 addition & 2 deletions
3
...ustomizer/annotations/ExampleDetails.java → ...customizer/annotation/ExampleDetails.java
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
2 changes: 1 addition & 1 deletion
2
...customizer/annotations/ExampleMethod.java → .../customizer/annotation/ExampleMethod.java
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
2 changes: 1 addition & 1 deletion
2
...customizer/annotations/ExampleTarget.java → .../customizer/annotation/ExampleTarget.java
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
2 changes: 1 addition & 1 deletion
2
...c/customizer/annotations/ExampleType.java → ...oc/customizer/annotation/ExampleType.java
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
80 changes: 80 additions & 0 deletions
80
...omizer/src/main/java/io/funky/fangs/springdoc/customizer/processor/ExamplesProcessor.java
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,80 @@ | ||
package io.funky.fangs.springdoc.customizer.processor; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.collect.Sets; | ||
|
||
import javax.annotation.processing.*; | ||
import javax.lang.model.SourceVersion; | ||
import javax.lang.model.element.Modifier; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.element.VariableElement; | ||
import javax.tools.FileObject; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static javax.tools.StandardLocation.CLASS_OUTPUT; | ||
|
||
@SupportedAnnotationTypes("io.funky.fangs.springdoc.customizer.annotation.ExampleDetails") | ||
@SupportedSourceVersion(SourceVersion.RELEASE_19) | ||
public class ExamplesProcessor extends AbstractProcessor { | ||
private static final Set<Modifier> EXAMPLE_MODIFIERS = Sets.immutableEnumSet(Modifier.STATIC, Modifier.FINAL); | ||
|
||
public static final String EXAMPLES_FILE_NAME = "examples.json"; | ||
public static final TypeReference<Map<String, Set<String>>> EXAMPLES_MAP_TYPE_REFERENCE = new TypeReference<>() {}; | ||
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
||
private FileObject examplesResourceFile; | ||
|
||
@Override | ||
public synchronized void init(ProcessingEnvironment processingEnv) { | ||
super.init(processingEnv); | ||
|
||
try { | ||
examplesResourceFile = processingEnv.getFiler() | ||
.createResource(CLASS_OUTPUT, getClass().getPackageName(), EXAMPLES_FILE_NAME, | ||
processingEnv.getElementUtils().getTypeElement(getClass().getCanonicalName())); | ||
} | ||
catch (IOException ioException) { | ||
processingEnv.getMessager().printError(ioException.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | ||
// Read map from file | ||
Map<String, Set<String>> elements; | ||
try { | ||
elements = OBJECT_MAPPER.readValue(examplesResourceFile.getCharContent(false).toString(), | ||
EXAMPLES_MAP_TYPE_REFERENCE); | ||
} | ||
catch (IllegalStateException | IOException exception) { | ||
// Map is likely not serialized yet, so create empty one | ||
elements = new HashMap<>(); | ||
} | ||
|
||
// Add fields to map | ||
for (var annotation : annotations) { | ||
for (var element : roundEnv.getElementsAnnotatedWith(annotation)) { | ||
if (element.getModifiers().containsAll(EXAMPLE_MODIFIERS) | ||
&& element instanceof VariableElement fieldElement | ||
&& fieldElement.getEnclosingElement() instanceof TypeElement classElement) { | ||
elements.computeIfAbsent(classElement.getQualifiedName().toString(), ignored -> new HashSet<>()) | ||
.add(fieldElement.getSimpleName().toString()); | ||
} | ||
} | ||
} | ||
|
||
// Write map to file | ||
try (var outputStream = examplesResourceFile.openOutputStream()) { | ||
outputStream.write(OBJECT_MAPPER.writeValueAsBytes(elements)); | ||
} | ||
catch (IOException ignored) { | ||
} | ||
|
||
return true; | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...omizer/utilities/ReflectionUtilities.java → ...stomizer/utility/ReflectionUtilities.java
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
2 changes: 1 addition & 1 deletion
2
...er/utilities/RequestMappingUtilities.java → ...izer/utility/RequestMappingUtilities.java
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
1 change: 1 addition & 0 deletions
1
...doc-customizer/src/main/resources/META-INF/services/javax.annotation.processing.Processor
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 @@ | ||
io.funky.fangs.springdoc.customizer.processor.ExamplesProcessor |
Oops, something went wrong.