mirror of
https://github.com/zaaarf/route-cartographer.git
synced 2024-11-14 16:49:19 +01:00
fix: annotation field default isnt null
This commit is contained in:
parent
1e5646a7fd
commit
9fc99e23d8
1 changed files with 21 additions and 5 deletions
|
@ -21,6 +21,7 @@ import java.lang.annotation.Annotation;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -142,7 +143,11 @@ public class RouteCompass extends AbstractProcessor {
|
||||||
*/
|
*/
|
||||||
private String getFullRoute(TypeElement annotationType, Element element) {
|
private String getFullRoute(TypeElement annotationType, Element element) {
|
||||||
try { //TODO support multiple routes
|
try { //TODO support multiple routes
|
||||||
String[] routes = this.getAnnotationFieldsValue(annotationType, element, "path", "value");
|
String[] routes = this.getAnnotationFieldsValue(
|
||||||
|
annotationType,
|
||||||
|
element,
|
||||||
|
(arr) -> Arrays.deepEquals(arr, new String[] {}),
|
||||||
|
"path", "value");
|
||||||
return this.getParentOrFallback(element, routes[0], (a, e) -> {
|
return this.getParentOrFallback(element, routes[0], (a, e) -> {
|
||||||
String parent = this.getFullRoute(a, e);
|
String parent = this.getFullRoute(a, e);
|
||||||
StringBuilder sb = new StringBuilder(parent);
|
StringBuilder sb = new StringBuilder(parent);
|
||||||
|
@ -178,7 +183,11 @@ public class RouteCompass extends AbstractProcessor {
|
||||||
*/
|
*/
|
||||||
private String[] getConsumedType(TypeElement annotationType, Element element) {
|
private String[] getConsumedType(TypeElement annotationType, Element element) {
|
||||||
try {
|
try {
|
||||||
String[] res = this.getAnnotationFieldsValue(annotationType, element, "consumes");
|
String[] res = this.getAnnotationFieldsValue(
|
||||||
|
annotationType,
|
||||||
|
element,
|
||||||
|
(arr) -> Arrays.deepEquals(arr, new String[] {}),
|
||||||
|
"consumes");
|
||||||
return res == null
|
return res == null
|
||||||
? this.getParentOrFallback(element, res, this::getConsumedType)
|
? this.getParentOrFallback(element, res, this::getConsumedType)
|
||||||
: res;
|
: res;
|
||||||
|
@ -195,7 +204,11 @@ public class RouteCompass extends AbstractProcessor {
|
||||||
*/
|
*/
|
||||||
private String[] getProducedType(TypeElement annotationType, Element element) {
|
private String[] getProducedType(TypeElement annotationType, Element element) {
|
||||||
try {
|
try {
|
||||||
String[] res = this.getAnnotationFieldsValue(annotationType, element, "produces");
|
String[] res = this.getAnnotationFieldsValue(
|
||||||
|
annotationType,
|
||||||
|
element,
|
||||||
|
(arr) -> Arrays.deepEquals(arr, new String[] {}),
|
||||||
|
"produces");
|
||||||
return res == null
|
return res == null
|
||||||
? this.getParentOrFallback(element, res, this::getProducedType)
|
? this.getParentOrFallback(element, res, this::getProducedType)
|
||||||
: res;
|
: res;
|
||||||
|
@ -273,13 +286,15 @@ public class RouteCompass extends AbstractProcessor {
|
||||||
* An annotation value.
|
* An annotation value.
|
||||||
* @param annotationType the {@link TypeElement} with the annotation we are processing
|
* @param annotationType the {@link TypeElement} with the annotation we are processing
|
||||||
* @param element the {@link Element} currently being examined
|
* @param element the {@link Element} currently being examined
|
||||||
|
* @param unsetPredicate lambda that returns true if the value is the default one (thus unset)
|
||||||
* @param fieldNames the field name(s) to look for; they are tried in order, and the first found is returned
|
* @param fieldNames the field name(s) to look for; they are tried in order, and the first found is returned
|
||||||
* @return the field value, cast to the expected type
|
* @return the field value, cast to the expected type
|
||||||
* @param <T> the expected type of the field
|
* @param <T> the expected type of the field
|
||||||
* @throws ReflectiveOperationException when given non-existing or inaccessible field names (hopefully never)
|
* @throws ReflectiveOperationException when given non-existing or inaccessible field names (hopefully never)
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"OptionalGetWithoutIsPresent", "unchecked"})
|
@SuppressWarnings({"OptionalGetWithoutIsPresent", "unchecked"})
|
||||||
private <T> T getAnnotationFieldsValue(TypeElement annotationType, Element element, String ... fieldNames)
|
private <T> T getAnnotationFieldsValue(TypeElement annotationType, Element element,
|
||||||
|
Predicate<T> unsetPredicate, String ... fieldNames)
|
||||||
throws ReflectiveOperationException {
|
throws ReflectiveOperationException {
|
||||||
|
|
||||||
Class<? extends Annotation> annClass = this.annotationClasses.stream()
|
Class<? extends Annotation> annClass = this.annotationClasses.stream()
|
||||||
|
@ -290,7 +305,8 @@ public class RouteCompass extends AbstractProcessor {
|
||||||
T result = null;
|
T result = null;
|
||||||
for(String fieldName : fieldNames) {
|
for(String fieldName : fieldNames) {
|
||||||
result = (T) annClass.getMethod(fieldName).invoke(element.getAnnotation(annClass));
|
result = (T) annClass.getMethod(fieldName).invoke(element.getAnnotation(annClass));
|
||||||
if(result != null) return result;
|
if(result != null && !unsetPredicate.test(result))
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
Loading…
Reference in a new issue