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 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
;
24 * String reference command set.
28 public enum JDWPHostCommandSetStringReference
29 implements JDWPCommandHandler
31 /** Return the value of the string. */
32 VALUE(JDWPCommandSetStringReference
.VALUE
)
39 public JDWPPacket
execute(JDWPHostController __controller
,
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
61 throw JDWPErrorType
.NOT_IMPLEMENTED
.toss(type
,
64 // Load the character array
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
);
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
);
81 throw JDWPErrorType
.INVALID_STRING
.toss(object
, strLen
);
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
)
96 else if (raw
instanceof Number
)
97 chars
[i
] = (char)((Number
)raw
).intValue();
99 // Could not read it so treat as unknown
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
));
115 /** The base command. */
116 public final JDWPCommand command
;
118 /** Badly represented character. */
119 private static final char _BAD_CHAR
=
122 /** String character array. */
123 private static final String _STRING_CHARS
=
127 private static final String _STRING
=
128 "Ljava/lang/String;";
130 /** The ID of the packet. */
134 * Returns the ID used.
136 * @param __id The ID used.
139 JDWPHostCommandSetStringReference(JDWPCommand __id
)
142 this.id
= __id
.debuggerId();
150 public final JDWPCommand
command()
160 public final int debuggerId()