Class Library

java.lang.Object
com.oracle.truffle.api.nodes.Node
com.oracle.truffle.api.library.Library
All Implemented Interfaces:
NodeInterface, Cloneable
Direct Known Subclasses:
DynamicDispatchLibrary, DynamicObjectLibrary, InteropLibrary, NodeLibrary, ReflectionLibrary

public abstract class Library extends Node
Base class for all Truffle library specifications. Required base class for classes annotated with GenerateLibrary. Implementations for the abstract methods specified in this classes are not supposed to be implemented manually.
Since:
19.0
See Also:
  • Constructor Details

    • Library

      protected Library()
      Default constructor for sub-classes. Libraries always have a no-arg protected constructor.
      Since:
      19.0
  • Method Details

    • accepts

      public abstract boolean accepts(Object receiver)
      Returns true if this library instance supports sending messages with the given receiver. If accepts returns false and a library message is sent anyway then an AssertionError is thrown if assertions are enabled (-ea). Otherwise a NullPointerException or ClassCastException may be thrown by the method.

      A library that was created using a receiver value i.e. a LibraryFactory.create(Object) only guarantees to accept the value it was constructed with. For other values, the method may return false. Such libraries need to check for acceptance before calling a library method with a receiver. If receiver values are not accepted then a new library needs to be created or fetched. Dispatched versions of libraries always return true for any value as they take care of dispatching to any receiver type.

      It is not necessary to call accepts manually for most use cases. Instead the CachedLibrary should be used instead. For slow-paths the internally dispatched versions of the uncached library should be used.

      The accepts message may be exported by receiver types. When exported it can only be further restricted in addition to the default accepts implementation.

      Since:
      19.0
      See Also: