2

I am wondering how one can search for all primitive float values that match a certain number.

When doing something like:

select n from java.lang.Float n where n.value == 1.00

Only the Float class instances are being found. The application I am exploring is using different wrappers than just Float (eg. Vectors) that use primitive float values as fields for which I need to search.

How would I accomplish this?

The following returns a "float is not found error":

select n from float n where n.value == 1.00
Mark
  • 28,783
  • 8
  • 63
  • 92
Tom
  • 8,536
  • 31
  • 133
  • 232

1 Answers1

3

A primitive value exists only as a field in the structure it's a part of (or directly on the stack). Because it isn't an object, it can't be referenced. Try something like the following:

select v from Vector v where v.x == 1.0 || v.y == 1.0 || v.z == 1.0

If you want to examine all float fields in all objects, it should be possible to use OQL's reflection capabilities to do so, using something like the following:

select filter(heap.objects(), function(it) {
  var cls = classof(it);
  while (cls) {
    for (var i = 0; i < cls.fields.length; i++) {
      var field = cls.fields[i];
      if (field.signature == 'F' && it[field.name] == 0.0)
        return true;
      }
    cls = cls.superclass;
  }
  return false;
})

However, while this works correctly using jhat, it doesn't work in my version of VisualVM (1.6.0_22), because cls.fields seems to improperly return a list of static fields rather than instance fields.

It's also very slow, taking 10 seconds to search a 1MB heap dump. It's probably possible to optimize the code and also speed things up by only searching a limited set of classes.

Miles
  • 31,360
  • 7
  • 64
  • 74
  • the problem here is that I do not know what classes use it. Is there no way to search through ALL classes and ALL fields that are primitive floats? – Tom Feb 16 '11 at 21:20
  • that approach is exactly what I need. Unfortunately I cannot make it work though, I think your code has some problems. I am trying to solve it but no luck so far. Will accept your answer when I get it working. Why do you get the class of the object? That is probably why it only returns static fields (objects have non-static fields). – Tom Feb 16 '11 at 22:38