Allow event handlers with non-void return type

Also removed unnecessary files from the Event Bus Proc JAR and
configured GPG signing as well as deployment to Sonatype OSSRH.
This commit is contained in:
Kai S. K. Engelbart 2021-02-17 08:56:42 +01:00
parent ff35e7f37d
commit 4a5b94a9b7
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
6 changed files with 59 additions and 32 deletions

View File

@ -5,13 +5,13 @@
This library allows passing events between different objects without them having a direct reference to each other. This library allows passing events between different objects without them having a direct reference to each other.
Any object can serve as an event. Any object can serve as an event.
Using an instance of the `EventBus` class, an instant of the event class can be dispatched. Using an instance of the `EventBus` class, an instance of the event class can be dispatched.
This means that it will be forwarded to all listeners registered for it at the event bus. This means that it will be forwarded to all listeners registered for it at the event bus.
In addition, a singleton instance of the event bus is provided by the `EventBus#getInstance()` method. In addition, a singleton instance of the event bus is provided by the `EventBus#getInstance()` method.
To listen to events, register event handling methods using the `Event` annotation. To listen to events, register event handling methods using the `Event` annotation.
For this to work, the method must have a return type of `void` and declare a single parameter of the desired event type. For this to work, the method must declare a single parameter of the desired event type.
Alternatively, a parameter-less event handler can be declared as shown [below](#parameter-less-event-handlers). Alternatively, a parameter-less event handler can be declared as shown [below](#parameter-less-event-handlers).
## A Simple Example ## A Simple Example
@ -125,21 +125,14 @@ As event handlers are ordered by priority, it is not defined which of them will
## Installation ## Installation
Event Bus is currently hosted at [kske.dev](https://kske.dev/maven-repo/dev/kske/event-bus/). Event Bus is available in Maven Central.
To include it inside your project, just add the Maven repository and the dependency to your `pom.xml`: To include it inside your project, just add the following dependency to your `pom.xml`:
```xml ```xml
<repositories>
<repository>
<id>kske-repo</id>
<url>https://kske.dev/maven-repo</url>
</repository>
</repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>dev.kske</groupId> <groupId>dev.kske</groupId>
<artifactId>event-bus</artifactId> <artifactId>event-bus-core</artifactId>
<version>1.0.0</version> <version>1.0.0</version>
</dependency> </dependency>
</dependencies> </dependencies>
@ -175,7 +168,7 @@ When using Maven, it can be registered using the Maven Compiler Plugin:
<annotationProcessorPaths> <annotationProcessorPaths>
<annotationProcessorPath> <annotationProcessorPath>
<groupId>dev.kske</groupId> <groupId>dev.kske</groupId>
<artifactId>event-bus-ap</artifactId> <artifactId>event-bus-proc</artifactId>
<version>1.0.0</version> <version>1.0.0</version>
</annotationProcessorPath> </annotationProcessorPath>
</annotationProcessorPaths> </annotationProcessorPaths>
@ -183,4 +176,4 @@ When using Maven, it can be registered using the Maven Compiler Plugin:
</plugin> </plugin>
``` ```
Alternatively, a JAR file containing the processor is offered with each release for the use within IDEs and environments without Maven support. Alternatively, a JAR file containing the processor is offered with each release for the use within IDEs and environments without Maven support.

View File

@ -6,17 +6,10 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.*; import java.lang.annotation.*;
/** /**
* Indicates that a method is an event handler. To be successfully used as such, the method has to * Indicates that a method is an event handler.
* comply with the following specifications: * <p>
* <ul> * To be successfully used as such, the method has to specify the event type by either declaring one
* <li>Specifying an event type by either * parameter of that type or setting the annotation value to the corresponding class.
* <ul>
* <li>Declaring one object parameter</li>
* <li>Defining the class of the event using the annotation value</li>
* </ul>
* </li>
* <li>Return type of {@code void}</li>
* </ul>
* *
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since 0.0.1 * @since 0.0.1

View File

@ -43,7 +43,7 @@ final class EventHandler implements Comparable<EventHandler> {
this.method = method; this.method = method;
useParameter = annotation.value() == USE_PARAMETER.class; useParameter = annotation.value() == USE_PARAMETER.class;
// Check for correct method signature and return type // Check handler signature
if (method.getParameterCount() == 0 && useParameter) if (method.getParameterCount() == 0 && useParameter)
throw new EventBusException(method + " does not define an event type!"); throw new EventBusException(method + " does not define an event type!");
@ -53,9 +53,6 @@ final class EventHandler implements Comparable<EventHandler> {
if (method.getParameterCount() > 1) if (method.getParameterCount() > 1)
throw new EventBusException(method + " defines more than one parameter!"); throw new EventBusException(method + " defines more than one parameter!");
if (!method.getReturnType().equals(void.class))
throw new EventBusException(method + " does not have a return type of void!");
// Determine handler properties // Determine handler properties
eventType = useParameter ? method.getParameterTypes()[0] : annotation.value(); eventType = useParameter ? method.getParameterTypes()[0] : annotation.value();
polymorphic = method.isAnnotationPresent(Polymorphic.class); polymorphic = method.isAnnotationPresent(Polymorphic.class);

View File

@ -50,6 +50,21 @@
</goals> </goals>
<configuration> <configuration>
<minimizeJar>true</minimizeJar> <minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>dev.kske:event-bus-core</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>module-info.class</exclude>
<exclude>META-INF/maven/**</exclude>
</excludes>
</filter>
</filters>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>

View File

@ -60,8 +60,6 @@ public class EventProcessor extends AbstractProcessor {
"Either the method or the annotation must define the event type"); "Either the method or the annotation must define the event type");
else if (eventHandler.getParameters().size() > 1) else if (eventHandler.getParameters().size() > 1)
error(eventHandler, "Method must not have more than one parameter"); error(eventHandler, "Method must not have more than one parameter");
else if (eventHandler.getReturnType().getKind() != TypeKind.VOID)
error(eventHandler, "Method must return void");
else else
pass = true; pass = true;

33
pom.xml
View File

@ -41,6 +41,7 @@
<scm> <scm>
<connection>scm:git:https://git.kske.dev/kske/event-bus.git</connection> <connection>scm:git:https://git.kske.dev/kske/event-bus.git</connection>
<developerConnection>scm:git:ssh://git@git.kske.dev:420/kske/event-bus.git</developerConnection> <developerConnection>scm:git:ssh://git@git.kske.dev:420/kske/event-bus.git</developerConnection>
<url>https://git.kske.dev/kske/event-bus</url>
</scm> </scm>
<properties> <properties>
@ -50,6 +51,18 @@
<maven.compiler.target>11</maven.compiler.target> <maven.compiler.target>11</maven.compiler.target>
</properties> </properties>
<!-- Configure deployment to OSSRH -->
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<build> <build>
<!-- Disable test resource folder --> <!-- Disable test resource folder -->
@ -64,7 +77,7 @@
<version>3.8.1</version> <version>3.8.1</version>
</plugin> </plugin>
<!-- Attach sources and Javadoc to JAR --> <!-- Attach sources to JAR -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId> <artifactId>maven-source-plugin</artifactId>
@ -78,6 +91,8 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<!-- Attach Javadoc to JAR -->
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>
@ -92,6 +107,22 @@
</executions> </executions>
</plugin> </plugin>
<!-- GPG sign JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>