Add Annotation Processor for compile time checks

This commit is contained in:
2025-11-23 20:27:44 +01:00
parent f6fc65a370
commit 18659385d2
40 changed files with 1029 additions and 185 deletions
@@ -0,0 +1,32 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
plugins {
steamwar.java
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
dependencies {
implementation(project(":CommandFramework:CommandFrameworkBase", "default"))
annotationProcessor(project(":CommandFramework:CommandFrameworkBase", "default"))
}
@@ -0,0 +1,5 @@
de.steamwar.command.annotationprocessor.impl.CachedAnnotationProcessor
de.steamwar.command.annotationprocessor.impl.MapperAnnotationProcessor
de.steamwar.command.annotationprocessor.impl.RegisterAnnotationProcessor
de.steamwar.command.annotationprocessor.impl.SupplierAnnotationProcessor
de.steamwar.command.annotationprocessor.impl.ValidatorAnnotationProcessor
@@ -0,0 +1,65 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import java.util.List;
public class HandlerForExecutableElement implements Handler.MethodWrapper {
private final ExecutableElement method;
private final Types types;
private final Elements elements;
public HandlerForExecutableElement(ExecutableElement method, Types types, Elements elements) {
this.method = method;
this.types = types;
this.elements = elements;
}
@Override
public String getName() {
return method.getSimpleName().toString();
}
@Override
public int getParameterCount() {
return method.getParameters().size();
}
@Override
public Handler.TypeWrapper getReturnType() {
return new HandlerForTypeMirror(method.getReturnType(), types, elements);
}
@Override
public Handler.ParameterWrapper[] getParameters() {
Handler.ParameterWrapper[] parameters = new Handler.ParameterWrapper[method.getParameters().size()];
List<? extends VariableElement> variableElements = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
VariableElement variableElement = variableElements.get(i);
parameters[i] = new HandlerForVariableElement(variableElement, types, elements, i == parameters.length - 1 && method.isVarArgs());
}
return parameters;
}
}
@@ -0,0 +1,107 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
public class HandlerForTypeMirror implements Handler.TypeWrapper {
private final TypeMirror type;
private final Types types;
private final Elements elements;
public HandlerForTypeMirror(TypeMirror type, Types types, Elements elements) {
this.type = type;
this.types = types;
this.elements = elements;
}
@Override
public boolean isAssignableTo(Class<?> clazz) {
return types.isAssignable(types.erasure(type), types.erasure(elements.getTypeElement(clazz.getTypeName()).asType()));
}
@Override
public boolean isAssignableTo(Handler.TypeWrapper type) {
if (type instanceof ForClass) {
return isAssignableTo(((ForClass) type).getClazz());
} else if (type instanceof HandlerForTypeMirror) {
return types.isAssignable(types.erasure(this.type), types.erasure(((HandlerForTypeMirror) type).type));
}
return false;
}
@Override
public boolean isPrimitive() {
return type.getKind().isPrimitive();
}
@Override
public boolean isArray() {
return type instanceof ArrayType;
}
@Override
public boolean isEnum() {
System.out.println(type);
return false;
}
@Override
public boolean is(Class<?> clazz) {
if (type.getKind() == TypeKind.VOID && clazz == Void.TYPE) {
return true;
} else if (type.getKind().isPrimitive() && clazz.isPrimitive()) {
return type.toString().equals(clazz.getTypeName());
} else if (!type.getKind().isPrimitive() && !clazz.isPrimitive()) {
return types.isSameType(types.erasure(type), types.erasure(elements.getTypeElement(clazz.getTypeName()).asType()));
} else {
return false;
}
}
@Override
public boolean is(Handler.TypeWrapper type) {
if (type instanceof ForClass) {
return is(((ForClass) type).getClazz());
} else if (type instanceof HandlerForTypeMirror) {
return types.isSameType(types.erasure(this.type), types.erasure(((HandlerForTypeMirror) type).type));
}
return false;
}
@Override
public Handler.TypeWrapper getComponentType() {
if (type instanceof ArrayType) {
return new HandlerForTypeMirror(((ArrayType) type).getComponentType(), types, elements);
} else {
return new HandlerForTypeMirror(null, types, elements);
}
}
@Override
public String getName() {
return type.toString();
}
}
@@ -0,0 +1,77 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
import de.steamwar.command.handler.RegisterHandler;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.List;
public class HandlerForVariableElement implements Handler.ParameterWrapper {
private final VariableElement parameter;
private final Types types;
private final Elements elements;
private final boolean varArgs;
public HandlerForVariableElement(VariableElement variableElement, Types types, Elements elements, boolean varArgs) {
this.parameter = variableElement;
this.types = types;
this.elements = elements;
this.varArgs = varArgs;
}
@Override
public Handler.TypeWrapper getType() {
return new HandlerForTypeMirror(parameter.asType(), types, elements);
}
@Override
public boolean isVarArgs() {
return varArgs;
}
@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotation) {
return parameter.getAnnotation(annotation) != null;
}
@Override
public <A extends Annotation> A getAnnotation(Class<A> annotation) {
return parameter.getAnnotation(annotation);
}
@Override
public String getName() {
return parameter.getSimpleName().toString();
}
@Override
public List<Annotation> getAnnotations() {
parameter.getAnnotationMirrors().forEach(annotationMirror -> {
System.out.println(annotationMirror);
});
return Collections.emptyList();
}
}
@@ -0,0 +1,176 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command.annotationprocessor;
import de.steamwar.command.Handler;
import de.steamwar.command.HandlerForExecutableElement;
import de.steamwar.command.HandlerForVariableElement;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public abstract class AbstractAnnotationProcessor extends AbstractProcessor {
private Messager messager;
private Types types;
private Elements elements;
public abstract Class<? extends Annotation> getAnnotationClass();
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
this.messager = processingEnv.getMessager();
this.types = processingEnv.getTypeUtils();
this.elements = processingEnv.getElementUtils();
}
@Override
public Set<String> getSupportedAnnotationTypes() {
return Collections.singleton(this.getAnnotationClass().getCanonicalName());
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
List<ExecutableElement> elements = roundEnv.getElementsAnnotatedWith(getAnnotationClass())
.stream()
.filter(element -> element.getKind() == ElementKind.METHOD)
.map(ExecutableElement.class::cast)
.collect(Collectors.toList());
if (elements.isEmpty()) return false;
elements.forEach(element -> {
checkMethodAnnotation(element, element.getAnnotation(getAnnotationClass()));
List<? extends VariableElement> parameters = element.getParameters();
for (int index = 0; index < parameters.size(); index++) {
VariableElement parameter = parameters.get(index);
for (AnnotationMirror annotationMirror : parameter.getAnnotationMirrors()) {
Class<?> clazz = getClass(annotationMirror.getAnnotationType().toString());
if (clazz == null) return;
checkParameterAnnotation(element, parameter, index, parameter.getAnnotation((Class<? extends Annotation>) clazz));
}
}
});
return false;
}
private Class<?> getClass(String name) {
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
// Ignore
}
if (!name.contains(".")) return null;
Class<?> outerClass = getClass(name.substring(0, name.lastIndexOf('.')));
if (outerClass == null) return null;
name = name.substring(name.lastIndexOf('.') + 1);
for (Class<?> declaredClass : outerClass.getDeclaredClasses()) {
if (declaredClass.getSimpleName().equals(name)) {
return declaredClass;
}
}
return null;
}
private Handler getHandler(Annotation annotation, Element element) {
Handler.Implementation implementation = annotation.annotationType().getAnnotation(Handler.Implementation.class);
if (implementation == null) return null;
try {
return implementation.value().getConstructor().newInstance();
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException |
InvocationTargetException e) {
messager.printMessage(Diagnostic.Kind.ERROR, "Handler " + implementation.value().getName() + " cannot be used to check the argument validity", element);
return null;
}
}
private void checkMethodAnnotation(ExecutableElement method, Annotation annotation) {
Handler handler = getHandler(annotation, method);
if (handler == null) return;
if (!(handler instanceof Handler.HandlerMethod)) {
messager.printMessage(Diagnostic.Kind.ERROR, "Handler " + handler.getClass().getName() + " is not a HandlerMethod", method);
return;
}
Handler.HandlerMethod handlerMethod = (Handler.HandlerMethod) handler;
try {
handlerMethod.check(annotation, new HandlerForExecutableElement(method, types, elements), DataCheckableImpl.INSTANCE);
} catch (Handler.HandlerException e) {
messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage(), method);
}
}
private void checkParameterAnnotation(ExecutableElement method, VariableElement parameter, int index, Annotation annotation) {
Handler handler = getHandler(annotation, parameter);
if (handler == null) return;
if (!(handler instanceof Handler.HandlerParameter)) {
messager.printMessage(Diagnostic.Kind.ERROR, "Handler " + handler.getClass().getName() + " is not a HandlerParameter", method);
return;
}
Handler.HandlerParameter handlerParameter = (Handler.HandlerParameter) handler;
try {
handlerParameter.check(annotation, new HandlerForExecutableElement(method, types, elements), new HandlerForVariableElement(parameter, types, elements, index == method.getParameters().size() - 1 && method.isVarArgs()), index, DataCheckableImpl.INSTANCE);
} catch (Handler.HandlerException e) {
messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage(), parameter);
}
}
private static final class DataCheckableImpl implements Handler.DataCheckable {
private static final DataCheckableImpl INSTANCE = new DataCheckableImpl();
@Override
public boolean hasExecutorMapper(Class<?> clazz) {
return true;
}
@Override
public boolean hasMapper(String key) {
return true;
}
@Override
public boolean hasValidator(String key) {
return true;
}
@Override
public boolean hasSupplier(String key) {
return true;
}
}
}
@@ -0,0 +1,33 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command.annotationprocessor.impl;
import de.steamwar.command.annotationprocessor.AbstractAnnotationProcessor;
import de.steamwar.command.annotations.Cached;
import java.lang.annotation.Annotation;
public class CachedAnnotationProcessor extends AbstractAnnotationProcessor {
@Override
public Class<? extends Annotation> getAnnotationClass() {
return Cached.class;
}
}
@@ -0,0 +1,33 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command.annotationprocessor.impl;
import de.steamwar.command.annotationprocessor.AbstractAnnotationProcessor;
import de.steamwar.command.annotations.Mapper;
import java.lang.annotation.Annotation;
public class MapperAnnotationProcessor extends AbstractAnnotationProcessor {
@Override
public Class<? extends Annotation> getAnnotationClass() {
return Mapper.class;
}
}
@@ -0,0 +1,33 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command.annotationprocessor.impl;
import de.steamwar.command.annotationprocessor.AbstractAnnotationProcessor;
import de.steamwar.command.annotations.Register;
import java.lang.annotation.Annotation;
public class RegisterAnnotationProcessor extends AbstractAnnotationProcessor {
@Override
public Class<? extends Annotation> getAnnotationClass() {
return Register.class;
}
}
@@ -0,0 +1,33 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command.annotationprocessor.impl;
import de.steamwar.command.annotationprocessor.AbstractAnnotationProcessor;
import de.steamwar.command.annotations.Supplier;
import java.lang.annotation.Annotation;
public class SupplierAnnotationProcessor extends AbstractAnnotationProcessor {
@Override
public Class<? extends Annotation> getAnnotationClass() {
return Supplier.class;
}
}
@@ -0,0 +1,33 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command.annotationprocessor.impl;
import de.steamwar.command.annotationprocessor.AbstractAnnotationProcessor;
import de.steamwar.command.annotations.Validator;
import java.lang.annotation.Annotation;
public class ValidatorAnnotationProcessor extends AbstractAnnotationProcessor {
@Override
public Class<? extends Annotation> getAnnotationClass() {
return Validator.class;
}
}