2

I want to test one of the class in a big project, however I cannot modify any files of the project (thus static public arrays are not the case).

Beside this, I can only initialize the framework of project (to be tested) but again there is no reference to objects.

The only thing I have, my test class and framework is running on the same Java Virtual machine.

Therefore, I want to reach the instances of a single class from my test class.

How can I find them in Virtual machine?

Zegor V
  • 25
  • 7

3 Answers3

2

You can use some aop framework, like spring-aop, to track each time an object is created.

dan
  • 13,132
  • 3
  • 38
  • 49
  • Do you have any idea on their approach to track these objects? anyway, i will check it, thanks – Zegor V Aug 28 '12 at 13:05
  • See: **6.2.2. Declaring an aspect** and **6.2.3. Declaring a pointcut** from [link](http://static.springsource.org/spring/docs/2.5.x/reference/aop.html). You will declare a pointcut on the constructor of you inspected class (the class that you need to study) and this way the aspect method will be called each time the constructor is called. – dan Aug 28 '12 at 16:56
2

There's a couple of other questions like this on here to, the best answer seems to be this one: https://stackoverflow.com/a/1947200/1140456

Community
  • 1
  • 1
Capn Sparrow
  • 2,030
  • 2
  • 15
  • 32
0

In Java you cannot access an object that you have no reference to. You need to have someone update the framework to allow you to access the objects either directly or through other objects.

Mathias Schwarz
  • 7,099
  • 23
  • 28
  • I supposed there should be some advanced - but maybe inefficient - techniques to reach them. Because JVM knows the objects and their classes during run time. anyway, thanks. – Zegor V Aug 28 '12 at 13:02
  • You might be able to hack something through the debugging interfaces of the JVM but that is by no means part of the Java language. In principle objects may not even exist if you have no references to them. That is the whole idea behind automatic garbage collection. – Mathias Schwarz Aug 28 '12 at 19:38