Skip to content

Commit

Permalink
feat(common): Removed PluginAware from PluginAnnotationScanner
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Apr 8, 2024
1 parent f69745e commit 77906f9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ final class LampCommandFacade extends CommandHandler.Command implements CommandF

final MindustryCommandHandler handler;
private final String name;
private final DescriptionFacade description;
private final DescriptionFacade descriptionFacade;
private final boolean alias;
private final boolean prefixed;

LampCommandFacade(
final MindustryCommandHandler handler,
final String name,
final DescriptionFacade description,
final DescriptionFacade descriptionFacade,
final boolean alias,
final boolean prefixed) {
super(
prefixed ? handler.getPlugin().getMetadata().getName() + ":" + name : name,
"[args...]",
description.getText(),
descriptionFacade.getText(),
new LampCommandRunner(name, handler));
this.handler = handler;
this.name = name;
this.description = description;
this.descriptionFacade = descriptionFacade;
this.alias = alias;
this.prefixed = prefixed;
}
Expand All @@ -67,7 +67,7 @@ public String getName() {

@Override
public DescriptionFacade getDescription() {
return this.description;
return this.descriptionFacade;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,23 @@
*/
package com.xpdustry.distributor.annotation;

import com.xpdustry.distributor.plugin.MindustryPlugin;
import java.util.List;
import java.util.Optional;

final class ListPluginAnnotationScanner implements PluginAnnotationScanner<List<?>> {

private final MindustryPlugin plugin;
private final List<PluginAnnotationScanner<?>> scanners;

ListPluginAnnotationScanner(final MindustryPlugin plugin, final List<PluginAnnotationScanner<?>> scanners) {
this.plugin = plugin;
ListPluginAnnotationScanner(final List<PluginAnnotationScanner<?>> scanners) {
this.scanners = scanners;
}

@Override
public List<?> scan(final Object instance) {
return this.scanners.stream().map(scanner -> scanner.scan(instance)).toList();
}

@Override
public MindustryPlugin getPlugin() {
return this.plugin;
public Optional<List<?>> scan(final Object instance) {
return Optional.of(this.scanners.stream()
.map(scanner -> scanner.scan(instance))
.filter(Optional::isPresent)
.map(Optional::get)
.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,28 @@
*/
package com.xpdustry.distributor.annotation;

import com.xpdustry.distributor.plugin.MindustryPlugin;
import com.xpdustry.distributor.plugin.PluginAware;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

public interface PluginAnnotationScanner<R> extends PluginAware {
@FunctionalInterface
public interface PluginAnnotationScanner<R> {

static PluginAnnotationScanner<List<?>> list(
final MindustryPlugin plugin, final PluginAnnotationScanner<?>... scanners) {
return new ListPluginAnnotationScanner(plugin, List.of(scanners));
static PluginAnnotationScanner<Void> consume(final Consumer<Object> consumer) {
return instance -> {
consumer.accept(instance);
return Optional.empty();
};
}

R scan(final Object instance);
static PluginAnnotationScanner<List<?>> list(final PluginAnnotationScanner<?>... scanners) {
return new ListPluginAnnotationScanner(List.of(scanners));
}

static PluginAnnotationScanner<List<?>> list(final Collection<PluginAnnotationScanner<?>> scanners) {
return new ListPluginAnnotationScanner(List.copyOf(scanners));
}

Optional<R> scan(final Object instance);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@
import com.xpdustry.distributor.DistributorProvider;
import com.xpdustry.distributor.event.EventSubscription;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.function.Consumer;

final class EventHandlerProcessor implements MethodAnnotationScanner.Processor<EventHandler, EventSubscription> {

@Override
public EventSubscription process(final MethodAnnotationScanner.Context<EventHandler> context) {
public Optional<EventSubscription> process(final MethodAnnotationScanner.Context<EventHandler> context) {
if (context.getMethod().getParameterCount() != 1) {
throw new IllegalArgumentException(
"The event handler on " + context.getMethod() + " hasn't the right parameter count.");
} else if (!context.getMethod().canAccess(context.getInstance())) {
context.getMethod().setAccessible(true);
}
final var handler = new MethodEventHandler<>(context.getInstance(), context.getMethod());
return DistributorProvider.get()
return Optional.of(DistributorProvider.get()
.getEventBus()
.subscribe(handler.getEventType(), context.getAnnotation().priority(), context.getPlugin(), handler);
.subscribe(handler.getEventType(), context.getAnnotation().priority(), context.getPlugin(), handler));
}

@SuppressWarnings("ClassCanBeRecord")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
import com.xpdustry.distributor.event.EventSubscription;
import com.xpdustry.distributor.internal.DistributorDataClass;
import com.xpdustry.distributor.plugin.MindustryPlugin;
import com.xpdustry.distributor.plugin.PluginAware;
import com.xpdustry.distributor.scheduler.Cancellable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.immutables.value.Value;
import org.jspecify.annotations.Nullable;

public interface MethodAnnotationScanner extends PluginAnnotationScanner<MethodAnnotationScanner.Result> {
public interface MethodAnnotationScanner extends PluginAnnotationScanner<MethodAnnotationScanner.Result>, PluginAware {

Key<TaskHandler, Cancellable> TASK_HANDLER_KEY = Key.of(TaskHandler.class, Cancellable.class);

Expand All @@ -56,7 +57,7 @@ default <I extends Annotation, O> MethodAnnotationScanner register(
@FunctionalInterface
interface Processor<I extends Annotation, O> {

@Nullable O process(final Context<I> context);
Optional<O> process(final Context<I> context);
}

@DistributorDataClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

final class MethodAnnotationScannerImpl implements MethodAnnotationScanner {

Expand All @@ -44,20 +45,20 @@ public <I extends Annotation, O> MethodAnnotationScanner register(final KeyWithP

@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public Result scan(final Object instance) {
public Optional<Result> scan(final Object instance) {
final var builder = Result.builder();
for (final var method : instance.getClass().getDeclaredMethods()) {
for (final var annotation : method.getDeclaredAnnotations()) {
final var pairs = processors.get(annotation.annotationType());
if (pairs == null) continue;
for (final KeyWithProcessor pair : pairs) {
final var output = pair.getProcessor().process(Context.of(instance, method, annotation, plugin));
if (output == null) continue;
builder.addOutput(pair.getKey(), output);
if (output.isEmpty()) continue;
builder.addOutput(pair.getKey(), output.get());
}
}
}
return builder.build();
return Optional.of(builder.build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import com.xpdustry.distributor.scheduler.Cancellable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.function.Consumer;

final class TaskHandlerProcessor implements MethodAnnotationScanner.Processor<TaskHandler, Cancellable> {

@Override
public Cancellable process(final MethodAnnotationScanner.Context<TaskHandler> context) {
public Optional<Cancellable> process(final MethodAnnotationScanner.Context<TaskHandler> context) {
if (context.getMethod().getParameterCount() > 1) {
throw new IllegalArgumentException(
"The event handler on " + context.getMethod() + " hasn't the right parameter count.");
Expand All @@ -52,7 +53,7 @@ public Cancellable process(final MethodAnnotationScanner.Context<TaskHandler> co
context.getAnnotation().interval(), context.getAnnotation().unit());
}

return builder.execute(new MethodTaskHandler(context.getInstance(), context.getMethod()));
return Optional.of(builder.execute(new MethodTaskHandler(context.getInstance(), context.getMethod())));
}

@SuppressWarnings("ClassCanBeRecord")
Expand Down

0 comments on commit 77906f9

Please sign in to comment.