1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.vm
.springcoat
;
12 import cc
.squirreljme
.vm
.springcoat
.brackets
.RefLinkHolder
;
13 import cc
.squirreljme
.vm
.springcoat
.exceptions
.SpringVirtualMachineException
;
14 import java
.lang
.ref
.Reference
;
15 import java
.lang
.ref
.WeakReference
;
18 * This is a representation of an object within the virtual machine.
22 public final class SpringSimpleObject
23 implements SpringObject
25 /** The type of object this is. */
26 protected final SpringClass type
;
28 /** The monitor for this object, every object has one. */
29 protected final SpringMonitor monitor
=
32 /** The reference link holder. */
33 protected final RefLinkHolder refLink
=
36 /** Field storage in the class. */
37 final SpringFieldStorage
[] _fields
;
39 /** String representation. */
40 private Reference
<String
> _string
;
43 * Initializes the object.
45 * @param __cl The class of the object.
46 * @throws NullPointerException On null arguments.
49 public SpringSimpleObject(SpringClass __cl
)
50 throws NullPointerException
53 throw new NullPointerException("NARG");
59 SpringFieldStorage
[] fields
;
60 this._fields
= (fields
=
61 new SpringFieldStorage
[(n
= __cl
.instanceFieldCount())]);
63 // Initialize variable for all fields
65 for (SpringField f
: __cl
.fieldTable())
68 fields
[thisDx
] = new SpringFieldStorage(f
, f
.index
);
73 * Returns the field by the given field.
75 * @param __f The field to get.
76 * @return The storage for the field.
77 * @throws NullPointerException On null arguments.
80 public final SpringFieldStorage
fieldByField(SpringField __f
)
81 throws NullPointerException
84 throw new NullPointerException("NARG");
86 return this.fieldByIndex(__f
.index());
90 * Returns the field by the given index.
92 * @param __dx The field to get.
93 * @return The storage for the field by the specified index.
96 public final SpringFieldStorage
fieldByIndex(int __dx
)
100 return this._fields
[__dx
];
102 catch (IndexOutOfBoundsException e
)
104 throw new SpringVirtualMachineException(
105 "Invalid field index: " + __dx
, e
);
110 * Returns the field by the name and type.
112 * @param __static Is this field static?
113 * @param __name The name of the field.
114 * @param __type The type of the field.
115 * @return The storage for the field.
118 public final SpringFieldStorage
fieldByNameAndType(boolean __static
,
119 String __name
, String __type
)
121 return this.fieldByField(
122 this.type().lookupField(__static
, __name
, __type
));
130 public final SpringMonitor
monitor()
140 public RefLinkHolder
refLink()
150 public final String
toString()
152 Reference
<String
> ref
= this._string
;
155 if (ref
== null || null == (rv
= ref
.get()))
156 this._string
= new WeakReference
<>((rv
= String
.format(
157 "%s@%08x", this.type
.name(), System
.identityHashCode(this))));
167 public final SpringClass
type()