eppleton commented on code in PR #9789:
URL: https://github.com/apache/netbeans/pull/9789#discussion_r4510810117
##########
profiler/profiler.heapwalker/src/org/netbeans/modules/profiler/heapwalk/details/basic/StringDetailsProvider.java:
##########
@@ -101,4 +122,13 @@ private String getJDK9String(Heap heap, Instance instance,
String field, byte co
}
return null;
}
+
+ private int hiLoShift(boolean hi, Heap heap) {
+ if (hiShift == -1) {
+ JavaClass utf16Class =
heap.getJavaClassByName("java.lang.StringUTF16"); // NOI18N
+ hiShift = (Integer)
utf16Class.getValueOfStaticField("HI_BYTE_SHIFT"); // NOI18N
Review Comment:
Static fields of the JPDA heap still come back as strings:
`JavaClassImpl.getValueOfStaticField` returns `fieldValue.getValue()`
unchanged, so this cast fails as soon as a string isn't Latin-1. I added
`String euro = "5 €"` to `SampleMain` and called `getDetailsString` on it in
`StringDetailsProviderTest`, which gives `ClassCastException: class
java.lang.String cannot be cast to class java.lang.Integer` at `hiLoShift`. The
old `StringDecoder` had the same cast, so this isn't a regression. But the PR's
fix makes the Latin-1 case work only, and the new tests only use `"Now"`.
Applying the same primitive conversion as in `InstanceImpl` to static fields,
and adding a non-Latin-1 string to the tests, would cover it.
##########
profiler/debugger.jpda.heapwalk/src/org/netbeans/modules/debugger/jpda/heapwalk/InstanceImpl.java:
##########
@@ -161,7 +162,20 @@ public Object getValueOfField(String name) {
if (matchingFieldValue instanceof ObjectFieldValue) {
return ((ObjectFieldValue) matchingFieldValue).getInstance();
} else {
- return ((FieldValue) matchingFieldValue).getValue();
+ FieldValue fv = (FieldValue) matchingFieldValue;
+ String v = fv.getValue();
+ Type typ = fv.getField().getType();
+ return switch (typ.getName()) {
+ case "byte" -> Byte.valueOf(v);
+ case "short" -> Short.valueOf(v);
+ case "int" -> Integer.valueOf(v);
+ case "long" -> Long.valueOf(v);
+ case "float" -> Float.valueOf(v);
+ case "double" -> Double.valueOf(v);
+ case "char" -> v.charAt(0);
Review Comment:
JPDA formats `char` values with surrounding apostrophes
(`AbstractVariable.getValue`: `"'" + v + "'"`), which is also why
`PrimitiveArrayInstanceImpl.getValues()` strips them. So `charAt(0)` is always
`'`. I checked it locally by adding `char ch = 'x'` to `SampleMain` and
asserting on `getValueOfField("ch")` in `HeapImplTest`: `char field
expected:<x> but was:<'>`. Escaped values such as `'\n'` would need handling as
well.
##########
profiler/profiler.heapwalker/test/unit/src/org/netbeans/modules/profiler/heapwalk/details/basic/StringDetailsProviderTest.java:
##########
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.profiler.heapwalk.details.basic;
+
+import com.sun.jdi.AbsentInformationException;
+import java.io.File;
+import java.net.URI;
+import junit.framework.Test;
+import static junit.framework.TestCase.assertEquals;
+import static junit.framework.TestCase.assertNotNull;
+import org.netbeans.api.debugger.DebuggerManager;
+import org.netbeans.api.debugger.Session;
+import org.netbeans.api.debugger.jpda.CallStackFrame;
+import org.netbeans.api.debugger.jpda.ExceptionBreakpoint;
+import org.netbeans.api.debugger.jpda.JPDADebugger;
+import org.netbeans.api.debugger.jpda.JPDASupport;
+import org.netbeans.api.debugger.jpda.This;
+import org.netbeans.api.debugger.jpda.event.JPDABreakpointListener;
+import org.netbeans.junit.NbTestCase;
+import org.netbeans.lib.profiler.heap.Instance;
+import org.netbeans.lib.profiler.heap.JavaClass;
+import org.netbeans.modules.debugger.jpda.heapwalk.HeapImpl;
+import org.openide.util.Utilities;
+
+public class StringDetailsProviderTest extends NbTestCase {
+ private JPDASupport support;
+ private ExceptionBreakpoint breakPoint;
+
+
+ public StringDetailsProviderTest(String name) {
+ super(name);
+ }
+
+ public static Test suite() {
+ return JPDASupport.createTestSuite(StringDetailsProviderTest.class);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ breakPoint = ExceptionBreakpoint.create(
+ "java.lang.IllegalStateException",
+ ExceptionBreakpoint.TYPE_EXCEPTION_CAUGHT_UNCAUGHT
+ );
+ DebuggerManager.getDebuggerManager().addBreakpoint(breakPoint);
+
+ final URI cpUrl =
test.SampleMain.class.getProtectionDomain().getCodeSource().getLocation().toURI();
+ System.setProperty ("test.dir.src", test.SampleMain.findSourcePath());
+ support = JPDASupport.attach(new String[0], "test.SampleMain", new
String[0], new File[] {
+ Utilities.toFile(cpUrl)
+ });
+ }
+
+
+
+ /**
+ * Check fields of {@link test.SampleMain}.
+ */
+ public void testHeapRepresentationWhenDebuggerIsStopped() throws Throwable
{
+ final Session session = support.getDebugger().getSession();
+
+ HeapImpl heap = new HeapImpl(support.getDebugger());
+
+ assertOnBreakpoint((event) -> {
+ try {
+ CallStackFrame frame = event.getThread().getCallStack(0, 1)[0];
+
+ This self = frame.getThisVariable();
+ String value = self.getField("when").getValue();
+ assertEquals("\"Now\"", value);
+
+ Instance thiz = null;
+ for (JavaClass clazz : heap.getAllClasses()) {
+ if
(test.SampleMain.class.getName().equals(clazz.getName())) {
+ thiz = (Instance) clazz.getInstances().get(0);
+ break;
+ }
+ }
+ assertNotNull("This instance found in the heap", thiz);
+
+ Instance str = (Instance) thiz.getValueOfField("when");
+ assertNotNull("When field is found", str);
+ assertEquals("java.lang.String", str.getJavaClass().getName());
+
+ StringDetailsProvider p = new StringDetailsProvider();
+ String detail = p.getDetailsString("java.lang.String", str,
heap);
+ assertEquals("Now", detail);
+ } catch (AbsentInformationException ex) {
+ throw new AssertionError(ex);
+ }
+ });
+ }
+
+ private void assertOnBreakpoint(JPDABreakpointListener listener) {
+ int[] cnt = { 0 };
+ JPDABreakpointListener countingListener = (event) -> {
+ cnt[0]++;
+ listener.breakpointReached(event);
Review Comment:
Something I noticed while checking the above: exceptions thrown inside the
listener don't fail the test. With the `ClassCastException` above,
`StringDetailsProviderTest` still reported `tests="1" failures="0" errors="0"`,
because the debugger catches the exception and only logs it via
`Exceptions.printStackTrace`. In `HeapImplTest` a failing `assertEquals` did
surface, but only after ~300 s. Capturing any `Throwable` from the listener and
rethrowing it after `assertOnBreakpoint` finishes would make both tests
reliable.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists