generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
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
feat: use JavaDoc as comments #2481
Merged
Merged
Changes from all commits
Commits
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
120 changes: 120 additions & 0 deletions
120
...ntime/java/runtime/src/main/java/xyz/block/ftl/runtime/processor/AnnotationProcessor.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,120 @@ | ||
package xyz.block.ftl.runtime.processor; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.annotation.processing.Completion; | ||
import javax.annotation.processing.ProcessingEnvironment; | ||
import javax.annotation.processing.Processor; | ||
import javax.annotation.processing.RoundEnvironment; | ||
import javax.lang.model.SourceVersion; | ||
import javax.lang.model.element.AnnotationMirror; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.ExecutableElement; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.tools.Diagnostic; | ||
import javax.tools.FileObject; | ||
import javax.tools.StandardLocation; | ||
|
||
import xyz.block.ftl.Verb; | ||
|
||
/** | ||
* POC annotation processor for capturing JavaDoc, this needs a lot more work. | ||
*/ | ||
public class AnnotationProcessor implements Processor { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this run as part of the module JVM instance, or as part of the build? |
||
private static final Pattern REMOVE_LEADING_SPACE = Pattern.compile("^ ", Pattern.MULTILINE); | ||
private ProcessingEnvironment processingEnv; | ||
|
||
final Map<String, String> saved = new HashMap<>(); | ||
|
||
@Override | ||
public Set<String> getSupportedOptions() { | ||
return Set.of(); | ||
} | ||
|
||
@Override | ||
public Set<String> getSupportedAnnotationTypes() { | ||
return Set.of(Verb.class.getName()); | ||
} | ||
|
||
@Override | ||
public SourceVersion getSupportedSourceVersion() { | ||
return SourceVersion.latestSupported(); | ||
} | ||
|
||
@Override | ||
public void init(ProcessingEnvironment processingEnv) { | ||
this.processingEnv = processingEnv; | ||
} | ||
|
||
@Override | ||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | ||
//TODO: @VerbName, HTTP, CRON etc | ||
roundEnv.getElementsAnnotatedWith(Verb.class) | ||
.forEach(element -> { | ||
Optional<String> javadoc = getJavadoc(element); | ||
javadoc.ifPresent(doc -> saved.put(element.getSimpleName().toString(), doc)); | ||
}); | ||
|
||
if (roundEnv.processingOver()) { | ||
write("META-INF/ftl-verbs.txt", saved.entrySet().stream().map( | ||
e -> e.getKey() + "=" + Base64.getEncoder().encodeToString(e.getValue().getBytes(StandardCharsets.UTF_8))) | ||
.collect(Collectors.toSet())); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* This method uses the annotation processor Filer API and we shouldn't use a Path as paths containing \ are not supported. | ||
*/ | ||
public void write(String filePath, Set<String> set) { | ||
if (set.isEmpty()) { | ||
return; | ||
} | ||
try { | ||
final FileObject listResource = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", | ||
filePath.toString()); | ||
|
||
try (BufferedWriter writer = new BufferedWriter( | ||
new OutputStreamWriter(listResource.openOutputStream(), StandardCharsets.UTF_8))) { | ||
for (String className : set) { | ||
writer.write(className); | ||
writer.newLine(); | ||
} | ||
} | ||
} catch (IOException e) { | ||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to write " + filePath + ": " + e); | ||
return; | ||
} | ||
} | ||
|
||
@Override | ||
public Iterable<? extends Completion> getCompletions(Element element, AnnotationMirror annotation, ExecutableElement member, | ||
String userText) { | ||
return null; | ||
} | ||
|
||
public Optional<String> getJavadoc(Element e) { | ||
String docComment = processingEnv.getElementUtils().getDocComment(e); | ||
|
||
if (docComment == null || docComment.isBlank()) { | ||
return Optional.empty(); | ||
} | ||
|
||
// javax.lang.model keeps the leading space after the "*" so we need to remove it. | ||
|
||
return Optional.of(REMOVE_LEADING_SPACE.matcher(docComment) | ||
.replaceAll("") | ||
.trim()); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...e/java/runtime/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 @@ | ||
xyz.block.ftl.runtime.processor.AnnotationProcessor |
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.
Just to make sure you know, most declarations in the FTL schema can include comments - data structures, enums, etc.