Skip to content

Commit

Permalink
chore: Fix SimplePluginAnnotationParser unable to parse package priva…
Browse files Browse the repository at this point in the history
…te listeners + Added javadoc
  • Loading branch information
phinner committed Dec 17, 2023
1 parent 55e14e5 commit edb68ba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import mindustry.Vars;
Expand Down Expand Up @@ -68,7 +69,6 @@ public void dispose() {
private final PluginDescriptor descriptor = PluginDescriptor.from(this);
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final List<PluginListener> listeners = new ArrayList<>();
private final PluginAnnotationParser annotationParser = PluginAnnotationParser.simple(this);
private boolean canParseListeners = false;

@Override
Expand All @@ -81,15 +81,15 @@ public final Logger getLogger() {
return this.logger;
}

protected final PluginAnnotationParser getAnnotationParser() {
return this.annotationParser;
}

/**
* Returns an unmodifiable list of the listeners registered to this plugin.
*/
protected final List<PluginListener> getListeners() {
return List.copyOf(this.listeners);
return Collections.unmodifiableList(this.listeners);
}

protected PluginAnnotationParser getPluginAnnotationParser() {
return PluginAnnotationParser.simple(this);
}

@Override
Expand All @@ -99,7 +99,7 @@ public void addListener(final PluginListener listener) {
}
this.listeners.add(listener);
if (this.canParseListeners) {
this.annotationParser.parse(listener);
getPluginAnnotationParser().parse(listener);
}
}

Expand Down Expand Up @@ -156,9 +156,9 @@ public void init() {
AbstractMindustryPlugin.this.onLoad();
AbstractMindustryPlugin.this.forEachListener(PluginListener::onPluginLoad);
AbstractMindustryPlugin.this.canParseListeners = true;

AbstractMindustryPlugin.this.annotationParser.parse(AbstractMindustryPlugin.this);
AbstractMindustryPlugin.this.forEachListener(AbstractMindustryPlugin.this.annotationParser::parse);
final var parser = AbstractMindustryPlugin.this.getPluginAnnotationParser();
parser.parse(AbstractMindustryPlugin.this);
AbstractMindustryPlugin.this.forEachListener(parser::parse);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,28 @@
*/
package fr.xpdustry.distributor.api.plugin;

/**
* A plugin component responsible for scanning the plugin instance and its listener for annotations.
*/
public interface PluginAnnotationParser {

/**
* Creates a simple plugin annotation parser that will parse and register
* {@link fr.xpdustry.distributor.api.scheduler.TaskHandler task handlers} and
* {@link fr.xpdustry.distributor.api.event.EventHandler event handlers}.
*
* @param plugin The owning plugin
* @return the created plugin annotation parser
*/
static PluginAnnotationParser simple(final MindustryPlugin plugin) {
return new SimplePluginAnnotationParser(plugin);
}

/**
* Parses the given object for annotations related to the plugin.
*
* @param object The object to be scanned for annotations.
* It can be the plugin instance or its listener.
*/
void parse(final Object object);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ private void parseEvents(final Object object, final Method method) {
}
if (method.getParameterCount() != 1) {
throw new IllegalArgumentException("The event handler on " + method + " hasn't the right parameter count.");
} else if (!method.canAccess(object) || !method.trySetAccessible()) {
throw new RuntimeException("Unable to make " + method + " accessible.");
} else if (!method.canAccess(object)) {
method.setAccessible(true);
}

final var handler = new MethodEventHandler<>(object, method, plugin);
Expand All @@ -66,8 +66,8 @@ private void parseTasks(final Object object, final Method method) {
}
if (method.getParameterCount() > 1) {
throw new IllegalArgumentException("The event handler on " + method + " hasn't the right parameter count.");
} else if (!method.canAccess(object) || !method.trySetAccessible()) {
throw new RuntimeException("Unable to make " + method + " accessible.");
} else if (!method.canAccess(object)) {
method.setAccessible(true);
} else if (method.getParameterCount() == 1 && !Cancellable.class.equals(method.getParameterTypes()[0])) {
throw new IllegalArgumentException("The event handler on " + method + " hasn't the right parameter type.");
}
Expand All @@ -80,7 +80,7 @@ private void parseTasks(final Object object, final Method method) {
if (annotation.delay() > -1) {
builder.delay(annotation.delay(), annotation.unit());
}
builder.execute(new MethodPluginTask(object, method));
builder.execute(new MethodTaskHandler(object, method));
}

private static final class MethodEventHandler<E> implements Consumer<E> {
Expand Down Expand Up @@ -118,12 +118,12 @@ public Class<E> getEventType() {
}
}

private static final class MethodPluginTask implements Consumer<Cancellable> {
private static final class MethodTaskHandler implements Consumer<Cancellable> {

private final Object object;
private final Method method;

private MethodPluginTask(final Object object, final Method method) {
private MethodTaskHandler(final Object object, final Method method) {
this.object = object;
this.method = method;
}
Expand Down

0 comments on commit edb68ba

Please sign in to comment.