forked from SteamWar/SteamWar
Fix HandlerForVariableElement.getAnnotations
This commit is contained in:
+29
-6
@@ -19,12 +19,13 @@
|
|||||||
|
|
||||||
package de.steamwar.command;
|
package de.steamwar.command;
|
||||||
|
|
||||||
import de.steamwar.command.handler.RegisterHandler;
|
import javax.lang.model.element.AnnotationMirror;
|
||||||
|
|
||||||
import javax.lang.model.element.VariableElement;
|
import javax.lang.model.element.VariableElement;
|
||||||
import javax.lang.model.util.Elements;
|
import javax.lang.model.util.Elements;
|
||||||
import javax.lang.model.util.Types;
|
import javax.lang.model.util.Types;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -69,9 +70,31 @@ public class HandlerForVariableElement implements Handler.ParameterWrapper {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Annotation> getAnnotations() {
|
public List<Annotation> getAnnotations() {
|
||||||
parameter.getAnnotationMirrors().forEach(annotationMirror -> {
|
List<Annotation> annotations = new ArrayList<>();
|
||||||
System.out.println(annotationMirror);
|
for (AnnotationMirror annotationMirror : parameter.getAnnotationMirrors()) {
|
||||||
});
|
Class<?> clazz = getClass(annotationMirror.getAnnotationType().toString());
|
||||||
return Collections.emptyList();
|
if (clazz == null) continue;
|
||||||
|
annotations.addAll(Arrays.asList(parameter.getAnnotationsByType((Class<? extends Annotation>) clazz)));
|
||||||
|
}
|
||||||
|
annotations.removeIf(annotation -> !annotation.annotationType().isAnnotationPresent(Handler.Implementation.class));
|
||||||
|
return annotations;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-45
@@ -21,14 +21,16 @@ package de.steamwar.command.annotationprocessor;
|
|||||||
|
|
||||||
import de.steamwar.command.Handler;
|
import de.steamwar.command.Handler;
|
||||||
import de.steamwar.command.HandlerForExecutableElement;
|
import de.steamwar.command.HandlerForExecutableElement;
|
||||||
import de.steamwar.command.HandlerForVariableElement;
|
|
||||||
|
|
||||||
import javax.annotation.processing.AbstractProcessor;
|
import javax.annotation.processing.AbstractProcessor;
|
||||||
import javax.annotation.processing.Messager;
|
import javax.annotation.processing.Messager;
|
||||||
import javax.annotation.processing.ProcessingEnvironment;
|
import javax.annotation.processing.ProcessingEnvironment;
|
||||||
import javax.annotation.processing.RoundEnvironment;
|
import javax.annotation.processing.RoundEnvironment;
|
||||||
import javax.lang.model.SourceVersion;
|
import javax.lang.model.SourceVersion;
|
||||||
import javax.lang.model.element.*;
|
import javax.lang.model.element.Element;
|
||||||
|
import javax.lang.model.element.ElementKind;
|
||||||
|
import javax.lang.model.element.ExecutableElement;
|
||||||
|
import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.util.Elements;
|
import javax.lang.model.util.Elements;
|
||||||
import javax.lang.model.util.Types;
|
import javax.lang.model.util.Types;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
@@ -75,38 +77,10 @@ public abstract class AbstractAnnotationProcessor extends AbstractProcessor {
|
|||||||
if (elements.isEmpty()) return false;
|
if (elements.isEmpty()) return false;
|
||||||
elements.forEach(element -> {
|
elements.forEach(element -> {
|
||||||
checkMethodAnnotation(element, element.getAnnotation(getAnnotationClass()));
|
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;
|
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) {
|
private Handler getHandler(Annotation annotation, Element element) {
|
||||||
Handler.Implementation implementation = annotation.annotationType().getAnnotation(Handler.Implementation.class);
|
Handler.Implementation implementation = annotation.annotationType().getAnnotation(Handler.Implementation.class);
|
||||||
if (implementation == null) return null;
|
if (implementation == null) return null;
|
||||||
@@ -134,21 +108,6 @@ public abstract class AbstractAnnotationProcessor extends AbstractProcessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 class DataCheckableImpl implements Handler.DataCheckable {
|
||||||
|
|
||||||
private static final DataCheckableImpl INSTANCE = new DataCheckableImpl();
|
private static final DataCheckableImpl INSTANCE = new DataCheckableImpl();
|
||||||
|
|||||||
Reference in New Issue
Block a user