public static String determineBeanNameFor(Method beanMethod) { String beanName = beanNameCache.get(beanMethod); if (beanName == null) { // By default, the bean name is the name of the @Bean-annotated method beanName = beanMethod.getName(); // Check to see if the user has explicitly set a custom bean name... AnnotationAttributes bean = AnnotatedElementUtils.findMergedAnnotationAttributes(beanMethod, Bean.class, false, false); if (bean != null) { String[] names = bean.getStringArray("name"); if (names.length > 0) { beanName = names[0]; } } beanNameCache.put(beanMethod, beanName); } return beanName; }
private Set<String> getPackagesToScan(AnnotationMetadata metadata) { AnnotationAttributes attributes = AnnotationAttributes.fromMap( metadata.getAnnotationAttributes(DubboComponentScan.class.getName())); String[] basePackages = attributes.getStringArray("basePackages"); Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses"); String[] value = attributes.getStringArray("value"); // Appends value array attributes Set<String> packagesToScan = new LinkedHashSet<String>(Arrays.asList(value)); packagesToScan.addAll(Arrays.asList(basePackages)); for (Class<?> basePackageClass : basePackageClasses) { packagesToScan.add(ClassUtils.getPackageName(basePackageClass)); } if (packagesToScan.isEmpty()) { return Collections.singleton(ClassUtils.getPackageName(metadata.getClassName())); } return packagesToScan; }
private Set<String> getPackagesToScan(AnnotationMetadata metadata) { AnnotationAttributes attributes = AnnotationAttributes.fromMap( metadata.getAnnotationAttributes(DubboComponentScan.class.getName())); String[] basePackages = attributes.getStringArray("basePackages"); Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses"); String[] value = attributes.getStringArray("value"); // Appends value array attributes Set<String> packagesToScan = new LinkedHashSet<String>(Arrays.asList(value)); packagesToScan.addAll(Arrays.asList(basePackages)); for (Class<?> basePackageClass : basePackageClasses) { packagesToScan.add(ClassUtils.getPackageName(basePackageClass)); } if (packagesToScan.isEmpty()) { return Collections.singleton(ClassUtils.getPackageName(metadata.getClassName())); } return packagesToScan; }
private Set<String> getPackagesToScan(AnnotationMetadata metadata) { AnnotationAttributes attributes = AnnotationAttributes.fromMap( metadata.getAnnotationAttributes(CompatibleDubboComponentScan.class.getName())); String[] basePackages = attributes.getStringArray("basePackages"); Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses"); String[] value = attributes.getStringArray("value"); // Appends value array attributes Set<String> packagesToScan = new LinkedHashSet<String>(Arrays.asList(value)); packagesToScan.addAll(Arrays.asList(basePackages)); for (Class<?> basePackageClass : basePackageClasses) { packagesToScan.add(ClassUtils.getPackageName(basePackageClass)); } if (packagesToScan.isEmpty()) { return Collections.singleton(ClassUtils.getPackageName(metadata.getClassName())); } return packagesToScan; }
private Set<String> getPackagesToScan(AnnotationMetadata metadata) { AnnotationAttributes attributes = AnnotationAttributes.fromMap( metadata.getAnnotationAttributes(CompatibleDubboComponentScan.class.getName())); String[] basePackages = attributes.getStringArray("basePackages"); Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses"); String[] value = attributes.getStringArray("value"); // Appends value array attributes Set<String> packagesToScan = new LinkedHashSet<String>(Arrays.asList(value)); packagesToScan.addAll(Arrays.asList(basePackages)); for (Class<?> basePackageClass : basePackageClasses) { packagesToScan.add(ClassUtils.getPackageName(basePackageClass)); } if (packagesToScan.isEmpty()) { return Collections.singleton(ClassUtils.getPackageName(metadata.getClassName())); } return packagesToScan; }
private void assertMetaAnnotationOverrides(AnnotationMetadata metadata) { AnnotationAttributes attributes = (AnnotationAttributes) metadata.getAnnotationAttributes( TestComponentScan.class.getName(), false); String[] basePackages = attributes.getStringArray("basePackages"); assertThat("length of basePackages[]", basePackages.length, is(1)); assertThat("basePackages[0]", basePackages[0], is("org.example.componentscan")); String[] value = attributes.getStringArray("value"); assertThat("length of value[]", value.length, is(0)); Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses"); assertThat("length of basePackageClasses[]", basePackageClasses.length, is(0)); }
private AnnotationAttributes assertComponentScanAttributes(Class<?> element, String... expected) { AnnotationAttributes attributes = findMergedAnnotationAttributes(element, ComponentScan.class); assertNotNull("Should find @ComponentScan on " + element, attributes); assertArrayEquals("value: ", expected, attributes.getStringArray("value")); assertArrayEquals("basePackages: ", expected, attributes.getStringArray("basePackages")); return attributes; }
private void getMergedAnnotationAttributesWithHalfConventionBasedAndHalfAliasedComposedAnnotation(Class<?> clazz) { String[] expected = asArray("explicitDeclaration"); String name = ContextConfig.class.getName(); String simpleName = clazz.getSimpleName(); AnnotationAttributes attributes = getMergedAnnotationAttributes(clazz, name); assertNotNull("Should find @ContextConfig on " + simpleName, attributes); assertArrayEquals("locations for class [" + clazz.getSimpleName() + "]", expected, attributes.getStringArray("locations")); assertArrayEquals("value for class [" + clazz.getSimpleName() + "]", expected, attributes.getStringArray("value")); // Verify contracts between utility methods: assertTrue(isAnnotated(clazz, name)); }
@Test public void getMergedAnnotationAttributesWithImplicitAliasesInMetaAnnotationOnComposedAnnotation() { Class<?> element = ComposedImplicitAliasesContextConfigClass.class; String name = ImplicitAliasesContextConfig.class.getName(); AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name); String[] expected = asArray("A.xml", "B.xml"); assertNotNull("Should find @ImplicitAliasesContextConfig on " + element.getSimpleName(), attributes); assertArrayEquals("groovyScripts", expected, attributes.getStringArray("groovyScripts")); assertArrayEquals("xmlFiles", expected, attributes.getStringArray("xmlFiles")); assertArrayEquals("locations", expected, attributes.getStringArray("locations")); assertArrayEquals("value", expected, attributes.getStringArray("value")); // Verify contracts between utility methods: assertTrue(isAnnotated(element, name)); }
@Test public void getMergedAnnotationAttributesWithShadowedAliasComposedAnnotation() { Class<?> element = ShadowedAliasComposedContextConfigClass.class; AnnotationAttributes attributes = getMergedAnnotationAttributes(element, ContextConfig.class); String[] expected = asArray("test.xml"); assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), attributes); assertArrayEquals("locations", expected, attributes.getStringArray("locations")); assertArrayEquals("value", expected, attributes.getStringArray("value")); }
/** * Construct a new {@link ContextConfigurationAttributes} instance for the * supplied {@link AnnotationAttributes} (parsed from a * {@link ContextConfiguration @ContextConfiguration} annotation) and * the {@linkplain Class test class} that declared them. * @param declaringClass the test class that declared {@code @ContextConfiguration} * @param annAttrs the annotation attributes from which to retrieve the attributes */ @SuppressWarnings("unchecked") public ContextConfigurationAttributes(Class<?> declaringClass, AnnotationAttributes annAttrs) { this(declaringClass, annAttrs.getStringArray("locations"), annAttrs.getClassArray("classes"), annAttrs.getBoolean("inheritLocations"), (Class<? extends ApplicationContextInitializer<?>>[]) annAttrs.getClassArray("initializers"), annAttrs.getBoolean("inheritInitializers"), annAttrs.getString("name"), annAttrs.getClass("loader")); }
@Test public void getMergedAnnotationAttributesWithAliasedComposedAnnotation() { Class<?> element = AliasedComposedContextConfigClass.class; String name = ContextConfig.class.getName(); AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name); assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), attributes); assertArrayEquals("value", asArray("test.xml"), attributes.getStringArray("value")); assertArrayEquals("locations", asArray("test.xml"), attributes.getStringArray("locations")); // Verify contracts between utility methods: assertTrue(isAnnotated(element, name)); }
@Test public void getMergedAnnotationAttributesWithConventionBasedComposedAnnotation() { Class<?> element = ConventionBasedComposedContextConfigClass.class; String name = ContextConfig.class.getName(); AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name); assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), attributes); assertArrayEquals("locations", asArray("explicitDeclaration"), attributes.getStringArray("locations")); assertArrayEquals("value", asArray("explicitDeclaration"), attributes.getStringArray("value")); // Verify contracts between utility methods: assertTrue(isAnnotated(element, name)); }
@Test public void getMergedAnnotationAttributesWithAliasedValueComposedAnnotation() { Class<?> element = AliasedValueComposedContextConfigClass.class; String name = ContextConfig.class.getName(); AnnotationAttributes attributes = getMergedAnnotationAttributes(element, name); assertNotNull("Should find @ContextConfig on " + element.getSimpleName(), attributes); assertArrayEquals("locations", asArray("test.xml"), attributes.getStringArray("locations")); assertArrayEquals("value", asArray("test.xml"), attributes.getStringArray("value")); // Verify contracts between utility methods: assertTrue(isAnnotated(element, name)); }
@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")); }
static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd, AnnotatedTypeMetadata metadata) { AnnotationAttributes lazy = attributesFor(metadata, Lazy.class); if (lazy != null) { abd.setLazyInit(lazy.getBoolean("value")); } else if (abd.getMetadata() != metadata) { lazy = attributesFor(abd.getMetadata(), Lazy.class); if (lazy != null) { abd.setLazyInit(lazy.getBoolean("value")); } } if (metadata.isAnnotated(Primary.class.getName())) { abd.setPrimary(true); } AnnotationAttributes dependsOn = attributesFor(metadata, DependsOn.class); if (dependsOn != null) { abd.setDependsOn(dependsOn.getStringArray("value")); } AnnotationAttributes role = attributesFor(metadata, Role.class); if (role != null) { abd.setRole(role.getNumber("value").intValue()); } AnnotationAttributes description = attributesFor(metadata, Description.class); if (description != null) { abd.setDescription(description.getString("value")); } }
@Test public void singleElementToSingleElementArrayConversionSupport() throws Exception { Filter filter = FilteredClass.class.getAnnotation(Filter.class); AnnotationAttributes nestedAttributes = new AnnotationAttributes(); nestedAttributes.put("name", "Dilbert"); // Store single elements attributes.put("names", "Dogbert"); attributes.put("classes", Number.class); attributes.put("nestedAttributes", nestedAttributes); attributes.put("filters", filter); // Get back arrays of single elements assertThat(attributes.getStringArray("names"), equalTo(new String[] {"Dogbert"})); assertThat(attributes.getClassArray("classes"), equalTo(new Class<?>[] {Number.class})); AnnotationAttributes[] array = attributes.getAnnotationArray("nestedAttributes"); assertNotNull(array); assertThat(array.length, is(1)); assertThat(array[0].getString("name"), equalTo("Dilbert")); Filter[] filters = attributes.getAnnotationArray("filters", Filter.class); assertNotNull(filters); assertThat(filters.length, is(1)); assertThat(filters[0].pattern(), equalTo("foo")); }
@Test public void typeSafeAttributeAccess() { AnnotationAttributes nestedAttributes = new AnnotationAttributes(); nestedAttributes.put("value", 10); nestedAttributes.put("name", "algernon"); attributes.put("name", "dave"); attributes.put("names", new String[] {"dave", "frank", "hal"}); attributes.put("bool1", true); attributes.put("bool2", false); attributes.put("color", Color.RED); attributes.put("class", Integer.class); attributes.put("classes", new Class<?>[] {Number.class, Short.class, Integer.class}); attributes.put("number", 42); attributes.put("anno", nestedAttributes); attributes.put("annoArray", new AnnotationAttributes[] {nestedAttributes}); assertThat(attributes.getString("name"), equalTo("dave")); assertThat(attributes.getStringArray("names"), equalTo(new String[] {"dave", "frank", "hal"})); assertThat(attributes.getBoolean("bool1"), equalTo(true)); assertThat(attributes.getBoolean("bool2"), equalTo(false)); assertThat(attributes.<Color>getEnum("color"), equalTo(Color.RED)); assertTrue(attributes.getClass("class").equals(Integer.class)); assertThat(attributes.getClassArray("classes"), equalTo(new Class<?>[] {Number.class, Short.class, Integer.class})); assertThat(attributes.<Integer>getNumber("number"), equalTo(42)); assertThat(attributes.getAnnotation("anno").<Integer>getNumber("value"), equalTo(10)); assertThat(attributes.getAnnotationArray("annoArray")[0].getString("name"), equalTo("algernon")); }
@Test public void overriddenContextConfigurationValue() throws Exception { Class<?> declaringClass = OverriddenMetaValueConfigTestCase.class; AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass, ContextConfiguration.class); assertNotNull(descriptor); assertEquals(declaringClass, descriptor.getRootDeclaringClass()); assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType()); assertEquals(ContextConfiguration.class, descriptor.getAnnotationType()); assertNotNull(descriptor.getComposedAnnotation()); assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType()); // direct access to annotation value: assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().value()); // overridden attribute: AnnotationAttributes attributes = descriptor.getAnnotationAttributes(); // NOTE: we would like to be able to override the 'value' attribute; however, // Spring currently does not allow overrides for the 'value' attribute. // See SPR-11393 for related discussions. assertArrayEquals(new String[] { "foo.xml" }, attributes.getStringArray("value")); }
@Test public void overriddenContextConfigurationLocationsAndInheritLocations() throws Exception { Class<?> declaringClass = OverriddenMetaLocationsConfigTestCase.class; AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass, ContextConfiguration.class); assertNotNull(descriptor); assertEquals(declaringClass, descriptor.getRootDeclaringClass()); assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType()); assertEquals(ContextConfiguration.class, descriptor.getAnnotationType()); assertNotNull(descriptor.getComposedAnnotation()); assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType()); // direct access to annotation attributes: assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().locations()); assertFalse(descriptor.getAnnotation().inheritLocations()); // overridden attributes: AnnotationAttributes attributes = descriptor.getAnnotationAttributes(); assertArrayEquals(new String[] { "bar.xml" }, attributes.getStringArray("locations")); assertTrue(attributes.getBoolean("inheritLocations")); }