Rename event-bus-ap to event-bus-proc
This commit is contained in:
32
event-bus-proc/.classpath
Normal file
32
event-bus-proc/.classpath
Normal file
@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="home/kske/git/event-bus/event-bus-ap">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
23
event-bus-proc/.project
Normal file
23
event-bus-proc/.project
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>event-bus-proc</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
60
event-bus-proc/pom.xml
Normal file
60
event-bus-proc/pom.xml
Normal file
@ -0,0 +1,60 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>event-bus-proc</artifactId>
|
||||
|
||||
<name>Event Bus Annotation Processor</name>
|
||||
<description>Annotation processor checking for errors related to the @Event annotation from Event Bus.</description>
|
||||
|
||||
<parent>
|
||||
<groupId>dev.kske</groupId>
|
||||
<artifactId>event-bus</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>dev.kske</groupId>
|
||||
<artifactId>event-bus-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
<!-- Disable test folder -->
|
||||
<testSourceDirectory />
|
||||
|
||||
<plugins>
|
||||
|
||||
<!-- Prevent annotation processing error during compilation -->
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<compilerArgument>-proc:none</compilerArgument>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- Include event-bus-core classes into JAR -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<minimizeJar>true</minimizeJar>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,97 @@
|
||||
package dev.kske.eventbus.proc;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.processing.*;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.lang.model.type.*;
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
|
||||
import dev.kske.eventbus.core.*;
|
||||
|
||||
/**
|
||||
* This annotation processor checks event handlers for common mistakes which can only be detected
|
||||
* during runtime otherwise.
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@SupportedAnnotationTypes("dev.kske.eventbus.core.Event")
|
||||
@SupportedSourceVersion(SourceVersion.RELEASE_11)
|
||||
public class EventProcessor extends AbstractProcessor {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
if (!roundEnv.errorRaised() && !roundEnv.processingOver())
|
||||
processRound(
|
||||
(Set<ExecutableElement>) roundEnv.getElementsAnnotatedWith(Event.class));
|
||||
|
||||
// Do not claim the processed annotations
|
||||
return false;
|
||||
}
|
||||
|
||||
private void processRound(Set<ExecutableElement> eventHandlers) {
|
||||
for (ExecutableElement eventHandler : eventHandlers) {
|
||||
Event eventAnnotation = eventHandler.getAnnotation(Event.class);
|
||||
|
||||
// Determine how the event type is defined
|
||||
boolean useParameter;
|
||||
try {
|
||||
eventAnnotation.value();
|
||||
throw new EventBusException(
|
||||
"Could not determine event type of handler " + eventHandler);
|
||||
} catch (MirroredTypeException e) {
|
||||
|
||||
// Task failed successfully
|
||||
useParameter = processingEnv.getTypeUtils().isSameType(e.getTypeMirror(),
|
||||
getTypeMirror(Event.USE_PARAMETER.class));
|
||||
}
|
||||
|
||||
// Check for correct method signature and return type
|
||||
if (eventHandler.getParameters().size() == 0 && useParameter)
|
||||
error(eventHandler, "The method or the annotation must define the event type");
|
||||
|
||||
if (eventHandler.getParameters().size() == 1 && !useParameter)
|
||||
error(eventHandler,
|
||||
"Either the method or the annotation must define the event type");
|
||||
|
||||
if (eventHandler.getParameters().size() > 1)
|
||||
error(eventHandler, "Method must not have more than one parameter");
|
||||
|
||||
if (eventHandler.getReturnType().getKind() != TypeKind.VOID)
|
||||
error(eventHandler, "Method must return void");
|
||||
|
||||
// Get first parameter as type and element
|
||||
var paramElement = eventHandler.getParameters().get(0);
|
||||
var paramType = paramElement.asType();
|
||||
|
||||
// Check for handlers for abstract types that aren't polymorphic
|
||||
if (eventHandler.getAnnotation(Polymorphic.class) == null
|
||||
&& paramType.getKind() == TypeKind.DECLARED) {
|
||||
var declaredElement = ((DeclaredType) paramType).asElement();
|
||||
if (declaredElement.getKind() == ElementKind.INTERFACE
|
||||
|| declaredElement.getModifiers().contains(Modifier.ABSTRACT))
|
||||
warning(paramElement,
|
||||
"Parameter should be instantiable or handler should use @Polymorphic");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private TypeMirror getTypeMirror(Class<?> clazz) {
|
||||
return getTypeElement(clazz).asType();
|
||||
}
|
||||
|
||||
private TypeElement getTypeElement(Class<?> clazz) {
|
||||
return processingEnv.getElementUtils().getTypeElement(clazz.getCanonicalName());
|
||||
}
|
||||
|
||||
private void warning(Element e, String msg, Object... args) {
|
||||
processingEnv.getMessager().printMessage(Kind.WARNING, String.format(msg, args), e);
|
||||
}
|
||||
|
||||
private void error(Element e, String msg, Object... args) {
|
||||
processingEnv.getMessager().printMessage(Kind.ERROR, String.format(msg, args), e);
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Contains the Event Bus annotation processor.
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since 1.0.0
|
||||
*/
|
||||
package dev.kske.eventbus.proc;
|
12
event-bus-proc/src/main/java/module-info.java
Normal file
12
event-bus-proc/src/main/java/module-info.java
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Contains an annotation processor for checking for errors related to the
|
||||
* {@link dev.kske.eventbus.core.Event} annotation from Event Bus.
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since 1.0.0
|
||||
*/
|
||||
module dev.kske.eventbus.ap {
|
||||
|
||||
requires java.compiler;
|
||||
requires dev.kske.eventbus.core;
|
||||
}
|
@ -0,0 +1 @@
|
||||
dev.kske.eventbus.proc.EventProcessor
|
Reference in New Issue
Block a user