generated from xpdustry/template-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Greatly simplified the plugin annotation processing API
- Loading branch information
Showing
11 changed files
with
223 additions
and
341 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
52 changes: 52 additions & 0 deletions
52
...-api/src/main/java/com/xpdustry/distributor/api/annotation/MethodAnnotationProcessor.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,52 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.api.annotation; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
abstract class MethodAnnotationProcessor<A extends Annotation, R, O> implements PluginAnnotationProcessor<O> { | ||
|
||
private final Class<A> annotationType; | ||
|
||
protected MethodAnnotationProcessor(final Class<A> annotationType) { | ||
this.annotationType = annotationType; | ||
} | ||
|
||
@SuppressWarnings({"unchecked"}) | ||
@Override | ||
public final Optional<O> process(final Object instance) { | ||
final List<R> results = new ArrayList<>(); | ||
for (final var method : instance.getClass().getDeclaredMethods()) { | ||
for (final var annotation : method.getDeclaredAnnotations()) { | ||
if (this.annotationType != annotation.annotationType()) continue; | ||
results.add(this.scan(instance, method, (A) annotation)); | ||
} | ||
} | ||
return Optional.of(reduce(Collections.unmodifiableList(results))); | ||
} | ||
|
||
protected abstract R scan(final Object instance, final Method method, final A annotation); | ||
|
||
protected abstract O reduce(final List<R> results); | ||
} |
107 changes: 107 additions & 0 deletions
107
...-api/src/main/java/com/xpdustry/distributor/api/annotation/PluginAnnotationProcessor.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,107 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.xpdustry.distributor.api.annotation; | ||
|
||
import com.xpdustry.distributor.api.event.EventSubscription; | ||
import com.xpdustry.distributor.api.plugin.MindustryPlugin; | ||
import com.xpdustry.distributor.api.scheduler.Cancellable; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* A centralized mechanism for processing annotations of plugin objects. | ||
* | ||
* @param <R> the result type of the processor | ||
*/ | ||
@FunctionalInterface | ||
public interface PluginAnnotationProcessor<R> { | ||
|
||
/** | ||
* Processes the {@link EventHandler} method annotations of a given object to create event listeners. | ||
* Returns a {@link EventSubscription} tied to all created event listeners for unsubscription. | ||
* | ||
* @param plugin the owning plugin | ||
* @return the processor | ||
*/ | ||
static PluginAnnotationProcessor<EventSubscription> events(final MindustryPlugin plugin) { | ||
return new EventHandlerProcessor(plugin); | ||
} | ||
|
||
/** | ||
* Processes the {@link TaskHandler} method annotations of a given object to create tasks. | ||
* Returns a {@link Cancellable} tied to all created tasks for cancellation. | ||
* | ||
* @param plugin the owning plugin | ||
* @return the processor | ||
*/ | ||
static PluginAnnotationProcessor<Cancellable> tasks(final MindustryPlugin plugin) { | ||
return new TaskHandlerProcessor(plugin); | ||
} | ||
|
||
/** | ||
* Creates a processor with no result. | ||
* | ||
* @param consumer the consumer | ||
* @return the scanner | ||
*/ | ||
static PluginAnnotationProcessor<Void> consuming(final Consumer<Object> consumer) { | ||
return instance -> { | ||
consumer.accept(instance); | ||
return Optional.empty(); | ||
}; | ||
} | ||
|
||
/** | ||
* Creates a processor that composes multiple processors. | ||
* The result is a list of the results of the processors. | ||
* | ||
* @param scanners the processors | ||
* @return the composed processor | ||
*/ | ||
static PluginAnnotationProcessor<List<?>> compose(final PluginAnnotationProcessor<?>... scanners) { | ||
return compose(Arrays.asList(scanners)); | ||
} | ||
|
||
/** | ||
* Creates a processor that composes multiple processors. | ||
* The result is a list of the results of the processors. | ||
* | ||
* @param scanners the processors | ||
* @return the composed processor | ||
*/ | ||
static PluginAnnotationProcessor<List<?>> compose(final Collection<PluginAnnotationProcessor<?>> scanners) { | ||
return new CompositePluginAnnotationProcessor(scanners.stream() | ||
.flatMap(scanner -> scanner instanceof CompositePluginAnnotationProcessor composite | ||
? composite.processors().stream() | ||
: Stream.of(scanner)) | ||
.toList()); | ||
} | ||
|
||
/** | ||
* Processes the annotations of the given object. | ||
* | ||
* @param instance the object | ||
* @return the result of the processing | ||
*/ | ||
Optional<R> process(final Object instance); | ||
} |
45 changes: 0 additions & 45 deletions
45
...on-api/src/main/java/com/xpdustry/distributor/api/annotation/PluginAnnotationScanner.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.