Package com.oracle.truffle.api.object
Class DynamicObject
java.lang.Object
com.oracle.truffle.api.object.DynamicObject
- All Implemented Interfaces:
TruffleObject
Represents a dynamic object, members of which can be dynamically added and removed at run time.
To use it, extend
DynamicObject
and use DynamicObjectLibrary
for object accesses.
When constructing a DynamicObject
, it has
to be initialized with an empty initial shape. Initial shapes are created using
Shape.newBuilder()
and should ideally be shared per TruffleLanguage instance to allow
shape caches to be shared across contexts.
Subclasses can provide in-object dynamic field slots using the DynamicObject.DynamicField
annotation
and Shape.Builder.layout
.
Example:
public class MyObject extends DynamicObject implements TruffleObject {
public MyObject(Shape shape) {
super(shape);
}
}
Shape initialShape = Shape.newBuilder().layout(MyObject.class).build();
MyObject obj = new MyObject(initialShape);
- Since:
- 0.8 or earlier
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static @interface
Using this annotation, subclasses can define additional dynamic fields to be used by the object layout. -
Constructor Summary
ModifierConstructorDescriptionprotected
DynamicObject
(Shape shape) Constructor forDynamicObject
subclasses. -
Method Summary
Modifier and TypeMethodDescriptionprotected Object
clone()
Theclone()
method is not supported byDynamicObject
at this point in time, so it always throwsCloneNotSupportedException
, even if theCloneable
interface is implemented in a subclass.final Shape
getShape()
Get the object's current shape.
-
Constructor Details
-
DynamicObject
Constructor forDynamicObject
subclasses. Initializes the object with the provided shape. The shape must have been constructed with a layout class assignable from this class (i.e., the concrete subclass, a superclass thereof, includingDynamicObject
) and must not have any instance properties (but may have constant properties).Examples:
Shape shape =
Shape.newBuilder()
.build
(); DynamicObject myObject = new MyObject(shape);Shape shape =
Shape.newBuilder()
.layout
(MyObject.class, MethodHandles.lookup()).build
(); DynamicObject myObject = new MyObject(shape);- Parameters:
shape
- the initial shape of this object- Throws:
IllegalArgumentException
- if called with an illegal (incompatible) shape- Since:
- 19.0
-
-
Method Details
-
getShape
Get the object's current shape.- Since:
- 0.8 or earlier
- See Also:
-
clone
Theclone()
method is not supported byDynamicObject
at this point in time, so it always throwsCloneNotSupportedException
, even if theCloneable
interface is implemented in a subclass. Subclasses may however override this method and create a copy of this object by constructing a new object and copying any properties over manually.- Overrides:
clone
in classObject
- Throws:
CloneNotSupportedException
- Since:
- 20.2.0
-