Implement get of command handlers.
[SquirrelJME.git] / modules / debug-jdwp / src / main / java / cc / squirreljme / jdwp / host / JDWPHostCommandSetObjectReference.java
blob05247bfee48c43f7b58afd81be3acba9e05eb109
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.jdwp.host;
12 import cc.squirreljme.jdwp.JDWPCommand;
13 import cc.squirreljme.jdwp.JDWPCommandSetObjectReference;
14 import cc.squirreljme.jdwp.JDWPErrorType;
15 import cc.squirreljme.jdwp.JDWPException;
16 import cc.squirreljme.jdwp.JDWPHostController;
17 import cc.squirreljme.jdwp.JDWPPacket;
18 import cc.squirreljme.jdwp.JDWPValue;
19 import cc.squirreljme.jdwp.JDWPValueTag;
20 import cc.squirreljme.jdwp.host.views.JDWPViewObject;
21 import cc.squirreljme.jdwp.host.views.JDWPViewType;
23 /**
24 * Object reference command set.
26 * @since 2021/03/14
28 public enum JDWPHostCommandSetObjectReference
29 implements JDWPCommandHandler
31 /** The type that an object is. */
32 REFERENCE_TYPE(JDWPCommandSetObjectReference.REFERENCE_TYPE)
34 /**
35 * {@inheritDoc}
36 * @since 2021/03/14
38 @Override
39 public JDWPPacket execute(JDWPHostController __controller,
40 JDWPPacket __packet)
41 throws JDWPException
43 // Obtain the object and the type of that object
44 Object object = __controller.readObject(__packet, false);
45 Object type = (__controller.viewType().isValid(object) ?
46 object : __controller.viewObject().type(object));
48 // Register it for future reference
49 __controller.getState().items.put(object);
50 if (type != null)
51 __controller.getState().items.put(type);
53 JDWPPacket rv = __controller.reply(
54 __packet.id(), JDWPErrorType.NO_ERROR);
56 // Write the details of this class
57 __controller.writeTaggedId(rv, type);
59 return rv;
63 /** Get field values. */
64 GET_VALUES(JDWPCommandSetObjectReference.GET_VALUES)
66 /**
67 * {@inheritDoc}
68 * @since 2021/03/17
70 @Override
71 public JDWPPacket execute(JDWPHostController __controller,
72 JDWPPacket __packet)
73 throws JDWPException
75 JDWPViewObject viewObject = __controller.viewObject();
76 JDWPViewType viewType = __controller.viewType();
78 // The type is needed to ensure the field class is valid
79 Object object = __controller.readObject(__packet, false);
80 Object type = viewObject.type(object);
82 // Read in all field indexes and check for their validity
83 int numFields = __packet.readInt();
84 int[] fields = new int[numFields];
85 for (int i = 0; i < numFields; i++)
87 int fieldDx = __packet.readId();
88 if (!viewType.isValidField(type, fieldDx))
89 throw JDWPErrorType.INVALID_FIELD_ID.toss(type, fieldDx);
91 fields[i] = fieldDx;
94 JDWPPacket rv = __controller.reply(
95 __packet.id(), JDWPErrorType.NO_ERROR);
97 // Write field mappings
98 rv.writeInt(numFields);
99 for (int i = 0; i < numFields; i++)
100 try (JDWPValue value = __controller.value())
102 // Determine the field type and its tag
103 String fieldSig = viewType.fieldSignature(type, fields[i]);
104 JDWPValueTag tag = JDWPValueTag.fromSignature(fieldSig);
106 // Read the field value, fallback if not valid
107 if (!viewObject.readValue(object, fields[i], value))
108 value.set(tag.defaultValue);
110 // Always write as tagged value
111 __controller.writeValue(rv, value, tag, false);
113 // Store object for later use
114 if (value.get() != null && tag.isObject)
115 __controller.getState().items.put(value.get());
118 return rv;
122 /** Is this object garbage collected? */
123 IS_COLLECTED(JDWPCommandSetObjectReference.IS_COLLECTED)
126 * {@inheritDoc}
127 * @since 2021/03/14
129 @Override
130 public JDWPPacket execute(JDWPHostController __controller,
131 JDWPPacket __packet)
132 throws JDWPException
134 // Read the value but do nothing for it
135 __controller.readObject(__packet, false);
137 // If we still know about this object it was not GCed
138 JDWPPacket rv = __controller.reply(
139 __packet.id(), JDWPErrorType.NO_ERROR);
140 rv.writeBoolean(false);
142 return rv;
146 /* End. */
149 /** The base command. */
150 public final JDWPCommand command;
152 /** The ID of the packet. */
153 public final int id;
156 * Returns the ID used.
158 * @param __id The ID used.
159 * @since 2021/03/14
161 JDWPHostCommandSetObjectReference(JDWPCommand __id)
163 this.command = __id;
164 this.id = __id.debuggerId();
168 * {@inheritDoc}
169 * @since 2024/01/23
171 @Override
172 public final JDWPCommand command()
174 return this.command;
178 * {@inheritDoc}
179 * @since 2021/03/14
181 @Override
182 public final int debuggerId()
184 return this.id;