diff --git a/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/AnnotationUtils.java b/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/AnnotationUtils.java index 34d36b608..f7d8aed46 100644 --- a/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/AnnotationUtils.java +++ b/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/AnnotationUtils.java @@ -22,6 +22,7 @@ import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; import javax.lang.model.type.ArrayType; import javax.lang.model.type.TypeMirror; import java.lang.annotation.Annotation; @@ -33,12 +34,15 @@ import java.util.Objects; import java.util.function.Predicate; +import static io.microsphere.annotation.processor.util.TypeUtils.getAllTypeElements; +import static io.microsphere.annotation.processor.util.TypeUtils.getTypeElement; import static io.microsphere.annotation.processor.util.TypeUtils.isSameType; -import static io.microsphere.annotation.processor.util.TypeUtils.isTypeElement; import static io.microsphere.annotation.processor.util.TypeUtils.ofTypeElement; +import static io.microsphere.collection.CollectionUtils.isEmpty; +import static io.microsphere.collection.CollectionUtils.size; import static io.microsphere.lang.function.Predicates.EMPTY_PREDICATE_ARRAY; import static io.microsphere.lang.function.Streams.filterAll; -import static io.microsphere.lang.function.Streams.filterFirst; +import static io.microsphere.util.ArrayUtils.isNotEmpty; import static io.microsphere.util.ClassLoaderUtils.resolveClass; import static java.lang.Enum.valueOf; import static java.util.Collections.emptyList; @@ -53,111 +57,242 @@ public interface AnnotationUtils { static AnnotationMirror getAnnotation(AnnotatedConstruct annotatedConstruct, Class annotationClass) { - return annotationClass == null ? null : getAnnotation(annotatedConstruct, annotationClass.getTypeName()); + if (annotatedConstruct == null || annotationClass == null) { + return null; + } + return getAnnotation(annotatedConstruct, annotationClass.getName()); } static AnnotationMirror getAnnotation(AnnotatedConstruct annotatedConstruct, CharSequence annotationClassName) { + if (annotatedConstruct == null || annotationClassName == null) { + return null; + } List annotations = getAnnotations(annotatedConstruct, annotationClassName); return annotations.isEmpty() ? null : annotations.get(0); } - static List getAnnotations(AnnotatedConstruct annotatedConstruct, Class annotationClass) { - return annotationClass == null ? emptyList() : getAnnotations(annotatedConstruct, annotationClass.getTypeName()); + static List getAnnotations(AnnotatedConstruct annotatedConstruct) { + if (annotatedConstruct == null) { + return emptyList(); + } + return findAnnotations(annotatedConstruct, EMPTY_PREDICATE_ARRAY); } - static List getAnnotations(AnnotatedConstruct annotatedConstruct, CharSequence annotationClassName) { - return findAnnotations(annotatedConstruct, annotation -> isSameType(annotation.getAnnotationType(), annotationClassName)); + static List getAnnotations(AnnotatedConstruct annotatedConstruct, Class annotationClass) { + if (annotatedConstruct == null || annotationClass == null) { + return emptyList(); + } + return getAnnotations(annotatedConstruct, annotationClass.getTypeName()); } - static List getAnnotations(AnnotatedConstruct annotatedConstruct) { - return findAnnotations(annotatedConstruct, EMPTY_PREDICATE_ARRAY); + static List getAnnotations(AnnotatedConstruct annotatedConstruct, CharSequence annotationClassName) { + if (annotatedConstruct == null || annotationClassName == null) { + return emptyList(); + } + return findAnnotations(annotatedConstruct, annotation -> matchesAnnotationClassName(annotation, annotationClassName)); } static List getAllAnnotations(TypeMirror type) { + if (type == null) { + return emptyList(); + } return getAllAnnotations(ofTypeElement(type)); } static List getAllAnnotations(Element element) { + if (element == null) { + return emptyList(); + } return findAllAnnotations(element, EMPTY_PREDICATE_ARRAY); } static List getAllAnnotations(TypeMirror type, Class annotationClass) { + if (type == null || annotationClass == null) { + return emptyList(); + } return getAllAnnotations(ofTypeElement(type), annotationClass); } static List getAllAnnotations(Element element, Class annotationClass) { - return element == null || annotationClass == null ? emptyList() : getAllAnnotations(element, annotationClass.getTypeName()); + if (element == null || annotationClass == null) { + return emptyList(); + } + return getAllAnnotations(element, annotationClass.getTypeName()); } static List getAllAnnotations(TypeMirror type, CharSequence annotationClassName) { + if (type == null || annotationClassName == null) { + return emptyList(); + } return getAllAnnotations(ofTypeElement(type), annotationClassName); } static List getAllAnnotations(Element element, CharSequence annotationClassName) { - return findAllAnnotations(element, annotation -> isSameType(annotation.getAnnotationType(), annotationClassName)); + if (element == null || annotationClassName == null) { + return emptyList(); + } + return findAllAnnotations(element, annotation -> matchesAnnotationClassName(annotation, annotationClassName)); } static List getAllAnnotations(ProcessingEnvironment processingEnv, Type annotatedType) { + if (processingEnv == null || annotatedType == null) { + return emptyList(); + } return findAllAnnotations(processingEnv, annotatedType, EMPTY_PREDICATE_ARRAY); } - static List findAnnotations(AnnotatedConstruct annotatedConstruct, Predicate... annotationFilters) { + static AnnotationMirror findAnnotation(TypeMirror type, Class annotationClass) { + if (type == null || annotationClass == null) { + return null; + } + return findAnnotation(type, annotationClass.getTypeName()); + } - AnnotatedConstruct actualAnnotatedConstruct = annotatedConstruct; + static AnnotationMirror findAnnotation(TypeMirror type, CharSequence annotationClassName) { + if (type == null || annotationClassName == null) { + return null; + } + return findAnnotation(ofTypeElement(type), annotationClassName); + } - if (annotatedConstruct instanceof TypeMirror) { - actualAnnotatedConstruct = ofTypeElement((TypeMirror) actualAnnotatedConstruct); + static AnnotationMirror findAnnotation(Element element, Class annotationClass) { + if (element == null || annotationClass == null) { + return null; } + return findAnnotation(element, annotationClass.getTypeName()); + } - return actualAnnotatedConstruct == null ? emptyList() : filterAll((List) actualAnnotatedConstruct.getAnnotationMirrors(), annotationFilters); + static AnnotationMirror findAnnotation(Element element, CharSequence annotationClassName) { + if (element == null || annotationClassName == null) { + return null; + } + List annotations = findAllAnnotations(element, annotation -> matchesAnnotationClassName(annotation, annotationClassName)); + return isEmpty(annotations) ? null : annotations.get(0); } - static List findAllAnnotations(TypeMirror type, Predicate... annotationFilters) { - return findAllAnnotations(ofTypeElement(type), annotationFilters); + static AnnotationMirror findMetaAnnotation(Element annotatedConstruct, Class metaAnnotationClass) { + if (annotatedConstruct == null || metaAnnotationClass == null) { + return null; + } + return findMetaAnnotation(annotatedConstruct, metaAnnotationClass.getName()); } - static List findAllAnnotations(ProcessingEnvironment processingEnv, Type annotatedType, Predicate... annotationFilters) { - return annotatedType == null ? emptyList() : findAllAnnotations(processingEnv, annotatedType.getTypeName(), annotationFilters); + static AnnotationMirror findMetaAnnotation(Element annotatedConstruct, CharSequence metaAnnotationClassName) { + if (annotatedConstruct == null || metaAnnotationClassName == null) { + return null; + } + + AnnotationMirror metaAnnotation = null; + + List annotations = getAllAnnotations(annotatedConstruct); + int size = size(annotations); + + for (int i = 0; i < size; i++) { + AnnotationMirror annotation = annotations.get(i); + if ((metaAnnotation = findAnnotation(annotation.getAnnotationType(), metaAnnotationClassName)) != null) { + break; + } + } + + return metaAnnotation; } - static List findAllAnnotations(ProcessingEnvironment processingEnv, CharSequence annotatedTypeName, Predicate... annotationFilters) { - return findAllAnnotations(TypeUtils.getTypeElement(processingEnv, annotatedTypeName), annotationFilters); + static boolean isAnnotationPresent(Element element, Class annotationClass) { + if (element == null || annotationClass == null) { + return false; + } + return findAnnotation(element, annotationClass) != null || findMetaAnnotation(element, annotationClass) != null; } - static List findAllAnnotations(Element element, Predicate... annotationFilters) { + static boolean isAnnotationPresent(Element element, CharSequence annotationClassName) { + if (element == null || annotationClassName == null) { + return false; + } + return findAnnotation(element, annotationClassName) != null || findMetaAnnotation(element, annotationClassName) != null; + } + + static List findAnnotations(AnnotatedConstruct annotatedConstruct, Predicate... annotationFilters) { + if (annotatedConstruct == null) { + return emptyList(); + } - List allAnnotations = isTypeElement(element) ? - TypeUtils.getAllTypeElements(ofTypeElement(element)) - .stream() - .map(AnnotationUtils::getAnnotations) - .flatMap(Collection::stream) - .collect(toList()) : element == null ? emptyList() : (List) element.getAnnotationMirrors(); + List annotations = (List) annotatedConstruct.getAnnotationMirrors(); + if (isEmpty(annotations)) { + return emptyList(); + } + + if (isNotEmpty(annotationFilters)) { + annotations = filterAll(annotations, annotationFilters); + } - return filterAll(allAnnotations, annotationFilters); + return annotations.isEmpty() ? emptyList() : annotations; } - static AnnotationMirror findAnnotation(TypeMirror type, Class annotationClass) { - return annotationClass == null ? null : findAnnotation(type, annotationClass.getTypeName()); + static List findAllAnnotations(TypeMirror type, Predicate... annotationFilters) { + if (type == null) { + return emptyList(); + } + return findAllAnnotations(ofTypeElement(type), annotationFilters); } - static AnnotationMirror findAnnotation(TypeMirror type, CharSequence annotationClassName) { - return findAnnotation(ofTypeElement(type), annotationClassName); + static List findAllAnnotations(TypeElement element, Predicate... annotationFilters) { + if (element == null) { + return emptyList(); + } + List typeElements = getAllTypeElements(element); + + List annotations = typeElements.stream() + .map(AnnotationUtils::getAnnotations) + .flatMap(Collection::stream) + .collect(toList()); + + if (isNotEmpty(annotationFilters)) { + annotations = filterAll(annotations, annotationFilters); + } + + return isEmpty(annotations) ? emptyList() : annotations; } - static AnnotationMirror findAnnotation(Element element, Class annotationClass) { - return annotationClass == null ? null : findAnnotation(element, annotationClass.getTypeName()); + static List findAllAnnotations(Element element, Predicate... annotationFilters) { + if (element == null) { + return emptyList(); + } + + TypeElement typeElement = ofTypeElement(element); + + if (typeElement == null) { + return findAnnotations(element, annotationFilters); + } + + return findAllAnnotations(typeElement, annotationFilters); } - static AnnotationMirror findAnnotation(Element element, CharSequence annotationClassName) { - return filterFirst(findAllAnnotations(element, annotation -> isSameType(annotation.getAnnotationType(), annotationClassName))); + static List findAllAnnotations(ProcessingEnvironment processingEnv, Type annotatedType, Predicate... annotationFilters) { + if (processingEnv == null || annotatedType == null) { + return emptyList(); + } + return findAllAnnotations(processingEnv, annotatedType.getTypeName(), annotationFilters); } - static AnnotationMirror findMetaAnnotation(Element annotatedConstruct, CharSequence metaAnnotationClassName) { - return annotatedConstruct == null ? null : getAnnotations(annotatedConstruct).stream().map(annotation -> findAnnotation(annotation.getAnnotationType(), metaAnnotationClassName)).filter(Objects::nonNull).findFirst().orElse(null); + static List findAllAnnotations(ProcessingEnvironment processingEnv, CharSequence annotatedTypeName, Predicate... annotationFilters) { + if (processingEnv == null || annotatedTypeName == null) { + return emptyList(); + } + return findAllAnnotations(getTypeElement(processingEnv, annotatedTypeName), annotationFilters); } - static boolean isAnnotationPresent(Element element, CharSequence annotationClassName) { - return findAnnotation(element, annotationClassName) != null || findMetaAnnotation(element, annotationClassName) != null; + static boolean matchesAnnotationClass(AnnotationMirror annotationMirror, Type annotationType) { + if (annotationMirror == null || annotationType == null) { + return false; + } + return matchesAnnotationClassName(annotationMirror, annotationType.getTypeName()); + } + + static boolean matchesAnnotationClassName(AnnotationMirror annotationMirror, CharSequence annotationClassName) { + if (annotationMirror == null || annotationClassName == null) { + return false; + } + return isSameType(annotationMirror.getAnnotationType(), annotationClassName); } static T getAttribute(AnnotationMirror annotation, String attributeName) { diff --git a/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/FieldUtils.java b/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/FieldUtils.java index 83cdce076..b6a32541d 100644 --- a/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/FieldUtils.java +++ b/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/FieldUtils.java @@ -20,19 +20,19 @@ import javax.lang.model.element.Modifier; import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeMirror; -import java.util.Collection; import java.util.List; import java.util.function.Predicate; -import java.util.stream.Collectors; +import static io.microsphere.annotation.processor.util.MemberUtils.getAllDeclaredMembers; import static io.microsphere.annotation.processor.util.MemberUtils.getDeclaredMembers; import static io.microsphere.annotation.processor.util.MemberUtils.hasModifiers; import static io.microsphere.annotation.processor.util.MemberUtils.matchesElementKind; -import static io.microsphere.annotation.processor.util.TypeUtils.getAllDeclaredTypes; import static io.microsphere.annotation.processor.util.TypeUtils.isEnumType; +import static io.microsphere.collection.CollectionUtils.isEmpty; import static io.microsphere.lang.function.Predicates.EMPTY_PREDICATE_ARRAY; import static io.microsphere.lang.function.Streams.filterAll; import static io.microsphere.lang.function.Streams.filterFirst; +import static io.microsphere.util.ArrayUtils.isNotEmpty; import static java.util.Collections.emptyList; import static javax.lang.model.element.ElementKind.ENUM_CONSTANT; import static javax.lang.model.element.ElementKind.FIELD; @@ -47,6 +47,14 @@ */ public interface FieldUtils { + static VariableElement getDeclaredField(Element element, String fieldName) { + return element == null ? null : getDeclaredField(element.asType(), fieldName); + } + + static VariableElement getDeclaredField(TypeMirror type, String fieldName) { + return filterFirst(findDeclaredFields(type, field -> fieldName.equals(field.getSimpleName().toString()))); + } + static List getDeclaredFields(Element element) { return findDeclaredFields(element, EMPTY_PREDICATE_ARRAY); } @@ -63,12 +71,20 @@ static List getAllDeclaredFields(TypeMirror type) { return findAllDeclaredFields(type, EMPTY_PREDICATE_ARRAY); } + static VariableElement findField(Element element, String fieldName) { + return element == null ? null : findField(element.asType(), fieldName); + } + + static VariableElement findField(TypeMirror type, String fieldName) { + return filterFirst(findAllDeclaredFields(type, field -> equalsFieldName(field, fieldName))); + } + static List findDeclaredFields(Element element, Predicate... fieldFilters) { return element == null ? emptyList() : findDeclaredFields(element.asType(), fieldFilters); } static List findDeclaredFields(TypeMirror type, Predicate... fieldFilters) { - return filterAll(fieldsIn(getDeclaredMembers(type)), fieldFilters); + return filterDeclaredFields(type, false, fieldFilters); } static List findAllDeclaredFields(Element element, Predicate... fieldFilters) { @@ -76,27 +92,29 @@ static List findAllDeclaredFields(Element element, Predicate findAllDeclaredFields(TypeMirror type, Predicate... fieldFilters) { - return getAllDeclaredTypes(type) - .stream() - .map(t -> findDeclaredFields(t, fieldFilters)) - .flatMap(Collection::stream) - .collect(Collectors.toList()); + return filterDeclaredFields(type, true, fieldFilters); } - static VariableElement getDeclaredField(Element element, String fieldName) { - return element == null ? null : getDeclaredField(element.asType(), fieldName); - } + static List filterDeclaredFields(TypeMirror type, boolean all, Predicate... fieldFilters) { + if (type == null) { + return emptyList(); + } - static VariableElement getDeclaredField(TypeMirror type, String fieldName) { - return filterFirst(findDeclaredFields(type, field -> fieldName.equals(field.getSimpleName().toString()))); - } + List declaredMembers = all ? getAllDeclaredMembers(type) : getDeclaredMembers(type); + if (isEmpty(declaredMembers)) { + return emptyList(); + } - static VariableElement findField(Element element, String fieldName) { - return element == null ? null : findField(element.asType(), fieldName); - } + List fields = fieldsIn(declaredMembers); + if (isEmpty(fields)) { + return emptyList(); + } - static VariableElement findField(TypeMirror type, String fieldName) { - return filterFirst(findAllDeclaredFields(type, field -> equals(field, fieldName))); + if (isNotEmpty(fieldFilters)) { + fields = filterAll(fields, fieldFilters); + } + + return isEmpty(fields) ? emptyList() : fields; } /** @@ -140,8 +158,7 @@ static List getAllNonStaticFields(Element element) { return element == null ? emptyList() : getAllNonStaticFields(element.asType()); } - static boolean equals(VariableElement field, CharSequence fieldName) { + static boolean equalsFieldName(VariableElement field, CharSequence fieldName) { return field != null && fieldName != null && field.getSimpleName().toString().equals(fieldName.toString()); } - } diff --git a/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/LoggerUtils.java b/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/LoggerUtils.java index e93d167f5..40b36b851 100644 --- a/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/LoggerUtils.java +++ b/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/LoggerUtils.java @@ -32,32 +32,22 @@ public interface LoggerUtils { Logger LOGGER = getLogger("microsphere-annotation-processor"); static void trace(String format, Object... args) { - if (LOGGER.isTraceEnabled()) { - LOGGER.trace(format, args); - } + LOGGER.trace(format, args); } static void debug(String format, Object... args) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(format, args); - } + LOGGER.debug(format, args); } static void info(String format, Object... args) { - if (LOGGER.isInfoEnabled()) { - LOGGER.info(format, args); - } + LOGGER.info(format, args); } static void warn(String format, Object... args) { - if (LOGGER.isWarnEnabled()) { - LOGGER.warn(format, args); - } + LOGGER.warn(format, args); } static void error(String format, Object... args) { - if (LOGGER.isErrorEnabled()) { - LOGGER.error(format, args); - } + LOGGER.error(format, args); } } diff --git a/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/MethodUtils.java b/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/MethodUtils.java index 078004abc..189ad0f58 100644 --- a/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/MethodUtils.java +++ b/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/MethodUtils.java @@ -111,7 +111,6 @@ static List findAllDeclaredMethods(TypeMirror type, Type... e static List findPublicNonStaticMethods(TypeElement type, Type... excludedTypes) { return type == null ? emptyList() : findPublicNonStaticMethods(ofDeclaredType(type), excludedTypes); - } static List findPublicNonStaticMethods(TypeMirror type, Type... excludedTypes) { diff --git a/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/TypeUtils.java b/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/TypeUtils.java index b72ef264e..e5d9c48f3 100644 --- a/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/TypeUtils.java +++ b/microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/TypeUtils.java @@ -110,7 +110,6 @@ static boolean isSameType(TypeMirror type, CharSequence typeName) { return Objects.equals(valueOf(type), valueOf(typeName)); } - static boolean isArrayType(TypeMirror type) { return type != null && ARRAY == type.getKind(); } @@ -278,6 +277,10 @@ static List getAllTypeElementsOfInterfaces(TypeElement type) { return findAllTypeElementsOfInterfaces(type, EMPTY_PREDICATE_ARRAY); } + static List getTypeElements(TypeElement type) { + return getTypeElements(type, true, false, true, true); + } + static List getAllTypeElements(TypeElement type) { return getTypeElements(type, true, true, true, true); } @@ -319,7 +322,7 @@ static List findTypeElements(TypeElement type, return emptyList(); } assertNoNullElements(typeFilters, () -> "Any element of 'typeFilters' array must not be null"); - return type == null ? emptyList() : typeElementFinder(type, includeSelf, includeHierarchicalTypes, includeSuperclass, includeSuperInterfaces).findTypes(typeFilters); + return typeElementFinder(type, includeSelf, includeHierarchicalTypes, includeSuperclass, includeSuperInterfaces).findTypes(typeFilters); } static DeclaredType getDeclaredTypeOfSuperclass(Element typeElement) { diff --git a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/AbstractAnnotationProcessingTest.java b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/AbstractAnnotationProcessingTest.java index 64b4b49df..d32f7327c 100644 --- a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/AbstractAnnotationProcessingTest.java +++ b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/AbstractAnnotationProcessingTest.java @@ -22,8 +22,14 @@ import org.junit.jupiter.api.extension.ExtendWith; import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.AnnotatedConstruct; +import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Elements; @@ -33,8 +39,11 @@ import java.util.Collection; import java.util.List; import java.util.Set; +import java.util.function.Predicate; import static io.microsphere.annotation.processor.util.TypeUtils.ofDeclaredType; +import static java.util.Collections.emptyList; +import static org.junit.jupiter.api.Assertions.assertSame; /** * Abstract {@link Annotation} Processing Test case @@ -55,8 +64,12 @@ public abstract class AbstractAnnotationProcessingTest { protected static final Collection NULL_COLLECTION = null; + protected static final List NULL_LIST = null; + protected static final Element NULL_ELEMENT = null; + protected static final ElementKind NULL_ELEMENT_KIND = null; + protected static final Element[] EMPTY_ELEMENT_ARRAY = new Element[0]; protected static final Element[] NULL_ELEMENT_ARRAY = null; @@ -75,6 +88,26 @@ public abstract class AbstractAnnotationProcessingTest { protected static final String[] NULL_STRING_ARRAY = null; + protected static final Class NULL_CLASS = null; + + protected static final Class[] NULL_CLASS_ARRAY = null; + + protected static final AnnotatedConstruct NULL_ANNOTATED_CONSTRUCT = null; + + protected static final Predicate[] NULL_PREDICATE_ARRAY = null; + + protected static final VariableElement NULL_FIELD = null; + + protected static final Modifier NULL_MODIFIER = null; + + protected static final Modifier[] NULL_MODIFIER_ARRAY = null; + + protected static final ExecutableElement NULL_METHOD = null; + + protected static final ExecutableElement[] NULL_METHOD_ARRAY = null; + + protected static final AnnotationMirror NULL_ANNOTATION_MIRROR = null; + static ThreadLocal testInstanceHolder = new ThreadLocal<>(); protected ProcessingEnvironment processingEnv; @@ -141,4 +174,8 @@ protected DeclaredType getDeclaredType(Type type) { return TypeUtils.getDeclaredType(processingEnv, type); } + protected void assertEmptyList(List list) { + assertSame(emptyList(), list); + } + } diff --git a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/Compiler.java b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/Compiler.java index fda33704d..22e0dbb6e 100644 --- a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/Compiler.java +++ b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/Compiler.java @@ -20,12 +20,12 @@ import javax.annotation.processing.Processor; import javax.tools.JavaCompiler; +import javax.tools.JavaCompiler.CompilationTask; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import java.io.File; import java.io.IOException; import java.net.URL; -import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -39,6 +39,7 @@ import static io.microsphere.logging.LoggerFactory.getLogger; import static io.microsphere.util.ClassUtils.getTypeName; import static io.microsphere.util.StringUtils.substringBefore; +import static java.util.Collections.singleton; import static javax.tools.StandardLocation.CLASS_OUTPUT; import static javax.tools.StandardLocation.SOURCE_OUTPUT; import static javax.tools.ToolProvider.getSystemJavaCompiler; @@ -73,8 +74,8 @@ public Compiler(File defaultSourceDirectory, File targetDirectory) throws IOExce this.sourcePaths = newLinkedHashSet(defaultSourceDirectory); this.javaCompiler = getSystemJavaCompiler(); this.javaFileManager = javaCompiler.getStandardFileManager(null, null, null); - this.javaFileManager.setLocation(CLASS_OUTPUT, Collections.singleton(targetDirectory)); - this.javaFileManager.setLocation(SOURCE_OUTPUT, Collections.singleton(targetDirectory)); + this.javaFileManager.setLocation(CLASS_OUTPUT, singleton(targetDirectory)); + this.javaFileManager.setLocation(SOURCE_OUTPUT, singleton(targetDirectory)); } public Compiler sourcePaths(File... sourcePaths) { @@ -191,7 +192,7 @@ static String resolveJavaSourceFileRelativePath(Class sourceClass) { } public boolean compile(Class... sourceClasses) { - JavaCompiler.CompilationTask task = javaCompiler.getTask(null, this.javaFileManager, null, + CompilationTask task = javaCompiler.getTask(null, this.javaFileManager, null, ofList("-parameters", "-Xlint:unchecked", "-nowarn", "-Xlint:deprecation"), // null, null, getJavaFileObjects(sourceClasses)); diff --git a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/TestServiceImpl.java b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/TestServiceImpl.java index 9bf49bbf1..91bc84d94 100644 --- a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/TestServiceImpl.java +++ b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/TestServiceImpl.java @@ -16,7 +16,10 @@ */ package io.microsphere.annotation.processor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import javax.xml.ws.ServiceMode; @@ -30,6 +33,19 @@ @ServiceMode public class TestServiceImpl extends GenericTestService implements TestService, AutoCloseable, Serializable { + @Autowired + private ApplicationContext context; + + private Environment environment; + + public TestServiceImpl() { + this(null); + } + + public TestServiceImpl(@Autowired Environment environment) { + this.environment = environment; + } + @Override @Cacheable(cacheNames = {"cache-1", "cache-2"}) public String echo(String message) { diff --git a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/AnnotationUtilsTest.java b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/AnnotationUtilsTest.java index 60b04e7ed..a79f7f5c0 100644 --- a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/AnnotationUtilsTest.java +++ b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/AnnotationUtilsTest.java @@ -19,26 +19,35 @@ import io.microsphere.annotation.processor.AbstractAnnotationProcessingTest; import io.microsphere.annotation.processor.TestService; import io.microsphere.annotation.processor.TestServiceImpl; +import io.microsphere.annotation.processor.model.Model; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; -import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.VariableElement; import javax.lang.model.type.DeclaredType; -import javax.lang.model.type.TypeMirror; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.HttpMethod; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.xml.ws.ServiceMode; +import java.io.Serializable; import java.lang.annotation.Annotation; +import java.lang.annotation.Documented; import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; import java.lang.annotation.Target; -import java.util.Iterator; import java.util.List; import static io.microsphere.annotation.processor.util.AnnotationUtils.findAllAnnotations; import static io.microsphere.annotation.processor.util.AnnotationUtils.findAnnotation; +import static io.microsphere.annotation.processor.util.AnnotationUtils.findAnnotations; import static io.microsphere.annotation.processor.util.AnnotationUtils.findMetaAnnotation; import static io.microsphere.annotation.processor.util.AnnotationUtils.getAllAnnotations; import static io.microsphere.annotation.processor.util.AnnotationUtils.getAnnotation; @@ -46,13 +55,18 @@ import static io.microsphere.annotation.processor.util.AnnotationUtils.getAttribute; import static io.microsphere.annotation.processor.util.AnnotationUtils.getValue; import static io.microsphere.annotation.processor.util.AnnotationUtils.isAnnotationPresent; +import static io.microsphere.annotation.processor.util.AnnotationUtils.matchesAnnotationClassName; +import static io.microsphere.annotation.processor.util.FieldUtils.findField; import static io.microsphere.annotation.processor.util.MethodUtils.findMethod; import static io.microsphere.annotation.processor.util.MethodUtils.getAllDeclaredMethods; +import static io.microsphere.lang.function.Predicates.alwaysFalse; +import static io.microsphere.lang.function.Predicates.alwaysTrue; import static io.microsphere.util.ArrayUtils.ofArray; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.TYPE; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -71,138 +85,331 @@ public void testGetAnnotation() { @Test public void testGetAnnotationWithClassName() { - asserGetAnnotation(Service.class.getName()); + asserGetAnnotation("org.springframework.stereotype.Service"); } @Test public void testGetAnnotationOnNull() { - assertNull(getAnnotation(testTypeElement, (Class) null)); - assertNull(getAnnotation(testTypeElement, (String) null)); - - assertNull(getAnnotation(testTypeElement.asType(), (Class) null)); - assertNull(getAnnotation(testTypeElement.asType(), (String) null)); - - assertNull(getAnnotation(null, (Class) null)); - assertNull(getAnnotation(null, (String) null)); + assertNull(getAnnotation(testTypeElement, NULL_CLASS)); + assertNull(getAnnotation(testTypeElement.asType(), NULL_CLASS)); + assertNull(getAnnotation(NULL_ANNOTATED_CONSTRUCT, NULL_CLASS)); + } - assertNull(getAnnotation(null, (Class) null)); - assertNull(getAnnotation(null, (String) null)); + @Test + public void testGetAnnotationWithClassNameOnNull() { + assertNull(getAnnotation(testTypeElement, NULL_STRING)); + assertNull(getAnnotation(testTypeElement.asType(), NULL_STRING)); + assertNull(getAnnotation(NULL_ANNOTATED_CONSTRUCT, NULL_STRING)); } @Test public void testGetAnnotations() { List annotations = getAnnotations(testTypeElement); - Iterator iterator = annotations.iterator(); - assertEquals(2, annotations.size()); - assertEquals(Service.class.getName(), iterator.next().getAnnotationType().toString()); - assertEquals(ServiceMode.class.getName(), iterator.next().getAnnotationType().toString()); + assertAnnotation(annotations.get(0), Service.class); + assertAnnotation(annotations.get(1), ServiceMode.class); } @Test - public void testGetAnnotationsWithAnnotationType() { + public void testGetAnnotationsOnNull() { + List annotations = getAnnotations(NULL_ANNOTATED_CONSTRUCT); + assertEmptyList(annotations); + } + + @Test + public void testGetAnnotationsWithAnnotationClass() { assertGetAnnotations(Service.class); assertGetAnnotations(ServiceMode.class); } @Test - public void testGetAnnotationsWithAnnotationTypeOnNotFound() { + public void testGetAnnotationsWithAnnotationClassOnNull() { + assertTrue(getAnnotations(NULL_ANNOTATED_CONSTRUCT, NULL_CLASS).isEmpty()); + assertTrue(getAnnotations(testTypeElement, NULL_CLASS).isEmpty()); + assertTrue(getAnnotations(NULL_ANNOTATED_CONSTRUCT, Service.class).isEmpty()); + } + + @Test + public void testGetAnnotationsWithAnnotationClassOnNotFound() { List annotations = getAnnotations(testTypeElement, Override.class); assertEquals(0, annotations.size()); } @Test public void testGetAnnotationsWithAnnotationClassName() { - assertGetAnnotations(Service.class.getName()); - assertGetAnnotations(ServiceMode.class.getName()); + assertGetAnnotations("org.springframework.stereotype.Service"); + assertGetAnnotations("javax.xml.ws.ServiceMode"); } @Test - public void testGetAnnotationsOnNull() { - assertTrue(getAnnotations(null, (Class) null).isEmpty()); - assertTrue(getAnnotations(null, (String) null).isEmpty()); - assertTrue(getAnnotations(testTypeElement, (Class) null).isEmpty()); - assertTrue(getAnnotations(testTypeElement, (String) null).isEmpty()); - - assertTrue(getAnnotations(null, Service.class).isEmpty()); - assertTrue(getAnnotations(null, Service.class.getTypeName()).isEmpty()); + public void testGetAnnotationsWithAnnotationClassNameOnNull() { + assertTrue(getAnnotations(NULL_ANNOTATED_CONSTRUCT, NULL_STRING).isEmpty()); + assertTrue(getAnnotations(testTypeElement, NULL_STRING).isEmpty()); + assertTrue(getAnnotations(NULL_ANNOTATED_CONSTRUCT, "org.springframework.stereotype.Service").isEmpty()); } @Test public void testGetAllAnnotations() { - List annotations = getAllAnnotations(testTypeElement); assertEquals(3, annotations.size()); - annotations = findAllAnnotations(testTypeElement.asType(), annotation -> true); + annotations = getAllAnnotations(testTypeMirror); assertEquals(3, annotations.size()); + } - annotations = getAllAnnotations(processingEnv, TestServiceImpl.class); - assertEquals(3, annotations.size()); + @Test + public void testGetAllAnnotationsOnNull() { + assertEmptyList(getAllAnnotations(NULL_ELEMENT)); + assertEmptyList(getAllAnnotations(NULL_TYPE_MIRROR)); + } - annotations = getAllAnnotations(testTypeElement.asType(), Service.class); - assertEquals(1, annotations.size()); + @Test + public void testGetAllAnnotationsWithAnnotationClass() { + List annotations = getAllAnnotations(testTypeElement, Override.class); + assertEquals(0, annotations.size()); - annotations = getAllAnnotations(testTypeElement, Override.class); + annotations = getAllAnnotations(testTypeMirror, Override.class); assertEquals(0, annotations.size()); - assertTrue(getAllAnnotations((Element) null, (Class) null).isEmpty()); - assertTrue(getAllAnnotations((TypeMirror) null, (String) null).isEmpty()); - assertTrue(getAllAnnotations((ProcessingEnvironment) null, (Class) null).isEmpty()); - assertTrue(findAllAnnotations((ProcessingEnvironment) null, (String) null).isEmpty()); + annotations = getAllAnnotations(testTypeElement, Service.class); + assertEquals(1, annotations.size()); - assertTrue(getAllAnnotations((Element) null).isEmpty()); - assertTrue(getAllAnnotations((TypeMirror) null).isEmpty()); - assertTrue(getAllAnnotations(processingEnv, (Class) null).isEmpty()); - assertTrue(findAllAnnotations(processingEnv, (String) null).isEmpty()); + annotations = getAllAnnotations(testTypeMirror, Service.class); + assertEquals(1, annotations.size()); + annotations = getAllAnnotations(processingEnv, TestServiceImpl.class); + assertEquals(3, annotations.size()); + } - assertTrue(getAllAnnotations(testTypeElement, (Class) null).isEmpty()); - assertTrue(getAllAnnotations(testTypeElement.asType(), (Class) null).isEmpty()); + @Test + public void testGetAllAnnotationsWithAnnotationClassOnNull() { + assertEmptyList(getAllAnnotations(NULL_ELEMENT, NULL_CLASS)); + assertEmptyList(getAllAnnotations(NULL_TYPE_MIRROR, NULL_CLASS)); + assertEmptyList(getAllAnnotations(NULL_PROCESSING_ENVIRONMENT, NULL_CLASS)); + + assertEmptyList(getAllAnnotations(NULL_ELEMENT, Service.class)); + assertEmptyList(getAllAnnotations(NULL_TYPE_MIRROR, Service.class)); + assertEmptyList(getAllAnnotations(NULL_PROCESSING_ENVIRONMENT, Service.class)); + + assertEmptyList(getAllAnnotations(testTypeElement, NULL_CLASS)); + assertEmptyList(getAllAnnotations(testTypeMirror, NULL_CLASS)); + assertEmptyList(getAllAnnotations(processingEnv, NULL_CLASS)); + } - assertTrue(getAllAnnotations(testTypeElement, (String) null).isEmpty()); - assertTrue(getAllAnnotations(testTypeElement.asType(), (String) null).isEmpty()); + @Test + public void testGetAllAnnotationsWithAnnotationClassName() { + List annotations = getAllAnnotations(testTypeElement, "java.lang.Override"); + assertEquals(0, annotations.size()); - assertTrue(getAllAnnotations((Element) null, Service.class).isEmpty()); - assertTrue(getAllAnnotations((TypeMirror) null, Service.class.getTypeName()).isEmpty()); + annotations = getAllAnnotations(testTypeMirror, "org.springframework.stereotype.Service"); + assertEquals(1, annotations.size()); } + @Test + public void testGetAllAnnotationsWithAnnotationClassNameOnNull() { + assertEmptyList(getAllAnnotations(NULL_ELEMENT, NULL_STRING)); + assertEmptyList(getAllAnnotations(NULL_TYPE_MIRROR, NULL_STRING)); + + assertTrue(getAllAnnotations(NULL_ELEMENT, "org.springframework.stereotype.Service").isEmpty()); + assertTrue(getAllAnnotations(NULL_TYPE_MIRROR, "org.springframework.stereotype.Service").isEmpty()); + + assertEmptyList(getAllAnnotations(testTypeElement, NULL_STRING)); + assertEmptyList(getAllAnnotations(testTypeMirror, NULL_STRING)); + } @Test public void testFindAnnotation() { + assertFindAnnotation(Service.class); + assertFindAnnotation(Path.class); + } - assertEquals("org.springframework.stereotype.Service", findAnnotation(testTypeElement, Service.class).getAnnotationType().toString()); - assertEquals("javax.ws.rs.Path", findAnnotation(testTypeElement, Path.class).getAnnotationType().toString()); - assertEquals("javax.ws.rs.Path", findAnnotation(testTypeElement.asType(), Path.class).getAnnotationType().toString()); - assertEquals("javax.ws.rs.Path", findAnnotation(testTypeElement.asType(), Path.class.getTypeName()).getAnnotationType().toString()); + @Test + public void testFindAnnotationOnNotFound() { + assertNull(findAnnotation(testTypeMirror, Target.class)); + assertNull(findAnnotation(testTypeElement, Target.class)); + assertNull(findAnnotation(testTypeMirror, Override.class)); assertNull(findAnnotation(testTypeElement, Override.class)); + } - assertNull(findAnnotation((Element) null, (Class) null)); - assertNull(findAnnotation((Element) null, (String) null)); - assertNull(findAnnotation((TypeMirror) null, (Class) null)); - assertNull(findAnnotation((TypeMirror) null, (String) null)); + @Test + public void testFindAnnotationOnNull() { + assertNull(findAnnotation(NULL_ELEMENT, NULL_CLASS)); + assertNull(findAnnotation(NULL_TYPE_MIRROR, NULL_CLASS)); + assertNull(findAnnotation(testTypeMirror, NULL_CLASS)); + assertNull(findAnnotation(testTypeElement, NULL_CLASS)); + + assertNull(findAnnotation(NULL_ELEMENT, NULL_STRING)); + assertNull(findAnnotation(NULL_TYPE_MIRROR, NULL_STRING)); + assertNull(findAnnotation(testTypeMirror, NULL_STRING)); + assertNull(findAnnotation(testTypeElement, NULL_STRING)); + } - assertNull(findAnnotation(testTypeElement, (Class) null)); - assertNull(findAnnotation(testTypeElement, (String) null)); - assertNull(findAnnotation(testTypeElement.asType(), (Class) null)); - assertNull(findAnnotation(testTypeElement.asType(), (String) null)); + @Test + public void testFindMetaAnnotationWithAnnotationClass() { + getAllDeclaredMethods(getTypeElement(TestService.class)).forEach(method -> { + assertFindMetaAnnotation(method, HttpMethod.class); + }); } @Test - public void testFindMetaAnnotation() { + public void testFindMetaAnnotationWithAnnotationClassOnNotFound() { + assertNull(findMetaAnnotation(testTypeElement, Service.class)); + } + + @Test + public void testFindMetaAnnotationWithAnnotationClassNameOnNotFound() { + assertNull(findMetaAnnotation(testTypeElement, "org.springframework.stereotype.Service")); + } + + @Test + public void testFindMetaAnnotationWithAnnotationClassOnNull() { + assertNull(findMetaAnnotation(NULL_ELEMENT, NULL_CLASS)); + assertNull(findMetaAnnotation(NULL_ELEMENT, Service.class)); + assertNull(findMetaAnnotation(testTypeElement, NULL_CLASS)); + } + + @Test + public void testFindMetaAnnotationWithAnnotationClassName() { getAllDeclaredMethods(getTypeElement(TestService.class)).forEach(method -> { - assertEquals("javax.ws.rs.HttpMethod", findMetaAnnotation(method, "javax.ws.rs.HttpMethod").getAnnotationType().toString()); + assertFindMetaAnnotation(method, "javax.ws.rs.HttpMethod"); }); } + @Test + public void testFindMetaAnnotationWithAnnotationClassNameOnNull() { + assertNull(findMetaAnnotation(NULL_ELEMENT, NULL_STRING)); + assertNull(findMetaAnnotation(NULL_ELEMENT, "test")); + assertNull(findMetaAnnotation(testTypeElement, NULL_STRING)); + } + + @Test + public void testFindAllAnnotationsWithTypeMirror() { + List annotations = findAllAnnotations(testTypeMirror, alwaysTrue()); + assertEquals(3, annotations.size()); + + annotations = findAllAnnotations(testTypeMirror, alwaysFalse()); + assertEmptyList(annotations); + } + + @Test + public void testFindAllAnnotationsWithTypeElement() { + List annotations = findAllAnnotations(testTypeElement, alwaysTrue()); + assertEquals(3, annotations.size()); + + annotations = findAllAnnotations(testTypeElement, alwaysFalse()); + assertEmptyList(annotations); + } + + @Test + public void testFindAllAnnotationsWithMethod() { + ExecutableElement method = findMethod(testTypeElement, "echo", String.class); + + List annotations = findAllAnnotations(method, alwaysTrue()); + assertEquals(1, annotations.size()); + assertAnnotation(annotations.get(0), Cacheable.class); + + method = findMethod(getTypeElement(TestService.class), "echo", String.class); + + annotations = findAllAnnotations(method); + assertEquals(1, annotations.size()); + assertAnnotation(annotations.get(0), GET.class); + } + + @Test + public void testFindAllAnnotationsWithMethodParameters() { + ExecutableElement method = findMethod(getTypeElement(TestService.class), "echo", String.class); + List parameters = method.getParameters(); + assertEquals(1, parameters.size()); + + List annotations = findAllAnnotations(parameters.get(0), alwaysTrue()); + assertEquals(2, annotations.size()); + assertAnnotation(annotations.get(0), PathParam.class); + assertAnnotation(annotations.get(1), DefaultValue.class); + + method = findMethod(getTypeElement(TestService.class), "model", Model.class); + parameters = method.getParameters(); + assertEquals(1, parameters.size()); + + annotations = findAllAnnotations(parameters.get(0)); + assertEquals(1, annotations.size()); + assertAnnotation(annotations.get(0), PathParam.class); + } + + @Test + public void testFindAllAnnotationsWithField() { + VariableElement field = findField(testTypeElement, "context"); + + List annotations = findAllAnnotations(field, alwaysTrue()); + assertEquals(1, annotations.size()); + assertAnnotation(annotations.get(0), Autowired.class); + + field = findField(testTypeElement, "environment"); + annotations = findAllAnnotations(field, alwaysTrue()); + assertEmptyList(annotations); + } + + @Test + public void testFindAllAnnotationsWithTypeMirrorOnNull() { + assertEmptyList(findAllAnnotations(NULL_TYPE_MIRROR, alwaysTrue())); + assertEmptyList(findAllAnnotations(NULL_TYPE_MIRROR, alwaysFalse())); + } + + @Test + public void testFindAllAnnotationsWithTypeElementOnNull() { + assertEmptyList(findAllAnnotations(NULL_TYPE_ELEMENT, alwaysTrue())); + assertEmptyList(findAllAnnotations(NULL_TYPE_ELEMENT, alwaysFalse())); + } + + @Test + public void testFindAllAnnotationsWithElementOnNull() { + assertEmptyList(findAllAnnotations(NULL_ELEMENT, alwaysTrue())); + assertEmptyList(findAllAnnotations(NULL_ELEMENT, alwaysFalse())); + } + + @Test + public void testFindAllAnnotationsOnNull() { + assertEmptyList(findAllAnnotations(NULL_PROCESSING_ENVIRONMENT, Service.class, alwaysTrue())); + assertEmptyList(findAllAnnotations(NULL_PROCESSING_ENVIRONMENT, Service.class, alwaysTrue())); + assertEmptyList(findAllAnnotations(NULL_PROCESSING_ENVIRONMENT, "org.springframework.stereotype.Service", alwaysFalse())); + assertEmptyList(findAllAnnotations(NULL_PROCESSING_ENVIRONMENT, "org.springframework.stereotype.Service", alwaysFalse())); + assertEmptyList(findAllAnnotations(processingEnv, NULL_TYPE, alwaysTrue())); + assertEmptyList(findAllAnnotations(processingEnv, NULL_TYPE, alwaysFalse())); + assertEmptyList(findAllAnnotations(processingEnv, NULL_STRING, alwaysTrue())); + assertEmptyList(findAllAnnotations(processingEnv, NULL_STRING, alwaysFalse())); + } + + @Test + public void testMatchesAnnotationClass() { + AnnotationMirror annotation = findAnnotation(testTypeElement, Service.class); + assertTrue(AnnotationUtils.matchesAnnotationClass(annotation, Service.class)); + } + + @Test + public void testMatchesAnnotationClassOnNull() { + assertFalse(AnnotationUtils.matchesAnnotationClass(NULL_ANNOTATION_MIRROR, Service.class)); + assertFalse(AnnotationUtils.matchesAnnotationClass(findAnnotation(testTypeElement, Service.class), NULL_CLASS)); + } + + @Test + public void testMatchesAnnotationClassName() { + AnnotationMirror annotation = findAnnotation(testTypeElement, "org.springframework.stereotype.Service"); + assertTrue(matchesAnnotationClassName(annotation, "org.springframework.stereotype.Service")); + } + + @Test + public void testMatchesAnnotationClassNameOnNull() { + assertFalse(matchesAnnotationClassName(NULL_ANNOTATION_MIRROR, "org.springframework.stereotype.Service")); + assertFalse(matchesAnnotationClassName(findAnnotation(testTypeElement, "org.springframework.stereotype.Service"), NULL_STRING)); + } + @Test public void testGetAttribute() { assertEquals("testService", getAttribute(findAnnotation(testTypeElement, Service.class), "value")); assertEquals("testService", getAttribute(findAnnotation(testTypeElement, Service.class).getElementValues(), "value")); assertEquals("/echo", getAttribute(findAnnotation(testTypeElement, Path.class), "value")); - assertNull(getAttribute(findAnnotation(testTypeElement, Path.class), null)); - assertNull(getAttribute(findAnnotation(testTypeElement, (Class) null), null)); + assertNull(getAttribute(findAnnotation(testTypeElement, Path.class), NULL_STRING)); + assertNull(getAttribute(findAnnotation(testTypeElement, NULL_CLASS), NULL_STRING)); ExecutableElement echoMethod = findMethod(testTypeElement, "echo", String.class); AnnotationMirror cacheableAnnotation = findAnnotation(echoMethod, Cacheable.class); @@ -223,40 +430,107 @@ public void testGetValue() { } @Test - public void testIsAnnotationPresent() { + public void testIsAnnotationPresentOnAnnotationClass() { + assertTrue(isAnnotationPresent(testTypeElement, Service.class)); + assertTrue(isAnnotationPresent(testTypeElement, Component.class)); + assertTrue(isAnnotationPresent(testTypeElement, ServiceMode.class)); + assertTrue(isAnnotationPresent(testTypeElement, Inherited.class)); + assertTrue(isAnnotationPresent(testTypeElement, Documented.class)); + } + + @Test + public void testIsAnnotationPresentOnAnnotationClassOnNull() { + assertFalse(isAnnotationPresent(NULL_ELEMENT, Service.class)); + assertFalse(isAnnotationPresent(testTypeElement, NULL_CLASS)); + assertFalse(isAnnotationPresent(testTypeElement, Override.class)); + } + + @Test + public void testIsAnnotationPresentOnAnnotationClassName() { assertTrue(isAnnotationPresent(testTypeElement, "org.springframework.stereotype.Service")); + assertTrue(isAnnotationPresent(testTypeElement, "org.springframework.stereotype.Component")); assertTrue(isAnnotationPresent(testTypeElement, "javax.xml.ws.ServiceMode")); - assertTrue(isAnnotationPresent(testTypeElement, "javax.ws.rs.Path")); + assertTrue(isAnnotationPresent(testTypeElement, "java.lang.annotation.Inherited")); + assertTrue(isAnnotationPresent(testTypeElement, "java.lang.annotation.Documented")); + } + + @Test + public void testIsAnnotationPresentOnAnnotationClassNameOnNull() { + assertFalse(isAnnotationPresent(NULL_ELEMENT, "org.springframework.stereotype.Service")); + assertFalse(isAnnotationPresent(testTypeElement, NULL_STRING)); + assertFalse(isAnnotationPresent(testTypeElement, "java.lang.Override")); + + } + + @Test + public void testFindAnnotations() { + List annotations = findAnnotations(testTypeElement); + assertEquals(2, annotations.size()); + assertAnnotation(annotations.get(0), Service.class); + assertAnnotation(annotations.get(1), ServiceMode.class); + + annotations = findAnnotations(testTypeElement, alwaysTrue()); + assertEquals(2, annotations.size()); + assertAnnotation(annotations.get(0), Service.class); + assertAnnotation(annotations.get(1), ServiceMode.class); + + annotations = findAnnotations(testTypeElement, alwaysFalse()); + assertEmptyList(annotations); + } + + @Test + public void testFindAnnotationsOnNotFound() { + assertEmptyList(findAnnotations(getTypeElement(Serializable.class))); + } + + @Test + public void testFindAnnotationsOnNull() { + assertEmptyList(findAnnotations(NULL_ELEMENT)); + } + + private void assertFindMetaAnnotation(Element element, Class annotationClass) { + assertAnnotation(findMetaAnnotation(element, annotationClass), annotationClass); } + private void assertFindMetaAnnotation(Element element, String annotationClassName) { + assertAnnotation(findMetaAnnotation(element, annotationClassName), annotationClassName); + } + + private void assertFindAnnotation(Class annotationClass) { + assertAnnotation(findAnnotation(testTypeMirror, annotationClass), annotationClass); + assertAnnotation(findAnnotation(testTypeElement, annotationClass), annotationClass); + assertAnnotation(findAnnotation(testTypeMirror, annotationClass.getName()), annotationClass); + assertAnnotation(findAnnotation(testTypeElement, annotationClass.getName()), annotationClass); + } private void asserGetAnnotation(Class annotationClass) { - AnnotationMirror serviceAnnotation = getAnnotation(testTypeElement, annotationClass); - assertEquals(annotationClass.getName(), serviceAnnotation.getAnnotationType().toString()); + AnnotationMirror annotation = getAnnotation(testTypeElement, annotationClass); + assertAnnotation(annotation, annotationClass); } private void asserGetAnnotation(String annotationClassName) { - AnnotationMirror serviceAnnotation = getAnnotation(testTypeElement, annotationClassName); - assertEquals(annotationClassName, serviceAnnotation.getAnnotationType().toString()); + AnnotationMirror annotation = getAnnotation(testTypeElement, annotationClassName); + assertAnnotation(annotation, annotationClassName); } - private void assertGetAnnotations(Class annotationType) { - List annotations = getAnnotations(testTypeElement, annotationType); - assertEquals(1, annotations.size()); - assertEquals(annotationType.getName(), annotations.get(0).getAnnotationType().toString()); - - annotations = getAnnotations(testTypeElement.asType(), annotationType); + private void assertGetAnnotations(Class annotationClass) { + List annotations = getAnnotations(testTypeElement, annotationClass); assertEquals(1, annotations.size()); - assertEquals(annotationType.getName(), annotations.get(0).getAnnotationType().toString()); + assertAnnotation(annotations.get(0), annotationClass); } private void assertGetAnnotations(String annotationClassName) { List annotations = getAnnotations(testTypeElement, annotationClassName); assertEquals(1, annotations.size()); - assertEquals(annotationClassName, annotations.get(0).getAnnotationType().toString()); + assertAnnotation(annotations.get(0), annotationClassName); + } - annotations = getAnnotations(testTypeElement.asType(), annotationClassName); - assertEquals(1, annotations.size()); - assertEquals(annotationClassName, annotations.get(0).getAnnotationType().toString()); + private void assertAnnotation(AnnotationMirror annotation, Class annotationClass) { + assertTrue(AnnotationUtils.matchesAnnotationClass(annotation, annotationClass)); + assertAnnotation(annotation, annotationClass.getName()); + } + + private void assertAnnotation(AnnotationMirror annotation, String annotationClassName) { + assertEquals(annotation.getAnnotationType().toString(), annotationClassName); } } diff --git a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/ExecutableElementComparatorTest.java b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/ExecutableElementComparatorTest.java index a9e734a3b..4586e3771 100644 --- a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/ExecutableElementComparatorTest.java +++ b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/ExecutableElementComparatorTest.java @@ -1,12 +1,12 @@ package io.microsphere.annotation.processor.util; import io.microsphere.annotation.processor.AbstractAnnotationProcessingTest; -import io.microsphere.annotation.processor.model.Model; +import io.microsphere.annotation.processor.TestService; import org.junit.jupiter.api.Test; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; -import java.util.Set; +import java.lang.reflect.Type; import static io.microsphere.annotation.processor.util.ExecutableElementComparator.INSTANCE; import static io.microsphere.annotation.processor.util.MethodUtils.findMethod; @@ -23,32 +23,54 @@ public class ExecutableElementComparatorTest extends AbstractAnnotationProcessin private final ExecutableElementComparator comparator = INSTANCE; - @Override - protected void addCompiledClasses(Set> compiledClasses) { - compiledClasses.add(Model.class); + @Test + public void testCompareOnSameMethods() { + // Object#toString() + String methodName = "toString"; + ExecutableElement method = getMethod(methodName); + assertEquals(0, comparator.compare(method, method)); + } + + @Test + public void testCompareOnDifferentMethods() { + assertEquals("toString".compareTo("hashCode"), comparator.compare(getMethod("toString"), getMethod("hashCode"))); + } + + @Test + public void testCompareOnOverloadMethodsWithSameParameterCount() { + // Integer#valueOf(int) | Integer#valueOf(String) + TypeElement typeElement = getTypeElement(Integer.class); + String methodName = "valueOf"; + assertEquals(int.class.getName().compareTo(String.class.getName()), comparator.compare(findMethod(typeElement, methodName, int.class), findMethod(typeElement, methodName, String.class))); + } + + @Test + public void testCompareOnOverloadMethodsWithDifferentParameterCount() { + // StringBuilder#append(char[]) | StringBuilder#append(char[],int,int) + TypeElement typeElement = getTypeElement(StringBuilder.class); + String methodName = "append"; + assertEquals(-2, comparator.compare( + findMethod(typeElement, methodName, char[].class), + findMethod(typeElement, methodName, char[].class, int.class, int.class))); } @Test public void testCompare() { - TypeElement type = getTypeElement(Model.class); - // Test methods from java.lang.Object - // Object#toString() - String toStringMethodName = "toString"; - ExecutableElement toStringMethod = findMethod(type.asType(), toStringMethodName); - - String hashCodeMethodName = "hashCode"; - ExecutableElement hashCodeMethod = findMethod(type.asType(), hashCodeMethodName); - assertEquals(0, comparator.compare(toStringMethod, toStringMethod)); - assertEquals(0, comparator.compare(hashCodeMethod, hashCodeMethod)); - assertEquals(toStringMethodName.compareTo(hashCodeMethodName), comparator.compare(toStringMethod, hashCodeMethod)); - - // Object#equals - assertEquals(0, comparator.compare(findMethod(getTypeMirror(getClass()), "equals", Object.class), - findMethod(getTypeMirror(Object.class), "equals", Object.class))); + // AutoCloseable#close() + assertEquals(0, comparator.compare(getMethod("close"), + findMethod(getTypeElement(AutoCloseable.class), "close"))); + + // TestService#echo(String) + assertEquals(0, comparator.compare(getMethod("echo", String.class), + findMethod(getTypeElement(TestService.class), "echo", String.class))); } @Override public boolean equals(Object object) { return super.equals(object); } + + private ExecutableElement getMethod(String methodName, Type... parameterTypes) { + return findMethod(testTypeElement, methodName, parameterTypes); + } } \ No newline at end of file diff --git a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/FieldUtilsTest.java b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/FieldUtilsTest.java index f9b9f6e6e..b3eb82a6d 100644 --- a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/FieldUtilsTest.java +++ b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/FieldUtilsTest.java @@ -21,16 +21,20 @@ import io.microsphere.annotation.processor.model.Model; import org.junit.jupiter.api.Test; -import javax.lang.model.element.Element; +import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeMirror; +import java.io.Serializable; +import java.lang.annotation.ElementType; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; import java.util.List; import java.util.concurrent.TimeUnit; +import static io.microsphere.annotation.processor.util.FieldUtils.equalsFieldName; +import static io.microsphere.annotation.processor.util.FieldUtils.filterDeclaredFields; import static io.microsphere.annotation.processor.util.FieldUtils.findAllDeclaredFields; import static io.microsphere.annotation.processor.util.FieldUtils.findDeclaredFields; import static io.microsphere.annotation.processor.util.FieldUtils.findField; @@ -42,6 +46,10 @@ import static io.microsphere.annotation.processor.util.FieldUtils.isEnumMemberField; import static io.microsphere.annotation.processor.util.FieldUtils.isField; import static io.microsphere.annotation.processor.util.FieldUtils.isNonStaticField; +import static io.microsphere.annotation.processor.util.MethodUtils.findMethod; +import static io.microsphere.lang.function.Predicates.alwaysFalse; +import static io.microsphere.lang.function.Predicates.alwaysTrue; +import static io.microsphere.util.StringUtils.EMPTY_STRING; import static javax.lang.model.element.Modifier.FINAL; import static javax.lang.model.element.Modifier.PRIVATE; import static javax.lang.model.element.Modifier.PUBLIC; @@ -59,6 +67,33 @@ */ public class FieldUtilsTest extends AbstractAnnotationProcessingTest { + @Test + public void testGetDeclaredField() { + TypeElement type = getTypeElement(Model.class); + testGetDeclaredField(type, "f", float.class); + testGetDeclaredField(type, "d", double.class); + testGetDeclaredField(type, "tu", TimeUnit.class); + testGetDeclaredField(type, "str", String.class); + testGetDeclaredField(type, "bi", BigInteger.class); + testGetDeclaredField(type, "bd", BigDecimal.class); + } + + @Test + public void testGetDeclaredFieldOnNotFound() { + TypeElement type = getTypeElement(Model.class); + assertNull(getDeclaredField(type, "b")); + assertNull(getDeclaredField(type, "s")); + assertNull(getDeclaredField(type, "i")); + assertNull(getDeclaredField(type, "l")); + assertNull(getDeclaredField(type, "z")); + } + + @Test + public void testGetDeclaredFieldOnNull() { + assertNull(getDeclaredField(NULL_ELEMENT, "z")); + assertNull(getDeclaredField(NULL_TYPE_MIRROR, "z")); + } + @Test public void testGetDeclaredFields() { TypeElement type = getTypeElement(Model.class); @@ -67,49 +102,25 @@ public void testGetDeclaredFields() { fields = getDeclaredFields(type.asType()); assertModelFields(fields); + } - assertTrue(getDeclaredFields((Element) null).isEmpty()); - assertTrue(getDeclaredFields((TypeMirror) null).isEmpty()); - - fields = findDeclaredFields(type, f -> "f".equals(f.getSimpleName().toString())); - assertEquals(1, fields.size()); - assertEquals("f", fields.get(0).getSimpleName().toString()); + @Test + public void testGetDeclaredFieldsOnNull() { + assertTrue(getDeclaredFields(NULL_ELEMENT).isEmpty()); + assertTrue(getDeclaredFields(NULL_TYPE_MIRROR).isEmpty()); } @Test public void testGetAllDeclaredFields() { TypeElement type = getTypeElement(Model.class); - List fields = getAllDeclaredFields(type); - assertModelAllFields(fields); - - assertTrue(getAllDeclaredFields((Element) null).isEmpty()); - assertTrue(getAllDeclaredFields((TypeMirror) null).isEmpty()); - - fields = findAllDeclaredFields(type, f -> "f".equals(f.getSimpleName().toString())); - assertEquals(1, fields.size()); - assertEquals("f", fields.get(0).getSimpleName().toString()); } @Test - public void testGetDeclaredField() { - TypeElement type = getTypeElement(Model.class); - testGetDeclaredField(type, "f", float.class); - testGetDeclaredField(type, "d", double.class); - testGetDeclaredField(type, "tu", TimeUnit.class); - testGetDeclaredField(type, "str", String.class); - testGetDeclaredField(type, "bi", BigInteger.class); - testGetDeclaredField(type, "bd", BigDecimal.class); - - assertNull(getDeclaredField(type, "b")); - assertNull(getDeclaredField(type, "s")); - assertNull(getDeclaredField(type, "i")); - assertNull(getDeclaredField(type, "l")); - assertNull(getDeclaredField(type, "z")); - - assertNull(getDeclaredField((Element) null, "z")); - assertNull(getDeclaredField((TypeMirror) null, "z")); + public void testGetAllDeclaredFieldsOnNull() { + assertTrue(getAllDeclaredFields(NULL_ELEMENT).isEmpty()); + assertTrue(getAllDeclaredFields(NULL_TYPE_MIRROR).isEmpty()); } @Test @@ -126,20 +137,111 @@ public void testFindField() { testFindField(type, "i", int.class); testFindField(type, "l", long.class); testFindField(type, "z", boolean.class); + } - assertNull(findField((Element) null, "f")); - assertNull(findField((Element) null, null)); + @Test + public void testFindFieldOnNull() { + TypeElement type = getTypeElement(Model.class); + assertNull(findField(NULL_ELEMENT, "f")); + assertNull(findField(NULL_ELEMENT, NULL_STRING)); - assertNull(findField((TypeMirror) null, "f")); - assertNull(findField((TypeMirror) null, null)); + assertNull(findField(NULL_TYPE_MIRROR, "f")); + assertNull(findField(NULL_TYPE_MIRROR, NULL_STRING)); - assertNull(findField(type, null)); - assertNull(findField(type.asType(), null)); + assertNull(findField(type, NULL_STRING)); + assertNull(findField(type.asType(), NULL_STRING)); + } + + @Test + public void testFindDeclaredFields() { + TypeElement type = getTypeElement(Model.class); + + List fields = findAllDeclaredFields(type, alwaysTrue()); + assertModelAllFields(fields); + + fields = findAllDeclaredFields(type, alwaysFalse()); + assertEmptyList(fields); + + fields = findDeclaredFields(type, f -> "f".equals(f.getSimpleName().toString())); + assertEquals(1, fields.size()); + assertEquals("f", fields.get(0).getSimpleName().toString()); + } + + @Test + public void testFindDeclaredFieldsOnNull() { + assertEmptyList(findDeclaredFields(NULL_ELEMENT, alwaysTrue())); + assertEmptyList(findDeclaredFields(NULL_TYPE_MIRROR, alwaysTrue())); + } + + @Test + public void testFindAllDeclaredFields() { + TypeElement type = getTypeElement(Model.class); + + List fields = findAllDeclaredFields(type, alwaysTrue()); + assertModelAllFields(fields); + + fields = findAllDeclaredFields(type, alwaysFalse()); + assertEmptyList(fields); + + fields = findAllDeclaredFields(type, f -> "f".equals(f.getSimpleName().toString())); + assertEquals(1, fields.size()); + assertEquals("f", fields.get(0).getSimpleName().toString()); + } + + @Test + public void testFindAllDeclaredFieldsOnNull() { + assertEmptyList(findAllDeclaredFields(NULL_ELEMENT, alwaysTrue())); + assertEmptyList(findAllDeclaredFields(NULL_TYPE_MIRROR, alwaysTrue())); + } + + @Test + public void testFilterDeclaredFieldsOnNull() { + assertFilterDeclaredFieldsReturningEmptyList(NULL_TYPE_MIRROR); + } + + @Test + public void testFilterDeclaredFields() { + TypeMirror type = getTypeMirror(Model.class); + List fields = filterDeclaredFields(type, true, alwaysTrue()); + assertModelAllFields(fields); + + fields = filterDeclaredFields(type, true, alwaysFalse()); + assertEmptyList(fields); + + fields = filterDeclaredFields(type, false, alwaysTrue()); + assertModelFields(fields); + + fields = filterDeclaredFields(type, false, alwaysFalse()); + assertEmptyList(fields); + } + + @Test + public void testFilterDeclaredFieldsOnNoDeclaredMembers() { + TypeMirror type = getTypeMirror(Serializable.class); + assertFilterDeclaredFieldsReturningEmptyList(type); + } + + @Test + public void testFilterDeclaredFieldsOnNoDeclaredFields() { + TypeMirror type = getTypeMirror(Object.class); + assertFilterDeclaredFieldsReturningEmptyList(type); + } + + private void assertFilterDeclaredFieldsReturningEmptyList(TypeMirror type) { + assertEmptyList(filterDeclaredFields(type, true, alwaysTrue())); + assertEmptyList(filterDeclaredFields(type, false, alwaysTrue())); + assertEmptyList(filterDeclaredFields(type, true, alwaysFalse())); + assertEmptyList(filterDeclaredFields(type, false, alwaysFalse())); + assertEmptyList(filterDeclaredFields(type, true, NULL_PREDICATE_ARRAY)); + assertEmptyList(filterDeclaredFields(type, false, NULL_PREDICATE_ARRAY)); + assertEmptyList(filterDeclaredFields(type, true)); + assertEmptyList(filterDeclaredFields(type, false)); } @Test public void testIsEnumField() { TypeElement type = getTypeElement(Color.class); + VariableElement field = findField(type, "RED"); assertTrue(isEnumMemberField(field)); @@ -153,16 +255,30 @@ public void testIsEnumField() { field = findField(type, "f"); assertFalse(isEnumMemberField(field)); - assertFalse(isEnumMemberField(null)); + assertFalse(isEnumMemberField(NULL_FIELD)); } @Test public void testIsNonStaticField() { TypeElement type = getTypeElement(Model.class); assertTrue(isNonStaticField(findField(type, "f"))); + } - type = getTypeElement(Color.class); - assertFalse(isNonStaticField(findField(type, "BLUE"))); + @Test + public void testIsNonStaticFieldOnStaticField() { + TypeElement type = getTypeElement(Color.class); + for (Color color : Color.values()) { + assertFalse(isNonStaticField(findField(type, color.name()))); + } + } + + @Test + public void testIsNonStaticFieldOnMethod() { + TypeElement type = getTypeElement(Model.class); + ExecutableElement method = findMethod(type, "setF", float.class); + for (VariableElement parameter : method.getParameters()) { + assertFalse(isNonStaticField(parameter)); + } } @Test @@ -173,10 +289,24 @@ public void testIsField() { type = getTypeElement(Color.class); assertTrue(isField(findField(type, "BLUE"), PUBLIC, STATIC, FINAL)); + } + @Test + public void testIsFieldOnMethod() { + TypeElement type = getTypeElement(Model.class); + ExecutableElement method = findMethod(type, "getF"); + for (VariableElement parameter : method.getParameters()) { + assertFalse(isField(parameter)); + } + } + + @Test + public void testIsFieldOnNull() { + assertFalse(isField(NULL_FIELD)); + assertFalse(isField(NULL_FIELD, PUBLIC, STATIC, FINAL)); - assertFalse(isField(null)); - assertFalse(isField(null, PUBLIC, STATIC, FINAL)); + TypeElement type = getTypeElement(Model.class); + assertFalse(isField(findField(type, "f"), NULL_MODIFIER_ARRAY)); } @Test @@ -189,8 +319,21 @@ public void testGetNonStaticFields() { fields = getNonStaticFields(type.asType()); assertModelFields(fields); - assertTrue(getAllNonStaticFields((Element) null).isEmpty()); - assertTrue(getAllNonStaticFields((TypeMirror) null).isEmpty()); + assertTrue(getAllNonStaticFields(NULL_ELEMENT).isEmpty()); + assertTrue(getAllNonStaticFields(NULL_TYPE_MIRROR).isEmpty()); + } + + @Test + public void testGetNonStaticFieldsOnNull() { + assertTrue(getNonStaticFields(NULL_TYPE_MIRROR).isEmpty()); + assertTrue(getNonStaticFields(NULL_ELEMENT).isEmpty()); + } + + @Test + public void testGetNonStaticFieldsOnEnum() { + TypeElement type = getTypeElement(ElementType.class); + List fields = getNonStaticFields(type); + assertEmptyList(fields); } @Test @@ -203,8 +346,27 @@ public void testGetAllNonStaticFields() { fields = getAllNonStaticFields(type.asType()); assertModelAllFields(fields); - assertTrue(getAllNonStaticFields((Element) null).isEmpty()); - assertTrue(getAllNonStaticFields((TypeMirror) null).isEmpty()); + assertTrue(getAllNonStaticFields(NULL_ELEMENT).isEmpty()); + assertTrue(getAllNonStaticFields(NULL_TYPE_MIRROR).isEmpty()); + } + + @Test + public void testEqualsFieldName() { + TypeElement type = getTypeElement(Model.class); + String fieldName = "f"; + VariableElement field = findField(type, fieldName); + assertTrue(equalsFieldName(field, fieldName)); + assertFalse(equalsFieldName(field, "d")); + } + + @Test + public void testEqualsFieldNameOnNull() { + TypeElement type = getTypeElement(Model.class); + String fieldName = "f"; + VariableElement field = findField(type, fieldName); + + assertFalse(equalsFieldName(NULL_FIELD, EMPTY_STRING)); + assertFalse(equalsFieldName(field, NULL_STRING)); } private void assertModelFields(List fields) { @@ -234,6 +396,9 @@ private void assertModelAllFields(List fields) { private void testGetDeclaredField(TypeElement type, String fieldName, Type fieldType) { VariableElement field = getDeclaredField(type, fieldName); assertField(field, fieldName, fieldType); + + field = getDeclaredField(type.asType(), fieldName); + assertField(field, fieldName, fieldType); } private void testFindField(TypeElement type, String fieldName, Type fieldType) { diff --git a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/MemberUtilsTest.java b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/MemberUtilsTest.java index 27df17e95..7ce0a6654 100644 --- a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/MemberUtilsTest.java +++ b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/MemberUtilsTest.java @@ -42,6 +42,7 @@ import static io.microsphere.lang.function.Predicates.alwaysFalse; import static io.microsphere.lang.function.Predicates.alwaysTrue; import static java.util.Collections.emptyList; +import static javax.lang.model.element.ElementKind.FIELD; import static javax.lang.model.element.Modifier.PRIVATE; import static javax.lang.model.util.ElementFilter.fieldsIn; import static javax.lang.model.util.ElementFilter.methodsIn; @@ -69,24 +70,31 @@ protected void beforeTest() { @Test public void testMatchesElementKind() { assertTrue(matchesElementKind(echoMethod, ElementKind.METHOD)); - assertFalse(matchesElementKind(echoMethod, ElementKind.FIELD)); + assertFalse(matchesElementKind(echoMethod, FIELD)); } @Test public void testMatchesElementKindOnNull() { - assertFalse(matchesElementKind(null, ElementKind.FIELD)); - assertFalse(matchesElementKind(echoMethod, null)); + assertFalse(matchesElementKind(NULL_ELEMENT, FIELD)); + assertFalse(matchesElementKind(echoMethod, NULL_ELEMENT_KIND)); } @Test public void testIsPublicNonStatic() { - assertFalse(isPublicNonStatic(null)); - methodsIn(getDeclaredMembers(testTypeElement.asType())).forEach(method -> assertTrue(isPublicNonStatic(method))); + methodsIn(getDeclaredMembers(testTypeElement)).forEach(method -> assertTrue(isPublicNonStatic(method))); + + // Integer#valueOf(String) is a public static method + assertFalse(isPublicNonStatic(findMethod(getTypeElement(Integer.class), "valueOf", String.class))); + } + + @Test + public void testIsPublicNonStaticOnNull() { + assertFalse(isPublicNonStatic(NULL_ELEMENT)); } @Test public void testHasModifiers() { - assertFalse(hasModifiers(null)); + assertFalse(hasModifiers(NULL_ELEMENT)); List members = getAllDeclaredMembers(testTypeElement.asType()); List fields = fieldsIn(members); assertTrue(hasModifiers(fields.get(0), PRIVATE)); @@ -99,8 +107,8 @@ public void testGetDeclaredMembers() { @Test public void testGetDeclaredMembersOnNul() { - assertSame(emptyList(), getDeclaredMembers(NULL_TYPE_ELEMENT)); - assertSame(emptyList(), getDeclaredMembers(NULL_TYPE_MIRROR)); + assertEmptyList(getDeclaredMembers(NULL_TYPE_ELEMENT)); + assertEmptyList(getDeclaredMembers(NULL_TYPE_MIRROR)); } @Test @@ -110,8 +118,8 @@ public void testGetAllDeclaredMembers() { @Test public void testGetAllDeclaredMembersOnNul() { - assertSame(emptyList(), getAllDeclaredMembers(NULL_TYPE_ELEMENT)); - assertSame(emptyList(), getAllDeclaredMembers(NULL_TYPE_MIRROR)); + assertEmptyList(getAllDeclaredMembers(NULL_TYPE_ELEMENT)); + assertEmptyList(getAllDeclaredMembers(NULL_TYPE_MIRROR)); } @Test @@ -121,10 +129,10 @@ public void testFindDeclaredMembers() { @Test public void testFindDeclaredMembersOnNul() { - assertSame(emptyList(), findDeclaredMembers(NULL_TYPE_ELEMENT, alwaysTrue())); - assertSame(emptyList(), findDeclaredMembers(NULL_TYPE_ELEMENT, alwaysFalse())); - assertSame(emptyList(), findDeclaredMembers(NULL_TYPE_MIRROR, alwaysTrue())); - assertSame(emptyList(), findDeclaredMembers(NULL_TYPE_MIRROR, alwaysFalse())); + assertEmptyList(findDeclaredMembers(NULL_TYPE_ELEMENT, alwaysTrue())); + assertEmptyList(findDeclaredMembers(NULL_TYPE_ELEMENT, alwaysFalse())); + assertEmptyList(findDeclaredMembers(NULL_TYPE_MIRROR, alwaysTrue())); + assertEmptyList(findDeclaredMembers(NULL_TYPE_MIRROR, alwaysFalse())); } @Test @@ -134,28 +142,28 @@ public void testFindAllDeclaredMembers() { @Test public void testFindAllDeclaredMembersOnNul() { - assertSame(emptyList(), findAllDeclaredMembers(NULL_TYPE_ELEMENT, alwaysTrue())); - assertSame(emptyList(), findAllDeclaredMembers(NULL_TYPE_ELEMENT, alwaysFalse())); - assertSame(emptyList(), findAllDeclaredMembers(NULL_TYPE_MIRROR, alwaysTrue())); - assertSame(emptyList(), findAllDeclaredMembers(NULL_TYPE_MIRROR, alwaysFalse())); + assertEmptyList(findAllDeclaredMembers(NULL_TYPE_ELEMENT, alwaysTrue())); + assertEmptyList(findAllDeclaredMembers(NULL_TYPE_ELEMENT, alwaysFalse())); + assertEmptyList(findAllDeclaredMembers(NULL_TYPE_MIRROR, alwaysTrue())); + assertEmptyList(findAllDeclaredMembers(NULL_TYPE_MIRROR, alwaysFalse())); } @Test public void testFilterMembers() { - assertSame(emptyList(), filterMembers(ofList(testTypeElement), alwaysFalse())); + assertEmptyList(filterMembers(ofList(testTypeElement), alwaysFalse())); } @Test public void testFilterMembersOnNull() { - assertSame(emptyList(), filterMembers(null, alwaysTrue())); + assertEmptyList(filterMembers(NULL_LIST, alwaysTrue())); List methods = ofList(echoMethod); - assertSame(methods, filterMembers(methods, null)); + assertSame(methods, filterMembers(methods, NULL_PREDICATE_ARRAY)); } @Test public void testFilterMembersOnEmpty() { - assertSame(emptyList(), filterMembers(emptyList(), alwaysTrue())); + assertEmptyList(filterMembers(emptyList(), alwaysTrue())); List methods = ofList(echoMethod); assertSame(methods, filterMembers(methods)); } @@ -168,8 +176,8 @@ public void testMatchParameterTypes() { @Test public void testMatchParameterTypesOnNull() { - assertFalse(matchParameterTypes(null, String.class)); - assertFalse(matchParameterTypes(emptyList(), null)); + assertFalse(matchParameterTypes(NULL_LIST, String.class)); + assertFalse(matchParameterTypes(emptyList(), NULL_CLASS_ARRAY)); } @Test @@ -180,8 +188,8 @@ public void testMatchParameterTypeNames() { @Test public void testMatchParameterTypeNamesOnNull() { - assertFalse(matchParameterTypeNames(null, "java.lang.String")); - assertFalse(matchParameterTypeNames(emptyList(), null)); + assertFalse(matchParameterTypeNames(NULL_LIST, "java.lang.String")); + assertFalse(matchParameterTypeNames(emptyList(), NULL_STRING_ARRAY)); } private void assertFindDeclaredMembersOfModel() { @@ -189,8 +197,8 @@ private void assertFindDeclaredMembersOfModel() { assertGetDeclaredMembersOfModel(findDeclaredMembers(type, alwaysTrue(), alwaysTrue())); assertGetDeclaredMembersOfModel(findDeclaredMembers(type.asType(), alwaysTrue())); - assertSame(emptyList(), findDeclaredMembers(type, alwaysFalse())); - assertSame(emptyList(), findDeclaredMembers(type.asType(), alwaysFalse())); + assertEmptyList(findDeclaredMembers(type, alwaysFalse())); + assertEmptyList(findDeclaredMembers(type.asType(), alwaysFalse())); } private void assertFindAllDeclaredMembersOfModel() { @@ -198,8 +206,8 @@ private void assertFindAllDeclaredMembersOfModel() { assertGetAllDeclaredMembersOfModel(findAllDeclaredMembers(type, alwaysTrue(), alwaysTrue())); assertGetAllDeclaredMembersOfModel(findAllDeclaredMembers(type.asType(), alwaysTrue())); - assertSame(emptyList(), findAllDeclaredMembers(type, alwaysFalse())); - assertSame(emptyList(), findAllDeclaredMembers(type.asType(), alwaysFalse())); + assertEmptyList(findAllDeclaredMembers(type, alwaysFalse())); + assertEmptyList(findAllDeclaredMembers(type.asType(), alwaysFalse())); } private void assertGetDeclaredMembersOfModel() { diff --git a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/MethodUtilsTest.java b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/MethodUtilsTest.java index 159d765d5..d7048f4d5 100644 --- a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/MethodUtilsTest.java +++ b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/MethodUtilsTest.java @@ -23,6 +23,7 @@ import io.microsphere.constants.PropertyConstants; import org.junit.jupiter.api.Test; +import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeMirror; @@ -31,6 +32,8 @@ import java.util.List; import java.util.Set; +import static io.microsphere.annotation.processor.util.MemberUtils.getDeclaredMembers; +import static io.microsphere.annotation.processor.util.MemberUtils.isPublicNonStatic; import static io.microsphere.annotation.processor.util.MethodUtils.filterMethods; import static io.microsphere.annotation.processor.util.MethodUtils.findAllDeclaredMethods; import static io.microsphere.annotation.processor.util.MethodUtils.findDeclaredMethods; @@ -54,6 +57,7 @@ import static io.microsphere.util.ArrayUtils.EMPTY_STRING_ARRAY; import static io.microsphere.util.ArrayUtils.ofArray; import static java.util.Collections.emptyList; +import static javax.lang.model.element.ElementKind.METHOD; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -128,24 +132,24 @@ public void testFindDeclaredMethods() { assertEquals(2, methods.size()); methods = findDeclaredMethods(testTypeElement, alwaysFalse()); - assertSame(emptyList(), methods); + assertEmptyList(methods); methods = findDeclaredMethods(testTypeMirror, alwaysFalse()); - assertSame(emptyList(), methods); + assertEmptyList(methods); } @Test public void testFindDeclaredMethodsOnNoMemberType() { TypeElement typeElement = getTypeElement(Serializable.class); List methods = findDeclaredMethods(typeElement, alwaysTrue()); - assertSame(emptyList(), methods); + assertEmptyList(methods); } @Test public void testFindDeclaredMethodsOnNoMethodType() { TypeElement typeElement = getTypeElement(PropertyConstants.class); List methods = findDeclaredMethods(typeElement, alwaysTrue()); - assertSame(emptyList(), methods); + assertEmptyList(methods); } @Test @@ -157,32 +161,32 @@ public void testFindAllDeclaredMethods() { assertEquals(objectMethodsSize + 14, methods.size()); methods = findAllDeclaredMethods(testTypeElement, alwaysFalse()); - assertSame(emptyList(), methods); + assertEmptyList(methods); methods = findAllDeclaredMethods(testTypeMirror, alwaysFalse()); - assertSame(emptyList(), methods); + assertEmptyList(methods); } @Test public void testFindAllDeclaredMethodsOnNoMemberType() { TypeElement typeElement = getTypeElement(Serializable.class); List methods = findAllDeclaredMethods(typeElement, alwaysTrue()); - assertSame(emptyList(), methods); + assertEmptyList(methods); } @Test public void testFindAllDeclaredMethodsOnNoMethodType() { TypeElement typeElement = getTypeElement(Constants.class); List methods = findAllDeclaredMethods(typeElement, alwaysTrue()); - assertSame(emptyList(), methods); + assertEmptyList(methods); } @Test public void testFindAllDeclaredMethodsOnNull() { - assertSame(emptyList(), findAllDeclaredMethods(NULL_TYPE_ELEMENT, alwaysTrue())); - assertSame(emptyList(), findAllDeclaredMethods(NULL_TYPE_ELEMENT, alwaysFalse())); - assertSame(emptyList(), findAllDeclaredMethods(NULL_TYPE_MIRROR, alwaysTrue())); - assertSame(emptyList(), findAllDeclaredMethods(NULL_TYPE_MIRROR, alwaysFalse())); + assertEmptyList(findAllDeclaredMethods(NULL_TYPE_ELEMENT, alwaysTrue())); + assertEmptyList(findAllDeclaredMethods(NULL_TYPE_ELEMENT, alwaysFalse())); + assertEmptyList(findAllDeclaredMethods(NULL_TYPE_MIRROR, alwaysTrue())); + assertEmptyList(findAllDeclaredMethods(NULL_TYPE_MIRROR, alwaysFalse())); } @Test @@ -193,8 +197,8 @@ public void testFindAllDeclaredMethodsWithExcludedTypes() { @Test public void testFindAllDeclaredMethodsWithExcludedTypesOnNull() { - assertSame(emptyList(), findAllDeclaredMethods(NULL_TYPE_ELEMENT, Object.class)); - assertSame(emptyList(), findAllDeclaredMethods(NULL_TYPE_MIRROR, Object.class)); + assertEmptyList(findAllDeclaredMethods(NULL_TYPE_ELEMENT, Object.class)); + assertEmptyList(findAllDeclaredMethods(NULL_TYPE_MIRROR, Object.class)); } @Test @@ -208,30 +212,51 @@ public void testFindPublicNonStaticMethods() { @Test public void testFindPublicNonStaticMethodsOnNull() { - assertSame(emptyList(), findPublicNonStaticMethods(NULL_TYPE_ELEMENT, Object.class)); - assertSame(emptyList(), findPublicNonStaticMethods(NULL_TYPE_MIRROR, Object.class)); + assertEmptyList(findPublicNonStaticMethods(NULL_TYPE_ELEMENT, Object.class)); + assertEmptyList(findPublicNonStaticMethods(NULL_TYPE_MIRROR, Object.class)); } @Test public void testIsMethod() { - List methods = findPublicNonStaticMethods(testTypeElement, Object.class); - assertEquals(14, methods.stream().map(MethodUtils::isMethod).count()); + List members = getDeclaredMembers(testTypeElement); + for (Element member : members) { + if (member instanceof ExecutableElement) { + ExecutableElement element = (ExecutableElement) member; + assertEquals(METHOD == member.getKind(), isMethod(element)); + } + } } @Test public void testIsMethodOnNull() { - assertFalse(isMethod(null)); + assertFalse(isMethod(NULL_METHOD)); } @Test public void testIsPublicNonStaticMethod() { - List methods = findPublicNonStaticMethods(testTypeElement, Object.class); - assertEquals(14, methods.stream().map(MethodUtils::isPublicNonStaticMethod).count()); + List members = getDeclaredMembers(testTypeElement); + for (Element member : members) { + if (member instanceof ExecutableElement) { + ExecutableElement element = (ExecutableElement) member; + switch (member.getKind()) { + case METHOD: + assertEquals(isPublicNonStaticMethod(element), isPublicNonStatic(element)); + break; + case CONSTRUCTOR: + assertFalse(isPublicNonStaticMethod(element)); + break; + } + } + } + + // Integer#valueOf(String) is a public static method + assertFalse(isPublicNonStaticMethod(findMethod(getTypeElement(Integer.class), "valueOf", String.class))); + } @Test public void testIsPublicNonStaticMethodOnNull() { - assertFalse(isPublicNonStaticMethod(null)); + assertFalse(isPublicNonStaticMethod(NULL_METHOD)); } @Test @@ -269,13 +294,48 @@ public void testFindMethod() { assertFindMethod(type, "equals", Object.class); } + @Test + public void testFindMethodOnNotFound() { + assertNull(findMethod(testTypeElement, "notFound")); + assertNull(findMethod(testTypeElement, "notFound", String.class)); + assertNull(findMethod(testTypeElement, "notFound", "java.lang.String")); + + assertNull(findMethod(testTypeMirror, "notFound")); + assertNull(findMethod(testTypeMirror, "notFound", String.class)); + assertNull(findMethod(testTypeMirror, "notFound", "java.lang.String")); + } + @Test public void testFindMethodOnNull() { assertNull(findMethod(NULL_TYPE_ELEMENT, "toString")); + assertNull(findMethod(NULL_TYPE_ELEMENT, "toString", String.class)); + assertNull(findMethod(NULL_TYPE_ELEMENT, "toString", "java.lang.String")); + assertNull(findMethod(NULL_TYPE_ELEMENT, "toString", NULL_TYPE_ARRAY)); + assertNull(findMethod(NULL_TYPE_ELEMENT, "toString", NULL_STRING_ARRAY)); + assertNull(findMethod(NULL_TYPE_MIRROR, "toString")); - assertNull(findMethod(testTypeElement, null)); - assertNull(findMethod(testTypeMirror, null)); + assertNull(findMethod(NULL_TYPE_MIRROR, "toString", String.class)); + assertNull(findMethod(NULL_TYPE_MIRROR, "toString", "java.lang.String")); + assertNull(findMethod(NULL_TYPE_MIRROR, "toString", NULL_TYPE_ARRAY)); + assertNull(findMethod(NULL_TYPE_MIRROR, "toString", NULL_STRING_ARRAY)); + + assertNull(findMethod(testTypeElement, NULL_STRING)); + assertNull(findMethod(testTypeElement, NULL_STRING, String.class)); + assertNull(findMethod(testTypeElement, NULL_STRING, "java.lang.String")); + assertNull(findMethod(testTypeElement, NULL_STRING, NULL_TYPE_ARRAY)); + assertNull(findMethod(testTypeElement, NULL_STRING, NULL_STRING_ARRAY)); + + assertNull(findMethod(testTypeMirror, NULL_STRING)); + assertNull(findMethod(testTypeMirror, NULL_STRING, String.class)); + assertNull(findMethod(testTypeMirror, NULL_STRING, "java.lang.String")); + assertNull(findMethod(testTypeMirror, NULL_STRING, NULL_TYPE_ARRAY)); + assertNull(findMethod(testTypeMirror, NULL_STRING, NULL_STRING_ARRAY)); + + assertNull(findMethod(testTypeElement, "toString", NULL_TYPE_ARRAY)); + assertNull(findMethod(testTypeElement, "toString", NULL_STRING_ARRAY)); + + assertNull(findMethod(testTypeMirror, "toString", NULL_TYPE_ARRAY)); assertNull(findMethod(testTypeMirror, "toString", NULL_STRING_ARRAY)); } @@ -299,20 +359,20 @@ public void testFilterMethods() { @Test public void testFilterMethodsOnNull() { - assertSame(emptyList(), filterMethods(null, alwaysTrue())); - assertSame(emptyList(), filterMethods(null, null)); + assertEmptyList(filterMethods(NULL_LIST, alwaysTrue())); + assertEmptyList(filterMethods(NULL_LIST, NULL_PREDICATE_ARRAY)); } @Test public void testFilterMethodsOnEmpty() { - assertSame(emptyList(), filterMethods(emptyList(), alwaysTrue())); - assertSame(emptyList(), filterMethods(emptyList(), null)); + assertEmptyList(filterMethods(emptyList(), alwaysTrue())); + assertEmptyList(filterMethods(emptyList(), NULL_PREDICATE_ARRAY)); } @Test public void testFilterMethodsOnReturningEmptyList() { List methods = getDeclaredMethods(testTypeElement); - assertSame(emptyList(), filterMethods(methods, alwaysFalse())); + assertEmptyList(filterMethods(methods, alwaysFalse())); assertSame(methods, filterMethods(methods)); } @@ -320,12 +380,12 @@ public void testFilterMethodsOnReturningEmptyList() { public void testGetMethodName() { ExecutableElement method = findMethod(testTypeElement, "echo", "java.lang.String"); assertEquals("echo", getMethodName(method)); - assertNull(getMethodName(null)); + assertNull(getMethodName(NULL_METHOD)); } @Test public void testGetMethodNameOnNull() { - assertNull(getMethodName(null)); + assertNull(getMethodName(NULL_METHOD)); } @Test @@ -336,7 +396,7 @@ public void testReturnTypeName() { @Test public void testReturnTypeNameOnNull() { - assertNull(getReturnTypeName(null)); + assertNull(getReturnTypeName(NULL_METHOD)); } @Test @@ -348,13 +408,13 @@ public void testMatchParameterTypeNames() { @Test public void testMatchParameterTypeNamesOnNull() { - assertSame(EMPTY_STRING_ARRAY, getMethodParameterTypeNames(null)); + assertSame(EMPTY_STRING_ARRAY, getMethodParameterTypeNames(NULL_METHOD)); } @Test public void testMatchParameterTypes() { ExecutableElement method = findMethod(testTypeElement, "toString"); - assertSame(emptyList(), getMethodParameterTypeMirrors(method)); + assertEmptyList(getMethodParameterTypeMirrors(method)); method = findMethod(testTypeElement, "equals", Object.class); List parameterTypes = getMethodParameterTypeMirrors(method); @@ -363,7 +423,7 @@ public void testMatchParameterTypes() { @Test public void testMatchParameterTypesOnNull() { - assertSame(emptyList(), getMethodParameterTypeMirrors(null)); + assertEmptyList(getMethodParameterTypeMirrors(NULL_METHOD)); } @Test @@ -393,11 +453,11 @@ public void testMatchesOnNull() { String[] parameterTypeNames = getTypeNames(parameterTypes); ExecutableElement method = findMethod(testTypeElement, methodName, parameterTypes); - assertFalse(matches(null, NULL_STRING, parameterTypes)); + assertFalse(matches(NULL_METHOD, NULL_STRING, parameterTypes)); assertFalse(matches(method, NULL_STRING, parameterTypes)); assertFalse(matches(method, methodName, NULL_TYPE_ARRAY)); - assertFalse(matches(null, NULL_STRING, parameterTypeNames)); + assertFalse(matches(NULL_METHOD, NULL_STRING, parameterTypeNames)); assertFalse(matches(method, NULL_STRING, parameterTypeNames)); assertFalse(matches(method, methodName, NULL_STRING_ARRAY)); } @@ -412,7 +472,7 @@ public void testGetEnclosingElement() { @Test public void testGetEnclosingElementOnNull() { - assertNull(getEnclosingElement(null)); + assertNull(getEnclosingElement(NULL_METHOD)); } private void assertFindMethod(Type type, String methodName, Type... parameterTypes) { diff --git a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/TypeUtilsTest.java b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/TypeUtilsTest.java index 064926f12..ce381e963 100644 --- a/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/TypeUtilsTest.java +++ b/microsphere-annotation-processor/src/test/java/io/microsphere/annotation/processor/util/TypeUtilsTest.java @@ -31,7 +31,6 @@ import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; -import javax.lang.model.element.VariableElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; @@ -43,9 +42,12 @@ import java.util.EventListener; import java.util.List; import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.function.Predicate; import static io.microsphere.annotation.processor.util.FieldUtils.findField; import static io.microsphere.annotation.processor.util.FieldUtils.getDeclaredFields; +import static io.microsphere.annotation.processor.util.MethodUtils.findMethod; import static io.microsphere.annotation.processor.util.TypeUtils.findAllDeclaredTypes; import static io.microsphere.annotation.processor.util.TypeUtils.findAllDeclaredTypesOfInterfaces; import static io.microsphere.annotation.processor.util.TypeUtils.findAllDeclaredTypesOfSuperTypes; @@ -94,6 +96,7 @@ import static io.microsphere.lang.function.Predicates.alwaysFalse; import static io.microsphere.lang.function.Predicates.alwaysTrue; import static io.microsphere.reflect.TypeUtils.getTypeNames; +import static io.microsphere.util.ArrayUtils.EMPTY_STRING_ARRAY; import static io.microsphere.util.ArrayUtils.combine; import static io.microsphere.util.ArrayUtils.length; import static io.microsphere.util.ArrayUtils.ofArray; @@ -213,7 +216,7 @@ public void testIsSimpleType() { @Test public void testIsSimpleTypeOnNull() { - assertFalse(isSimpleType((TypeElement) null)); + assertFalse(isSimpleType(NULL_TYPE_ELEMENT)); assertFalse(isSimpleType(NULL_TYPE_MIRROR)); } @@ -231,26 +234,32 @@ public void testIsSameTypeOnNull() { assertFalse(isSameType(NULL_ELEMENT, testClass)); assertFalse(isSameType(NULL_ELEMENT, testClassName)); - assertFalse(isSameType(testTypeElement, (Type) null)); - assertFalse(isSameType(testTypeElement, (String) null)); + assertFalse(isSameType(testTypeElement, NULL_TYPE)); + assertFalse(isSameType(testTypeElement, NULL_STRING)); - assertFalse(isSameType(testTypeMirror, (Type) null)); - assertFalse(isSameType(testTypeMirror, (String) null)); + assertFalse(isSameType(testTypeMirror, NULL_TYPE)); + assertFalse(isSameType(testTypeMirror, NULL_STRING)); - assertTrue(isSameType(NULL_TYPE_MIRROR, (Type) null)); - assertTrue(isSameType(NULL_TYPE_MIRROR, (String) null)); - assertTrue(isSameType(NULL_ELEMENT, (Type) null)); - assertTrue(isSameType(NULL_ELEMENT, (String) null)); + assertTrue(isSameType(NULL_TYPE_MIRROR, NULL_TYPE)); + assertTrue(isSameType(NULL_TYPE_MIRROR, NULL_STRING)); + assertTrue(isSameType(NULL_ELEMENT, NULL_TYPE)); + assertTrue(isSameType(NULL_ELEMENT, NULL_STRING)); } @Test public void testIsArrayTypeOnTypeMirror() { assertIsArrayType(ArrayTypeModel.class); + + assertFalse(isArrayType(getTypeMirror(Color.class))); + assertFalse(isArrayType(getTypeMirror(ArrayTypeModel.class))); } @Test public void testIsArrayTypeOnElement() { assertIsArrayType(getTypeElement(ArrayTypeModel.class)); + + assertFalse(isArrayType(getTypeElement(Color.class))); + assertFalse(isArrayType(getTypeElement(ArrayTypeModel.class))); } @Test @@ -273,8 +282,20 @@ public void testIsEnumTypeOnNull() { @Test public void testIsClassType() { + // class assertTrue(isClassType(getTypeElement(ArrayTypeModel.class))); + assertTrue(isClassType(getDeclaredType(ArrayTypeModel.class))); + + assertTrue(isClassType(getTypeElement(Model.class))); assertTrue(isClassType(getDeclaredType(Model.class))); + + // enum + assertFalse(isClassType(getTypeElement(TimeUnit.class))); + assertFalse(isClassType(getDeclaredType(TimeUnit.class))); + + // interface + assertFalse(isClassType(getTypeElement(Serializable.class))); + assertFalse(isClassType(getDeclaredType(Serializable.class))); } @Test @@ -286,10 +307,11 @@ public void testIsClassTypeOnNull() { @Test public void testIsPrimitiveType() { TypeElement type = getTypeElement(PrimitiveTypeModel.class); - getDeclaredFields(type.asType()) - .stream() - .map(VariableElement::asType) - .forEach(t -> assertTrue(isPrimitiveType(t))); + + getDeclaredFields(type).forEach(t -> { + assertTrue(isPrimitiveType(t)); + assertTrue(isPrimitiveType(t.asType())); + }); assertFalse(isPrimitiveType(getTypeElement(ArrayTypeModel.class))); } @@ -334,6 +356,10 @@ public void testIsAnnotationTypeOnNull() { public void testIsTypeElement() { assertTrue(isTypeElement(testTypeElement)); assertTrue(isTypeElement(testTypeMirror)); + assertTrue(isTypeElement(getFieldType(testTypeElement, "context"))); + + // primitive type + assertFalse(isTypeElement(getTypeMirror(int.class))); } @Test @@ -346,11 +372,15 @@ public void testIsTypeElementOnNull() { public void testIsDeclaredType() { assertTrue(isDeclaredType(testTypeElement)); assertTrue(isDeclaredType(testTypeMirror)); - assertFalse(isDeclaredType(NULL_ELEMENT)); - assertFalse(isDeclaredType(NULL_TYPE_MIRROR)); assertFalse(isDeclaredType(types.getNullType())); assertFalse(isDeclaredType(types.getPrimitiveType(TypeKind.BYTE))); assertFalse(isDeclaredType(types.getArrayType(types.getPrimitiveType(TypeKind.BYTE)))); + + // field + assertFalse(isDeclaredType(findField(getTypeMirror(PrimitiveTypeModel.class), "z"))); + + // method + assertFalse(isDeclaredType(findMethod(testTypeElement, "close"))); } @Test @@ -391,14 +421,14 @@ public void testOfTypeMirrors() { @Test public void testOfTypeMirrorsOnNull() { - assertTrue(ofTypeMirrors(EMPTY_ELEMENT_ARRAY).isEmpty()); - assertTrue(ofTypeMirrors(NULL_COLLECTION).isEmpty()); + assertEmptyList(ofTypeMirrors(EMPTY_ELEMENT_ARRAY)); + assertEmptyList(ofTypeMirrors(NULL_COLLECTION)); } @Test public void testOfTypeMirrorsOnEmpty() { - assertTrue(ofTypeMirrors(EMPTY_ELEMENT_ARRAY).isEmpty()); - assertTrue(ofTypeMirrors(emptyList()).isEmpty()); + assertEmptyList(ofTypeMirrors(EMPTY_ELEMENT_ARRAY)); + assertEmptyList(ofTypeMirrors(emptyList())); } @Test @@ -408,14 +438,14 @@ public void testOfTypeElements() { @Test public void testOfTypeElementsOnNull() { - assertTrue(ofTypeElements(NULL_TYPE_MIRROR_ARRAY).isEmpty()); - assertTrue(ofTypeElements(NULL_COLLECTION).isEmpty()); + assertEmptyList(ofTypeElements(NULL_TYPE_MIRROR_ARRAY)); + assertEmptyList(ofTypeElements(NULL_COLLECTION)); } @Test public void testOfTypeElementsOnEmpty() { - assertTrue(ofTypeElements(EMPTY_TYPE_MIRROR_ARRAY).isEmpty()); - assertTrue(ofTypeElements(emptyList()).isEmpty()); + assertEmptyList(ofTypeElements(EMPTY_TYPE_MIRROR_ARRAY)); + assertEmptyList(ofTypeElements(emptyList())); } @Test @@ -431,13 +461,13 @@ public void testOfDeclaredTypesWithFilter() { @Test public void testOfDeclaredTypesOnNull() { - assertTrue(ofDeclaredTypes(NULL_ELEMENT_ARRAY).isEmpty()); - assertTrue(ofDeclaredTypes(NULL_COLLECTION).isEmpty()); + assertEmptyList(ofDeclaredTypes(NULL_ELEMENT_ARRAY)); + assertEmptyList(ofDeclaredTypes(NULL_COLLECTION)); } @Test public void testOfDeclaredTypesOnEmpty() { - assertTrue(ofDeclaredTypes(emptyList()).isEmpty()); + assertEmptyList(ofDeclaredTypes(emptyList())); } @Test @@ -456,7 +486,7 @@ public void testGetTypeElementOfSuperclass() { @Test public void testGetTypeElementOfSuperclassOnNull() { - assertNull(getTypeElementOfSuperclass(null)); + assertNull(getTypeElementOfSuperclass(NULL_TYPE_ELEMENT)); } @Test @@ -467,7 +497,7 @@ public void testGetAllTypeElementsOfSuperclasses() { @Test public void testGetAllTypeElementsOfSuperclassesOnNull() { - assertTrue(getAllTypeElementsOfSuperclasses(null).isEmpty()); + assertEmptyList(getAllTypeElementsOfSuperclasses(NULL_TYPE_ELEMENT)); } @Test @@ -478,7 +508,7 @@ public void testGetTypeElementsOfInterfaces() { @Test public void testGetTypeElementsOfInterfacesOnNull() { - assertTrue(getTypeElementsOfInterfaces(null).isEmpty()); + assertEmptyList(getTypeElementsOfInterfaces(NULL_TYPE_ELEMENT)); } @Test @@ -489,7 +519,7 @@ public void testGetAllTypeElementsOfInterfaces() { @Test public void testGetAllTypeElementsOfInterfacesOnNull() { - assertTrue(getAllTypeElementsOfInterfaces(null).isEmpty()); + assertEmptyList(getAllTypeElementsOfInterfaces(NULL_TYPE_ELEMENT)); } @Test @@ -500,7 +530,13 @@ public void testGetAllTypeElements() { @Test public void testGetAllTypeElementsOnNull() { - assertTrue(getAllTypeElements(null).isEmpty()); + assertEmptyList(getAllTypeElements(NULL_TYPE_ELEMENT)); + } + + @Test + public void testGetTypeElementsWithNoArgument() { + List typeElements = TypeUtils.getTypeElements(testTypeElement); + assertTypeElements(typeElements, SELF_TYPE_PLUS_SUPER_CLASS_PLUS_SUPER_INTERFACES); } @Test @@ -556,7 +592,7 @@ public void testGetTypeElements() { // false true false false : nothing typeElements = TypeUtils.getTypeElements(testTypeElement, false, true, false, false); assertTypeElements(typeElements); - assertSame(emptyList(), typeElements); + assertEmptyList(typeElements); // false false true true : super class + super interfaces typeElements = TypeUtils.getTypeElements(testTypeElement, false, false, true, true); @@ -575,30 +611,30 @@ public void testGetTypeElements() { // false false false false : nothing typeElements = TypeUtils.getTypeElements(testTypeElement, false, false, false, false); assertTypeElements(typeElements); - assertSame(emptyList(), typeElements); + assertEmptyList(typeElements); } @Test public void testGetTypeElementsOnNull() { - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, true, true, true, true).isEmpty()); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, true, true, true, false).isEmpty()); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, true, true, false, true).isEmpty()); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, true, true, false, false).isEmpty()); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, true, true, true, true)); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, true, true, true, false)); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, true, true, false, true)); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, true, true, false, false)); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, true, false, true, true).isEmpty()); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, true, false).isEmpty()); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, false, true).isEmpty()); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, false, false).isEmpty()); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, true, false, true, true)); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, true, false)); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, false, true)); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, false, false)); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, true, true, true).isEmpty()); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, true, true, false).isEmpty()); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, true, false, true).isEmpty()); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, true, false, false).isEmpty()); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, true, true, true)); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, true, true, false)); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, true, false, true)); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, true, false, false)); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, true, true).isEmpty()); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, true, false).isEmpty()); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, false, true).isEmpty()); - assertTrue(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, false, false).isEmpty()); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, true, true)); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, true, false)); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, false, true)); + assertEmptyList(TypeUtils.getTypeElements(NULL_TYPE_ELEMENT, false, false, false, false)); } @Test @@ -607,13 +643,13 @@ public void testFindAllTypeElementsOfSuperclasses() { assertTypeElements(typeElements, ALL_SUPER_CLASSES); assertEquals(getAllTypeElementsOfSuperclasses(testTypeElement), typeElements); - assertSame(emptyList(), findAllTypeElementsOfSuperclasses(testTypeElement, alwaysFalse())); + assertEmptyList(findAllTypeElementsOfSuperclasses(testTypeElement, alwaysFalse())); } @Test public void testFindAllTypeElementsOfSuperclassesOnNull() { - assertSame(emptyList(), findAllTypeElementsOfSuperclasses(null, alwaysTrue())); - assertSame(emptyList(), findAllTypeElementsOfSuperclasses(null, alwaysFalse())); + assertEmptyList(findAllTypeElementsOfSuperclasses(NULL_TYPE_ELEMENT, alwaysTrue())); + assertEmptyList(findAllTypeElementsOfSuperclasses(NULL_TYPE_ELEMENT, alwaysFalse())); } @Test @@ -622,13 +658,13 @@ public void testFindAllTypeElementsOfInterfaces() { assertTypeElements(typeElements, ALL_SUPER_INTERFACES); assertEquals(getAllTypeElementsOfInterfaces(testTypeElement), typeElements); - assertSame(emptyList(), findAllTypeElementsOfInterfaces(testTypeElement, alwaysFalse())); + assertEmptyList(findAllTypeElementsOfInterfaces(testTypeElement, alwaysFalse())); } @Test public void testFindAllTypeElementsOfInterfacesOnNull() { - assertSame(emptyList(), findAllTypeElementsOfInterfaces(null, alwaysTrue())); - assertSame(emptyList(), findAllTypeElementsOfInterfaces(null, alwaysFalse())); + assertEmptyList(findAllTypeElementsOfInterfaces(NULL_TYPE_ELEMENT, alwaysTrue())); + assertEmptyList(findAllTypeElementsOfInterfaces(NULL_TYPE_ELEMENT, alwaysFalse())); } @Test @@ -637,13 +673,13 @@ public void testFindTypeElementsOfInterfaces() { assertTypeElements(typeElements, SUPER_INTERFACES); assertEquals(getTypeElementsOfInterfaces(testTypeElement), typeElements); - assertSame(emptyList(), findTypeElementsOfInterfaces(testTypeElement, alwaysFalse())); + assertEmptyList(findTypeElementsOfInterfaces(testTypeElement, alwaysFalse())); } @Test public void testFindTypeElementsOfInterfacesOnNull() { - assertSame(emptyList(), findTypeElementsOfInterfaces(null, alwaysTrue())); - assertSame(emptyList(), findTypeElementsOfInterfaces(null, alwaysFalse())); + assertEmptyList(findTypeElementsOfInterfaces(NULL_TYPE_ELEMENT, alwaysTrue())); + assertEmptyList(findTypeElementsOfInterfaces(NULL_TYPE_ELEMENT, alwaysFalse())); } @Test @@ -699,7 +735,7 @@ public void testFindTypeElements() { // false true false false : nothing typeElements = findTypeElements(testTypeElement, false, true, false, false, alwaysTrue()); assertTypeElements(typeElements); - assertSame(emptyList(), typeElements); + assertEmptyList(typeElements); // false false true true : super types typeElements = findTypeElements(testTypeElement, false, false, true, true, alwaysTrue()); @@ -718,9 +754,16 @@ public void testFindTypeElements() { // false false false false : nothing typeElements = findTypeElements(testTypeElement, false, false, false, false, alwaysTrue()); assertTypeElements(typeElements); - assertSame(emptyList(), typeElements); + assertEmptyList(typeElements); } + @Test + public void testFindTypeElementsOnNullFilterElement() { + assertThrows(IllegalArgumentException.class, + () -> findTypeElements(testTypeElement, true, true, true, true, new Predicate[]{null})); + } + + @Test public void testGetDeclaredTypeOfSuperclass() { DeclaredType superDeclaredType = getDeclaredTypeOfSuperclass(testTypeMirror); @@ -749,8 +792,8 @@ public void testGetDeclaredTypesOfInterfaces() { @Test public void testGetDeclaredTypesOfInterfacesOnNull() { - assertTrue(getDeclaredTypesOfInterfaces(NULL_ELEMENT).isEmpty()); - assertTrue(getDeclaredTypesOfInterfaces(NULL_TYPE_MIRROR).isEmpty()); + assertEmptyList(getDeclaredTypesOfInterfaces(NULL_ELEMENT)); + assertEmptyList(getDeclaredTypesOfInterfaces(NULL_TYPE_MIRROR)); } @Test @@ -761,8 +804,8 @@ public void testGetAllDeclaredTypesOfSuperclasses() { @Test public void testGetAllDeclaredTypesOfSuperclassesOnNull() { - assertTrue(getAllDeclaredTypesOfSuperclasses(NULL_ELEMENT).isEmpty()); - assertTrue(getAllDeclaredTypesOfSuperclasses(NULL_TYPE_MIRROR).isEmpty()); + assertEmptyList(getAllDeclaredTypesOfSuperclasses(NULL_ELEMENT)); + assertEmptyList(getAllDeclaredTypesOfSuperclasses(NULL_TYPE_MIRROR)); } @Test @@ -773,8 +816,8 @@ public void testGetAllDeclaredTypesOfInterfaces() { @Test public void testGetAllDeclaredTypesOfInterfacesOnNull() { - assertTrue(getAllDeclaredTypesOfInterfaces(NULL_ELEMENT).isEmpty()); - assertTrue(getAllDeclaredTypesOfInterfaces(NULL_TYPE_MIRROR).isEmpty()); + assertEmptyList(getAllDeclaredTypesOfInterfaces(NULL_ELEMENT)); + assertEmptyList(getAllDeclaredTypesOfInterfaces(NULL_TYPE_MIRROR)); } @Test @@ -785,8 +828,8 @@ public void testGetAllDeclaredTypesOfSuperTypes() { @Test public void testGetAllDeclaredTypesOfSuperTypesOnNull() { - assertTrue(getAllDeclaredTypesOfSuperTypes(NULL_ELEMENT).isEmpty()); - assertTrue(getAllDeclaredTypesOfSuperTypes(NULL_TYPE_MIRROR).isEmpty()); + assertEmptyList(getAllDeclaredTypesOfSuperTypes(NULL_ELEMENT)); + assertEmptyList(getAllDeclaredTypesOfSuperTypes(NULL_TYPE_MIRROR)); } @Test @@ -797,8 +840,8 @@ public void testGetAllDeclaredTypes() { @Test public void testGetAllDeclaredTypesOnNull() { - assertTrue(getAllDeclaredTypes(NULL_ELEMENT).isEmpty()); - assertTrue(getAllDeclaredTypes(NULL_TYPE_MIRROR).isEmpty()); + assertEmptyList(getAllDeclaredTypes(NULL_ELEMENT)); + assertEmptyList(getAllDeclaredTypes(NULL_TYPE_MIRROR)); } @Test @@ -854,7 +897,7 @@ public void testGetDeclaredTypes() { // false true false false : nothing declaredTypes = getDeclaredTypes(testTypeElement, false, true, false, false); assertDeclaredTypes(declaredTypes); - assertSame(emptyList(), declaredTypes); + assertEmptyList(declaredTypes); // false false true true : super class + super interfaces declaredTypes = getDeclaredTypes(testTypeElement, false, false, true, true); @@ -873,7 +916,7 @@ public void testGetDeclaredTypes() { // false false false false : nothing declaredTypes = getDeclaredTypes(testTypeElement, false, false, false, false); assertDeclaredTypes(declaredTypes); - assertSame(emptyList(), declaredTypes); + assertEmptyList(declaredTypes); } @Test @@ -905,12 +948,20 @@ public void testFindDeclaredTypesWithExcludedTypes() { @Test public void testFindDeclaredTypesWithExcludedTypesOnNull() { - assertTrue(findDeclaredTypes(NULL_ELEMENT, NULL_TYPE_ARRAY).isEmpty()); - assertTrue(findDeclaredTypes(NULL_ELEMENT, EMPTY_TYPE_ARRAY).isEmpty()); - assertTrue(findDeclaredTypes(NULL_ELEMENT, ALL_TYPES).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, NULL_TYPE_ARRAY).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, EMPTY_TYPE_ARRAY).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, ALL_TYPES).isEmpty()); + assertEmptyList(findDeclaredTypes(NULL_ELEMENT, NULL_TYPE_ARRAY)); + assertEmptyList(findDeclaredTypes(NULL_ELEMENT, EMPTY_TYPE_ARRAY)); + assertEmptyList(findDeclaredTypes(NULL_ELEMENT, ALL_TYPES)); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, NULL_TYPE_ARRAY)); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, EMPTY_TYPE_ARRAY)); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, ALL_TYPES)); + } + + @Test + public void testFindDeclaredTypesWithExcludedTypeNamesOnNull() { + assertEmptyList(findDeclaredTypes(NULL_ELEMENT, NULL_STRING_ARRAY)); + assertEmptyList(findDeclaredTypes(NULL_ELEMENT, EMPTY_STRING_ARRAY)); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, NULL_STRING_ARRAY)); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, EMPTY_STRING_ARRAY)); } @Test @@ -918,16 +969,22 @@ public void testFindDeclaredTypesOfInterfaces() { List declaredTypes = findDeclaredTypesOfInterfaces(testTypeMirror, alwaysTrue()); assertDeclaredTypes(declaredTypes, SUPER_INTERFACES); + findDeclaredTypesOfInterfaces(testTypeElement, alwaysTrue()); + assertDeclaredTypes(declaredTypes, SUPER_INTERFACES); + declaredTypes = findDeclaredTypesOfInterfaces(testTypeMirror, alwaysFalse()); - assertSame(emptyList(), declaredTypes); + assertEmptyList(declaredTypes); + + declaredTypes = findDeclaredTypesOfInterfaces(testTypeElement, alwaysFalse()); + assertEmptyList(declaredTypes); } @Test public void testFindDeclaredTypesOfInterfacesOnNull() { - assertTrue(findDeclaredTypesOfInterfaces(NULL_ELEMENT, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypesOfInterfaces(NULL_ELEMENT, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypesOfInterfaces(NULL_TYPE_MIRROR, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypesOfInterfaces(NULL_TYPE_MIRROR, alwaysTrue()).isEmpty()); + assertEmptyList(findDeclaredTypesOfInterfaces(NULL_ELEMENT, alwaysTrue())); + assertEmptyList(findDeclaredTypesOfInterfaces(NULL_ELEMENT, alwaysFalse())); + assertEmptyList(findDeclaredTypesOfInterfaces(NULL_TYPE_MIRROR, alwaysTrue())); + assertEmptyList(findDeclaredTypesOfInterfaces(NULL_TYPE_MIRROR, alwaysTrue())); } @Test @@ -936,15 +993,15 @@ public void testFindAllDeclaredTypesOfSuperclasses() { assertDeclaredTypes(declaredTypes, ALL_SUPER_CLASSES); declaredTypes = findAllDeclaredTypesOfSuperclasses(testTypeMirror, alwaysFalse()); - assertSame(emptyList(), declaredTypes); + assertEmptyList(declaredTypes); } @Test public void testFindAllDeclaredTypesOfSuperclassesOnNull() { - assertTrue(findAllDeclaredTypesOfSuperclasses(NULL_ELEMENT, alwaysTrue()).isEmpty()); - assertTrue(findAllDeclaredTypesOfSuperclasses(NULL_ELEMENT, alwaysFalse()).isEmpty()); - assertTrue(findAllDeclaredTypesOfSuperclasses(NULL_TYPE_MIRROR, alwaysTrue()).isEmpty()); - assertTrue(findAllDeclaredTypesOfSuperclasses(NULL_TYPE_MIRROR, alwaysFalse()).isEmpty()); + assertEmptyList(findAllDeclaredTypesOfSuperclasses(NULL_ELEMENT, alwaysTrue())); + assertEmptyList(findAllDeclaredTypesOfSuperclasses(NULL_ELEMENT, alwaysFalse())); + assertEmptyList(findAllDeclaredTypesOfSuperclasses(NULL_TYPE_MIRROR, alwaysTrue())); + assertEmptyList(findAllDeclaredTypesOfSuperclasses(NULL_TYPE_MIRROR, alwaysFalse())); } @Test @@ -953,15 +1010,15 @@ public void testFindAllDeclaredTypesOfInterfaces() { assertDeclaredTypes(declaredTypes, ALL_SUPER_INTERFACES); declaredTypes = findAllDeclaredTypesOfInterfaces(testTypeMirror, alwaysFalse()); - assertSame(emptyList(), declaredTypes); + assertEmptyList(declaredTypes); } @Test public void testFindAllDeclaredTypesOfInterfacesOnNull() { - assertTrue(findAllDeclaredTypesOfInterfaces(NULL_ELEMENT, alwaysTrue()).isEmpty()); - assertTrue(findAllDeclaredTypesOfInterfaces(NULL_ELEMENT, alwaysFalse()).isEmpty()); - assertTrue(findAllDeclaredTypesOfInterfaces(NULL_TYPE_MIRROR, alwaysTrue()).isEmpty()); - assertTrue(findAllDeclaredTypesOfInterfaces(NULL_TYPE_MIRROR, alwaysFalse()).isEmpty()); + assertEmptyList(findAllDeclaredTypesOfInterfaces(NULL_ELEMENT, alwaysTrue())); + assertEmptyList(findAllDeclaredTypesOfInterfaces(NULL_ELEMENT, alwaysFalse())); + assertEmptyList(findAllDeclaredTypesOfInterfaces(NULL_TYPE_MIRROR, alwaysTrue())); + assertEmptyList(findAllDeclaredTypesOfInterfaces(NULL_TYPE_MIRROR, alwaysFalse())); } @Test @@ -969,14 +1026,20 @@ public void testFindAllDeclaredTypesOfSuperTypes() { List declaredTypes = findAllDeclaredTypesOfSuperTypes(testTypeMirror, alwaysTrue()); assertDeclaredTypes(declaredTypes, ALL_SUPER_TYPES); + findAllDeclaredTypesOfSuperTypes(testTypeElement, alwaysTrue()); + assertDeclaredTypes(declaredTypes, ALL_SUPER_TYPES); + declaredTypes = findAllDeclaredTypesOfSuperTypes(testTypeMirror, alwaysFalse()); - assertSame(emptyList(), declaredTypes); + assertEmptyList(declaredTypes); + + declaredTypes = findAllDeclaredTypesOfSuperTypes(testTypeElement, alwaysFalse()); + assertEmptyList(declaredTypes); } @Test public void testFindAllDeclaredTypesOfSuperTypesOnNull() { - assertTrue(findAllDeclaredTypesOfSuperTypes(NULL_ELEMENT).isEmpty()); - assertTrue(findAllDeclaredTypesOfSuperTypes(NULL_TYPE_MIRROR).isEmpty()); + assertEmptyList(findAllDeclaredTypesOfSuperTypes(NULL_ELEMENT)); + assertEmptyList(findAllDeclaredTypesOfSuperTypes(NULL_TYPE_MIRROR)); } @Test @@ -985,15 +1048,15 @@ public void testFindAllDeclaredTypes() { assertDeclaredTypes(declaredTypes, ALL_TYPES); declaredTypes = findAllDeclaredTypes(testTypeMirror, alwaysFalse()); - assertSame(emptyList(), declaredTypes); + assertEmptyList(declaredTypes); } @Test public void testFindAllDeclaredTypesOnNull() { - assertTrue(findAllDeclaredTypes(NULL_ELEMENT, alwaysTrue()).isEmpty()); - assertTrue(findAllDeclaredTypes(NULL_ELEMENT, alwaysFalse()).isEmpty()); - assertTrue(findAllDeclaredTypes(NULL_TYPE_MIRROR, alwaysTrue()).isEmpty()); - assertTrue(findAllDeclaredTypes(NULL_TYPE_MIRROR, alwaysFalse()).isEmpty()); + assertEmptyList(findAllDeclaredTypes(NULL_ELEMENT, alwaysTrue())); + assertEmptyList(findAllDeclaredTypes(NULL_ELEMENT, alwaysFalse())); + assertEmptyList(findAllDeclaredTypes(NULL_TYPE_MIRROR, alwaysTrue())); + assertEmptyList(findAllDeclaredTypes(NULL_TYPE_MIRROR, alwaysFalse())); } @Test @@ -1001,11 +1064,15 @@ public void testFindAllDeclaredTypesWithExcludedTypes() { List declaredTypes = findAllDeclaredTypes(testTypeElement, testClass); assertDeclaredTypes(declaredTypes, ALL_SUPER_TYPES); - declaredTypes = findAllDeclaredTypes(testTypeElement, testClassName); - assertDeclaredTypes(declaredTypes, ALL_SUPER_TYPES); declaredTypes = findAllDeclaredTypes(testTypeMirror, testClass); assertDeclaredTypes(declaredTypes, ALL_SUPER_TYPES); + } + + @Test + public void testFindAllDeclaredTypesWithExcludedTypeNames() { + List declaredTypes = findAllDeclaredTypes(testTypeElement, testClassName); + assertDeclaredTypes(declaredTypes, ALL_SUPER_TYPES); declaredTypes = findAllDeclaredTypes(testTypeMirror, testClassName); assertDeclaredTypes(declaredTypes, ALL_SUPER_TYPES); @@ -1013,12 +1080,20 @@ public void testFindAllDeclaredTypesWithExcludedTypes() { @Test public void testFindAllDeclaredTypesWithExcludedTypesOnNull() { - assertTrue(findAllDeclaredTypes(NULL_ELEMENT, NULL_TYPE_ARRAY).isEmpty()); - assertTrue(findAllDeclaredTypes(NULL_ELEMENT, EMPTY_TYPE_ARRAY).isEmpty()); - assertTrue(findAllDeclaredTypes(NULL_ELEMENT, ALL_TYPES).isEmpty()); - assertTrue(findAllDeclaredTypes(NULL_TYPE_MIRROR, NULL_TYPE_ARRAY).isEmpty()); - assertTrue(findAllDeclaredTypes(NULL_TYPE_MIRROR, EMPTY_TYPE_ARRAY).isEmpty()); - assertTrue(findAllDeclaredTypes(NULL_TYPE_MIRROR, ALL_TYPES).isEmpty()); + assertEmptyList(findAllDeclaredTypes(NULL_ELEMENT, NULL_TYPE_ARRAY)); + assertEmptyList(findAllDeclaredTypes(NULL_ELEMENT, EMPTY_TYPE_ARRAY)); + assertEmptyList(findAllDeclaredTypes(NULL_ELEMENT, ALL_TYPES)); + assertEmptyList(findAllDeclaredTypes(NULL_TYPE_MIRROR, NULL_TYPE_ARRAY)); + assertEmptyList(findAllDeclaredTypes(NULL_TYPE_MIRROR, EMPTY_TYPE_ARRAY)); + assertEmptyList(findAllDeclaredTypes(NULL_TYPE_MIRROR, ALL_TYPES)); + } + + @Test + public void testFindAllDeclaredTypesWithExcludedTypeNamesOnNull() { + assertEmptyList(findAllDeclaredTypes(NULL_ELEMENT, NULL_STRING_ARRAY)); + assertEmptyList(findAllDeclaredTypes(NULL_ELEMENT, EMPTY_STRING_ARRAY)); + assertEmptyList(findAllDeclaredTypes(NULL_TYPE_MIRROR, NULL_STRING_ARRAY)); + assertEmptyList(findAllDeclaredTypes(NULL_TYPE_MIRROR, EMPTY_STRING_ARRAY)); } @Test @@ -1074,7 +1149,7 @@ public void testFindDeclaredTypes() { // false true false false : nothing declaredTypes = findDeclaredTypes(testTypeElement, false, true, false, false, alwaysTrue()); assertDeclaredTypes(declaredTypes); - assertSame(emptyList(), declaredTypes); + assertEmptyList(declaredTypes); // false false true true : super class + super interfaces declaredTypes = findDeclaredTypes(testTypeElement, false, false, true, true, alwaysTrue()); @@ -1093,90 +1168,90 @@ public void testFindDeclaredTypes() { // false false false false : nothing declaredTypes = findDeclaredTypes(testTypeElement, false, false, false, false, alwaysTrue()); assertDeclaredTypes(declaredTypes); - assertSame(emptyList(), declaredTypes); + assertEmptyList(declaredTypes); } @Test public void testFindDeclaredTypesOnNull() { - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, true, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, true, true, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, true, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, true, true, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, true, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, true, false, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, true, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, true, false, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, false, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, false, true, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, false, true, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, false, true, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, false, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, false, false, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, false, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, false, false, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, true, false, true, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, true, false, true, true, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, true, false, true, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, true, false, true, true, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, true, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, true, false, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, true, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, true, false, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, true, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, true, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, false, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, false, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, true, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, true, true, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, true, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, true, true, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, true, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, true, false, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, true, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, true, false, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, false, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, false, true, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, false, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, false, true, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, false, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, false, false, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, false, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, false, false, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, true, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, true, true, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, true, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, true, true, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, true, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, true, false, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, true, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, true, false, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, true, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, true, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, true, alwaysFalse()).isEmpty()); - - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, false, alwaysFalse()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, false, alwaysTrue()).isEmpty()); - assertTrue(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, false, alwaysFalse()).isEmpty()); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, true, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, true, true, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, true, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, true, true, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, true, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, true, false, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, true, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, true, false, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, false, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, false, true, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, false, true, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, false, true, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, false, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, true, true, false, false, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, false, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, true, true, false, false, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, true, false, true, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, true, false, true, true, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, true, false, true, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, true, false, true, true, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, true, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, true, false, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, true, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, true, false, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, true, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, true, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, false, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, false, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, true, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, true, true, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, true, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, true, true, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, true, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, true, false, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, true, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, true, false, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, false, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, false, true, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, false, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, false, true, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, false, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, true, false, false, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, false, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, true, false, false, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, true, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, true, true, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, true, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, true, true, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, true, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, true, false, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, true, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, true, false, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, true, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, true, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, true, alwaysFalse())); + + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_ELEMENT, false, false, false, false, alwaysFalse())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, false, alwaysTrue())); + assertEmptyList(findDeclaredTypes(NULL_TYPE_MIRROR, false, false, false, false, alwaysFalse())); } @Test @@ -1188,7 +1263,7 @@ public void testGetTypeMirrorsOfInterfaces() { assertTypeMirrors(typeMirrors, SUPER_INTERFACES); typeMirrors = getTypeMirrorsOfInterfaces(getTypeElement(Object.class)); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = getTypeMirrorsOfInterfaces(getTypeMirror(Object.class)); assertSame(typeMirrors, typeMirrors); @@ -1197,10 +1272,10 @@ public void testGetTypeMirrorsOfInterfaces() { @Test public void testGetTypeMirrorsOfInterfacesOnNull() { List typeMirrors = getTypeMirrorsOfInterfaces(NULL_TYPE_MIRROR); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = getTypeMirrorsOfInterfaces(NULL_TYPE_ELEMENT); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); } @Test @@ -1212,16 +1287,16 @@ public void testFindTypeMirrorsOfInterfaces() { assertTypeMirrors(typeMirrors, SUPER_INTERFACES); typeMirrors = findTypeMirrorsOfInterfaces(testTypeMirror, alwaysFalse()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findTypeMirrorsOfInterfaces(testTypeElement, alwaysFalse()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findTypeMirrorsOfInterfaces(getTypeElement(Object.class), alwaysTrue()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findTypeMirrorsOfInterfaces(getTypeElement(Object.class), alwaysFalse()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findTypeMirrorsOfInterfaces(getTypeMirror(Object.class), alwaysTrue()); assertSame(typeMirrors, typeMirrors); @@ -1233,16 +1308,16 @@ public void testFindTypeMirrorsOfInterfaces() { @Test public void testFindTypeMirrorsOfInterfacesOnNull() { List typeMirrors = findTypeMirrorsOfInterfaces(NULL_TYPE_MIRROR, alwaysTrue()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findTypeMirrorsOfInterfaces(NULL_TYPE_ELEMENT, alwaysTrue()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findTypeMirrorsOfInterfaces(NULL_TYPE_MIRROR, alwaysFalse()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findTypeMirrorsOfInterfaces(NULL_TYPE_ELEMENT, alwaysFalse()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); } @Test @@ -1254,7 +1329,7 @@ public void testGetAllTypeMirrorsOfInterfaces() { assertTypeMirrors(typeMirrors, ALL_SUPER_INTERFACES); typeMirrors = getAllTypeMirrorsOfInterfaces(getTypeElement(Object.class)); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = getAllTypeMirrorsOfInterfaces(getTypeMirror(Object.class)); assertSame(typeMirrors, typeMirrors); @@ -1263,10 +1338,10 @@ public void testGetAllTypeMirrorsOfInterfaces() { @Test public void testGetAllTypeMirrorsOfInterfacesOnNull() { List typeMirrors = getAllTypeMirrorsOfInterfaces(NULL_TYPE_MIRROR); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = getAllTypeMirrorsOfInterfaces(NULL_TYPE_ELEMENT); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); } @Test @@ -1278,16 +1353,16 @@ public void testFindAllTypeMirrorsOfInterfaces() { assertTypeMirrors(typeMirrors, ALL_SUPER_INTERFACES); typeMirrors = findAllTypeMirrorsOfInterfaces(testTypeMirror, alwaysFalse()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findAllTypeMirrorsOfInterfaces(testTypeElement, alwaysFalse()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findAllTypeMirrorsOfInterfaces(getTypeElement(Object.class), alwaysTrue()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findAllTypeMirrorsOfInterfaces(getTypeElement(Object.class), alwaysFalse()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findAllTypeMirrorsOfInterfaces(getTypeMirror(Object.class), alwaysTrue()); assertSame(typeMirrors, typeMirrors); @@ -1299,16 +1374,16 @@ public void testFindAllTypeMirrorsOfInterfaces() { @Test public void testFindAllTypeMirrorsOfInterfacesOnNull() { List typeMirrors = findAllTypeMirrorsOfInterfaces(NULL_TYPE_MIRROR, alwaysTrue()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findAllTypeMirrorsOfInterfaces(NULL_TYPE_MIRROR, alwaysFalse()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findAllTypeMirrorsOfInterfaces(NULL_TYPE_ELEMENT, alwaysTrue()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); typeMirrors = findAllTypeMirrorsOfInterfaces(NULL_TYPE_MIRROR, alwaysFalse()); - assertSame(emptyList(), typeMirrors); + assertEmptyList(typeMirrors); } @Test @@ -1437,9 +1512,9 @@ public void testGetTypeElementsWithProcessingEnvironment() { @Test public void testGetTypeElementsWithProcessingEnvironmentOnNull() { - assertSame(emptyList(), TypeUtils.getTypeElements(this.processingEnv, NULL_TYPE)); - assertSame(emptyList(), TypeUtils.getTypeElements(this.processingEnv, NULL_TYPE_ARRAY)); - assertSame(emptyList(), TypeUtils.getTypeElements(this.processingEnv, EMPTY_TYPE_ARRAY)); + assertEmptyList(TypeUtils.getTypeElements(this.processingEnv, NULL_TYPE)); + assertEmptyList(TypeUtils.getTypeElements(this.processingEnv, NULL_TYPE_ARRAY)); + assertEmptyList(TypeUtils.getTypeElements(this.processingEnv, EMPTY_TYPE_ARRAY)); assertGetTypeElementsOnNullProcessingEnvironment(SELF_TYPE); assertGetTypeElementsOnNullProcessingEnvironment(SUPER_CLASS); @@ -1475,10 +1550,10 @@ public void testGetTypeElementOnType() { @Test public void testGetTypeElementOnNull() { - assertNull(TypeUtils.getTypeElement(processingEnv, (Type) null)); + assertNull(TypeUtils.getTypeElement(processingEnv, NULL_TYPE)); assertNull(TypeUtils.getTypeElement(processingEnv, NULL_TYPE_MIRROR)); - assertNull(TypeUtils.getTypeElement(processingEnv, (CharSequence) null)); - assertNull(TypeUtils.getTypeElement(null, (CharSequence) null)); + assertNull(TypeUtils.getTypeElement(processingEnv, NULL_STRING)); + assertNull(TypeUtils.getTypeElement(NULL_PROCESSING_ENVIRONMENT, NULL_STRING)); } @Test @@ -1621,11 +1696,11 @@ private void assertIsArrayType(Type type) { } private void assertIsArrayType(Element element) { - assertTrue(isArrayType(findField(element, "integers").asType())); - assertTrue(isArrayType(findField(element, "strings").asType())); - assertTrue(isArrayType(findField(element, "primitiveTypeModels").asType())); - assertTrue(isArrayType(findField(element, "models").asType())); - assertTrue(isArrayType(findField(element, "colors").asType())); + assertTrue(isArrayType(findField(element, "integers"))); + assertTrue(isArrayType(findField(element, "strings"))); + assertTrue(isArrayType(findField(element, "primitiveTypeModels"))); + assertTrue(isArrayType(findField(element, "models"))); + assertTrue(isArrayType(findField(element, "colors"))); } private void assertTypeMirrors(List typeMirrors, Type... types) { @@ -1708,7 +1783,7 @@ private void assertGetTypeElementsWithProcessingEnvironment(Type... types) { private void assertGetTypeElementsOnNullProcessingEnvironment(Type... types) { List typeElements = TypeUtils.getTypeElements(NULL_PROCESSING_ENVIRONMENT, types); - assertSame(emptyList(), typeElements); + assertEmptyList(typeElements); } private void assertGetDeclaredType(Type... types) { @@ -1760,6 +1835,10 @@ private TypeMirror getFieldType(Type type, String fieldName) { return findField(typeMirror, fieldName).asType(); } + private TypeMirror getFieldType(Element element, String fieldName) { + return findField(element, fieldName).asType(); + } + private void assertToStringOnClasses() { assertToString(NULL_TYPE); assertToString(SELF_TYPE); diff --git a/microsphere-java-core/src/main/java/io/microsphere/beans/BeanProperty.java b/microsphere-java-core/src/main/java/io/microsphere/beans/BeanProperty.java index b63b13890..04d8b398d 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/beans/BeanProperty.java +++ b/microsphere-java-core/src/main/java/io/microsphere/beans/BeanProperty.java @@ -93,10 +93,10 @@ public boolean equals(Object o) { @Override public int hashCode() { - int result = name != null ? name.hashCode() : 0; + int result = name.hashCode(); result = 31 * result + (value != null ? value.hashCode() : 0); - result = 31 * result + (beanClass != null ? beanClass.hashCode() : 0); - result = 31 * result + (descriptor != null ? descriptor.hashCode() : 0); + result = 31 * result + beanClass.hashCode(); + result = 31 * result + descriptor.hashCode(); return result; } diff --git a/microsphere-java-core/src/main/java/io/microsphere/classloading/AbstractArtifactResolver.java b/microsphere-java-core/src/main/java/io/microsphere/classloading/AbstractArtifactResolver.java index b78fccdae..4fd8a55aa 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/classloading/AbstractArtifactResolver.java +++ b/microsphere-java-core/src/main/java/io/microsphere/classloading/AbstractArtifactResolver.java @@ -13,6 +13,7 @@ import static io.microsphere.logging.LoggerFactory.getLogger; import static io.microsphere.net.URLUtils.resolveArchiveFile; import static io.microsphere.util.ArrayUtils.isEmpty; +import static io.microsphere.util.ClassLoaderUtils.newURLClassLoader; import static java.util.Collections.emptySet; /** @@ -48,10 +49,6 @@ public int getPriority() { protected abstract void doResolve(Collection artifactSet, URLClassLoader urlClassLoader); - protected URLClassLoader newURLClassLoader(URL[] urls) { - return new URLClassLoader(urls, null); - } - protected URL resolveArtifactResourceURL(URL resourceURL) { URL url = null; try { diff --git a/microsphere-java-core/src/main/java/io/microsphere/classloading/MavenArtifactResolver.java b/microsphere-java-core/src/main/java/io/microsphere/classloading/MavenArtifactResolver.java index 000942078..d89ea5743 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/classloading/MavenArtifactResolver.java +++ b/microsphere-java-core/src/main/java/io/microsphere/classloading/MavenArtifactResolver.java @@ -1,7 +1,6 @@ package io.microsphere.classloading; import io.microsphere.filter.JarEntryFilter; -import io.microsphere.util.jar.JarUtils; import java.io.IOException; import java.io.InputStream; @@ -16,6 +15,7 @@ import static io.microsphere.net.URLUtils.isJarURL; import static io.microsphere.util.ClassLoaderUtils.findAllClassPathURLs; +import static io.microsphere.util.jar.JarUtils.filter; import static io.microsphere.util.jar.JarUtils.toJarFile; /** @@ -68,16 +68,16 @@ protected void doResolve(Collection artifactSet, URLClassLoader urlCla } } - private URL findMavenPomPropertiesResource(URL classPathURL, URLClassLoader urlClassLoader) throws IOException { + URL findMavenPomPropertiesResource(URL classPathURL, URLClassLoader urlClassLoader) throws IOException { if (isJarURL(classPathURL)) { return findMavenPomPropertiesResourceInJar(classPathURL, urlClassLoader); } return null; } - private URL findMavenPomPropertiesResourceInJar(URL classPathURL, URLClassLoader urlClassLoader) throws IOException { + URL findMavenPomPropertiesResourceInJar(URL classPathURL, URLClassLoader urlClassLoader) throws IOException { JarFile jarFile = toJarFile(classPathURL); - List entries = JarUtils.filter(jarFile, MAVEN_POM_PROPERTIES_FILTER); + List entries = filter(jarFile, MAVEN_POM_PROPERTIES_FILTER); if (entries.isEmpty()) { return null; } @@ -86,7 +86,7 @@ private URL findMavenPomPropertiesResourceInJar(URL classPathURL, URLClassLoader return urlClassLoader.getResource(relativePath); } - private Artifact resolveArtifactMetaInfoInMavenPomProperties(URL mavenPomPropertiesResourceURL) { + Artifact resolveArtifactMetaInfoInMavenPomProperties(URL mavenPomPropertiesResourceURL) { Artifact artifact = null; try (InputStream mavenPomPropertiesStream = mavenPomPropertiesResourceURL.openStream()) { Properties properties = new Properties(); @@ -99,7 +99,7 @@ private Artifact resolveArtifactMetaInfoInMavenPomProperties(URL mavenPomPropert return artifact; } - private Artifact resolveArtifactMetaInfoInMavenPomProperties(Properties properties, + Artifact resolveArtifactMetaInfoInMavenPomProperties(Properties properties, URL artifactResourceURL) { String groupId = properties.getProperty(GROUP_ID_PROPERTY_NAME); String artifactId = properties.getProperty(ARTIFACT_ID_PROPERTY_NAME); @@ -107,7 +107,7 @@ private Artifact resolveArtifactMetaInfoInMavenPomProperties(Properties properti return MavenArtifact.create(groupId, artifactId, version, artifactResourceURL); } - private static class MavenPomPropertiesFilter implements JarEntryFilter { + static class MavenPomPropertiesFilter implements JarEntryFilter { @Override public boolean accept(JarEntry entry) { diff --git a/microsphere-java-core/src/main/java/io/microsphere/collection/CollectionUtils.java b/microsphere-java-core/src/main/java/io/microsphere/collection/CollectionUtils.java index 6dba9bf01..8588ecd2a 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/collection/CollectionUtils.java +++ b/microsphere-java-core/src/main/java/io/microsphere/collection/CollectionUtils.java @@ -18,7 +18,6 @@ import io.microsphere.annotation.Nonnull; import io.microsphere.annotation.Nullable; -import io.microsphere.util.BaseUtils; import java.util.AbstractSet; import java.util.Collection; @@ -36,7 +35,7 @@ * @author Mercy * @see Collections */ -public abstract class CollectionUtils extends BaseUtils { +public abstract class CollectionUtils { public static boolean isEmpty(@Nullable Collection collection) { return collection == null || collection.isEmpty(); diff --git a/microsphere-java-core/src/main/java/io/microsphere/collection/ListUtils.java b/microsphere-java-core/src/main/java/io/microsphere/collection/ListUtils.java index 0cd47dbb4..7e0d4335f 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/collection/ListUtils.java +++ b/microsphere-java-core/src/main/java/io/microsphere/collection/ListUtils.java @@ -16,8 +16,6 @@ */ package io.microsphere.collection; -import io.microsphere.util.BaseUtils; - import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; @@ -41,7 +39,7 @@ * @see List * @since 1.0.0 */ -public abstract class ListUtils extends BaseUtils { +public abstract class ListUtils { public static boolean isList(Object values) { return values instanceof List; diff --git a/microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java b/microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java index bce010b05..312d9ea92 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java +++ b/microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java @@ -17,7 +17,6 @@ package io.microsphere.collection; import io.microsphere.annotation.Nonnull; -import io.microsphere.util.BaseUtils; import java.util.Collection; import java.util.Comparator; @@ -47,7 +46,7 @@ * @see Map * @since 1.0.0 */ -public abstract class MapUtils extends BaseUtils { +public abstract class MapUtils { /** * The min load factor for {@link HashMap} or {@link Hashtable} diff --git a/microsphere-java-core/src/main/java/io/microsphere/collection/SetUtils.java b/microsphere-java-core/src/main/java/io/microsphere/collection/SetUtils.java index ba2d4ca86..7efb3fd54 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/collection/SetUtils.java +++ b/microsphere-java-core/src/main/java/io/microsphere/collection/SetUtils.java @@ -18,7 +18,6 @@ import io.microsphere.annotation.Nonnull; import io.microsphere.annotation.Nullable; -import io.microsphere.util.BaseUtils; import java.util.Collection; import java.util.Enumeration; @@ -41,7 +40,7 @@ * @see Set * @since 1.0.0 */ -public abstract class SetUtils extends BaseUtils { +public abstract class SetUtils { public static boolean isSet(@Nullable Iterable elements) { return elements instanceof Set; diff --git a/microsphere-java-core/src/main/java/io/microsphere/filter/FilterUtils.java b/microsphere-java-core/src/main/java/io/microsphere/filter/FilterUtils.java index deb386b2e..ecc04d7ef 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/filter/FilterUtils.java +++ b/microsphere-java-core/src/main/java/io/microsphere/filter/FilterUtils.java @@ -4,7 +4,6 @@ package io.microsphere.filter; import io.microsphere.annotation.Nonnull; -import io.microsphere.util.BaseUtils; import java.util.ArrayList; import java.util.Iterator; @@ -19,7 +18,7 @@ * @see FilterUtils * @since 1.0.0 */ -public abstract class FilterUtils extends BaseUtils { +public abstract class FilterUtils { private FilterUtils() { } diff --git a/microsphere-java-core/src/main/java/io/microsphere/io/IOUtils.java b/microsphere-java-core/src/main/java/io/microsphere/io/IOUtils.java index 1adc3556a..f607c9afe 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/io/IOUtils.java +++ b/microsphere-java-core/src/main/java/io/microsphere/io/IOUtils.java @@ -18,7 +18,6 @@ import io.microsphere.logging.Logger; import io.microsphere.nio.charset.CharsetUtils; -import io.microsphere.util.BaseUtils; import io.microsphere.util.SystemUtils; import java.io.Closeable; @@ -46,7 +45,7 @@ * @see Paths * @since 1.0.0 */ -public abstract class IOUtils extends BaseUtils { +public abstract class IOUtils { private static final Logger logger = getLogger(IOUtils.class); diff --git a/microsphere-java-core/src/main/java/io/microsphere/management/ManagementUtils.java b/microsphere-java-core/src/main/java/io/microsphere/management/ManagementUtils.java index 7435c46a1..91b2a9fb0 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/management/ManagementUtils.java +++ b/microsphere-java-core/src/main/java/io/microsphere/management/ManagementUtils.java @@ -8,6 +8,7 @@ import java.util.List; import static io.microsphere.logging.LoggerFactory.getLogger; +import static io.microsphere.process.ProcessIdResolver.UNKNOWN_PROCESS_ID; import static io.microsphere.util.ServiceLoaderUtils.loadServicesList; /** @@ -21,21 +22,29 @@ public abstract class ManagementUtils extends BaseUtils { private static final Logger logger = getLogger(ManagementUtils.class); - static final int UNKNOWN_PROCESS_ID = -1; - static final long currentProcessId = resolveCurrentProcessId(); private static long resolveCurrentProcessId() { List resolvers = loadServicesList(ProcessIdResolver.class); Long processId = null; for (ProcessIdResolver resolver : resolvers) { - if ((processId = resolver.current()) != null) { - break; + if (resolver.supports()) { + if ((processId = resolver.current()) != null) { + log(resolver, processId); + break; + } } } return processId == null ? UNKNOWN_PROCESS_ID : processId; } + static void log(ProcessIdResolver resolver, Long processId) { + if (logger.isTraceEnabled()) { + logger.trace("The process id was resolved by ProcessIdResolver[class : '{}' , priority : {}] successfully : {}", + resolver.getClass().getName(), resolver.getPriority(), processId); + } + } + /** * Get the process ID of current JVM * diff --git a/microsphere-java-core/src/main/java/io/microsphere/process/ClassicProcessIdResolver.java b/microsphere-java-core/src/main/java/io/microsphere/process/ClassicProcessIdResolver.java index e560fc3f9..77f6c48a4 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/process/ClassicProcessIdResolver.java +++ b/microsphere-java-core/src/main/java/io/microsphere/process/ClassicProcessIdResolver.java @@ -18,11 +18,10 @@ import io.microsphere.logging.Logger; -import java.lang.management.RuntimeMXBean; - import static io.microsphere.constants.SymbolConstants.AT; import static io.microsphere.logging.LoggerFactory.getLogger; import static io.microsphere.management.JmxUtils.getRuntimeMXBean; +import static io.microsphere.util.StringUtils.isNumeric; import static io.microsphere.util.StringUtils.substringBefore; import static java.lang.Long.valueOf; @@ -37,19 +36,20 @@ public class ClassicProcessIdResolver implements ProcessIdResolver { private static final Logger logger = getLogger(ClassicProcessIdResolver.class); + private static final String runtimeName = getRuntimeMXBean().getName(); + + private static final String processIdValue = substringBefore(runtimeName, AT); + + @Override + public boolean supports() { + return isNumeric(processIdValue); + } + @Override public Long current() { - RuntimeMXBean runtimeMXBean = getRuntimeMXBean(); - String name = runtimeMXBean.getName(); - Long processId = null; - try { - String processIdValue = substringBefore(name, AT); - processId = valueOf(processIdValue); - if (logger.isTraceEnabled()) { - logger.trace("The PID was resolved from the method 'java.lang.management.RuntimeMXBean#getName()' = {} : {}", name, processId); - } - } catch (Throwable e) { - logger.warn("The PID can't be resolved from the method 'java.lang.management.RuntimeMXBean#getName()' = {} : {}", name, e); + Long processId = valueOf(processIdValue); + if (logger.isTraceEnabled()) { + logger.trace("The PID was resolved from the method 'java.lang.management.RuntimeMXBean#getName()' = {} : {}", runtimeName, processId); } return processId; } diff --git a/microsphere-java-core/src/main/java/io/microsphere/process/ModernProcessIdResolver.java b/microsphere-java-core/src/main/java/io/microsphere/process/ModernProcessIdResolver.java index d9586c3b1..2fea05c0e 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/process/ModernProcessIdResolver.java +++ b/microsphere-java-core/src/main/java/io/microsphere/process/ModernProcessIdResolver.java @@ -40,22 +40,16 @@ public class ModernProcessIdResolver implements ProcessIdResolver { private static final Class PROCESS_HANDLE_CLASS = resolveClass(PROCESS_HANDLE_CLASS_NAME); @Override - public Long current() { - if (PROCESS_HANDLE_CLASS == null) { - return null; - } + public boolean supports() { + return PROCESS_HANDLE_CLASS != null; + } - Long pid = null; - try { - Object processHandle = invokeStaticMethod(PROCESS_HANDLE_CLASS, "current"); - pid = invokeMethod(processHandle, PROCESS_HANDLE_CLASS, "pid"); - if (logger.isTraceEnabled()) { - logger.trace("The PID was resolved from the method 'java.lang.ProcessHandle#pid()' : {}", pid); - } - } catch (Throwable e) { - if (logger.isTraceEnabled()) { - logger.trace("It's failed to invoke method 'java.lang.ProcessHandle#pid()'", e); - } + @Override + public Long current() { + Object processHandle = invokeStaticMethod(PROCESS_HANDLE_CLASS, "current"); + Long pid = invokeMethod(processHandle, PROCESS_HANDLE_CLASS, "pid"); + if (logger.isTraceEnabled()) { + logger.trace("The PID was resolved from the method 'java.lang.ProcessHandle#pid()' : {}", pid); } return pid; } diff --git a/microsphere-java-core/src/main/java/io/microsphere/process/ProcessIdResolver.java b/microsphere-java-core/src/main/java/io/microsphere/process/ProcessIdResolver.java index 7917284bc..cf168b1c0 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/process/ProcessIdResolver.java +++ b/microsphere-java-core/src/main/java/io/microsphere/process/ProcessIdResolver.java @@ -27,6 +27,18 @@ */ public interface ProcessIdResolver extends Prioritized { + /** + * The unknown process id + */ + long UNKNOWN_PROCESS_ID = -1L; + + /** + * Whether supports to resolve the process id or not? + * + * @return true if supports, otherwise false + */ + boolean supports(); + /** * Resolve the current process id * diff --git a/microsphere-java-core/src/main/java/io/microsphere/process/VirtualMachineProcessIdResolver.java b/microsphere-java-core/src/main/java/io/microsphere/process/VirtualMachineProcessIdResolver.java index 59d8a3fb6..79875fb2a 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/process/VirtualMachineProcessIdResolver.java +++ b/microsphere-java-core/src/main/java/io/microsphere/process/VirtualMachineProcessIdResolver.java @@ -19,11 +19,14 @@ import io.microsphere.logging.Logger; import java.lang.management.RuntimeMXBean; +import java.lang.reflect.Field; import static io.microsphere.logging.LoggerFactory.getLogger; import static io.microsphere.management.JmxUtils.getRuntimeMXBean; +import static io.microsphere.reflect.FieldUtils.findField; import static io.microsphere.reflect.FieldUtils.getFieldValue; import static io.microsphere.reflect.MethodUtils.invokeMethod; +import static java.lang.Long.valueOf; /** * {@link ProcessIdResolver} class for SUN JVM @@ -48,20 +51,25 @@ public class VirtualMachineProcessIdResolver implements ProcessIdResolver { */ final static String GET_PROCESS_ID_METHOD_NAME = "getProcessId"; + /** + * The {@link Field} of "jvm" + */ + final static Field JVM_FIELD = findField(getRuntimeMXBean(), JVM_FIELD_NAME); + + @Override + public boolean supports() { + return JVM_FIELD != null; + } + @Override public Long current() { - Integer processId = null; - try { - RuntimeMXBean runtimeMXBean = getRuntimeMXBean(); - Object jvm = getFieldValue(runtimeMXBean, JVM_FIELD_NAME); - processId = invokeMethod(jvm, GET_PROCESS_ID_METHOD_NAME); - if (logger.isTraceEnabled()) { - logger.trace("The PID was resolved from the native method 'sun.management.VMManagementImpl#getProcessId()' : {}", processId); - } - } catch (Throwable e) { - logger.warn("It's failed to invoke the native method 'sun.management.VMManagementImpl#getProcessId()'", e); + RuntimeMXBean runtimeMXBean = getRuntimeMXBean(); + Object jvm = getFieldValue(runtimeMXBean, JVM_FIELD); + Integer processId = invokeMethod(jvm, GET_PROCESS_ID_METHOD_NAME); + if (logger.isTraceEnabled()) { + logger.trace("The PID was resolved from the native method 'sun.management.VMManagementImpl#getProcessId()' : {}", processId); } - return Long.valueOf(processId.longValue()); + return valueOf(processId.longValue()); } @Override diff --git a/microsphere-java-core/src/main/java/io/microsphere/reflect/ConstructorUtils.java b/microsphere-java-core/src/main/java/io/microsphere/reflect/ConstructorUtils.java index c8e1f807a..cf08c525e 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/reflect/ConstructorUtils.java +++ b/microsphere-java-core/src/main/java/io/microsphere/reflect/ConstructorUtils.java @@ -17,7 +17,6 @@ package io.microsphere.reflect; import io.microsphere.logging.Logger; -import io.microsphere.util.BaseUtils; import java.lang.reflect.Constructor; import java.util.List; @@ -37,7 +36,7 @@ * @author Mercy * @since 1.0.0 */ -public abstract class ConstructorUtils extends BaseUtils { +public abstract class ConstructorUtils { private static final Logger logger = getLogger(ConstructorUtils.class); diff --git a/microsphere-java-core/src/main/java/io/microsphere/reflect/ExecutableUtils.java b/microsphere-java-core/src/main/java/io/microsphere/reflect/ExecutableUtils.java index bad7bcbac..462df4968 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/reflect/ExecutableUtils.java +++ b/microsphere-java-core/src/main/java/io/microsphere/reflect/ExecutableUtils.java @@ -20,7 +20,6 @@ import io.microsphere.lang.function.ThrowableFunction; import io.microsphere.lang.function.ThrowableSupplier; import io.microsphere.logging.Logger; -import io.microsphere.util.BaseUtils; import java.lang.reflect.Constructor; import java.lang.reflect.Executable; @@ -40,7 +39,7 @@ * @see Executable * @since 1.0.0 */ -public abstract class ExecutableUtils extends BaseUtils { +public abstract class ExecutableUtils { private static final Logger logger = getLogger(ExecutableUtils.class); diff --git a/microsphere-java-core/src/main/java/io/microsphere/reflect/MethodUtils.java b/microsphere-java-core/src/main/java/io/microsphere/reflect/MethodUtils.java index e9e27c93c..39e83b7e4 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/reflect/MethodUtils.java +++ b/microsphere-java-core/src/main/java/io/microsphere/reflect/MethodUtils.java @@ -18,7 +18,6 @@ import io.microsphere.annotation.Nullable; import io.microsphere.logging.Logger; -import io.microsphere.util.BaseUtils; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; @@ -65,7 +64,7 @@ * @author Mercy * @since 1.0.0 */ -public abstract class MethodUtils extends BaseUtils { +public abstract class MethodUtils { private static final Logger logger = getLogger(MethodUtils.class); diff --git a/microsphere-java-core/src/main/java/io/microsphere/reflect/ProxyUtils.java b/microsphere-java-core/src/main/java/io/microsphere/reflect/ProxyUtils.java index ef53cda7d..4a909a8e2 100644 --- a/microsphere-java-core/src/main/java/io/microsphere/reflect/ProxyUtils.java +++ b/microsphere-java-core/src/main/java/io/microsphere/reflect/ProxyUtils.java @@ -16,8 +16,6 @@ */ package io.microsphere.reflect; -import io.microsphere.util.BaseUtils; - import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.List; @@ -39,7 +37,7 @@ * @author Mercy * @since 1.0.0 */ -public abstract class ProxyUtils extends BaseUtils { +public abstract class ProxyUtils { /** *