How to search for instances of a class using eclipse? - java

I want to find instances of a class and modify some of the properties of one of the instance. But i cannot find a way to break in a block of code that references that class, so that it may appear in the variables view. What i'm thinking is to pause the jvm and the search for instances of MyClass but i can't find a tool to do this. Someone got an idea ?

I would open the call-hierarchie of the constructor of this class. Mark the constructor, left click, call hierarchie. That will give you a list wherever the object gets instantiated.

Related

Find all calls to interface implementers?

There are a large number of classes in this codebase which use a specific interface. However, picking a few at random, I've been unable to find one which is actually called anywhere; as such, I don't have a great idea of how to use it.
Is there a way in Eclipse to find every instance of any class which implements this interface?
In other words, suppose there exists an interface Interface, and classes ClassA, ClassB, ClassC, ..., ClassX, which all implement it. I want to see every point in the code where something like `ClassX obj = new ClassX(). Most of the classes I'm finding that implement this interface don't have any point where they're actually used; I assume they're for future use.
Open the interface class, hold Control and move your mouse to interface's name, select open implementation. That's the simplest and easiest way to do.
Yes, highlight the interface name and hit F4 or right click -> Open type hierarchy.
Update after OP's edit:
If you are using a framework that uses dependency injection like spring probably you don't find any reference because some of the implementations are defined in a xml file.
Also consider if some implementations are created and invoked via reflection.
Some classes might be loaded during runtime e.g. using reflection. To catch-them-all you can set a method entry breakpoint on the interface method. This is explained in this answer. That way all calls to implementation methods will suspend the JVM regardless of what is the object type.
Do note that unlike the line breakpoints the method breakpoints will really slow down the performance of the JVM.

Make 2 classes share the same instance of an object in Java

I was wondering if anyone could help me. I am stuck with something that seems simple. Say I initialize a Student object with the values of name, age and address in one class. How can I share THAT INSTANCE with another class, eg enable methods in the second class to make changes to the instance, thereby affecting the first class etc.
I was thinking of using JFrames where a new frame would popup and a button would affect values on the first frame.
Thank you
mutators can help you there. or, the singleton pattern, if you want to go "hard-knox".
it's a pattern that allows only one single instance of a class in a jvm to exist.
just look at this
in java if you pass reference of an object to a method and do any changes to that object property, it will change the actual object properties(call by reference)
also if you want to have only single object of a class that needs to be shared across other classes create a singleton pattern

JPanel with anonymous EventListener - why doesn't GC destroy listener?

I have been perusing the open source code of JMapViewer. If anyone else wishes to look at it, check the SVN.
In a nutshell, the main class is JMapViewer, which is an extension of a JPanel. There is another very important class called DefaultMapController which acts as a MouseListener for the main class.
The first weird thing I noticed is that the viewer has no references to the controller. The JMapViewer constructor instantiates an anonymous instance of the DefaultMapController, like this:
public JMapViewer() {
// other stuff
new DefaultMapController(this);
}
This seems to me to be a poor design choice, since the controller has tons of methods (options, toggles, etc - example shown below), which now can not be accessed at all, so what good are they?
public void setMovementMouseButton(int movementMouseButton) {
// changes which mouse button is used to move the map
}
The controller does have a reference to the viewer as shown in the first snippet above, which is how it is able to exercise control.
However, then I thought of something even weirder! If this anonymous instance of the listener has no references, why is it allowed to even survive? Shouldn't the GC destroy it quickly? Or is GC smart enough to know that a listener class which references a live JComponent must also stay alive to work properly, even if it has no name for some strange reason?
So, two real questions:
why does GC not destroy object?
is this indeed a poor design choice, or is there some way I'm unaware of to access the controller from the class which instantiates the viewer?
I want to contribute to this open source library, and my first idea for a change is to change the JMapViewer class to have a field referencing its controller, and to change the constructor to assign the currently anonymous controller to this new field. But, I want to make sure I'm not ignorantly missing something. I have searched the entire codebase for the text DefaultMapController, and it only occurs in its own class definitions, and in the anonymous instantiations in the JMapViewer constructors.
EDIT:
It does indeed appear that there is a way to access the anonymous listeners, by using the java.awt.Component method getMouseListeners(). So technically in my application I could search this collection for instances of DefaultMapController, and use that to access the methods I need to use to change the controller options.
To play devil's advocate though, if I go with original idea and give the map a reference of its controller, now I have a sort of circular reference (map knows of controller and controller knows of map). Is this a bad idea?
The abstract parent, JMapController, holds a reference to the JMapViewer passed there by the DefaultMapController constructor:
public DefaultMapController(JMapViewer map) {
super(map);
}
Addendum: The map reference held by the controller is used to (selectively) add up to three controller references to the map's EventListenerList, discussed here. Any one of these would preclude GC. At least one salutary design benefit is that a concrete JMapController need only implement available interfaces.
As suggested in this MVC outline, it would be unusual to give the view a reference to the controller. In contrast, there's nothing wrong with letting the controller register as a listener to the view, as suggested here.
Note that only the no-argument JMapViewer constructor installs a DefaultMapController. You can use the alternate constructor, as noted in comments at line 57-59 in revision 29113 of Demo.java. A complete example is examined here.
1) Everything you know is that, if and when the VM deems it to be appropriate, it will collect some or all of the dead objects. The GC is not required to do anything.
2) The best thing is to ask to the maintainer of the library. Anyway, as a general rule, I would not bother to change anything unless there's a good reason to, e.g. if it sensibly improves readability, and would rather focus myself on real problems.
3) Not sure if that's the case, but, when you serialize a JComponent, you also serialize all of its fields. And you do not want to serialize a lot of unused stuff.

eclipse - find references only in specific class and all subclasses of that class

I need to find the references to external method say Foo.getInstance() in specific type hierarchy, basically i want to restrict the search results to specific class and all subclasses, i am not interested in all references. Is it possible to do with eclipse or any custom plugin? Thanks
You can do this creating a working set with only the files holding your hierarchy.
Then CTRL+H and Java Search with scope "Working Set".
If the classes are all within a package you can select that in Project Explorer then perform Java Search with scope "Selected".
Working sets are awesome
I have identified a workaround. Requires viewing eclipse search results(in "show as list" mode).
For example ClassA has several subclasses (ClassB,C..Z) which inherits methodA defined on class A and calls them.
Perform method reference in eclipse and change the view to "show as list" using triangle/View menu appearing on top-right corner. This will list method signatures containing super-class/sub-class references so you can easily pick or drop from the search result.
If there are too many results to filter through you can Copy this onto file editor and use search keyword of subclass name (ClassB) and each line containing classB is a potential result which calls methodA on classB object.
Note: If method signature uses classA object reference and within body it type-casts to classB then you need to pick that entry and not drop it. I'd also recommend working set approach mentioned above. This is alternate approach to the question posted.

Changing a method of an instance from another class in java

Im trying to change the method of just one instance from another class. Is this possible, and if so how can I do it?
Im trying to rewrite the entire method durring runtime. I only want the method to be changed for one instance of the class, and all the other instance's methods should stay the same. I hope that clears up some confusion
No this is not possible.
Method definitions are stored by class, not instance (and are immutable anyway). One thing you can do is to store a callable object per instance and call that.
I think you need to provide more details and perhaps some sample code to get a sufficient answer. That being said, if class Foo extends class Bar, class Foo can override a method of class Bar. Is that what you're trying to do?

Categories

Resources