Commit Graph

299 Commits

Author SHA1 Message Date
Chris Beams
d9f7fdd120 Support reading nested annotations via ASM
Background

  Spring 3.1 introduced the @ComponentScan annotation, which can accept
  an optional array of include and/or exclude @Filter annotations, e.g.

     @ComponentScan(
         basePackages = "com.acme.app",
         includeFilters = { @Filter(MyStereotype.class), ... }
     )
     @Configuration
     public class AppConfig { ... }

  @ComponentScan and other annotations related to @Configuration class
  processing such as @Import, @ImportResource and the @Enable*
  annotations are parsed using reflection in certain code paths, e.g.
  when registered directly against AnnotationConfigApplicationContext,
  and via ASM in other code paths, e.g. when a @Configuration class is
  discovered via an XML bean definition or when included via the
  @Import annotation.

  The ASM-based approach is designed to avoid premature classloading of
  user types and is instrumental in providing tooling support (STS, etc).

  Prior to this commit, the ASM-based routines for reading annotation
  attributes were unable to recurse into nested annotations, such as in
  the @Filter example above. Prior to Spring 3.1 this was not a problem,
  because prior to @ComponentScan, there were no cases of nested
  annotations in the framework.

  This limitation manifested itself in cases where users encounter
  the ASM-based annotation parsing code paths AND declare
  @ComponentScan annotations with explicit nested @Filter annotations.
  In these cases, the 'includeFilters' and 'excludeFilters' attributes
  are simply empty where they should be populated, causing the framework
  to ignore the filter directives and provide incorrect results from
  component scanning.

  The purpose of this change then, is to introduce the capability on the
  ASM side to recurse into nested annotations and annotation arrays. The
  challenge in doing so is that the nested annotations themselves cannot
  be realized as annotation instances, so must be represented as a
  nested Map (or, as described below, the new AnnotationAttributes type).

  Furthermore, the reflection-based annotation parsing must also be
  updated to treat nested annotations in a similar fashion; even though
  the reflection-based approach has no problem accessing nested
  annotations (it just works out of the box), for substitutability
  against the AnnotationMetadata SPI, both ASM- and reflection-based
  implementations should return the same results in any case. Therefore,
  the reflection-based StandardAnnotationMetadata has also been updated
  with an optional 'nestedAnnotationsAsMap' constructor argument that is
  false by default to preserve compatibility in the rare case that
  StandardAnnotationMetadata is being used outside the core framework.
  Within the framework, all uses of StandardAnnotationMetadata have been
  updated to set this new flag to true, meaning that nested annotation
  results will be consistent regardless the parsing approach used.

  Spr9031Tests corners this bug and demonstrates that nested @Filter
  annotations can be parsed and read in both the ASM- and
  reflection-based paths.

Major changes

 - AnnotationAttributes has been introduced as a concrete
   LinkedHashMap<String, Object> to be used anywhere annotation
   attributes are accessed, providing error reporting on attribute
   lookup and convenient type-safe access to common annotation types
   such as String, String[], boolean, int, and nested annotation and
   annotation arrays, with the latter two also returned as
   AnnotationAttributes instances.

 - AnnotationUtils#getAnnotationAttributes methods now return
   AnnotationAttributes instances, even though for binary compatibility
   the signatures of these methods have been preserved as returning
   Map<String, Object>.

 - AnnotationAttributes#forMap provides a convenient mechanism for
   adapting any Map<String, Object> into an AnnotationAttributes
   instance. In the case that the Map is already actually of
   type AnnotationAttributes, it is simply casted and returned.
   Otherwise, the map is supplied to the AnnotationAttributes(Map)
   constructor and wrapped in common collections style.

 - The protected MetadataUtils#attributesFor(Metadata, Class) provides
   further convenience in the many locations throughout the
   .context.annotation packagage that depend on annotation attribute
   introspection.

 - ASM-based core.type.classreading package reworked

   Specifically, AnnotationAttributesReadingVisitor has been enhanced to
   support recursive reading of annotations and annotation arrays, for
   example in @ComponentScan's nested array of @Filter annotations,
   ensuring that nested AnnotationAttributes objects are populated as
   described above.

   AnnotationAttributesReadingVisitor has also been refactored for
   clarity, being broken up into several additional ASM
   AnnotationVisitor implementations. Given that all types are
   package-private here, these changes represent no risk to binary
   compatibility.

 - Reflection-based StandardAnnotationMetadata updated

   As described above, the 'nestedAnnotationsAsMap' constructor argument
   has been added, and all framework-internal uses of this class have
   been updated to set this flag to true.

Issue: SPR-7979, SPR-8719, SPR-9031
2012-02-07 21:57:49 +01:00
Chris Beams
88913f2b23 Convert CRLF (dos) to LF (unix)
Prior to this change, roughly 5% (~300 out of 6000+) of files under the
source tree had CRLF line endings as opposed to the majority which have
LF endings.

This change normalizes these files to LF for consistency going forward.

Command used:

$ git ls-files | xargs file | grep CRLF | cut -d":" -f1 | xargs dos2unix

Issue: SPR-5608
2011-12-21 14:52:47 +01:00
Juergen Hoeller
e0d922d352 ConversionService is able to work with "Collections.emptyList()" as target type (again; SPR-7293) 2011-12-06 21:17:25 +00:00
Juergen Hoeller
33b53b7cca alignment with 3.0.7 backports (SPR-8674) 2011-12-01 18:51:36 +00:00
Juergen Hoeller
d6d169ac56 resolved package dependency tangles 2011-11-29 01:16:09 +00:00
Chris Beams
143db0d8de Introduce SystemEnvironmentPropertySource
Properties such as 'spring.profiles.active' cannot be specified at the
command line under Bash and other shells due to variable naming
constraints. This change allows for exchanging underscores for periods
as well as capitalizing property names for more idiomatic naming when
dealing with environment variables.

For example, Spring will respect equally either of the following:

    spring.profiles.active=p1 java -classpath ... MyApp

    SPRING_PROFILES_ACTIVE=p1 java -classpath ... MyApp

The former is not possible under Bash, while the latter is. No code or
configuration changes are required; SystemEnvironmentPropertySource
adapts for these varations automatically.

SystemEnvironmentPropertySource is added by default as
"systemEnvironment" to StandardEnvironment and all subtypes, taking the
place of the plain MapPropertySource that was in use before this change.

Issue: SPR-8869
2011-11-26 05:20:25 +00:00
Chris Beams
2c26a23c46 Rename EnvironmentTests => StandardEnvironmentTests 2011-11-26 05:20:20 +00:00
Chris Beams
a53d592f62 Use 'name' vs 'key' consistently in PropertySource 2011-11-26 05:20:17 +00:00
Rossen Stoyanchev
b5bcfa0ae3 SPR-8858 Fix in AntPathMatcher.combine(..)
Currently the combine method consolidates "/*" and "/hotel" 
into "/hotel". However if the first pattern contains URI template 
variables, the consolidation seems wrong. The fix is to prevent
the consolidation if the first pattern contains "{".
2011-11-23 17:53:18 +00:00
Juergen Hoeller
e2d9142c5a LocaleEditor and StringToLocaleConverter do not restrict variant part through validation (SPR-8637) 2011-10-20 11:53:02 +00:00
Juergen Hoeller
36616a0c2c fixed GenericTypeResolver to consistently return null if not resolvable (SPR-8698) 2011-10-20 11:17:42 +00:00
Juergen Hoeller
1cea52b66b renamed mapKey/ValueTypeDescriptor methods back to getMapKey/ValueTypeDescriptor (for Spring 3.0.x compatibility) 2011-10-11 16:55:07 +00:00
Chris Beams
6837111bda Refactor AnnotationUtils#findAllAnnotationAttributes
Remove all convenience variants of #findAllAnnotationAttributes and
refactor the remaining method to accept a MetadataReaderFactory
instead of creating its own SimpleMetadataReaderFactory internally.
This allows clients to use non-default class loaders as well as
customize the particular MetadataReaderFactory to be used (e.g.
'simple' vs 'caching', etc).

Issue: SPR-8752
2011-10-09 20:32:21 +00:00
Rossen Stoyanchev
cb5954ed02 SPR-8714 Do not create copy in map-to-map and collection-to-collection conversion if not necessary 2011-10-06 14:17:31 +00:00
Rossen Stoyanchev
00a726b098 SPR-8718 Prevent Converter<?,?> from converting target sub-type. 2011-10-06 14:17:12 +00:00
Rossen Stoyanchev
3d50d416eb SPR-8718 Revert fix from earlier for now (need a different approach). 2011-09-26 17:58:49 +00:00
Rossen Stoyanchev
1d7a6c53da SPR-8718 Prevent ClassCastException when the target of Converter<?,?> is a super-class of the actual target. 2011-09-26 12:30:38 +00:00
Sam Brannen
16fb3cb4b3 [SPR-8644] Introduced a failing (ignored) test that demonstrates that findMethod() does not currently support var-args. 2011-08-30 14:05:51 +00:00
Chris Beams
76bf72c9f8 Introduce ConfigurableEnvironment#addActiveProfile
Provides a convenient mechanism for activating an additional profile
while preserving those that are already active, as opposed to
calling #setActiveProfiles with the contents of #getActiveProfiles plus
the new profile.

Issue: SPR-8548
2011-08-20 03:02:20 +00:00
Chris Beams
c0eeb8bacd Introduce AbstractEnvironment#validateProfile
Consolidates validation for profiles and provides a mechanism for
AbstractEnvironment subclasses to customize validation logic if
desired.
2011-08-20 03:02:12 +00:00
Sam Brannen
2d6340af74 Deleting unnecessary TODOs and suppressing warnings. 2011-08-13 13:38:54 +00:00
Juergen Hoeller
8bf019b675 fixed @ExceptionHandler exception type matching (ExceptionDepthComparator; SPR-8231) 2011-07-21 07:15:26 +00:00
Juergen Hoeller
576b8fec31 ConvertiblePair implements equals and hashCode (SPR-8459) 2011-07-19 15:51:19 +00:00
Chris Beams
605f0e7a22 Introduce GenericTypeResolver#resolveReturnTypeArgument
Issue: SPR-8514
2011-07-06 09:15:32 +00:00
Chris Beams
b5b2add5cf Rename {DEFAULT_=>}COMMAND_LINE_PROPERTY_SOURCE_NAME
For consistency with all other constants representing default
property source names, such as
StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME and
StandardEnvironment#SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME

Issue: SPR-8482
2011-07-02 21:39:52 +00:00
Chris Beams
1eb5811347 Introduce CommandLinePropertySource and impls
Users may now work with command line arguments as a source of
properties for use with the PropertySource and Environment APIs.
An implementation based on the jopt library and a "simple"
implementation requiring no external libraries are are provided
out-of-the box.

See Javadoc for CommandLinePropertySource, JOptCommandLinePropertySource
and SimpleCommandLinePropertySource for details.

Issue: SPR-8482
2011-06-30 22:33:56 +00:00
Keith Donald
c9d73e2bde SPR-8465 2011-06-27 21:36:48 +00:00
Rossen Stoyanchev
2d29439130 SPR-7787 Allow qualifiers in regular expressions of URI template patterns. 2011-06-22 20:39:10 +00:00
Arjen Poutsma
3a332e556c SPR-8457 - XMLEventStreamReader.getText() fails for COMMENT and ENTITY_REFERENCE events 2011-06-17 09:07:12 +00:00
Chris Beams
5dc2d56600 Fix regression with covariant property return types
Prior to this change, the Property class introduced in 3.1 M2 validated
read/write property method pairs based on whether their parameter/return
types were equal to one another.  This precluded the valid possibility
of read method that returns a subtype of the write method's parameter
type, and represented a regression against 3.1 M1 and earlier versions.

The implementation now uses isAssignableFrom rather than a straight
equals check against the types.

Issue: SPR-8432
2011-06-16 07:53:42 +00:00
Juergen Hoeller
cd933c7f84 full support for arbitrary nesting of collections in fields (SPR-8394); proper type detection in nested collections within arrays 2011-06-07 22:34:35 +00:00
Keith Donald
0601f0e520 assignability examples 2011-06-07 20:14:02 +00:00
Keith Donald
1e39b0bbbc implemented collection/map converter conditional matching checks; updated SpEL to reflect this behavior 2011-06-07 20:00:28 +00:00
Keith Donald
a60cb43c6a added back element type checks in TypeDescriptor#isAssignable; clarified semantics in javadoc 2011-06-07 15:33:44 +00:00
Keith Donald
5e3a5202fb restored TypeDescriptor getElementType, getMapKeyType, and getMapValueType compatibility; StringToCollection and Array Converters are now conditional and check targetElementType if present; TypeDesciptor#isAssignable no longer bothers with element type and map key/value types in checking assignability for consistency elsewhere; improved javadoc 2011-06-07 02:51:44 +00:00
Keith Donald
a1a7c32052 string to collection and array converters now are conditional and apply target element type match 2011-06-06 22:48:00 +00:00
Keith Donald
76283ed321 narrow and elementType/keyValueType tests 2011-06-05 18:51:37 +00:00
Keith Donald
a40f6585b4 added convert(Object, TypeDescriptor) convenience method; collection and map tests 2011-06-05 18:36:58 +00:00
Keith Donald
5f8faa3ae7 improved null handling and javadoc 2011-06-05 17:41:08 +00:00
Keith Donald
cfb387383b broke out to top-level class for readability 2011-06-05 08:37:08 +00:00
Keith Donald
c09227a712 removed dependency on java.beans 2011-06-05 08:29:14 +00:00
Keith Donald
94d690fb2c javadoc and polishing 2011-06-05 07:14:34 +00:00
Keith Donald
c84cccf06d revised TypeDescriptor NULL and element/mapKey/mapValue type semantics 2011-06-05 04:43:18 +00:00
Keith Donald
5db1687d29 added TypeDescriptor resolveCollectionElement and Map key/value types 2011-06-04 05:38:51 +00:00
Chris Beams
385d8e9482 Fix system environment tests on all platforms
Issue: SPR-8245
2011-06-03 05:16:35 +00:00
Keith Donald
a1b7af5c9c broke out pkg private classes from TypeDescriptor to improve manageability and testability 2011-06-03 03:09:05 +00:00
Keith Donald
07f985ac91 more tests; several assertions for the programmer 2011-06-03 02:18:22 +00:00
Keith Donald
a8dbac6d8d more tests 2011-06-03 01:55:41 +00:00
Keith Donald
6f146737f4 simplified TypeDescriptor usage and updated use of the API across BeanWrapper and SpEL; collapsed PropertyTypeDescriptor into TypeDescriptor for simplicity and ease of use; improved docs 2011-06-02 23:37:19 +00:00
Chris Beams
67661693fe Ignore failing tests on Windows
Attempt to access and modify the system environment works on OS X /
Linux but not under Windows. Does not represent any real failure for
production code - the need to modify the system environment is a
testing concern only, and one we can probably live without, considering
the losing battle necessary to make such a hack cross-platform.

Issue: SPR-8245
2011-05-31 10:58:24 +00:00