Rename JDWPValue to JDWPHostValue.
[SquirrelJME.git] / modules / debug-jdwp / src / main / java / cc / squirreljme / jdwp / host / JDWPHostCommandSetStringReference.java
bloba0384f07f51d5f4ebbcb64235e64c3304e822bbb
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.JDWPCommandSetStringReference;
14 import cc.squirreljme.jdwp.JDWPErrorType;
15 import cc.squirreljme.jdwp.JDWPException;
16 import cc.squirreljme.jdwp.JDWPHostController;
17 import cc.squirreljme.jdwp.JDWPHostUtils;
18 import cc.squirreljme.jdwp.JDWPPacket;
19 import cc.squirreljme.jdwp.JDWPHostValue;
20 import cc.squirreljme.jdwp.host.views.JDWPViewObject;
21 import cc.squirreljme.jdwp.host.views.JDWPViewType;
23 /**
24 * String reference command set.
26 * @since 2021/03/20
28 public enum JDWPHostCommandSetStringReference
29 implements JDWPCommandHandler
31 /** Return the value of the string. */
32 VALUE(JDWPCommandSetStringReference.VALUE)
34 /**
35 * {@inheritDoc}
36 * @since 2021/03/20
38 @Override
39 public JDWPPacket execute(JDWPHostController __controller,
40 JDWPPacket __packet)
41 throws JDWPException
43 // Get the desired object
44 Object object = __controller.readObject(__packet, false);
46 // Is this the string type?
47 JDWPViewObject viewObject = __controller.viewObject();
48 JDWPViewType viewType = __controller.viewType();
49 Object type = viewObject.type(object);
50 if (type == null || !JDWPHostCommandSetStringReference._STRING.equals(
51 viewType.signature(type)))
52 throw JDWPErrorType.INVALID_STRING.toss(object,
53 System.identityHashCode(object));
55 // Locate the char field index
56 int charFieldDx = JDWPHostUtils.findFieldId(viewType, type,
57 JDWPHostCommandSetStringReference._STRING_CHARS, "[C");
59 // Is missing? We do not know how strings work then
60 if (charFieldDx < 0)
61 throw JDWPErrorType.NOT_IMPLEMENTED.toss(type,
62 charFieldDx);
64 // Load the character array
65 Object charArray;
66 try (JDWPHostValue value = __controller.value())
68 // Is this a valid field?
69 if (!viewObject.readValue(object, charFieldDx, value))
70 throw JDWPErrorType.INVALID_STRING.toss(object, charFieldDx);
72 // Missing?
73 charArray = value.get();
74 if (charArray == null)
75 throw JDWPErrorType.INVALID_STRING.toss(object, charFieldDx);
78 // How big is this string?
79 int strLen = viewObject.arrayLength(charArray);
80 if (strLen < 0)
81 throw JDWPErrorType.INVALID_STRING.toss(object, strLen);
83 // Load in characters
84 char[] chars = new char[strLen];
85 for (int i = 0; i < strLen; i++)
86 try (JDWPHostValue value = __controller.value())
88 // Is this value valid?
89 if (!viewObject.readArray(charArray, i, value))
90 value.set(JDWPHostCommandSetStringReference._BAD_CHAR);
92 // Try to map the value
93 Object raw = value.get();
94 if (raw instanceof Character)
95 chars[i] = (char)raw;
96 else if (raw instanceof Number)
97 chars[i] = (char)((Number)raw).intValue();
99 // Could not read it so treat as unknown
100 else
101 chars[i] = JDWPHostCommandSetStringReference._BAD_CHAR;
104 // Report string value
105 JDWPPacket rv = __controller.reply(
106 __packet.id(), JDWPErrorType.NO_ERROR);
107 rv.writeString(new String(chars));
108 return rv;
112 /* End. */
115 /** The base command. */
116 public final JDWPCommand command;
118 /** Badly represented character. */
119 private static final char _BAD_CHAR =
120 0xFFFD;
122 /** String character array. */
123 private static final String _STRING_CHARS =
124 "_chars";
126 /** String class. */
127 private static final String _STRING =
128 "Ljava/lang/String;";
130 /** The ID of the packet. */
131 public final int id;
134 * Returns the ID used.
136 * @param __id The ID used.
137 * @since 2021/03/20
139 JDWPHostCommandSetStringReference(JDWPCommand __id)
141 this.command = __id;
142 this.id = __id.debuggerId();
146 * {@inheritDoc}
147 * @since 2024/01/23
149 @Override
150 public final JDWPCommand command()
152 return this.command;
156 * {@inheritDoc}
157 * @since 2021/03/20
159 @Override
160 public final int debuggerId()
162 return this.id;