英文:
OQL in VisualVM v1.4.4 - Get A Class's Field Names
问题
我想在VisualVM(v1.4.4)中执行一个OQL查询,以检索对象的(非静态)字段名称。
OQL文档描述了heap.findClass(className)
。这将返回一个对象,其中包括一个fields
属性(字段名称数组)。
当我执行以下OQL时...
heap.findClass('java.io.ByteArrayInputStream').fields;
它返回一个包含4个字段对象的数组(ByteArrayInputStream
有4个字段 - buf
、count
、mark
和pos
- 我假设这些是返回的内容):
org.netbeans.lib.profiler.heap.HprofField@56de8c
org.netbeans.lib.profiler.heap.HprofField@56de95
org.netbeans.lib.profiler.heap.HprofField@56de9e
org.netbeans.lib.profiler.heap.HprofField@56dea7
如果我尝试操作这个数组,例如访问每个字段的name
和signature
属性(如OQL文档中所描述的),我得不到结果。我甚至无法获取数组的长度。例如:
heap.findClass('java.io.ByteArrayInputStream').fields.length;
和:
heap.findClass('java.io.ByteArrayInputStream').fields[0];
上述两个语句都返回<no results>
。
我做错了什么? 可能是一些基本的事情。我对JavaScript不是很熟悉,对于VisualVM中的数据显示也不太了解。
英文:
I would like to execute an OQL query in VisualVM (v1.4.4) to retrieve the (non-static) field names for an object.
The OQL documentation describes heap.findClass(className)
. This returns an object which includes a fields
property (an array of field names).
When I execute the following OQL...
heap.findClass('java.io.ByteArrayInputStream').fields;
... it returns an array of 4 field objects (ByteArrayInputStream
has 4 fields - buf
, count
, mark
, and pos
- I am assuming these are what are being returned):
org.netbeans.lib.profiler.heap.HprofField@56de8c
org.netbeans.lib.profiler.heap.HprofField@56de95
org.netbeans.lib.profiler.heap.HprofField@56de9e
org.netbeans.lib.profiler.heap.HprofField@56dea7
If I then try to manipulate this array, for example to access each field's name
and signature
properties (as described in the OQL docs), I get no results. I can't even get the length of the array. For example:
heap.findClass('java.io.ByteArrayInputStream').fields.length;
and:
heap.findClass('java.io.ByteArrayInputStream').fields[0];
Both of the above statements return <no results>
.
What am I doing wrong? Probably something basic. I not very familiar with JavaScript - or with how data is displayed in VisualVM, for that matter.
答案1
得分: 4
你需要使用 map() 函数。以下是检索 ByteArrayInputStream
类的字段名称的 OQL:
select map(heap.findClass('java.io.ByteArrayInputStream').fields, 'it.name')
英文:
You need to use map() function. The following OQL retrieves the field names of ByteArrayInputStream
class:
select map(heap.findClass('java.io.ByteArrayInputStream').fields, 'it.name')
答案2
得分: 2
基于@Tomas非常有帮助的答案,我还可以在OQL中执行类似以下的操作,使用回调而不是表达式字符串:
map(heap.findClass('java.io.ByteArrayInputStream').fields, function (it) {
var res = '';
res += toHtml(it.name) + " : " + toHtml(it.signature);
return res + "<br>";
});
上面的示例很简单,但它打开了更多的可能性。
他的答案还让我意识到我犯了错误:OQL使用JavaScript的表达式语言,并不完全与JavaScript相同。
英文:
Just to add to the very helpful answer from @Tomas - which I have accepted.
Based on his insight, I can also now do things like this in OQL - using a callback instead of an expression string:
map(heap.findClass('java.io.ByteArrayInputStream').fields, function (it) {
var res = '';
res += toHtml(it.name) + " : " + toHtml(it.signature);
return res + "<br>";
});
The above example is trivial, but it opens up more possibilities.
His answer also made me realize where I was going wrong: OQL uses JavaScript expression language - not the exactly the same as JavaScript.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论