Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for defining classes during LTW #275

Merged
merged 1 commit into from
Jan 29, 2024
Merged

Workaround for defining classes during LTW #275

merged 1 commit into from
Jan 29, 2024

Conversation

kriegaex
Copy link
Contributor

@kriegaex kriegaex commented Jan 28, 2024

Overhaul ClassLoaderWeavingAdaptor to use statically initialised Unsafe instances and method handles pointing to their defineClass methods. Those now work universally on JDKs 8-21. In older JDKs, the method used to be in sun.misc.Unsafe, in more recent ones on jdk.internal.misc.Unsafe. It is challenging to fetch instances, especially as reflection protection and module boundaries have been increased in the JDK progressively. But finally, a solution was adapted from Byte Buddy (BB).
Kudos to BB author Rafael Winterhalter. The previous solution to use ClassLoader::defineClass and require --add-opens is no longer necessary for the first time since it became necessary in AspectJ 1.9.7 with Java 16 support.

Add org.ow2.asm:asm-common as a dependency everywhere org.ow2.asm:asm was used before. Maybe that is too many places, but no worse than before.

Add missing dependency on loadtime to aspectjweaver. This kept a build like mvn install -am -pl aspectjweaver from picking up changed loadtime classes.

Fixes #117.

Overhaul ClassLoaderWeavingAdaptor to use statically initialised Unsafe
instances and method handles pointing to their 'defineClass' methods.
Those now work universally on JDKs 8-21. In older JDKs, the method used
to be in sun.misc.Unsafe, in more recent ones on jdk.internal.misc.Unsafe.
It is challenging to fetch instances, especially as reflection
protection and module boundaries have been increased in the JDK
progressively. But finally, a solution was adapted from Byte Buddy (BB).
Kudos to BB author Rafael Winterhalter. The previous solution to use
ClassLoader::defineClass and require '--add-opens' is no longer
necessary for the first time since it became necessary in AspectJ 1.9.7
with Java 16 support.

Add org.ow2.asm:asm-common as a dependency everywhere org.ow2.asm:asm
was used before. Maybe that is too many places, but no worse than before.

Add missing dependency on loadtime to aspectjweaver. This kept a build
like "mvn install -am -pl aspectjweaver" from picking up changed
loadtime classes.

Fixes #117.

Signed-off-by: Alexander Kriegisch <[email protected]>
@kriegaex
Copy link
Contributor Author

@raphw: Thanks for the inspiration. 😄

* Inspired by and shamelessly adapted from <a href="https://bit.ly/3w10oH5">Byte Buddy's {@code ClassInjector}</a>.
* Special thanks to Byte Buddy (BB) author Rafael Winterhalter, who briefly mentioned this approach in a
* <a href="https://bit.ly/3SjFOZY">GitHub comment</a> related to JDK issue
* <a href="https://bugs.openjdk.org/browse/JDK-8200559">JDK-8200559</a>.

@kriegaex kriegaex self-assigned this Jan 29, 2024
@kriegaex kriegaex added the enhancement New feature or request label Jan 29, 2024
@kriegaex kriegaex added this to the 1.9.21.1 milestone Jan 29, 2024
@kriegaex kriegaex merged commit f986c3d into master Jan 29, 2024
1 of 3 checks passed
@kriegaex kriegaex deleted the gh-117 branch January 29, 2024 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable LTW in JDK 16+ without --add-opens
1 participant