Commit 0356be7b authored by Stephane Nicoll's avatar Stephane Nicoll

Refine AliasFor usage

This commit makes sure to use `getAliasedStringArray` rather than
`getStringArray` as the latter does not work with ASM. While this will
probably be fixed in the core framework, this commit also adds dedicated
tests with ASM to ensure that the code works as expected.

Closes gh-6337
parent d4011687
...@@ -155,7 +155,8 @@ public class EntityScanPackages { ...@@ -155,7 +155,8 @@ public class EntityScanPackages {
private Set<String> getPackagesToScan(AnnotationMetadata metadata) { private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
AnnotationAttributes attributes = AnnotationAttributes.fromMap( AnnotationAttributes attributes = AnnotationAttributes.fromMap(
metadata.getAnnotationAttributes(EntityScan.class.getName())); metadata.getAnnotationAttributes(EntityScan.class.getName()));
String[] basePackages = attributes.getStringArray("basePackages"); String[] basePackages = attributes.getAliasedStringArray("basePackages",
EntityScan.class, metadata.getClassName());
Class<?>[] basePackageClasses = attributes Class<?>[] basePackageClasses = attributes
.getClassArray("basePackageClasses"); .getClassArray("basePackageClasses");
Set<String> packagesToScan = new LinkedHashSet<String>(); Set<String> packagesToScan = new LinkedHashSet<String>();
......
...@@ -24,6 +24,7 @@ import org.junit.Rule; ...@@ -24,6 +24,7 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationConfigurationException; import org.springframework.core.annotation.AnnotationConfigurationException;
...@@ -112,6 +113,17 @@ public class EntityScanPackagesTests { ...@@ -112,6 +113,17 @@ public class EntityScanPackagesTests {
assertThat(packages.getPackageNames()).containsExactly("a"); assertThat(packages.getPackageNames()).containsExactly("a");
} }
@Test
public void entityScanAnnotationWhenHasValueAttributeShouldSetupPackagesAsm()
throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.registerBeanDefinition("entityScanValueConfig",
new RootBeanDefinition(EntityScanValueConfig.class.getName()));
this.context.refresh();
EntityScanPackages packages = EntityScanPackages.get(this.context);
assertThat(packages.getPackageNames()).containsExactly("a");
}
@Test @Test
public void entityScanAnnotationWhenHasBasePackagesAttributeShouldSetupPackages() public void entityScanAnnotationWhenHasBasePackagesAttributeShouldSetupPackages()
throws Exception { throws Exception {
......
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -63,7 +63,8 @@ class EntityScanRegistrar implements ImportBeanDefinitionRegistrar { ...@@ -63,7 +63,8 @@ class EntityScanRegistrar implements ImportBeanDefinitionRegistrar {
private Set<String> getPackagesToScan(AnnotationMetadata metadata) { private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
AnnotationAttributes attributes = AnnotationAttributes AnnotationAttributes attributes = AnnotationAttributes
.fromMap(metadata.getAnnotationAttributes(EntityScan.class.getName())); .fromMap(metadata.getAnnotationAttributes(EntityScan.class.getName()));
String[] basePackages = attributes.getStringArray("basePackages"); String[] basePackages = attributes.getAliasedStringArray("basePackages",
EntityScan.class, metadata.getClassName());
Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses"); Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
Set<String> packagesToScan = new LinkedHashSet<String>(); Set<String> packagesToScan = new LinkedHashSet<String>();
packagesToScan.addAll(Arrays.asList(basePackages)); packagesToScan.addAll(Arrays.asList(basePackages));
......
...@@ -76,7 +76,8 @@ class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar { ...@@ -76,7 +76,8 @@ class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar {
private Set<String> getPackagesToScan(AnnotationMetadata metadata) { private Set<String> getPackagesToScan(AnnotationMetadata metadata) {
AnnotationAttributes attributes = AnnotationAttributes.fromMap( AnnotationAttributes attributes = AnnotationAttributes.fromMap(
metadata.getAnnotationAttributes(ServletComponentScan.class.getName())); metadata.getAnnotationAttributes(ServletComponentScan.class.getName()));
String[] basePackages = attributes.getStringArray("basePackages"); String[] basePackages = attributes.getAliasedStringArray("basePackages",
ServletComponentScan.class, metadata.getClassName());
Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses"); Class<?>[] basePackageClasses = attributes.getClassArray("basePackageClasses");
Set<String> packagesToScan = new LinkedHashSet<String>(); Set<String> packagesToScan = new LinkedHashSet<String>();
packagesToScan.addAll(Arrays.asList(basePackages)); packagesToScan.addAll(Arrays.asList(basePackages));
......
...@@ -26,6 +26,7 @@ import org.junit.rules.ExpectedException; ...@@ -26,6 +26,7 @@ import org.junit.rules.ExpectedException;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -61,6 +62,15 @@ public class EntityScanTests { ...@@ -61,6 +62,15 @@ public class EntityScanTests {
assertSetPackagesToScan("com.mycorp.entity"); assertSetPackagesToScan("com.mycorp.entity");
} }
@Test
public void simpleValueAsm() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.registerBeanDefinition("valueConfig",
new RootBeanDefinition(ValueConfig.class.getName()));
this.context.refresh();
assertSetPackagesToScan("com.mycorp.entity");
}
@Test @Test
public void needsEntityManageFactory() throws Exception { public void needsEntityManageFactory() throws Exception {
this.thrown.expect(IllegalStateException.class); this.thrown.expect(IllegalStateException.class);
......
...@@ -21,6 +21,7 @@ import org.junit.Rule; ...@@ -21,6 +21,7 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationConfigurationException; import org.springframework.core.annotation.AnnotationConfigurationException;
...@@ -57,6 +58,18 @@ public class ServletComponentScanRegistrarTests { ...@@ -57,6 +58,18 @@ public class ServletComponentScanRegistrarTests {
"com.example.bar"); "com.example.bar");
} }
@Test
public void packagesConfiguredWithValueAsm() {
this.context = new AnnotationConfigApplicationContext();
this.context.registerBeanDefinition("valuePackages",
new RootBeanDefinition(ValuePackages.class.getName()));
this.context.refresh();
ServletComponentRegisteringPostProcessor postProcessor = this.context
.getBean(ServletComponentRegisteringPostProcessor.class);
assertThat(postProcessor.getPackagesToScan()).contains("com.example.foo",
"com.example.bar");
}
@Test @Test
public void packagesConfiguredWithBackPackages() { public void packagesConfiguredWithBackPackages() {
this.context = new AnnotationConfigApplicationContext(BasePackages.class); this.context = new AnnotationConfigApplicationContext(BasePackages.class);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment