Skip to content

refactor/use method handles#819

Merged
joe-chacko merged 9 commits into
OpenLiberty:mainfrom
ngmr:refactor/use-method-handles
Jun 22, 2026
Merged

refactor/use method handles#819
joe-chacko merged 9 commits into
OpenLiberty:mainfrom
ngmr:refactor/use-method-handles

Conversation

@ngmr

@ngmr ngmr commented Jun 19, 2026

Copy link
Copy Markdown
Member

ngmr added 3 commits June 19, 2026 13:52
…ndle

- Added MethodHandle support to MethodDescriptor with lazy initialization
- Implemented genMethodHandle() using privileged blocks for setAccessible()
- Changed RMIServant.invoke_method() to use MethodHandle.bindTo().invokeWithArguments()
- Removed debug_name() helper, updated logging to use MethodDescriptor.toString()
- Changed getReflectedMethod() visibility from public to package-private for security
- Updated RMIStubHandler to pass MethodDescriptor instead of Method
- Substantive lines added: 50, lines removed: 25

This targets the hottest path in RMI invocations, providing expected 2-5x
performance improvement for remote method calls.

Co-authored-by-AI: IBM Bob 1.0.4
- Added MethodHandle support for writeReplace, readResolve, and readObject methods
- Implemented genReadObjectHandle() to create MethodHandle for readObject
- Updated genWriteReplacer() to use MethodHandle instead of Method.invoke()
- Updated genReadResolver() to use MethodHandle instead of Method.invoke()
- Converted readObject invocations in buildReader() and buildCustomMarshalReaderWithReadObject()
- Simplified exception handling for MethodHandle (throws Throwable directly)
- Substantive lines added: 51, lines removed: 31

This improves performance for custom serialization during RMI-IIOP value marshaling.

Co-authored-by-AI: IBM Bob 1.0.4
- Added MethodHandle support for writeObject method in ValueDescriptor
- Implemented genWriteObjectHandle() and getWriteObjectHandle() accessor
- Updated ObjectWriter.invokeWriteObject() to accept MethodHandle instead of Method
- Simplified exception handling in invokeWriteObject() for MethodHandle
- Updated buildCustomWriter() to pass MethodHandle via getWriteObjectHandle()
- Substantive lines added: 24, lines removed: 14

This completes the custom serialization MethodHandle conversion, improving
performance for writeObject during RMI-IIOP value marshaling.

Co-authored-by-AI: IBM Bob 1.0.4
@ngmr ngmr self-assigned this Jun 19, 2026
@ngmr ngmr requested a review from joe-chacko June 19, 2026 15:56
ngmr added 2 commits June 19, 2026 18:17
- Moved privileged setAccessible calls into gen*Handle methods
- Removed unused writeObjectMethodRef and readObjectMethodRef lazy references
- Removed genWriteObjectMethod() and genReadObjectMethod() methods
- Made genWriteObjectHandle() and genReadObjectHandle() package-private for subclass overrides
- Updated logic to check MethodHandle null-ness via getOptional*Handle() accessors
- Added customMarshalledRef and chunkedRef as lazy references
- Updated UncustomizableValueDescriptor and FVDUncustomizableValueDescriptor to override gen*Handle
- Substantive lines added: 75, lines removed: 68

This refactoring consolidates the MethodHandle generation logic, eliminating redundant
Method-based lazy references and ensuring all access control is handled within the
MethodHandle generators.

Co-authored-by-AI: IBM Bob 1.0.4
Convert Method.invoke() calls to MethodHandle.invoke() in IDLEntityDescriptor
for better performance when calling IDL Helper methods (read, write, type).

Changes:
- Renamed findMethod() to findMethodHandle() to return MethodHandle directly
- Updated genReader() to use MethodHandle for Helper.read()
- Updated genWriter() to use MethodHandle for Helper.write()
- Updated genTypeCode() to use MethodHandle for Helper.type()
- Simplified exception handling with consistent pattern

Co-authored-by-AI: IBM Bob 1.0.4 (Claude 3.5 Sonnet)
@ngmr ngmr force-pushed the refactor/use-method-handles branch 2 times, most recently from 92a4739 to d0bd406 Compare June 22, 2026 11:59
ngmr and others added 4 commits June 22, 2026 16:06
Convert the remaining reflective defineClass invocation in
org.apache.yoko.rmi.util.stub.Util from Method.invoke() to
MethodHandle.invoke().

Changes:
- store ClassLoader.defineClass as a MethodHandle
- use PrivilegedActions.makeAccessible() before unreflect()
- simplify invoke() exception handling for MethodHandle semantics

Co-authored-by-AI: IBM Bob 1.0.4
…Accessible

- Simplified ClassBaseDescriptor.findField() to use getDeclaredField() and makeAccessible()
- Simplified MethodDescriptor.genMethodHandle() to use getDeclaredMethod() and makeAccessible()
- Simplified FieldDescriptor field copy creation to use getDeclaredField() and makeAccessible()
- Removed manual setAccessible(true) calls in favor of PrivilegedActions.makeAccessible()
- Lines added: 21, lines removed: 23

Co-authored-by-AI: IBM Bob 1.0.4
- Removed doPrivileged(exAction(...)) wrapper from CorbaObjectWriter instantiation in writeValue()
- Removed doPrivileged(exAction(...)) wrapper from CorbaObjectReader instantiation in makeCorbaObjectReader()
- Removed associated PrivilegedActionException catch blocks
- Both constructors only perform simple field assignments without security-sensitive operations
- Lines added: 2, lines removed: 7

Co-authored-by-AI: IBM Bob 1.0.4
Convert Method.invoke() to MethodHandle.invoke() for IDL Helper narrow()
calls in PortableRemoteObjectImpl with ClassValue-based caching.

Changes:
- Added HELPER_NARROW_CACHE using ClassValue<MethodHandle>, mapping from
  helper class to its "narrow" method
- Updated narrowIDL() to use cached MethodHandle instead of Method.invoke()
- Simplified exception handling with consistent pattern
- Updated MultiAddressUrlTest to remove InvocationTargetException from expected causal chains, as MethodHandle.invoke() does not add this wrapper layer (unlike the old Method.invoke() approach)
- Fixed exception handling in StubWriteReplaceMethodHolder to properly unwrap PrivilegedActionException
- Lines added: 29, lines removed: 13

Performance: ClassValue provides thread-safe per-class caching without locks

Co-authored-by: Joe Chacko <chackoj@uk.ibm.com>
Co-authored-by-AI: IBM Bob 1.0.4
@ngmr ngmr force-pushed the refactor/use-method-handles branch from d0bd406 to 9e7fedb Compare June 22, 2026 15:25

@joe-chacko joe-chacko left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah!

@joe-chacko joe-chacko merged commit e518655 into OpenLiberty:main Jun 22, 2026
2 checks passed
@ngmr ngmr deleted the refactor/use-method-handles branch June 22, 2026 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Java 7: Replace method invocation via reflection with use of MethodHandles

2 participants