/** * Post-process the aggregated results into a set of synthesized annotations. * @param element the annotated element * @param aggregatedResults the aggregated results for the given element * @return the set of annotations */ @SuppressWarnings("unchecked") private static <A extends Annotation> Set<A> postProcessAndSynthesizeAggregatedResults( AnnotatedElement element, List<AnnotationAttributes> aggregatedResults) { Set<A> annotations = new LinkedHashSet<>(); for (AnnotationAttributes attributes : aggregatedResults) { AnnotationUtils.postProcessAnnotationAttributes(element, attributes, false, false); Class<? extends Annotation> annType = attributes.annotationType(); if (annType != null) { annotations.add((A) AnnotationUtils.synthesizeAnnotation(attributes, annType, element)); } } return annotations; }
@Override public void visitEnd() { if (!this.allNestedAttributes.isEmpty()) { this.attributes.put(this.attributeName, this.allNestedAttributes.toArray(new AnnotationAttributes[0])); } else if (!this.attributes.containsKey(this.attributeName)) { Class<? extends Annotation> annotationType = this.attributes.annotationType(); if (annotationType != null) { try { Class<?> attributeType = annotationType.getMethod(this.attributeName).getReturnType(); if (attributeType.isArray()) { this.attributes.put(this.attributeName, Array.newInstance(attributeType.getComponentType(), 0)); } } catch (NoSuchMethodException ex) { // Corresponding attribute method not found: cannot expose empty array. } } } }
super.visitEnd(); Class<? extends Annotation> annotationClass = this.attributes.annotationType(); if (annotationClass != null) { List<AnnotationAttributes> attributeList = this.attributesMap.get(this.annotationType);
@Override public void postProcess(@Nullable AnnotatedElement element, Annotation annotation, AnnotationAttributes attributes) { annotation = AnnotationUtils.synthesizeAnnotation(annotation, element); Class<? extends Annotation> targetAnnotationType = attributes.annotationType();
/** * Post-process the aggregated results into a set of synthesized annotations. * @param element the annotated element * @param aggregatedResults the aggregated results for the given element * @return the set of annotations */ @SuppressWarnings("unchecked") private static <A extends Annotation> Set<A> postProcessAndSynthesizeAggregatedResults( AnnotatedElement element, List<AnnotationAttributes> aggregatedResults) { Set<A> annotations = new LinkedHashSet<>(); for (AnnotationAttributes attributes : aggregatedResults) { AnnotationUtils.postProcessAnnotationAttributes(element, attributes, false, false); Class<? extends Annotation> annType = attributes.annotationType(); if (annType != null) { annotations.add((A) AnnotationUtils.synthesizeAnnotation(attributes, annType, element)); } } return annotations; }
@Override public void visitEnd() { if (!this.allNestedAttributes.isEmpty()) { this.attributes.put(this.attributeName, this.allNestedAttributes.toArray(new AnnotationAttributes[0])); } else if (!this.attributes.containsKey(this.attributeName)) { Class<? extends Annotation> annotationType = this.attributes.annotationType(); if (annotationType != null) { try { Class<?> attributeType = annotationType.getMethod(this.attributeName).getReturnType(); if (attributeType.isArray()) { this.attributes.put(this.attributeName, Array.newInstance(attributeType.getComponentType(), 0)); } } catch (NoSuchMethodException ex) { // Corresponding attribute method not found: cannot expose empty array. } } } }
super.visitEnd(); Class<? extends Annotation> annotationClass = this.attributes.annotationType(); if (annotationClass != null) { List<AnnotationAttributes> attributeList = this.attributesMap.get(this.annotationType);
Class<? extends Annotation> annotationType = attributes.annotationType(); if (annotationType != null && Modifier.isPublic(annotationType.getModifiers())) {
@Override public void postProcess(@Nullable AnnotatedElement element, Annotation annotation, AnnotationAttributes attributes) { annotation = AnnotationUtils.synthesizeAnnotation(annotation, element); Class<? extends Annotation> targetAnnotationType = attributes.annotationType();
Class<? extends Annotation> annotationType = attributes.annotationType();
@Test public void getAnnotationAttributesWithNestedAnnotations() { ComponentScan componentScan = ComponentScanClass.class.getAnnotation(ComponentScan.class); assertNotNull(componentScan); AnnotationAttributes attributes = getAnnotationAttributes(ComponentScanClass.class, componentScan); assertNotNull(attributes); assertEquals(ComponentScan.class, attributes.annotationType()); Filter[] filters = attributes.getAnnotationArray("excludeFilters", Filter.class); assertNotNull(filters); List<String> patterns = stream(filters).map(Filter::pattern).collect(toList()); assertEquals(asList("*Foo", "*Bar"), patterns); }
Class<? extends Annotation> annotationType = attributes.annotationType(); if (annotationType != null && Modifier.isPublic(annotationType.getModifiers())) {
@Test @SuppressWarnings("unchecked") public void synthesizeAnnotationFromMapWithNestedArrayOfMaps() throws Exception { ComponentScan componentScan = ComponentScanClass.class.getAnnotation(ComponentScan.class); assertNotNull(componentScan); AnnotationAttributes attributes = getAnnotationAttributes(ComponentScanClass.class, componentScan, false, true); assertNotNull(attributes); assertEquals(ComponentScan.class, attributes.annotationType()); Map<String, Object>[] filters = (Map[]) attributes.get("excludeFilters"); assertNotNull(filters); List<String> patterns = stream(filters).map(m -> (String) m.get("pattern")).collect(toList()); assertEquals(asList("*Foo", "*Bar"), patterns); // Modify nested maps filters[0].put("pattern", "newFoo"); filters[0].put("enigma", 42); filters[1].put("pattern", "newBar"); filters[1].put("enigma", 42); ComponentScan synthesizedComponentScan = synthesizeAnnotation(attributes, ComponentScan.class, ComponentScanClass.class); assertNotNull(synthesizedComponentScan); assertNotSame(componentScan, synthesizedComponentScan); patterns = stream(synthesizedComponentScan.excludeFilters()).map(Filter::pattern).collect(toList()); assertEquals(asList("newFoo", "newBar"), patterns); }
Class<? extends Annotation> annotationType = attributes.annotationType();
@Test public void getAnnotationAttributesWithoutAttributeAliases() { Component component = WebController.class.getAnnotation(Component.class); assertNotNull(component); AnnotationAttributes attributes = (AnnotationAttributes) getAnnotationAttributes(component); assertNotNull(attributes); assertEquals("value attribute: ", "webController", attributes.getString(VALUE)); assertEquals(Component.class, attributes.annotationType()); }
@Test public void getAnnotationAttributesWithAttributeAliases() throws Exception { Method method = WebController.class.getMethod("handleMappedWithValueAttribute"); WebMapping webMapping = method.getAnnotation(WebMapping.class); AnnotationAttributes attributes = (AnnotationAttributes) getAnnotationAttributes(webMapping); assertNotNull(attributes); assertEquals(WebMapping.class, attributes.annotationType()); assertEquals("name attribute: ", "foo", attributes.getString("name")); assertArrayEquals("value attribute: ", asArray("/test"), attributes.getStringArray(VALUE)); assertArrayEquals("path attribute: ", asArray("/test"), attributes.getStringArray("path")); method = WebController.class.getMethod("handleMappedWithPathAttribute"); webMapping = method.getAnnotation(WebMapping.class); attributes = (AnnotationAttributes) getAnnotationAttributes(webMapping); assertNotNull(attributes); assertEquals(WebMapping.class, attributes.annotationType()); assertEquals("name attribute: ", "bar", attributes.getString("name")); assertArrayEquals("value attribute: ", asArray("/test"), attributes.getStringArray(VALUE)); assertArrayEquals("path attribute: ", asArray("/test"), attributes.getStringArray("path")); }
@Test @SuppressWarnings("unchecked") public void synthesizeAnnotationFromMapWithNestedMap() throws Exception { ComponentScanSingleFilter componentScan = ComponentScanSingleFilterClass.class.getAnnotation(ComponentScanSingleFilter.class); assertNotNull(componentScan); assertEquals("value from ComponentScan: ", "*Foo", componentScan.value().pattern()); AnnotationAttributes attributes = getAnnotationAttributes( ComponentScanSingleFilterClass.class, componentScan, false, true); assertNotNull(attributes); assertEquals(ComponentScanSingleFilter.class, attributes.annotationType()); Map<String, Object> filterMap = (Map<String, Object>) attributes.get("value"); assertNotNull(filterMap); assertEquals("*Foo", filterMap.get("pattern")); // Modify nested map filterMap.put("pattern", "newFoo"); filterMap.put("enigma", 42); ComponentScanSingleFilter synthesizedComponentScan = synthesizeAnnotation( attributes, ComponentScanSingleFilter.class, ComponentScanSingleFilterClass.class); assertNotNull(synthesizedComponentScan); assertNotSame(componentScan, synthesizedComponentScan); assertEquals("value from synthesized ComponentScan: ", "newFoo", synthesizedComponentScan.value().pattern()); }
private static void print(AnnotationAttributes annotationAttributes) { System.out.printf("注解 @%s 属性集合 : \n", annotationAttributes.annotationType().getName()); annotationAttributes.forEach((name, value) -> System.out.printf("\t属性 %s : %s \n", name, value) ); } }
protected static EvictionPolicyMetaData from(AnnotationAttributes evictionPolicyAttributes, ApplicationContext applicationContext) { Assert.isAssignable(EvictionPolicy.class, evictionPolicyAttributes.annotationType()); return from(evictionPolicyAttributes.getEnum("type"), (Integer) evictionPolicyAttributes.get("maximum"), evictionPolicyAttributes.getEnum("action"), resolveObjectSizer(evictionPolicyAttributes.getString("objectSizerName"), applicationContext), evictionPolicyAttributes.getStringArray("regionNames")); }
protected static EvictionPolicyMetaData from(AnnotationAttributes evictionPolicyAttributes, ApplicationContext applicationContext) { Assert.isAssignable(EvictionPolicy.class, evictionPolicyAttributes.annotationType()); return from(evictionPolicyAttributes.getEnum("type"), (Integer) evictionPolicyAttributes.get("maximum"), evictionPolicyAttributes.getEnum("action"), resolveObjectSizer(evictionPolicyAttributes.getString("objectSizerName"), applicationContext), evictionPolicyAttributes.getStringArray("regionNames")); }