From b2b1a738bdddfaf002f169570117abd9fe51f2d3 Mon Sep 17 00:00:00 2001 From: Tobias Stamann Date: Mon, 3 Jul 2017 12:31:53 +0200 Subject: [PATCH] [#2] Added more testcases and restructed TypeUtils class. Grouped methods in non static embedded classes. --- .../tools/TypeUtils.java | 452 ++++++++++-------- ...xecutableElementCharacteristicMatcher.java | 2 +- ...xecutableElementCharacteristicMatcher.java | 2 +- .../TypeElementCharacteristicMatcher.java | 2 +- .../FluentExecutableElementValidator.java | 9 +- .../FluentTypeElementValidator.java | 4 +- .../AbstractAnnotationProcessorTest.java | 2 +- .../FluentElementFilterTest.java | 4 +- .../FluentExecutableElementValidatorTest.java | 2 +- .../tools/TypeUtilsTest.java | 272 +++++++---- .../tools/TypeUtilsTest_CheckTypeKind.java | 166 +++++++ .../AnnotationProcessorTestClass.java | 12 +- 12 files changed, 618 insertions(+), 311 deletions(-) create mode 100644 annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/TypeUtilsTest_CheckTypeKind.java diff --git a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/TypeUtils.java b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/TypeUtils.java index 262636e4..f6947bb3 100644 --- a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/TypeUtils.java +++ b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/TypeUtils.java @@ -15,14 +15,30 @@ public class TypeUtils { protected final FrameworkToolWrapper frameworkToolWrapper; + public final TypeRetrieval TYPE_RETRIEVAL = new TypeRetrieval(); + public final TypeComparison TYPE_COMPARISON = new TypeComparison(); + public final Arrays ARRAYS = new Arrays(); + public final CheckTypeKind CHECK_TYPE_KIND = CheckTypeKind.INSTANCE; + + private TypeUtils(FrameworkToolWrapper frameworkToolWrapper) { this.frameworkToolWrapper = frameworkToolWrapper; } + /** - * Embedded class for hanlding the TypeKind checks. + * Embedded class for handling the TypeKind checks. */ - public class CheckTypeKind { + public final static class CheckTypeKind { + + public final static CheckTypeKind INSTANCE = new CheckTypeKind(); + + /** + * Hidden Constructor + */ + private CheckTypeKind() { + + } /** * Checks whether passed {@link TypeMirror} is a void type or not. @@ -69,248 +85,286 @@ public boolean isOfTypeKind(TypeMirror typeMirror, TypeKind kind) { } - public CheckTypeKind checkTypeKind() { - return new CheckTypeKind(); - } - - /** - * Check wheteher passed typeMirror is of passed kind - * - * @param typeMirror - * @param typeKind - * @return true if passed typeMirror is of passed kind, otherwise false - */ - public boolean checkTypeKind(TypeMirror typeMirror, TypeKind typeKind) { - return checkTypeKind().isOfTypeKind(typeMirror, typeKind); - } + public class TypeRetrieval { - /** - * Gets a type element for a full qualified class name - * - * @param fullQualifiedClassName - * @return the type element for the passed full qualified class name or null if type element can't be found - */ - public TypeElement getTypeElement(String fullQualifiedClassName) { + /** + * Hidden Constructor + */ + private TypeRetrieval() { - if (fullQualifiedClassName == null) { - return null; } - return frameworkToolWrapper.getElements().getTypeElement(fullQualifiedClassName); + /** + * Gets a type element for a full qualified class name + * + * @param fullQualifiedClassName + * @return the type element for the passed full qualified class name or null if type element can't be found + */ + public TypeElement getTypeElement(String fullQualifiedClassName) { - } + if (fullQualifiedClassName == null) { + return null; + } - /** - * Gets a TypeMirror for a full qualified class name - * - * @param fullQualifiedClassName - * @return the type mirror for the passed full qualified class name or null if corresponding type element can't be found - */ - public TypeMirror getTypeMirror(String fullQualifiedClassName) { + return frameworkToolWrapper.getElements().getTypeElement(fullQualifiedClassName); - TypeElement typeElement = getTypeElement(fullQualifiedClassName); - return typeElement != null ? typeElement.asType() : null; + } - } + /** + * Gets a TypeMirror for a full qualified class name + * + * @param fullQualifiedClassName + * @return the type mirror for the passed full qualified class name or null if corresponding type element can't be found + */ + public TypeMirror getTypeMirror(String fullQualifiedClassName) { - /** - * Gets {@link TypeElement} for class. - * - * @param type the class to get the {@link TypeElement} for - * @return The {@link TypeElement} that is related with the passed class or null if a TypeElement can't be found for passed class (f.e. if passed type represents an array) - */ - public TypeElement getTypeElement(Class type) { + TypeElement typeElement = getTypeElement(fullQualifiedClassName); + return typeElement != null ? typeElement.asType() : null; - TypeMirror typeMirror = getTypeMirror(type); - return typeMirror == null ? null : (TypeElement) frameworkToolWrapper.getTypes().asElement(typeMirror); + } - } + /** + * Gets {@link TypeElement} for class. + * + * @param type the class to get the {@link TypeElement} for + * @return The {@link TypeElement} that is related with the passed class or null if a TypeElement can't be found for passed class (f.e. if passed type represents an array) + */ + public TypeElement getTypeElement(Class type) { - /** - * Gets {@link TypeMirror} for class. - * - * @param type the class to get the {@link TypeMirror} for - * @return The {@link TypeMirror} that is related with the passed class - */ - public TypeMirror getTypeMirror(Class type) { + TypeMirror typeMirror = getTypeMirror(type); + return typeMirror == null ? null : (TypeElement) frameworkToolWrapper.getTypes().asElement(typeMirror); - if (type == null) { - return null; } - if (type.isArray()) { - return frameworkToolWrapper.getTypes().getArrayType(getTypeMirror(type.getComponentType())); + /** + * Gets {@link TypeMirror} for class. + * + * @param type the class to get the {@link TypeMirror} for + * @return The {@link TypeMirror} that is related with the passed class + */ + public TypeMirror getTypeMirror(Class type) { + + // handle null safety and anonymous types for which we cannot get TypeMirror of a class + if (type == null || type.isAnonymousClass()) { + return null; + } + + if (type.isArray()) { + return frameworkToolWrapper.getTypes().getArrayType(getTypeMirror(type.getComponentType())); + } + + if (type.isPrimitive()) { + return getPrimitiveTypeMirror(type); + } + + return frameworkToolWrapper.getElements().getTypeElement(type.getCanonicalName()).asType(); } - if (type.isPrimitive()) { - return getPrimitiveTypeMirror(type); + /** + * Gets the {@link TypeMirror} for a passed primitive type. + * + * @param primitiveType the primitive type to get the type mirror for + * @return null if passed primitive type is null or doesn't represent a primitive type, otherwise the type mirror of the primitive type. + */ + public TypeMirror getPrimitiveTypeMirror(Class primitiveType) { + + if (primitiveType == null || !primitiveType.isPrimitive()) { + return null; + } + + if (boolean.class.equals(primitiveType)) { + return getTypes().getPrimitiveType(TypeKind.BOOLEAN); + } else if (byte.class.equals(primitiveType)) { + return getTypes().getPrimitiveType(TypeKind.BYTE); + } else if (char.class.equals(primitiveType)) { + return getTypes().getPrimitiveType(TypeKind.CHAR); + } else if (double.class.equals(primitiveType)) { + return getTypes().getPrimitiveType(TypeKind.DOUBLE); + } else if (float.class.equals(primitiveType)) { + return getTypes().getPrimitiveType(TypeKind.FLOAT); + } else if (int.class.equals(primitiveType)) { + return getTypes().getPrimitiveType(TypeKind.INT); + } else if (long.class.equals(primitiveType)) { + return getTypes().getPrimitiveType(TypeKind.LONG); + } else if (short.class.equals(primitiveType)) { + return getTypes().getPrimitiveType(TypeKind.SHORT); + } + + return null; } - return frameworkToolWrapper.getElements().getTypeElement(type.getCanonicalName()).asType(); } - public TypeMirror getPrimitiveTypeMirror(Class primitiveType) { - if (primitiveType == null || !primitiveType.isPrimitive()) { - return null; + public class TypeComparison { + + /** + * Hidden Constructor + */ + private TypeComparison() { + } - if (boolean.class.equals(primitiveType)) { - return getTypes().getPrimitiveType(TypeKind.BOOLEAN); - } else if (byte.class.equals(primitiveType)) { - return getTypes().getPrimitiveType(TypeKind.BYTE); - } else if (char.class.equals(primitiveType)) { - return getTypes().getPrimitiveType(TypeKind.CHAR); - } else if (double.class.equals(primitiveType)) { - return getTypes().getPrimitiveType(TypeKind.DOUBLE); - } else if (float.class.equals(primitiveType)) { - return getTypes().getPrimitiveType(TypeKind.FLOAT); - } else if (int.class.equals(primitiveType)) { - return getTypes().getPrimitiveType(TypeKind.INT); - } else if (long.class.equals(primitiveType)) { - return getTypes().getPrimitiveType(TypeKind.LONG); - } else if (short.class.equals(primitiveType)) { - return getTypes().getPrimitiveType(TypeKind.SHORT); + /** + * Checks if passed typeElement is assignable to passed type + * + * @param typeElement the type element to check + * @param type the class which typeElement must assignable to + * @return true if typeElement is assignable to type otherwise false. + */ + public boolean isAssignableTo(TypeElement typeElement, Class type) { + return isAssignableTo(typeElement, frameworkToolWrapper.getElements().getTypeElement(type.getCanonicalName()).asType()); } - return null; - } + /** + * Checks if passed typeElement is assignable to passed typeMirror + * + * @param typeElement the type element to check + * @param typeMirror the type mirror which typeElement must assignable to + * @return true if typeElement is assignable to typeMirror otherwise false. + */ + public boolean isAssignableTo(TypeElement typeElement, TypeMirror typeMirror) { + return typeElement != null && isAssignableTo(typeElement.asType(), typeMirror); + } - /** - * Checks if passed typeElement is assignable to passed type - * - * @param typeElement the type element to check - * @param type the class which typeElement must assignable to - * @return true if typeElement is assignable to type otherwise false. - */ - public boolean isAssignableTo(TypeElement typeElement, Class type) { - return isAssignableTo(typeElement, frameworkToolWrapper.getElements().getTypeElement(type.getCanonicalName()).asType()); - } + /** + * Checks whether passed first {@link TypeElement} is assignable to passed second {@link TypeElement}. + * + * @param typeElement1 the type element to check + * @param typeElement2 the type element which typeElement1 must be assignable to + * @return true if typeElement1 is assignable to typeElement2 otherwise false. + */ + public boolean isAssignableTo(TypeElement typeElement1, TypeElement typeElement2) { + return isAssignableTo(typeElement1, typeElement2.asType()); + } - /** - * Checks if passed typeElement is assignable to passed typeMirror - * - * @param typeElement the type element to check - * @param typeMirror the type mirror which typeElement must assignable to - * @return true if typeElement is assignable to typeMirror otherwise false. - */ - public boolean isAssignableTo(TypeElement typeElement, TypeMirror typeMirror) { - return typeElement != null && isAssignableTo(typeElement.asType(), typeMirror); - } + public boolean isAssignableTo(TypeMirror typeMirror1, TypeMirror typeMirror2) { + return typeMirror1 != null && typeMirror2 != null && frameworkToolWrapper.getTypes().isAssignable(typeMirror1, typeMirror2); + } - /** - * Checks whether passed first {@link TypeElement} is assignable to passed second {@link TypeElement}. - * - * @param typeElement1 the type element to check - * @param typeElement2 the type element which typeElement1 must be assignable to - * @return true if typeElement1 is assignable to typeElement2 otherwise false. - */ - public boolean isAssignableTo(TypeElement typeElement1, TypeElement typeElement2) { - return isAssignableTo(typeElement1, typeElement2.asType()); - } + /** + * Check if passed TypeElement matches the passed type. + * + * @param typeElement the type element to check + * @param type the class which the typeElement must match + * @return + */ - public boolean isAssignableTo(TypeMirror typeMirror1, TypeMirror typeMirror2) { - return typeMirror1 != null && typeMirror2 != null && frameworkToolWrapper.getTypes().isAssignable(typeMirror1, typeMirror2); - } + public boolean isTypeEqual(TypeElement typeElement, Class type) { + return isTypeEqual(typeElement, frameworkToolWrapper.getElements().getTypeElement(type.getCanonicalName()).asType()); + } - /** - * Check if passed typeElement matches the passed type. - * - * @param typeElement the type element to check - * @param type the class which the typeElement must match - * @return - */ + /** + * Check if passed TypeElement matches the passed TypeElement. + * + * @param typeElement the type element to check + * @param typeMirror the TypeMirror which the typeElement must match + * @return + */ + public boolean isTypeEqual(TypeElement typeElement, TypeMirror typeMirror) { + return typeElement == null || typeMirror == null ? false : frameworkToolWrapper.getTypes().isSameType(typeElement.asType(), typeMirror); + } - public boolean isTypeEqual(TypeElement typeElement, Class type) { - return isTypeEqual(typeElement, frameworkToolWrapper.getElements().getTypeElement(type.getCanonicalName()).asType()); - } + /** + * Check if passed TypeElement matches the passed TypeElement. + * + * @param typeElement1 the type element to check + * @param typeElement2 the TypeElement which the typeElement must match + * @return + */ + public boolean isTypeEqual(TypeElement typeElement1, TypeElement typeElement2) { + return isTypeEqual(typeElement1, typeElement2.asType()); + } - public boolean isTypeEqual(TypeElement typeElement, TypeMirror typeMirror) { - return typeElement == null || typeMirror == null ? false : frameworkToolWrapper.getTypes().isSameType(typeElement.asType(), typeMirror); - } + /** + * Check if passed TypeMirror matches passed type. + * + * @param typeMirror the TypeMirror to check + * @param type the type in form of a Class to check for + * @return true if passed type represents same type as passed type, otherwise false + */ + public boolean isTypeEqual(TypeMirror typeMirror, Class type) { + return typeMirror == null || typeMirror == null ? false : isTypeEqual(typeMirror, TYPE_RETRIEVAL.getTypeMirror(type)); + } - public boolean isTypeEqual(TypeElement typeElement1, TypeElement typeElement2) { - return isTypeEqual(typeElement1, typeElement2.asType()); - } + /** + * Checks whether both TypeMirror parameters represent the same type. + * Generic type attributes will be taken into account during the comparison. + * + * @param typeMirror1 + * @param typeMirror2 + * @return true if both TypeMirrors represent the same type + */ + public boolean isTypeEqual(TypeMirror typeMirror1, TypeMirror typeMirror2) { + return typeMirror1 != null && typeMirror2 != null && getTypes().isSameType(typeMirror1, typeMirror2); + } + /** + * Checks whether both erased TypeMirror parameters represent the same type. + * + * @param typeMirror1 + * @param typeMirror2 + * @return true if both TypeMirrors represent the same type + */ + public boolean isErasedTypeEqual(TypeMirror typeMirror1, TypeMirror typeMirror2) { + return typeMirror1 != null && typeMirror2 != null && getTypes().isSameType(getTypes().erasure(typeMirror1), getTypes().erasure(typeMirror2)); + } - /** - * Check if passed TypeMirror matches passed type. - * - * @param typeMirror the TypeMirror to check - * @param type the type in form of a Class to check for - * @return true if passed type represents same type as passed type, otherwise false - */ - public boolean isTypeEqual(TypeMirror typeMirror, Class type) { - return typeMirror == null || typeMirror == null ? false : isTypeEqual(typeMirror, getTypeMirror(type)); } - /** - * Checks whether both TypeMirror parameters represent the same type. - * Generic type attributes will be taken into account during the comparison. - * - * @param typeMirror1 - * @param typeMirror2 - * @return true if both TypeMirrors represent the same type - */ - public boolean isTypeEqual(TypeMirror typeMirror1, TypeMirror typeMirror2) { - return typeMirror1 != null && typeMirror2 != null && getTypes().isSameType(typeMirror1, typeMirror2); - } + public class Arrays { - /** - * Checks whether both erased TypeMirror parameters represent the same type. - * - * @param typeMirror1 - * @param typeMirror2 - * @return true if both TypeMirrors represent the same type - */ - public boolean isErasedTypeEqual(TypeMirror typeMirror1, TypeMirror typeMirror2) { - return typeMirror1 != null && typeMirror2 != null && getTypes().isSameType(getTypes().erasure(typeMirror1), getTypes().erasure(typeMirror2)); - } + /** + * Hidden Constructor + */ + private Arrays() { + } - /** - * Gets the component type of an array TypeMirror. - * - * @param typeMirror - * @return returns the component TypeMirror of the passed array TypeMirror, returns null if passed TypeMirror isn't an array or null - */ - public TypeMirror getArraysComponentType(TypeMirror typeMirror) { - return typeMirror != null && checkTypeKind().isArray(typeMirror) ? ((ArrayType) typeMirror).getComponentType() : null; - } + /** + * Gets the component type of an array TypeMirror. + * + * @param typeMirror + * @return returns the component TypeMirror of the passed array TypeMirror, returns null if passed TypeMirror isn't an array or null + */ + public TypeMirror getArraysComponentType(TypeMirror typeMirror) { + return typeMirror != null && CHECK_TYPE_KIND.isArray(typeMirror) ? ((ArrayType) typeMirror).getComponentType() : null; + } - /** - * Checks whether passed {@link TypeMirror} is a void type or not. - * - * @param typeMirror the {@link TypeMirror} to check - * @param type the component type to check for - * @return true id passed type mirror is of kind array with component type, otherwise false - */ - public boolean isArrayOfType(TypeMirror typeMirror, Class type) { - return type != null & isArrayOfType(typeMirror, getTypeMirror(type)); - } + /** + * Checks whether passed {@link TypeMirror} is a void type or not. + * + * @param typeMirror the {@link TypeMirror} to check + * @param type the component type to check for + * @return true id passed type mirror is of kind array with component type, otherwise false + */ + public boolean isArrayOfType(TypeMirror typeMirror, Class type) { + return type != null & isArrayOfType(typeMirror, TYPE_RETRIEVAL.getTypeMirror(type)); + } - /** - * Checks whether passed {@link TypeMirror} is a void type or not. - * - * @param typeMirror the {@link TypeMirror} to check - * @param fullQualifiedClassName the component type to check for - * @return true id passed type mirror is of kind array with component type, otherwise false - */ - public boolean isArrayOfType(TypeMirror typeMirror, String fullQualifiedClassName) { - return fullQualifiedClassName != null & isArrayOfType(typeMirror, getTypeMirror(fullQualifiedClassName)); - } + /** + * Checks whether passed {@link TypeMirror} is a void type or not. + * + * @param typeMirror the {@link TypeMirror} to check + * @param fullQualifiedClassName the component type to check for + * @return true id passed type mirror is of kind array with component type, otherwise false + */ + public boolean isArrayOfType(TypeMirror typeMirror, String fullQualifiedClassName) { + return fullQualifiedClassName != null & isArrayOfType(typeMirror, TYPE_RETRIEVAL.getTypeMirror(fullQualifiedClassName)); + } + + /** + * Checks whether passed {@link TypeMirror} is a void type or not. + * + * @param typeMirror the {@link TypeMirror} to check + * @param componentType the arrays component type to check for + * @return true id passed type mirror is of kind array with component type, otherwise false + */ + public boolean isArrayOfType(TypeMirror typeMirror, TypeMirror componentType) { + return typeMirror != null && componentType != null && CHECK_TYPE_KIND.isArray(typeMirror) && frameworkToolWrapper.getTypes().isSameType(getArraysComponentType(typeMirror), componentType); + } - /** - * Checks whether passed {@link TypeMirror} is a void type or not. - * - * @param typeMirror the {@link TypeMirror} to check - * @param componentType the arrays component type to check for - * @return true id passed type mirror is of kind array with component type, otherwise false - */ - public boolean isArrayOfType(TypeMirror typeMirror, TypeMirror componentType) { - return typeMirror != null && componentType != null && checkTypeKind().isArray(typeMirror) && frameworkToolWrapper.getTypes().isSameType(getArraysComponentType(typeMirror), componentType); } diff --git a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsmatcher/ParameterExecutableElementCharacteristicMatcher.java b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsmatcher/ParameterExecutableElementCharacteristicMatcher.java index 50009d58..699ff662 100644 --- a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsmatcher/ParameterExecutableElementCharacteristicMatcher.java +++ b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsmatcher/ParameterExecutableElementCharacteristicMatcher.java @@ -39,7 +39,7 @@ public boolean checkForMatchingCharacteristic(Element element, Class[] toCheckFo for (int i = 0; i < executableElement.getParameters().size(); i++) { - if (!executableElement.getParameters().get(i).asType().equals(TypeUtils.getTypeUtils(tools).getTypeMirror(toCheckFor[i]))) { + if (!executableElement.getParameters().get(i).asType().equals(TypeUtils.getTypeUtils(tools).TYPE_RETRIEVAL.getTypeMirror(toCheckFor[i]))) { return false; } } diff --git a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsmatcher/ParameterFQNExecutableElementCharacteristicMatcher.java b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsmatcher/ParameterFQNExecutableElementCharacteristicMatcher.java index b0a14265..51bf277c 100644 --- a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsmatcher/ParameterFQNExecutableElementCharacteristicMatcher.java +++ b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsmatcher/ParameterFQNExecutableElementCharacteristicMatcher.java @@ -42,7 +42,7 @@ public boolean checkForMatchingCharacteristic(Element element, String[] toCheckF for (int i = 0; i < executableElement.getParameters().size(); i++) { - TypeMirror parameterTypeMirror = TypeUtils.getTypeUtils(tools).getTypeMirror(toCheckFor[i]); + TypeMirror parameterTypeMirror = TypeUtils.getTypeUtils(tools).TYPE_RETRIEVAL.getTypeMirror(toCheckFor[i]); if (parameterTypeMirror == null) { return false; } diff --git a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsmatcher/TypeElementCharacteristicMatcher.java b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsmatcher/TypeElementCharacteristicMatcher.java index a762c262..81ff0b49 100644 --- a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsmatcher/TypeElementCharacteristicMatcher.java +++ b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/tools/characteristicsmatcher/TypeElementCharacteristicMatcher.java @@ -25,7 +25,7 @@ public boolean checkForMatchingCharacteristic(Element element, Class toCheckFor) // cast to executable element for further checks TypeUtils typeUtils = TypeUtils.getTypeUtils(tools); - return typeUtils.isErasedTypeEqual(element.asType(), typeUtils.getTypeMirror(toCheckFor)); + return typeUtils.TYPE_COMPARISON.isErasedTypeEqual(element.asType(), typeUtils.TYPE_RETRIEVAL.getTypeMirror(toCheckFor)); } diff --git a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/validators/FluentExecutableElementValidator.java b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/validators/FluentExecutableElementValidator.java index 182d5dfd..419a8600 100644 --- a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/validators/FluentExecutableElementValidator.java +++ b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/validators/FluentExecutableElementValidator.java @@ -2,6 +2,7 @@ import de.holisticon.annotationprocessortoolkit.internal.FrameworkToolWrapper; import de.holisticon.annotationprocessortoolkit.tools.ElementUtils; +import de.holisticon.annotationprocessortoolkit.tools.TypeUtils; import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; @@ -72,7 +73,7 @@ public FluentExecutableElementValidator hasVoidReturnType() { boolean nextResult = this.currentValidationResult; - if (ElementUtils.CheckKindOfElement.isMethod(element) && !typeUtils.checkTypeKind().isVoid(element.getReturnType())) { + if (ElementUtils.CheckKindOfElement.isMethod(element) && !TypeUtils.CheckTypeKind.INSTANCE.isVoid(element.getReturnType())) { // validation failed - output message messagerUtils.printMessage(element, getMessageLevel(), getCustomOrDefaultMessage("Method must have void return type")); @@ -94,7 +95,7 @@ public FluentExecutableElementValidator hasNonVoidReturnType() { boolean nextResult = this.currentValidationResult; - if (ElementUtils.CheckKindOfElement.isMethod(element) && typeUtils.checkTypeKind().isVoid(element.getReturnType())) { + if (ElementUtils.CheckKindOfElement.isMethod(element) && TypeUtils.CheckTypeKind.INSTANCE.isVoid(element.getReturnType())) { // validation failed - output message messagerUtils.printMessage(element, getMessageLevel(), getCustomOrDefaultMessage("Method must have non void return type")); @@ -117,7 +118,7 @@ public FluentExecutableElementValidator hasReturnType(Class type) { if (ElementUtils.CheckKindOfElement.isMethod(element) && hasNonVoidReturnType().getValidationResult()) { - if (type == null || !typeUtils.getTypes().isAssignable(element.getReturnType(), typeUtils.getTypeMirror(type))) { + if (type == null || !typeUtils.getTypes().isAssignable(element.getReturnType(), typeUtils.TYPE_RETRIEVAL.getTypeMirror(type))) { // validation failed - output message messagerUtils.printMessage(element, getMessageLevel(), getCustomOrDefaultMessage("Methods return type must be assignable to type ${0}", type.getSimpleName())); @@ -182,7 +183,7 @@ public FluentExecutableElementValidator hasParameters(Class... parameterTypes) { nextResult = isErrorLevel() ? false : nextResult; } else { for (int i = 0; i < element.getParameters().size(); i++) { - if (!element.getParameters().get(i).asType().equals(typeUtils.getTypeMirror(parameterTypes[i]))) { + if (!element.getParameters().get(i).asType().equals(typeUtils.TYPE_RETRIEVAL.getTypeMirror(parameterTypes[i]))) { triggerMismatchingParameterError(parameterTypes); nextResult = isErrorLevel() ? false : nextResult; } diff --git a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/validators/FluentTypeElementValidator.java b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/validators/FluentTypeElementValidator.java index 9fae54bc..b6a60008 100644 --- a/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/validators/FluentTypeElementValidator.java +++ b/annotationprocessor/src/main/java/de/holisticon/annotationprocessortoolkit/validators/FluentTypeElementValidator.java @@ -36,7 +36,7 @@ private FluentTypeElementValidator(FluentTypeElementValidator previousFluentType * @return an immutable FluentExecutableElementValidator instance */ public FluentTypeElementValidator isAssignableTo(Class type) { - return type != null ? isAssignableTo(typeUtils.getTypeElement(type)) : new FluentTypeElementValidator(this, false); + return type != null ? isAssignableTo(typeUtils.TYPE_RETRIEVAL.getTypeElement(type)) : new FluentTypeElementValidator(this, false); } /** @@ -58,7 +58,7 @@ public FluentTypeElementValidator isAssignableTo(TypeElement typeElementToCheck) public FluentTypeElementValidator isAssignableTo(TypeMirror typeMirror) { boolean check = this.currentValidationResult; - if (typeMirror == null || !typeUtils.isAssignableTo(element, typeMirror)) { + if (typeMirror == null || !typeUtils.TYPE_COMPARISON.isAssignableTo(element, typeMirror)) { messagerUtils.printMessage(element, getMessageLevel(), getCustomOrDefaultMessage("type must be assignable to ${0}", typeMirror)); check = isErrorLevel() ? false : check; } diff --git a/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/AbstractAnnotationProcessorTest.java b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/AbstractAnnotationProcessorTest.java index 7fe9d6c5..845e43bd 100644 --- a/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/AbstractAnnotationProcessorTest.java +++ b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/AbstractAnnotationProcessorTest.java @@ -36,7 +36,7 @@ protected void testCase(TypeElement element) { List result = createFluentElementFilter(element.getEnclosedElements()) .applyFilter(Filter.ELEMENT_KIND_FILTER).filterByOneOf(ElementKind.FIELD) .getResult(); - MatcherAssert.assertThat(result, Matchers.hasSize(7)); + MatcherAssert.assertThat(result, Matchers.hasSize(8)); result = createFluentElementFilter( diff --git a/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/FluentElementFilterTest.java b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/FluentElementFilterTest.java index 2f4faf0e..b191a931 100644 --- a/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/FluentElementFilterTest.java +++ b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/FluentElementFilterTest.java @@ -41,7 +41,7 @@ protected void testCase(TypeElement element) { .applyFilter(Filter.ELEMENT_KIND_FILTER).filterByOneOf(ElementKind.FIELD) .getResult(); - MatcherAssert.assertThat(results, Matchers.hasSize(7)); + MatcherAssert.assertThat(results, Matchers.hasSize(8)); for (Element resultElement : results) { MatcherAssert.assertThat(resultElement.getKind(), Matchers.is(ElementKind.FIELD)); @@ -144,7 +144,7 @@ protected void testCase(TypeElement element) { .applyFilter(Filter.ELEMENT_KIND_FILTER).invert().filterByOneOf(ElementKind.FIELD) .getResult(); - MatcherAssert.assertThat(results, Matchers.hasSize(element.getEnclosedElements().size() - 7)); + MatcherAssert.assertThat(results, Matchers.hasSize(element.getEnclosedElements().size() - 8)); for (Element resultElement : results) { MatcherAssert.assertThat(resultElement.getKind(), Matchers.not(ElementKind.FIELD)); diff --git a/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/FluentExecutableElementValidatorTest.java b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/FluentExecutableElementValidatorTest.java index bcc1a7d0..544901ca 100644 --- a/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/FluentExecutableElementValidatorTest.java +++ b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/FluentExecutableElementValidatorTest.java @@ -56,7 +56,7 @@ protected void testCase(TypeElement element) { MatcherAssert.assertThat(getFluentMethodValidator(testElement).hasReturnType(String.class).getValidationResult(), Matchers.is(false)); - getTypeUtils().getTypeElement(AbstractAnnotationProcessorTestBaseClass.AbstractTestAnnotationProcessorClass.class); + getTypeUtils().TYPE_RETRIEVAL.getTypeElement(AbstractAnnotationProcessorTestBaseClass.AbstractTestAnnotationProcessorClass.class); } }, diff --git a/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/TypeUtilsTest.java b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/TypeUtilsTest.java index a27ba093..2c1e35b7 100644 --- a/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/TypeUtilsTest.java +++ b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/TypeUtilsTest.java @@ -34,12 +34,12 @@ public static List data() { new Object[][]{ { - "TypeUtils : Get TypeElement for class", + "TypeUtils.TYPE_RETRIEVAL.getTypeElement : Get TypeElement for class", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - TypeElement typeElement = getTypeUtils().getTypeElement(AbstractTestAnnotationProcessorClass.class); + TypeElement typeElement = getTypeUtils().TYPE_RETRIEVAL.getTypeElement(AbstractTestAnnotationProcessorClass.class); MatcherAssert.assertThat(typeElement, Matchers.notNullValue()); MatcherAssert.assertThat(typeElement.getSimpleName().toString(), Matchers.is(AbstractTestAnnotationProcessorClass.class.getSimpleName())); @@ -51,12 +51,12 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils : Get TypeElement for array class", + "TypeUtils.TYPE_RETRIEVAL.getTypeElement : Get TypeElement for array class", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - TypeElement typeElement = getTypeUtils().getTypeElement(String[].class); + TypeElement typeElement = getTypeUtils().TYPE_RETRIEVAL.getTypeElement(String[].class); MatcherAssert.assertThat("An array TypeMirror can't be converted into a TypeElement so result has to be null", typeElement, Matchers.nullValue()); @@ -67,12 +67,12 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils : Get TypeElement for primitive class", + "TypeUtils.TYPE_RETRIEVAL.getTypeElement : Get TypeElement for primitive class", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - TypeElement typeElement = getTypeUtils().getTypeElement(int.class); + TypeElement typeElement = getTypeUtils().TYPE_RETRIEVAL.getTypeElement(int.class); MatcherAssert.assertThat("A primitive TypeMirror can't be converted into a TypeElement so result has to be null", typeElement, Matchers.nullValue()); @@ -83,12 +83,12 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils : Get TypeMirror for class", + "TypeUtils.TYPE_RETRIEVAL.getTypeMirror : Get TypeMirror for class", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - TypeMirror typeMirror = getTypeUtils().getTypeMirror(AbstractTestAnnotationProcessorClass.class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(AbstractTestAnnotationProcessorClass.class); MatcherAssert.assertThat(typeMirror, Matchers.notNullValue()); MatcherAssert.assertThat(typeMirror.getKind(), Matchers.is(TypeKind.DECLARED)); @@ -101,16 +101,16 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils : Get TypeMirror for array class", + "TypeUtils.TYPE_RETRIEVAL.getTypeMirror : Get TypeMirror for array class", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - TypeMirror typeMirror = getTypeUtils().getTypeMirror(String[].class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(String[].class); MatcherAssert.assertThat(typeMirror, Matchers.notNullValue()); MatcherAssert.assertThat(typeMirror.getKind(), Matchers.is(TypeKind.ARRAY)); - MatcherAssert.assertThat(((ArrayType) typeMirror).getComponentType(), Matchers.is(getTypeUtils().getTypeMirror(String.class))); + MatcherAssert.assertThat(((ArrayType) typeMirror).getComponentType(), Matchers.is(getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(String.class))); } @@ -120,12 +120,29 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils : Get TypeMirror for atomic type", + "TypeUtils.TYPE_RETRIEVAL.getTypeMirror : Test null safety", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - TypeMirror typeMirror = getTypeUtils().getTypeMirror(int.class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getTypeMirror((Class) null); + + MatcherAssert.assertThat("Should return null for null valued input parameter", typeMirror, Matchers.nullValue()); + + + } + }, + true + + + }, + { + "TypeUtils.TYPE_RETRIEVAL.getTypeMirror : Get TypeMirror for atomic type", + new AbstractTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { + + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(int.class); MatcherAssert.assertThat(typeMirror, Matchers.notNullValue()); MatcherAssert.assertThat(typeMirror.getKind(), Matchers.is(TypeKind.INT)); @@ -138,13 +155,13 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getPrimitiveTypeMirror : Get TypeMirror for int", + "TypeUtils.TYPE_RETRIEVAL.getPrimitiveTypeMirror : Get TypeMirror for int", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { // int - TypeMirror typeMirror = getTypeUtils().getPrimitiveTypeMirror(int.class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getPrimitiveTypeMirror(int.class); MatcherAssert.assertThat(typeMirror, Matchers.notNullValue()); MatcherAssert.assertThat(typeMirror.getKind(), Matchers.is(TypeKind.INT)); } @@ -154,12 +171,12 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getPrimitiveTypeMirror : Get TypeMirror for long", + "TypeUtils.TYPE_RETRIEVAL.getPrimitiveTypeMirror : Get TypeMirror for long", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { // long - TypeMirror typeMirror = getTypeUtils().getPrimitiveTypeMirror(long.class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getPrimitiveTypeMirror(long.class); MatcherAssert.assertThat(typeMirror, Matchers.notNullValue()); MatcherAssert.assertThat(typeMirror.getKind(), Matchers.is(TypeKind.LONG)); } @@ -169,12 +186,12 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getPrimitiveTypeMirror : Get TypeMirror for short", + "TypeUtils.TYPE_RETRIEVAL.getPrimitiveTypeMirror : Get TypeMirror for short", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { // short - TypeMirror typeMirror = getTypeUtils().getPrimitiveTypeMirror(short.class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getPrimitiveTypeMirror(short.class); MatcherAssert.assertThat(typeMirror, Matchers.notNullValue()); MatcherAssert.assertThat(typeMirror.getKind(), Matchers.is(TypeKind.SHORT)); } @@ -184,12 +201,12 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getPrimitiveTypeMirror : Get TypeMirror for boolean", + "TypeUtils.TYPE_RETRIEVAL.getPrimitiveTypeMirror : Get TypeMirror for boolean", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { // boolean - TypeMirror typeMirror = getTypeUtils().getPrimitiveTypeMirror(boolean.class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getPrimitiveTypeMirror(boolean.class); MatcherAssert.assertThat(typeMirror, Matchers.notNullValue()); MatcherAssert.assertThat(typeMirror.getKind(), Matchers.is(TypeKind.BOOLEAN)); } @@ -199,12 +216,12 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getPrimitiveTypeMirror : Get TypeMirror for byte", + "TypeUtils.TYPE_RETRIEVAL.getPrimitiveTypeMirror : Get TypeMirror for byte", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { // byte - TypeMirror typeMirror = getTypeUtils().getPrimitiveTypeMirror(byte.class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getPrimitiveTypeMirror(byte.class); MatcherAssert.assertThat(typeMirror, Matchers.notNullValue()); MatcherAssert.assertThat(typeMirror.getKind(), Matchers.is(TypeKind.BYTE)); } @@ -214,12 +231,12 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getPrimitiveTypeMirror : Get TypeMirror for float", + "TypeUtils.TYPE_RETRIEVAL.getPrimitiveTypeMirror : Get TypeMirror for float", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { // float - TypeMirror typeMirror = getTypeUtils().getPrimitiveTypeMirror(float.class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getPrimitiveTypeMirror(float.class); MatcherAssert.assertThat(typeMirror, Matchers.notNullValue()); MatcherAssert.assertThat(typeMirror.getKind(), Matchers.is(TypeKind.FLOAT)); } @@ -229,12 +246,12 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getPrimitiveTypeMirror : Get TypeMirror for double", + "TypeUtils.TYPE_RETRIEVAL.getPrimitiveTypeMirror : Get TypeMirror for double", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { // double - TypeMirror typeMirror = getTypeUtils().getPrimitiveTypeMirror(double.class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getPrimitiveTypeMirror(double.class); MatcherAssert.assertThat(typeMirror, Matchers.notNullValue()); MatcherAssert.assertThat(typeMirror.getKind(), Matchers.is(TypeKind.DOUBLE)); } @@ -244,12 +261,12 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getPrimitiveTypeMirror : Get TypeMirror for char", + "TypeUtils.TYPE_RETRIEVAL.getPrimitiveTypeMirror : Get TypeMirror for char", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { // char - TypeMirror typeMirror = getTypeUtils().getPrimitiveTypeMirror(char.class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getPrimitiveTypeMirror(char.class); MatcherAssert.assertThat(typeMirror, Matchers.notNullValue()); MatcherAssert.assertThat(typeMirror.getKind(), Matchers.is(TypeKind.CHAR)); } @@ -259,12 +276,12 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getPrimitiveTypeMirror : Get TypeMirror for null value should return null", + "TypeUtils.TYPE_RETRIEVAL.getPrimitiveTypeMirror : Get TypeMirror for null value should return null", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { // null value - TypeMirror typeMirror = getTypeUtils().getPrimitiveTypeMirror(null); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getPrimitiveTypeMirror(null); MatcherAssert.assertThat(typeMirror, Matchers.nullValue()); } }, @@ -273,12 +290,12 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getPrimitiveTypeMirror : Get TypeMirror for non primitive type should return String", + "TypeUtils.TYPE_RETRIEVAL.getPrimitiveTypeMirror : Get TypeMirror for non primitive type should return String", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { // non primitive type value - TypeMirror typeMirror = getTypeUtils().getPrimitiveTypeMirror(String.class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getPrimitiveTypeMirror(String.class); MatcherAssert.assertThat(typeMirror, Matchers.nullValue()); } }, @@ -287,16 +304,16 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.isArrayOfType : ", + "TypeUtils.TYPE_RETRIEVAL.getTypeMirror : get TypeMirror of array", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - TypeMirror typeMirror = getTypeUtils().getTypeMirror(String[].class); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(String[].class); MatcherAssert.assertThat(typeMirror, Matchers.notNullValue()); MatcherAssert.assertThat(typeMirror.getKind(), Matchers.is(TypeKind.ARRAY)); - MatcherAssert.assertThat(((ArrayType) typeMirror).getComponentType(), Matchers.is(getTypeUtils().getTypeMirror(String.class))); + MatcherAssert.assertThat(((ArrayType) typeMirror).getComponentType(), Matchers.is(getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(String.class))); } @@ -306,17 +323,26 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getArraysComponentType : Get component type of TypeMirror array ", + "TypeUtils.TYPE_RETRIEVAL.getTypes : get encapsulated javax.lang.model.util.Types instance", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - TypeMirror typeMirror = getTypeUtils().getTypeMirror(String[].class); + MatcherAssert.assertThat(getTypeUtils().getTypes(), Matchers.notNullValue()); - MatcherAssert.assertThat("PRECONDITION : typeMirror should not be null", typeMirror, Matchers.notNullValue()); - MatcherAssert.assertThat("PRECONDITION : typeMirror should be array", typeMirror.getKind(), Matchers.is(TypeKind.ARRAY)); + } + }, + true + + + }, + { + "TypeUtils.TYPE_RETRIEVAL.getTypeElement() : test to get Element by class name", + new AbstractTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { - MatcherAssert.assertThat(getTypeUtils().getArraysComponentType(typeMirror), Matchers.is(getTypeUtils().getTypeMirror(String.class))); + MatcherAssert.assertThat(getTypeUtils().TYPE_RETRIEVAL.getTypeElement("de.holisticon.annotationprocessor.AnnotationProcessorTestClass"), Matchers.is(element)); } @@ -326,18 +352,27 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.isArrayOfType : Should check if the TypeMirror has a specific component type correctly", + "TypeUtils.TYPE_RETRIEVAL.getTypeElement() : test behavior with non existing class name parameter", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - TypeMirror typeMirror = getTypeUtils().getTypeMirror(String[].class); + MatcherAssert.assertThat(getTypeUtils().TYPE_RETRIEVAL.getTypeElement("de.holisticon.annotationprocessor.AnnotationProcessorTestClassXXXX"), Matchers.nullValue()); - MatcherAssert.assertThat("PRECONDITION : typeMirror should not be null", typeMirror, Matchers.notNullValue()); - MatcherAssert.assertThat("PRECONDITION : typeMirror should be array", typeMirror.getKind(), Matchers.is(TypeKind.ARRAY)); - MatcherAssert.assertThat("Should detect matching component type correctly", getTypeUtils().isArrayOfType(typeMirror, String.class)); - MatcherAssert.assertThat("Should detect non matching component type correctly", !getTypeUtils().isArrayOfType(typeMirror, Boolean.class)); + } + }, + true + }, + { + "TypeUtils.TYPE_RETRIEVAL.getTypeElement() : test behavior with null valued class name parameter", + new AbstractTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { + + MatcherAssert.assertThat(getTypeUtils().TYPE_RETRIEVAL.getTypeElement((String) null), + Matchers.nullValue()); + } }, @@ -345,20 +380,14 @@ protected void testCase(TypeElement element) { }, - { - "TypeUtils.isArrayOfType : Should check if the TypeMirror has a specific component type correctly", + "TypeUtils.TYPE_RETRIEVAL.getTypeElement() : test to get TypeMirror by class name", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - TypeMirror typeMirror = getTypeUtils().getTypeMirror(String[].class); + MatcherAssert.assertThat(getTypeUtils().TYPE_RETRIEVAL.getTypeMirror("de.holisticon.annotationprocessor.AnnotationProcessorTestClass"), Matchers.is(element.asType())); - MatcherAssert.assertThat("PRECONDITION : typeMirror should not be null", typeMirror, Matchers.notNullValue()); - MatcherAssert.assertThat("PRECONDITION : typeMirror should be array", typeMirror.getKind(), Matchers.is(TypeKind.ARRAY)); - - MatcherAssert.assertThat("Should detect matching component type correctly", getTypeUtils().isArrayOfType(typeMirror, String.class.getCanonicalName())); - MatcherAssert.assertThat("Should detect non matching component type correctly", !getTypeUtils().isArrayOfType(typeMirror, Boolean.class.getCanonicalName())); } }, @@ -367,18 +396,13 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.isArrayOfType : Should check if the TypeMirror has a specific component type correctly", + "TypeUtils.TYPE_RETRIEVAL.getTypeMirror() : test to get TypeMirror by class name", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - TypeMirror typeMirror = getTypeUtils().getTypeMirror(String[].class); + MatcherAssert.assertThat(getTypeUtils().TYPE_RETRIEVAL.getTypeMirror("de.holisticon.annotationprocessor.AnnotationProcessorTestClass"), Matchers.is(element.asType())); - MatcherAssert.assertThat("PRECONDITION : typeMirror should not be null", typeMirror, Matchers.notNullValue()); - MatcherAssert.assertThat("PRECONDITION : typeMirror should be array", typeMirror.getKind(), Matchers.is(TypeKind.ARRAY)); - - MatcherAssert.assertThat("Should detect matching component type correctly", getTypeUtils().isArrayOfType(typeMirror, getTypeUtils().getTypeMirror(String.class))); - MatcherAssert.assertThat("Should detect non matching component type correctly", !getTypeUtils().isArrayOfType(typeMirror, getTypeUtils().getTypeMirror(Boolean.class))); } }, @@ -387,29 +411,26 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils : test isAssignableTo", + "TypeUtils.TYPE_RETRIEVAL.getTypeMirror() : test behavior with non existing class name parameter", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - MatcherAssert.assertThat("type element should be detected as assignable to Object", getTypeUtils().isAssignableTo(element, Object.class)); - MatcherAssert.assertThat("type element shouldn't be detected as assignable to InputStream", !getTypeUtils().isAssignableTo(element, InputStream.class)); + MatcherAssert.assertThat(getTypeUtils().TYPE_RETRIEVAL.getTypeMirror("de.holisticon.annotationprocessor.AnnotationProcessorTestClassXXXX"), Matchers.nullValue()); } }, true - - }, { - "TypeUtils : test isAssignableTo", + "TypeUtils.TYPE_RETRIEVAL.getTypeMirror() : test behavior with null valued class name parameter", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - MatcherAssert.assertThat("type element should be detected as assignable to Object", getTypeUtils().isAssignableTo(element, getTypeUtils().getTypeElement(Object.class))); - MatcherAssert.assertThat("type element shouldn't be detected as assignable to InputStream", !getTypeUtils().isAssignableTo(element, getTypeUtils().getTypeElement(InputStream.class))); + MatcherAssert.assertThat(getTypeUtils().TYPE_RETRIEVAL.getTypeMirror((String) null), Matchers.nullValue()); + } }, @@ -418,28 +439,40 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils : test isAssignableTo", + "TypeUtils.TYPE_RETRIEVAL.getTypeElement() : test behavior with anonymous class type mirror - should return null because no TyoeElement exists", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - MatcherAssert.assertThat("type element should be detected as assignable to Object", getTypeUtils().isAssignableTo(element, getTypeUtils().getTypeMirror(Object.class))); - MatcherAssert.assertThat("type element shouldn't be detected as assignable to InputStream", !getTypeUtils().isAssignableTo(element, getTypeUtils().getTypeMirror(InputStream.class))); + Comparable anonymousComparable = new Comparable() { + @Override + public int compareTo(Long o) { + return 0; + } + }; + + MatcherAssert.assertThat(getTypeUtils().TYPE_RETRIEVAL.getTypeElement(anonymousComparable.getClass()), Matchers.nullValue()); + } }, true + }, { - "TypeUtils : test check for void type ", + "TypeUtils.ARRAYS.getArraysComponentType : Get component type of TypeMirror array ", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - MatcherAssert.assertThat(getTypeUtils().checkTypeKind().isVoid(CastElement.castMethod(AccessEnclosedElements.getEnclosedElementsByName(element, "synchronizedMethod").get(0)).getReturnType()), Matchers.is(true)); - MatcherAssert.assertThat(getTypeUtils().checkTypeKind().isVoid(element.asType()), Matchers.is(false)); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(String[].class); + + MatcherAssert.assertThat("PRECONDITION : typeMirror should not be null", typeMirror, Matchers.notNullValue()); + MatcherAssert.assertThat("PRECONDITION : typeMirror should be array", typeMirror.getKind(), Matchers.is(TypeKind.ARRAY)); + + MatcherAssert.assertThat(getTypeUtils().ARRAYS.getArraysComponentType(typeMirror), Matchers.is(getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(String.class))); } @@ -449,12 +482,18 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils : get encapsulated javax.lang.model.util.Types instance", + "TypeUtils.ARRAYS.isArrayOfType : Should check if the TypeMirror has a specific component type correctly", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - MatcherAssert.assertThat(getTypeUtils().getTypes(), Matchers.notNullValue()); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(String[].class); + + MatcherAssert.assertThat("PRECONDITION : typeMirror should not be null", typeMirror, Matchers.notNullValue()); + MatcherAssert.assertThat("PRECONDITION : typeMirror should be array", typeMirror.getKind(), Matchers.is(TypeKind.ARRAY)); + + MatcherAssert.assertThat("Should detect matching component type correctly", getTypeUtils().ARRAYS.isArrayOfType(typeMirror, String.class)); + MatcherAssert.assertThat("Should detect non matching component type correctly", !getTypeUtils().ARRAYS.isArrayOfType(typeMirror, Boolean.class)); } }, @@ -462,14 +501,20 @@ protected void testCase(TypeElement element) { }, + { - "TypeUtils.getTypeElement() : test to get Element by class name", + "TypeUtils.ARRAYS.isArrayOfType : Should check if the TypeMirror has a specific component type correctly", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - MatcherAssert.assertThat(getTypeUtils().getTypeElement("de.holisticon.annotationprocessor.AnnotationProcessorTestClass"), Matchers.is(element)); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(String[].class); + MatcherAssert.assertThat("PRECONDITION : typeMirror should not be null", typeMirror, Matchers.notNullValue()); + MatcherAssert.assertThat("PRECONDITION : typeMirror should be array", typeMirror.getKind(), Matchers.is(TypeKind.ARRAY)); + + MatcherAssert.assertThat("Should detect matching component type correctly", getTypeUtils().ARRAYS.isArrayOfType(typeMirror, String.class.getCanonicalName())); + MatcherAssert.assertThat("Should detect non matching component type correctly", !getTypeUtils().ARRAYS.isArrayOfType(typeMirror, Boolean.class.getCanonicalName())); } }, @@ -478,27 +523,35 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getTypeElement() : test behavior with non existing class name parameter", + "TypeUtils.ARRAYS.isArrayOfType : Should check if the TypeMirror has a specific component type correctly", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - MatcherAssert.assertThat(getTypeUtils().getTypeElement("de.holisticon.annotationprocessor.AnnotationProcessorTestClassXXXX"), Matchers.nullValue()); + TypeMirror typeMirror = getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(String[].class); + + MatcherAssert.assertThat("PRECONDITION : typeMirror should not be null", typeMirror, Matchers.notNullValue()); + MatcherAssert.assertThat("PRECONDITION : typeMirror should be array", typeMirror.getKind(), Matchers.is(TypeKind.ARRAY)); + MatcherAssert.assertThat("Should detect matching component type correctly", getTypeUtils().ARRAYS.isArrayOfType(typeMirror, getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(String.class))); + MatcherAssert.assertThat("Should detect non matching component type correctly", !getTypeUtils().ARRAYS.isArrayOfType(typeMirror, getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(Boolean.class))); } }, true + + }, { - "TypeUtils.getTypeElement() : test behavior with null valued class name parameter", + "TypeUtils.ARRAYS.getArraysComponentType() : test if component type of TypeMirror of kind ARRAY is returned correctly", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - MatcherAssert.assertThat(getTypeUtils().getTypeElement((String) null), - Matchers.nullValue()); + TypeMirror input = getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(String[].class); + + MatcherAssert.assertThat(getTypeUtils().ARRAYS.getArraysComponentType(input), Matchers.is(getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(String.class))); } }, @@ -507,12 +560,13 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getTypeElement() : test to get TypeMirror by class name", + "TypeUtils.TYPE_COMPARISON.isAssignableTo : test isAssignableTo", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - MatcherAssert.assertThat(getTypeUtils().getTypeMirror("de.holisticon.annotationprocessor.AnnotationProcessorTestClass"), Matchers.is(element.asType())); + MatcherAssert.assertThat("type element should be detected as assignable to Object", getTypeUtils().TYPE_COMPARISON.isAssignableTo(element, Object.class)); + MatcherAssert.assertThat("type element shouldn't be detected as assignable to InputStream", !getTypeUtils().TYPE_COMPARISON.isAssignableTo(element, InputStream.class)); } @@ -522,13 +576,13 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getTypeMirror() : test to get TypeMirror by class name", + "TypeUtils.TYPE_COMPARISON.isAssignableTo : test isAssignableTo", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - MatcherAssert.assertThat(getTypeUtils().getTypeMirror("de.holisticon.annotationprocessor.AnnotationProcessorTestClass"), Matchers.is(element.asType())); - + MatcherAssert.assertThat("type element should be detected as assignable to Object", getTypeUtils().TYPE_COMPARISON.isAssignableTo(element, getTypeUtils().TYPE_RETRIEVAL.getTypeElement(Object.class))); + MatcherAssert.assertThat("type element shouldn't be detected as assignable to InputStream", !getTypeUtils().TYPE_COMPARISON.isAssignableTo(element, getTypeUtils().TYPE_RETRIEVAL.getTypeElement(InputStream.class))); } }, @@ -537,43 +591,65 @@ protected void testCase(TypeElement element) { }, { - "TypeUtils.getTypeMirror() : test behavior with non existing class name parameter", + "TypeUtils.TYPE_COMPARISON.isAssignableTo : test isAssignableTo", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - MatcherAssert.assertThat(getTypeUtils().getTypeMirror("de.holisticon.annotationprocessor.AnnotationProcessorTestClassXXXX"), Matchers.nullValue()); + MatcherAssert.assertThat("type element should be detected as assignable to Object", getTypeUtils().TYPE_COMPARISON.isAssignableTo(element, getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(Object.class))); + MatcherAssert.assertThat("type element shouldn't be detected as assignable to InputStream", !getTypeUtils().TYPE_COMPARISON.isAssignableTo(element, getTypeUtils().TYPE_RETRIEVAL.getTypeMirror(InputStream.class))); } }, true + }, { - "TypeUtils.getTypeMirror() : test behavior with null valued class name parameter", + "TypeUtils.TYPE_COMPARISON.isTypeEqual(TypeElement,Class) : test if matching class is detected correctly", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { - MatcherAssert.assertThat(getTypeUtils().getTypeMirror((String) null), Matchers.nullValue()); + Class type = StringBuilder.class; + TypeElement elementForComparison = getTypeUtils().TYPE_RETRIEVAL.getTypeElement(type); + MatcherAssert.assertThat("Should have found match", getTypeUtils().TYPE_COMPARISON.isTypeEqual(elementForComparison, type)); + } }, true - }, { - "TypeUtils.getArraysComponentType() : test if component type of TypeMirror of kind ARRAY is returned correctly", + "TypeUtils.TYPE_COMPARISON.isTypeEqual(TypeElement,Class) : test if non matching class is detected correctly", new AbstractTestAnnotationProcessorClass() { @Override protected void testCase(TypeElement element) { + Class type = StringBuilder.class; + TypeElement elementForComparison = getTypeUtils().TYPE_RETRIEVAL.getTypeElement(type); + + + MatcherAssert.assertThat("Should have found match", !getTypeUtils().TYPE_COMPARISON.isTypeEqual(elementForComparison, String.class)); + + } + }, + true + + }, + + + { + "TypeUtils.CHECK_TYPE_KIND.isVoid : test check for void type ", + new AbstractTestAnnotationProcessorClass() { + @Override + protected void testCase(TypeElement element) { - TypeMirror input = getTypeUtils().getTypeMirror(String[].class); + MatcherAssert.assertThat(getTypeUtils().CHECK_TYPE_KIND.isVoid(CastElement.castMethod(AccessEnclosedElements.getEnclosedElementsByName(element, "synchronizedMethod").get(0)).getReturnType()), Matchers.is(true)); + MatcherAssert.assertThat(getTypeUtils().CHECK_TYPE_KIND.isVoid(element.asType()), Matchers.is(false)); - MatcherAssert.assertThat(getTypeUtils().getArraysComponentType(input), Matchers.is(getTypeUtils().getTypeMirror(String.class))); } }, diff --git a/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/TypeUtilsTest_CheckTypeKind.java b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/TypeUtilsTest_CheckTypeKind.java new file mode 100644 index 00000000..1648ae25 --- /dev/null +++ b/annotationprocessor/src/test/java/de/holisticon/annotationprocessortoolkit/tools/TypeUtilsTest_CheckTypeKind.java @@ -0,0 +1,166 @@ +package de.holisticon.annotationprocessortoolkit.tools; + +import org.hamcrest.MatcherAssert; +import org.junit.Test; +import org.mockito.Mockito; + +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; + +/** + * Unit test for {@link TypeUtils.CheckTypeKind}. + */ +public class TypeUtilsTest_CheckTypeKind { + + private TypeMirror getTypeMirrorMockOfKind(TypeKind typeKind) { + TypeMirror typeMirror = Mockito.mock(TypeMirror.class); + Mockito.when(typeMirror.getKind()).thenReturn(typeKind); + return typeMirror; + + } + + + // --------------------------------------------- + // -- isVoid tests + // --------------------------------------------- + + + @Test + public void testCheckTypeKind_isVoid() { + + + MatcherAssert.assertThat("Should detect void type kind", TypeUtils.CheckTypeKind.INSTANCE.isArray(getTypeMirrorMockOfKind(TypeKind.ARRAY))); + + } + + @Test + public void testCheckTypeKind_isVoid_noVoidKind() { + + MatcherAssert.assertThat("Should not detect void type kind", !TypeUtils.CheckTypeKind.INSTANCE.isArray(getTypeMirrorMockOfKind(TypeKind.DECLARED))); + + } + + @Test + public void testCheckTypeKind_isVoid_nullSafety() { + + MatcherAssert.assertThat("Should return false for null valued parameter", !TypeUtils.CheckTypeKind.INSTANCE.isVoid(null)); + + } + + // --------------------------------------------- + // -- isArray tests + // --------------------------------------------- + + + @Test + public void testCheckTypeKind_isArray() { + + + MatcherAssert.assertThat("Should detect void type kind", TypeUtils.CheckTypeKind.INSTANCE.isArray(getTypeMirrorMockOfKind(TypeKind.ARRAY))); + + } + + @Test + public void testCheckTypeKind_isArray_noVoidKind() { + + + MatcherAssert.assertThat("Should not detect void type kind", !TypeUtils.CheckTypeKind.INSTANCE.isArray(getTypeMirrorMockOfKind(TypeKind.DECLARED))); + + } + + @Test + public void testCheckTypeKind_isArray_nullSafety() { + + MatcherAssert.assertThat("Should return false for null valued parameter", !TypeUtils.CheckTypeKind.INSTANCE.isArray(null)); + + } + + // --------------------------------------------- + // -- isPrimitive tests + // --------------------------------------------- + + + @Test + public void testCheckTypeKind_isPrimitive() { + + + MatcherAssert.assertThat("Should detect void primitive kind for long", TypeUtils.CheckTypeKind.INSTANCE.isPrimitive(getTypeMirrorMockOfKind(TypeKind.LONG))); + MatcherAssert.assertThat("Should detect void primitive kind for int", TypeUtils.CheckTypeKind.INSTANCE.isPrimitive(getTypeMirrorMockOfKind(TypeKind.INT))); + MatcherAssert.assertThat("Should detect void primitive kind for float", TypeUtils.CheckTypeKind.INSTANCE.isPrimitive(getTypeMirrorMockOfKind(TypeKind.FLOAT))); + MatcherAssert.assertThat("Should detect void primitive kind for double", TypeUtils.CheckTypeKind.INSTANCE.isPrimitive(getTypeMirrorMockOfKind(TypeKind.DOUBLE))); + MatcherAssert.assertThat("Should detect void primitive kind for short", TypeUtils.CheckTypeKind.INSTANCE.isPrimitive(getTypeMirrorMockOfKind(TypeKind.SHORT))); + MatcherAssert.assertThat("Should detect void primitive kind for boolean", TypeUtils.CheckTypeKind.INSTANCE.isPrimitive(getTypeMirrorMockOfKind(TypeKind.BOOLEAN))); + MatcherAssert.assertThat("Should detect void primitive kind for byte", TypeUtils.CheckTypeKind.INSTANCE.isPrimitive(getTypeMirrorMockOfKind(TypeKind.BYTE))); + MatcherAssert.assertThat("Should detect void primitive kind for char", TypeUtils.CheckTypeKind.INSTANCE.isPrimitive(getTypeMirrorMockOfKind(TypeKind.CHAR))); + + } + + @Test + public void testCheckTypeKind_isPrimitive_noVoidKind() { + + + MatcherAssert.assertThat("Should not detect primitive type kind", !TypeUtils.CheckTypeKind.INSTANCE.isPrimitive(getTypeMirrorMockOfKind(TypeKind.DECLARED))); + + } + + @Test + public void testCheckTypeKind_isPrimitive_nullSafety() { + + MatcherAssert.assertThat("Should return false for null valued parameter", !TypeUtils.CheckTypeKind.INSTANCE.isPrimitive(null)); + + } + +// --------------------------------------------- + // -- isOfTypeKind tests + // --------------------------------------------- + + + @Test + public void testCheckTypeKind_isOfTypeKind_matchingTypeKinds() { + + + MatcherAssert.assertThat("Should detect matching kind for long", TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.LONG), TypeKind.LONG)); + MatcherAssert.assertThat("Should detect matching kind for int", TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.INT), TypeKind.INT)); + MatcherAssert.assertThat("Should detect matching kind for float", TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.FLOAT), TypeKind.FLOAT)); + MatcherAssert.assertThat("Should detect matching kind for double", TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.DOUBLE), TypeKind.DOUBLE)); + MatcherAssert.assertThat("Should detect matching kind for short", TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.SHORT), TypeKind.SHORT)); + MatcherAssert.assertThat("Should detect matching kind for boolean", TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.BOOLEAN), TypeKind.BOOLEAN)); + MatcherAssert.assertThat("Should detect matching kind for byte", TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.BYTE), TypeKind.BYTE)); + MatcherAssert.assertThat("Should detect matching kind for char", TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.CHAR), TypeKind.CHAR)); + MatcherAssert.assertThat("Should detect matching kind for char", TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.DECLARED), TypeKind.DECLARED)); + MatcherAssert.assertThat("Should detect matching kind for char", TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.ARRAY), TypeKind.ARRAY)); + MatcherAssert.assertThat("Should detect matching kind for char", TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.VOID), TypeKind.VOID)); + } + + public void testCheckTypeKind_isOfTypeKind_nonMatchingTypeKinds() { + + + MatcherAssert.assertThat("Should detect matching kind for long", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.LONG), TypeKind.DECLARED)); + MatcherAssert.assertThat("Should detect matching kind for int", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.INT), TypeKind.DECLARED)); + MatcherAssert.assertThat("Should detect matching kind for float", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.FLOAT), TypeKind.DECLARED)); + MatcherAssert.assertThat("Should detect matching kind for double", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.DOUBLE), TypeKind.DECLARED)); + MatcherAssert.assertThat("Should detect matching kind for short", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.SHORT), TypeKind.DECLARED)); + MatcherAssert.assertThat("Should detect matching kind for boolean", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.BOOLEAN), TypeKind.DECLARED)); + MatcherAssert.assertThat("Should detect matching kind for byte", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.BYTE), TypeKind.DECLARED)); + MatcherAssert.assertThat("Should detect matching kind for char", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.CHAR), TypeKind.DECLARED)); + MatcherAssert.assertThat("Should detect matching kind for char", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.DECLARED), TypeKind.ARRAY)); + MatcherAssert.assertThat("Should detect matching kind for char", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.ARRAY), TypeKind.DECLARED)); + MatcherAssert.assertThat("Should detect matching kind for char", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.VOID), TypeKind.DECLARED)); + } + + @Test + public void testCheckTypeKind_isOfTypeKind_nullSafetyFirstParameter() { + + MatcherAssert.assertThat("Should return false for null valued parameter", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(null, TypeKind.VOID)); + + } + + @Test + public void testCheckTypeKind_isOfTypeKind_nullSafetySecondParameter() { + + MatcherAssert.assertThat("Should return false for null valued parameter", !TypeUtils.CheckTypeKind.INSTANCE.isOfTypeKind(getTypeMirrorMockOfKind(TypeKind.VOID), null)); + + } + + +} \ No newline at end of file diff --git a/annotationprocessor/src/test/resources/AnnotationProcessorTestClass.java b/annotationprocessor/src/test/resources/AnnotationProcessorTestClass.java index 20314201..7cd2b20f 100644 --- a/annotationprocessor/src/test/resources/AnnotationProcessorTestClass.java +++ b/annotationprocessor/src/test/resources/AnnotationProcessorTestClass.java @@ -1,8 +1,10 @@ package de.holisticon.annotationprocessor; -import de.holisticon.annotationprocessortoolkit.TestAnnotation; import de.holisticon.annotationprocessortoolkit.FilterTestAnnotation1; import de.holisticon.annotationprocessortoolkit.FilterTestAnnotation2; +import de.holisticon.annotationprocessortoolkit.TestAnnotation; + +import java.util.Comparator; /** * Test class for annotation processor tools. @@ -30,6 +32,14 @@ public static class EmbeddedStaticClass { } + public Comparator comparatorWithAnonymousClass = new Comparator() { + @Override + public int compare(Long o1, Long o2) { + return 0; + } + }; + + public class EmbeddedClass { }