OQL在VisualVM v1.4.4中 – 获取类的字段名称

huangapple go评论74阅读模式
英文:

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个字段 - bufcountmarkpos - 我假设这些是返回的内容):

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

如果我尝试操作这个数组,例如访问每个字段的namesignature属性(如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(&#39;java.io.ByteArrayInputStream&#39;).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(&#39;java.io.ByteArrayInputStream&#39;).fields.length;

and:

heap.findClass(&#39;java.io.ByteArrayInputStream&#39;).fields[0];

Both of the above statements return &lt;no results&gt;.

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(&#39;java.io.ByteArrayInputStream&#39;).fields, &#39;it.name&#39;)

答案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(&#39;java.io.ByteArrayInputStream&#39;).fields, function (it) { 
  var res = &#39;&#39;; 
  res += toHtml(it.name) + &quot; : &quot; + toHtml(it.signature); 
  return res + &quot;&lt;br&gt;&quot;; 
});

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.

huangapple
  • 本文由 发表于 2020年1月4日 01:15:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定