Package com.oracle.truffle.api.library
Class ReflectionLibrary
java.lang.Object
com.oracle.truffle.api.nodes.Node
com.oracle.truffle.api.library.Library
com.oracle.truffle.api.library.ReflectionLibrary
- All Implemented Interfaces:
NodeInterface
,Cloneable
The reflection library allows to send to and proxy messages of receivers. The reflection library
may be used for any receiver object. If a sent message is not implemented by the target receiver
then the default library export will be used, otherwise it is forwarded to the resolved exported
message. If the reflection library is exported by a receiver directly then the reflection
behavior can be customized. This allows, for example, to proxy all messages to a delegate
instance.
Sending Messages
Messages can be sent to receivers by firstresolving
a
target message. Then the message can be sent to a receiver that may implement that message. The
message name and class may be resolved dynamically.
Usage example
String messageName = "isArray"; Message message = Message.resolve(ArrayLibrary.class, messageName); Object receiver = 42; try { ReflectionLibrary.getFactory().getUncached().send(receiver, message); } catch (Exception e) { // handle error }
Proxies
It is possible to proxy library messages to a delegate object. To achieve that the reflection library can be exported by a receiver type.Usage example
@ExportLibrary(ReflectionLibrary.class) static class AgnosticWrapper { final Object delegate; AgnosticWrapper(Object delegate) { this.delegate = delegate; } @ExportMessage final Object send(Message message, Object[] args, @CachedLibrary("this.delegate") ReflectionLibrary reflection) throws Exception { // do before Object result = reflection.send(delegate, message, args); // do after return result; } }
- Since:
- 19.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.oracle.truffle.api.nodes.Node
Node.Child, Node.Children
-
Constructor Summary
ModifierConstructorDescriptionprotected
Constructor for generated subclasses. -
Method Summary
Modifier and TypeMethodDescriptionstatic LibraryFactory
<ReflectionLibrary> Returns the library factory forReflectionLibrary
.static ReflectionLibrary
Returns the uncached automatically dispatched version of the reflection library.static ReflectionLibrary
Returns the uncached manually dispatched version of the reflection library.Sends a given message to the target receiver with the provided arguments.Methods inherited from class com.oracle.truffle.api.nodes.Node
accept, adoptChildren, atomic, atomic, copy, deepCopy, getChildren, getCost, getDebugProperties, getDescription, getEncapsulatingSourceSection, getLock, getParent, getRootNode, getSourceSection, insert, insert, isAdoptable, isSafelyReplaceableBy, notifyInserted, onReplace, replace, replace, reportPolymorphicSpecialize, toString
-
Constructor Details
-
ReflectionLibrary
protected ReflectionLibrary()Constructor for generated subclasses. Subclasses of this class are generated, do not extend this class directly.- Since:
- 19.0
-
-
Method Details
-
send
Sends a given message to the target receiver with the provided arguments. The provided receiver and message must not be null. If the argument types don't match the expected message signature then anIllegalArgumentException
will be thrown.- Throws:
Exception
- Since:
- 19.0
-
getFactory
Returns the library factory forReflectionLibrary
.- Since:
- 19.0
-
getUncached
Returns the uncached automatically dispatched version of the reflection library. This is a short-cut for callingReflectionLibrary.getFactory().getUncached()
.- Since:
- 21.3
- See Also:
-
getUncached
Returns the uncached manually dispatched version of the reflection library. This is a short-cut for callingReflectionLibrary.getFactory().getUncached(v)
.- Since:
- 21.3
- See Also:
-