Commit Graph

3030 Commits

Author SHA1 Message Date
유예본(Yebon You)/Platform Engineering팀/11ST fa1f7f6dc7 Use InputStream's contracts instead of StreamUtils
Since Spring Framework 6.0 requires Java 17, we can now use
`InputStream` new contracts when manipulating streams.

Closes gh-27702
2022-09-12 10:21:50 +02:00
Brian Clozel 0770d86936 Deprecate StreamUtils.emptyInput()
Closes gh-29125
2022-09-12 10:21:50 +02:00
Patrick Strawderman d4a74c8f9d Update StreamUtils drain and emptyInput to use JDK builtins
Update StreamUtils.drain to use InputStream.transferTo with a null
OutputStream. This avoids allocating buffers for cases where the
supplied InputStream has an optimized transferTo method (e.g.,
ByteArrayInputStream and FileInputStream).

Additionally, update StreamUtils.emptyInput to simply call
InputStream.nullInputStream.

Closes gh-28961
2022-09-12 10:21:50 +02:00
Stephane Nicoll 7642ac82f6 Apply @PropertySource in AOT-generated context
This commit records `@PropertySource` declarations defined on
configuration classes so that these are contributed to the environment
of a context that is initialized by generated code.

Closes gh-28976
2022-09-12 10:09:37 +02:00
Stephane Nicoll ae706f3954 Allow MethodReference to define a more flexible signature
This commit moves MethodReference to an interface with a default
implementation that relies on a MethodSpec. Such an arrangement avoid
the need of specifying attributes of the method such as whether it is
static or not.

The resolution of the invocation block now takes an
ArgumentCodeGenerator rather than the raw arguments. Doing so gives
the opportunity to create more flexible signatures.

See gh-29005
2022-09-12 10:07:31 +02:00
Stephane Nicoll 8a4a89b9d9 Allow a MethodReference to be produced from a GeneratedMethod
This commit updates GeneratedMethod and its underlying infrastructure
to be able to produce a MethodReference. This simplifies the need when
such a reference needs to be created manually and reuses more of what
MethodReference has to offer.

See gh-29005
2022-09-12 10:07:13 +02:00
Sam Brannen 649c2f56fd Accept only ClassPathResource in ResourceHints#registerResource()
This commit throws an exception in registerResource() if the supplied
resource is not a ClassPathResource.

See gh-29083
2022-09-11 23:33:39 +02:00
Sam Brannen 6d83a959fb Rename registerResourceIfNecessary to registerResource
This commit renames registerResourceIfNecessary() to registerResource()
and throws an exception if the class path resource does not exist.

Closes gh-29083
2022-09-10 19:49:56 +02:00
Patrick Strawderman cdb38e8482 Increase StreamUtils.BUFFER_SIZE to 8192
This aligns the buffer size used in StreamUtils with the buffer sizes
used throughout the JDK (see InputStream, BufferedInputStream, Files).

Closes gh-28965
2022-09-09 22:04:10 +02:00
Brian Clozel 2b5ca63339 Fix serialization compiler warnings with Java 18
As of Java 18, the serial lint warning in javac has been expanded to
check for class fields that are not marked as `Serializable`.
See https://www.oracle.com/java/technologies/javase/18all-relnotes.html#JDK-8202056

In the Spring Framework codebase, this can happen with `Map`, `Set` or
`List` attributes which are often assigned with an unmodifiable
implementation variant. Such implementations are `Serializable` but
cannot be used as field types.

This commit ensures that the following changes are applied:
* fields are marked as transient if they can't be serialized
* classes are marked as `Serializable` if this was missing
* `@SuppressWarnings("serial")` is applied where relevant
2022-09-09 20:24:26 +02:00
Brian Clozel 21612eba19 Disable BlockHound tests on Java 18+
BlockHound is not currently compatible with Java 18.
2022-09-09 20:22:16 +02:00
Phillip Webb 665deb4d82 Add examples to Javadoc for Throwing*.of factory methods
Closes gh-28969
2022-09-09 09:33:50 -07:00
Sam Brannen d46a816745 Remove deprecated SynthesizedAnnotation interface
The SynthesizedAnnotation interface is no longer used in the core
Spring Framework and should not be used by external parties either.

Closes gh-29092
2022-09-08 12:12:51 +02:00
Sam Brannen 8a0e1969b0 Revert "Introduce isSynthesizable in MergedAnnotation"
This reverts commit 32346b8382.

Closes gh-29093
2022-09-08 12:12:51 +02:00
Sam Brannen 75dfd47826 Delete unused code in ReflectionHintsPredicatesTests 2022-09-08 12:12:51 +02:00
Sébastien Deleuze e1c94d7e6b Remove obsolete RuntimeHintsUtils
Closes gh-29058
2022-09-08 09:25:09 +02:00
Sam Brannen ae861a2b3e Document leading slash semantics for ClassPathResource.getPath()
See gh-29094
2022-09-07 15:28:35 +02:00
Sébastien Deleuze 9cfe79186d Stop using RuntimeHintsUtils
Due to gh-29053, we can stop using RuntimeHintsUtils to
register SynthesizedAnnotation proxies.

Closes gh-29059
2022-09-07 10:10:07 +02:00
Brian Clozel 323d1907c1 Do not require full type reflection when listing methods/fields
Prior to this commit, the `RuntimeHintsAgent` and its testing
infrastructure would assume that calling `MyClass.class.getMethods()`
requires a reflection hint on the class for introspecting public/private
methods.

GraalVM does not require this, in fact this call only returns methods
that have reflection hints in the native image.

This commit refines the agent behavior for `Class.getMethods()`,
`Class.getDeclaredMethods()`, `Class.getFields()` and
`Class.getDeclaredFields()`. With this change, registering at least one
method/field for reflection is enough to match.

During the execution of Java tests, all methods and fields will be
provided, regardless of hints being registered or not. This could cause
false negatives where we're missing reflection hints on methods or
fields.
This risk is mitigated thanks to additional instrumentation on
`Method.getAnnotations()`, `Method.getParameterTypes()` and
`Method.invoke()`. If a method is found reflectively, chances are it
will be used for further reflection.

Closes gh-29091
2022-09-06 22:14:10 +02:00
Sam Brannen 6cce47176a Further clarify the semantics of ClassPathResource.getPath()
See gh-29094
2022-09-06 20:34:12 +02:00
Sam Brannen 018122f684 Update Javadoc for ResourceHints.registerResourceIfNecessary()
See gh-29094
2022-09-06 20:18:24 +02:00
Sam Brannen d57e061d3f Introduce getAbsolutePath() in ClassPathResource
Since getPath() returns a relative path if the resource was created
using the ClassPathResource(String,Class) constructor, there was
previously no way to consistently obtain the absolute path to the
resource within the class path.

This commit addresses this shortcoming by introducing a new
getAbsolutePath() for consistently obtaining the absolute path to the
resource within the class path.

See gh-29083
Closes gh-29094
2022-09-06 20:09:24 +02:00
Sam Brannen 1052f48eb6 Clarify semantics of ClassPathResource.getPath()
This commit also improves the overall Javadoc in ClassPathResource.

See gh-29094
2022-09-06 20:08:21 +02:00
Sébastien Deleuze b8c1fc9202 Revert "Remove RuntimeHintsUtils"
This reverts commit 3e327f5641.
2022-09-06 18:21:34 +02:00
Sébastien Deleuze 3e327f5641 Remove RuntimeHintsUtils
Due to gh-29053, we can stop using RuntimeHintsUtils to
register SynthesizedAnnotation proxies.

Closes gh-29058
Closes gh-29059
2022-09-06 15:31:02 +02:00
Sam Brannen 8fbd2141b7 Move registerResourceIfNecessary() to ResourceHints
See gh-29083
2022-09-06 15:22:18 +02:00
Sam Brannen 5c2859ffa7 Make fields in ClassPathResource final 2022-09-06 14:47:19 +02:00
Sam Brannen 28c492cbdd Introduce RuntimeHintsUtils.registerResourceIfNecessary
This commit introduces a new registerResourceIfNecessary() method in
RuntimeHintsUtils that simplifies the registration of hints for
`classpath:` resources.

Closes gh-29083
2022-09-06 14:36:28 +02:00
Sam Brannen 10ade235e3 Prevent resource hint registration for pattern with leading slash
Prior to this commit, if a ResourcePatternHint was created with a
resource pattern with a leading slash, the hint was registered and
eventually converted to configuration for the GraalVM native image
compiler.

However, such a resource pattern is invalid for GraalVM. Consequently,
the registered resources were not available within the compiled native
image.

This commit ensures that registered patterns are applicable in a native
image by preventing creation of a ResourcePatternHint with a pattern
with a leading slash.

Closes gh-29088
2022-09-06 12:53:31 +02:00
Sam Brannen 04bb336b0b Polish ResourcePatternHints Javadoc 2022-09-06 12:53:31 +02:00
Sam Brannen 97b98c3378 Support default package for TypeReference in ResourceHintsPredicates
Prior to this commit, if the TypeReference supplied to
ResourceHintsPredicates.forResource(TypeReference,String) was for a
class declared in the default package (i.e., without a package), the
resolveAbsoluteResourceName() method incorrectly prepended two leading
slashes (//) to the absolute resource name, causing correct matches to
fail.

This commit fixes this by adding special handling for a TypeReference
without a package name. In addition, this commit introduces lenient
handling of resource names by consistently removing a leading slash in
ResourceHintsPredicates.forResource(*) methods. The latter aligns with
absolute resource path handling in other places in the framework, such
as ClassPathResource.

Closes gh-29086
2022-09-06 12:53:31 +02:00
Janne Valkealahti 135f9070c5 Add jni-config generation
This commit adds support generating graalvm `jni-config.json` file.

Configuration format for jni/reflection in graalvm is documented
to be exactly same so we can re-use facilities for reflection hints
which should be relatively clean for a user as also graalvm uses same
classes for both jni/reflection.

Closes gh-29007
2022-09-06 12:22:13 +02:00
Stephane Nicoll 58b0251af1 Relocate TestGenerationContext to spring-core-test
This commit moves the test implementation for GenerationContext in
spring-core-test. This also removes the copy we had in testfixtures

See gh-28877
2022-09-05 16:21:17 +02:00
Sébastien Deleuze f50c301387 Change BindingReflectionHintsRegistrar package
Move it to org.springframework.aot.hint.

See gh-28979
2022-09-02 19:31:26 +02:00
Brian Clozel 7ace1f9dc5 Align RuntimeHintsPredicates with new FieldMode
Closes gh-29063
2022-09-02 18:16:43 +02:00
Sébastien Deleuze 044f3728a3 Introduce @RegisterReflectionForBinding
This annotation and the related processor allows to
register reflection hints for data binding purpose
(class, fields, properties, record components for
the whole type hierarchy)

Closes gh-28979
2022-09-02 17:31:43 +02:00
Sébastien Deleuze aaffb8b27e Move BindingReflectionHintsRegistrar to spring-core
See gh-28979
2022-09-02 17:31:43 +02:00
Sébastien Deleuze cc555d2ff4 Avoid shipping AWT classes in native images
This commit provides an alternative
java.beans.Introspector#findCustomizerClass implementation
via a GraalVM substitution that avoids to include thousands
of unused AWT classes in the native image.

Closes gh-29060
2022-09-02 15:06:50 +02:00
Sam Brannen 69f23095b8 Deprecate SynthesizedAnnotation and related methods
Since Spring no longer adds the SynthesizedAnnotation interface to the
JDK dynamic proxy used to synthesize an annotation, this commit
officially deprecates SynthesizedAnnotation and related methods in
RuntimeHintsUtils.

See gh-29041, gh-29054
Closes gh-29053
2022-09-02 12:04:17 +02:00
Sam Brannen 0ec03a8dd6 Stop implementing SynthesizedAnnotation in annotation proxies
SynthesizedAnnotation was originally introduced as a convenience for
easily detecting if an annotation had been synthesized by Spring via a
simple `if (myAnnotation instanceof SynthesizedAnnotation)` check.

However, the introduction of SynthesizedAnnotation in the JDK dynamic
proxy for a synthesized annotation results in a separate proxy class
for each annotation synthesized by Spring, and this causes issues with
GraalVM native images since users and framework developers must always
ensure that the additional proxy classes are registered.

This commit completely removes the use of SynthesizedAnnotation in
synthesized annotation proxies. This change allows the proxy class for
an annotation to be reused for a synthesized annotation of the same
annotation type.

Consequently:

- Extra proxy classes are not generated on the JVM or in a native image.
- Extra proxy classes are not required to be registered for a native image.

Closes gh-29041
2022-09-02 11:27:00 +02:00
Brian Clozel fc47d25bc7 Polish
See gh-26320
2022-09-02 10:55:40 +02:00
Sam Brannen 73abd58202 Polishing 2022-09-02 10:23:53 +02:00
Phillip Webb 505da5c602 Migrate hint registration to shortcuts
Migrate code to make use of the `MemberCategory` and `FieldMode`
shortcuts.

See gh-29011
2022-09-01 17:26:06 -07:00
Phillip Webb bc0bf1fac3 Add additional shortcuts for hint registration
Add `MemberCategory` and `FieldMode` shortcuts for type registration.
Helper `builtWith` methods have also been extracted to the Hint types
to allow general reuse (for example with `registerTypes`).

See gh-29011
2022-09-01 17:25:46 -07:00
Phillip Webb da1005cd66 Add FieldMode for field hints and ensure that it cannot be downgraded
Add a `FieldMode` enum analogous to `ExecutableHint` and update
`FieldHint` to ensure that registration cannot downgrade `WRITE`
to `READ`.

Fixes gh-29055
2022-09-01 17:25:43 -07:00
Phillip Webb 0bd923b0a7 Polishing 2022-09-01 16:28:02 -07:00
Phillip Webb cea06e6a9e Polish ReflectionHints and TypeHint method order 2022-09-01 16:27:54 -07:00
Phillip Webb cfd37d948d Reduce visibility of hint factory methods
Reduce the visibility of hint factory methods that return a `Builder`.
Since the `build()` method of the `Builder` is package-private the
factory methods can also be package-private.
2022-09-01 13:37:08 -07:00
Sam Brannen a5584dcaa6 Merge branch '5.3.x'
# Conflicts:
#	spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java
2022-09-01 17:44:24 +02:00
Sam Brannen 6a68bd58f9 Introduce AnnotationUtils.isSynthesizedAnnotation(Annotation)
Since SynthesizedAnnotation will be deprecated (and potentially
completely removed) in Spring Framework 6.0, this commit introduces
AnnotationUtils.isSynthesizedAnnotation(Annotation) in 5.3.x to allow
people to migrate away from relying on SynthesizedAnnotation.

Closes gh-29054
2022-09-01 17:35:22 +02:00