|
28 | 28 | import org.springframework.stereotype.Service; |
29 | 29 |
|
30 | 30 | import javax.lang.model.element.AnnotationMirror; |
| 31 | +import javax.lang.model.element.AnnotationValue; |
31 | 32 | import javax.lang.model.element.Element; |
32 | 33 | import javax.lang.model.element.ExecutableElement; |
33 | 34 | import javax.lang.model.element.VariableElement; |
|
46 | 47 | import java.lang.annotation.Target; |
47 | 48 | import java.util.List; |
48 | 49 | import java.util.Map; |
| 50 | +import java.util.Map.Entry; |
49 | 51 |
|
50 | 52 | import static io.microsphere.annotation.processor.util.AnnotationUtils.findAllAnnotations; |
51 | 53 | import static io.microsphere.annotation.processor.util.AnnotationUtils.findAnnotation; |
|
55 | 57 | import static io.microsphere.annotation.processor.util.AnnotationUtils.getAnnotation; |
56 | 58 | import static io.microsphere.annotation.processor.util.AnnotationUtils.getAnnotations; |
57 | 59 | import static io.microsphere.annotation.processor.util.AnnotationUtils.getAttribute; |
| 60 | +import static io.microsphere.annotation.processor.util.AnnotationUtils.getAttributeName; |
| 61 | +import static io.microsphere.annotation.processor.util.AnnotationUtils.getAttributes; |
58 | 62 | import static io.microsphere.annotation.processor.util.AnnotationUtils.getAttributesMap; |
59 | 63 | import static io.microsphere.annotation.processor.util.AnnotationUtils.getValue; |
60 | 64 | import static io.microsphere.annotation.processor.util.AnnotationUtils.isAnnotationPresent; |
61 | 65 | import static io.microsphere.annotation.processor.util.AnnotationUtils.matchesAnnotationTypeName; |
| 66 | +import static io.microsphere.annotation.processor.util.AnnotationUtils.matchesAttributeMethod; |
62 | 67 | import static io.microsphere.annotation.processor.util.FieldUtils.findField; |
63 | 68 | import static io.microsphere.annotation.processor.util.MethodUtils.findMethod; |
64 | 69 | import static io.microsphere.annotation.processor.util.MethodUtils.getAllDeclaredMethods; |
65 | 70 | import static io.microsphere.lang.function.Predicates.alwaysFalse; |
66 | 71 | import static io.microsphere.lang.function.Predicates.alwaysTrue; |
| 72 | +import static io.microsphere.util.ArrayUtils.EMPTY_STRING_ARRAY; |
67 | 73 | import static io.microsphere.util.ArrayUtils.ofArray; |
| 74 | +import static io.microsphere.util.StringUtils.EMPTY_STRING; |
68 | 75 | import static java.lang.annotation.ElementType.METHOD; |
69 | 76 | import static java.lang.annotation.ElementType.TYPE; |
70 | 77 | import static java.util.Collections.emptyMap; |
@@ -422,6 +429,9 @@ public void testGetAttribute() { |
422 | 429 | String[] cacheNames = getAttribute(cacheableAnnotation, "cacheNames"); |
423 | 430 | assertArrayEquals(ofArray("cache-1", "cache-2"), cacheNames); |
424 | 431 |
|
| 432 | + String key = getAttribute(cacheableAnnotation, "key"); |
| 433 | + assertEquals(EMPTY_STRING, key); |
| 434 | + |
425 | 435 | DeclaredType cacheableAnnotationType = cacheableAnnotation.getAnnotationType(); |
426 | 436 | AnnotationMirror targetAnnotation = findAnnotation(cacheableAnnotationType, Target.class); |
427 | 437 | ElementType[] elementTypes = getAttribute(targetAnnotation, "value"); |
@@ -529,6 +539,73 @@ public void testGetAttributesMapOnNull() { |
529 | 539 | assertSame(emptyMap(), attributesMap); |
530 | 540 | } |
531 | 541 |
|
| 542 | + @Test |
| 543 | + public void testGetAttributesOnAnnotatedClass() { |
| 544 | + Map<ExecutableElement, AnnotationValue> attributes = getAttributes(testTypeElement, Service.class); |
| 545 | + assertServiceAttributes(attributes); |
| 546 | + |
| 547 | + attributes = getAttributes(testTypeElement, Service.class, false); |
| 548 | + assertServiceAttributes(attributes); |
| 549 | + } |
| 550 | + |
| 551 | + @Test |
| 552 | + public void testGetAttributesOnAnnotatedMethod() { |
| 553 | + ExecutableElement method = findMethod(testTypeElement, "echo", String.class); |
| 554 | + Map<ExecutableElement, AnnotationValue> attributes = getAttributes(method, Cacheable.class, false); |
| 555 | + assertEquals(1, attributes.size()); |
| 556 | + assertAttributeEntry(attributes, "cacheNames", ofArray("cache-1", "cache-2")); |
| 557 | + |
| 558 | + |
| 559 | + attributes = getAttributes(method, Cacheable.class, true); |
| 560 | + assertEquals(9, attributes.size()); |
| 561 | + assertAttributeEntry(attributes, "value", EMPTY_STRING_ARRAY); |
| 562 | + assertAttributeEntry(attributes, "cacheNames", ofArray("cache-1", "cache-2")); |
| 563 | + assertAttributeEntry(attributes, "key", EMPTY_STRING); |
| 564 | + assertAttributeEntry(attributes, "keyGenerator", EMPTY_STRING); |
| 565 | + assertAttributeEntry(attributes, "cacheManager", EMPTY_STRING); |
| 566 | + assertAttributeEntry(attributes, "cacheResolver", EMPTY_STRING); |
| 567 | + assertAttributeEntry(attributes, "condition", EMPTY_STRING); |
| 568 | + assertAttributeEntry(attributes, "unless", EMPTY_STRING); |
| 569 | + assertAttributeEntry(attributes, "sync", false); |
| 570 | + } |
| 571 | + |
| 572 | + void assertServiceAttributes(Map<ExecutableElement, AnnotationValue> attributes) { |
| 573 | + assertEquals(1, attributes.size()); |
| 574 | + assertAttributeEntry(attributes, "value", "testService"); |
| 575 | + } |
| 576 | + |
| 577 | + void assertAttributeEntry(Map<ExecutableElement, AnnotationValue> attributes, String attributeName, Object attributeValue) { |
| 578 | + for (Entry<ExecutableElement, AnnotationValue> entry : attributes.entrySet()) { |
| 579 | + ExecutableElement attributeMethod = entry.getKey(); |
| 580 | + if (matchesAttributeMethod(attributeMethod, attributeName)) { |
| 581 | + assertAttributeEntry(entry, attributeName, attributeValue); |
| 582 | + break; |
| 583 | + } |
| 584 | + } |
| 585 | + } |
| 586 | + |
| 587 | + void assertAttributeEntry(Entry<ExecutableElement, AnnotationValue> attributeEntry, String attributeName, Object attributeValue) { |
| 588 | + ExecutableElement attributeMethod = attributeEntry.getKey(); |
| 589 | + AnnotationValue annotationValue = attributeEntry.getValue(); |
| 590 | + assertEquals(attributeName, getAttributeName(attributeMethod)); |
| 591 | + Object value = getAttribute(attributeMethod.getReturnType(), annotationValue); |
| 592 | + Class<?> attributeValueClass = value.getClass(); |
| 593 | + if (attributeValueClass.isArray()) { |
| 594 | + Class<?> componentType = attributeValueClass.getComponentType(); |
| 595 | + if (String.class.equals(componentType)) { |
| 596 | + assertArrayEquals((String[]) attributeValue, (String[]) value); |
| 597 | + } |
| 598 | + } else { |
| 599 | + assertEquals(attributeValue, value); |
| 600 | + } |
| 601 | + } |
| 602 | + |
| 603 | + @Test |
| 604 | + public void testGetAttributesOnNull() { |
| 605 | + Map<ExecutableElement, AnnotationValue> attributes = getAttributes(null); |
| 606 | + assertSame(emptyMap(), attributes); |
| 607 | + } |
| 608 | + |
532 | 609 | private void assertFindMetaAnnotation(Element element, Class<? extends Annotation> annotationClass) { |
533 | 610 | assertAnnotation(findMetaAnnotation(element, annotationClass), annotationClass); |
534 | 611 | } |
|
0 commit comments