-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(annotations): add command containers (#364)
This is the first part of the introduction of annotation processing to cloud. A new `@CommandContainer` annotation has been introduced, which can be placed on classes to have the annotation parser automatically construct & parse the classes when `AnnotationParser.parseContainers()` is invoked. A future PR will introduce another processor that will scan for `@CommandMethod` annotations and verify the integrity of the annotated methods (visibility, argument annotations, etc.).
- Loading branch information
1 parent
32198cf
commit ab849ff
Showing
15 changed files
with
543 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ plugins { | |
|
||
dependencies { | ||
implementation(projects.cloudCore) | ||
|
||
testImplementation(libs.compileTesting) | ||
} |
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
56 changes: 56 additions & 0 deletions
56
...tations/src/main/java/cloud/commandframework/annotations/processing/CommandContainer.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,56 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2021 Alexander Söderberg & Contributors | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
package cloud.commandframework.annotations.processing; | ||
|
||
import cloud.commandframework.annotations.AnnotationParser; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Indicates that the class contains | ||
* {@link cloud.commandframework.annotations.CommandMethod command metods}. | ||
* <p> | ||
* If using <i>cloud-annotations</i> as an annotation processor, then the class will | ||
* be listed in a special file under META-INF. These containers can be collectively | ||
* parsed using {@link AnnotationParser#parseContainers()}, which will create instances | ||
* of the containers and then call {@link AnnotationParser#parse(Object)} with the created instance. | ||
* <p> | ||
* Every class annotated with {@link CommandContainer} needs to be {@code public}, and it | ||
* also needs to have one of the following: | ||
* <ul> | ||
* <li>A {@code public} no-arg constructor</li> | ||
* <li>A {@code public} constructor with {@link AnnotationParser} as the sole parameter</li> | ||
* </ul> | ||
* <p> | ||
* <b>NOTE:</b> For container parsing to work, you need to make sure that <i>cloud-annotations</i> is added | ||
* as an annotation processor. | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface CommandContainer { | ||
String ANNOTATION_PATH = "cloud.commandframework.annotations.processing.CommandContainer"; | ||
|
||
} |
115 changes: 115 additions & 0 deletions
115
...rc/main/java/cloud/commandframework/annotations/processing/CommandContainerProcessor.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,115 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2021 Alexander Söderberg & Contributors | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
package cloud.commandframework.annotations.processing; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import javax.annotation.processing.AbstractProcessor; | ||
import javax.annotation.processing.RoundEnvironment; | ||
import javax.annotation.processing.SupportedAnnotationTypes; | ||
import javax.lang.model.SourceVersion; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.ElementKind; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.tools.Diagnostic; | ||
import javax.tools.StandardLocation; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
@SupportedAnnotationTypes(CommandContainer.ANNOTATION_PATH) | ||
public final class CommandContainerProcessor extends AbstractProcessor { | ||
|
||
/** | ||
* The file in which all command container names are stored. | ||
*/ | ||
public static final String PATH = "META-INF/commands/cloud.commandframework.annotations.processing.CommandContainer"; | ||
|
||
@Override | ||
public boolean process( | ||
final @NonNull Set<? extends TypeElement> annotations, | ||
final @NonNull RoundEnvironment roundEnv | ||
) { | ||
final List<String> validTypes = new ArrayList<>(); | ||
|
||
final Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(CommandContainer.class); | ||
if (elements.isEmpty()) { | ||
return false; // Nothing to process... | ||
} | ||
|
||
for (final Element element : elements) { | ||
if (element.getKind() != ElementKind.CLASS) { | ||
this.processingEnv.getMessager().printMessage( | ||
Diagnostic.Kind.ERROR, | ||
String.format( | ||
"@CommandMethod found on unsupported element type '%s' (%s)", | ||
element.getKind().name(), | ||
element.getSimpleName().toString() | ||
), | ||
element | ||
); | ||
return false; | ||
} | ||
|
||
element.accept(new CommandContainerVisitor(this.processingEnv, validTypes), null); | ||
} | ||
|
||
for (final String type : validTypes) { | ||
this.processingEnv.getMessager().printMessage( | ||
Diagnostic.Kind.NOTE, | ||
String.format( | ||
"Found valid @CommandMethod annotated class: %s", | ||
type | ||
) | ||
); | ||
} | ||
this.writeCommandFile(validTypes); | ||
|
||
// https://errorprone.info/bugpattern/DoNotClaimAnnotations | ||
return false; | ||
} | ||
|
||
@Override | ||
public SourceVersion getSupportedSourceVersion() { | ||
return SourceVersion.latest(); | ||
} | ||
|
||
@SuppressWarnings({"unused", "try"}) | ||
private void writeCommandFile(final @NonNull List<String> types) { | ||
try (BufferedWriter writer = new BufferedWriter(this.processingEnv.getFiler().createResource( | ||
StandardLocation.CLASS_OUTPUT, | ||
"", | ||
PATH | ||
).openWriter())) { | ||
for (final String t : types) { | ||
writer.write(t); | ||
writer.newLine(); | ||
} | ||
writer.flush(); | ||
} catch (final IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Oops, something went wrong.