How to change references of a class to another class in eclipse - java

I have created a new class out of an old class with different method implementations. Now, I want to update all the references of that old class to this new class. Is there anyway to do this automatically in eclipse?
I can actually do a rename(with update references) to this old class and copy the new code into this but the entire code being in source control makes my life a tougher with this process.
I am just wondering if there is an easy way to do this and have both classes at the end and have the references updated to the new class.

Using the refactoring functionality should do the trick. Refactor the old class into the new one:
Right click on the class name, Refactor > Rename > Put the new name of the class > Press Enter.
This should do it.

I don't know of any existing refactoring that does exactly that, but it's simple enough to:
Temporarily rename your new class to something else.
Do a refactor/rename of the old class to the new class name.
Manually (non-refactor) rename the old class back to its original name.
Manually (non-refactor) rename the new class back to its original name.
That way you get the power of the rename refactoring to help you update the references, but in the end, the classes are still the same as they were.

right click your class file select refactor then rename it what ever you want ..

My guess is that person asking the question is using something like Perforce or ClearCase where temporarily renaming files is a pain.
The best bet is to do a simple Eclipse global file search and replace.
Click on Search -> File
and click on the Replace button on the bottom.

Related

Java IntelliJ: Change existing class usages to a new class

In Java Spring Boot using IntelliJ, how do I refactor existing class usages to a different class? I don't want to rename an existing class, but migrate to a new class. For example, change Fruit to Automobile class,
This reference does not help, https://www.jetbrains.com/help/idea/rename-refactorings.html#rename_class_example
The problem, as you know, is that all usages of your class are linked to it in IntelliJ. You can solve this by relinking all the usages to the new class.
Say you have ClassA and ClassB. You want to move all usages of ClassA to use ClassB.
Step 1 - rename ClassA to ClassATemp. Make sure to not use Shift-F6 for this, i.e. don't use IntelliJ's refactoring rename. Just change the text. Do the same for the filename. Now you have successfully changed ClassA to ClassATemp, and your code is completely broken. Hurray!
Step 2 - rename ClassB to ClassA. During this step, links to ClassA will "re-establish", but with the new class. You may need to reimport everything to the new package, or alternatively change the package as well (might be more of a hassle).
Step 3 - re-rename the newly renamed ClassA back to ClassB. Make sure to do this using IntelliJ's refactor. All links will now follow.
Final step - re-rename ClassATemp to ClassA.
You can try to use "Type migration" refactoring, see this
On the toolbar:
Edit -> Find -> Replace in Files...

Matlab's Tab completion for classes

I have the following project on my hands, and I am banging my head to the wall for this "little" caveat.
In the project Matlab classes are used. Due to the structure of the project, I have the folders structured as follows:
+a/+b/+c/
Then, on c there are a bunch of other subfolders declared:
+a/+b/+c/+d
+a/+b/+c/+e
+a/+b/+c/+f
+a/+b/+c/+g
On one of those folders (let's sat +e) is where I am implementing my .m classes, which contain properties, as well as Static methods:
+a/+b/+c/+e/my_class_1.m
+a/+b/+c/+e/my_class_2.m
+a/+b/+c/+e/my_class_3.m
+a/+b/+c/+e/my_class_4.m
So let's take a look into my startup.m file:
% add the path to the class
addpath(genpath('<previous_path_to_a>'));
% import the module
import a.b.c.e.*
What I would like to do now is to be able to press my_class_1. + Tab on the Matlab prompt and be shown the properties and methods available for that given class.
I know I could just use Matlab's methods() function for this, or the properties() one, but it would be really nice to be able to just type:
help my_class_1. + Tab
to be able to select the given method and see it's documentation.
Otherwise I have, as I said, to call methods() first to see what the names of the class's own methods are for this particular class, to be able to access its documentation.
Edit:
Of course, what does work is typing the whole path, in my example:
help a.b.c.e.my_class_1. + Tab
The question is how to get rid off those previously annoying a.b.c.e.
Hmm, looks like you're right. Tab completion of methods and properties only seems to work with fully qualified class names, even if the class is on the path and imported.
I don't know of a workaround. If I were you, I'd enter an enhancement request with MathWorks for that. It would seem like an obvious and nice thing to have.

Multiple classes in the same eclipse window

I'm reading Thinking in Java and it's frustrating to declare each class in a separate window in Eclipse, as the examples often contain 6-7 very simple classes.
I can just make a new class file, make one class public in this class file and the others with default access, but I don't know what should be the class' name I created. For example, I do the following:
New -> Class -> and then I must choose a class name, let's say it's Dog.
Now, in this file, I have this:
public class Dog {
}
class Cat {
}
But since I have two classes, it's a little weird to have this class file (I don't know if it's the right word here?) to be named Dog in Eclipse (The name in the src folder).
Is there a better way to declare multiple classes in the same window(?) in Eclipse?
A java file can have at most only one public class into it. And the name of that file should be same as of that public class.
I would say the frustration are not genuine because:
This is the how Java is designed and makes all sense to define each
class in a separate file. (Unless you want to write your own compiler)
You may want to use some shortcuts e.g.
Cntrl + Shift + R` to search a class
Alt + Shift + R to rename
You can update Eclipse to use shortcut for switching within classes.
What you're doing isn't going to compile. Each top level java class must be declared in a file with the same name. It will give you an error "Cat must be declared in its own file" or something like that. If you really want to, you can put the Cat class inside of the Dog class, which is called an inner class. However since they aren't related classes you shouldn't do that. Just declare each one in its own file.
Keep each class in it's own position. If your class is small and data can be exposed you can consider using nested (inner) class.
By the way, in Eclipse you can show multiple class at same time. Just drag you file title to some place.
To actually answer your question, rather than leave a bunch of comments stating why you shouldn't (which you seem to understand already), no. There isn't really a better way to do what you want. I don't know if it will compile or not (I seem to recall seeing that in the past in Java 5), but KyleM seems to think not so we'll go with that.
Short answer: no, there is not a better way to declare multiple classes in the same file.
(I don't want to suggest inner classes because that is kind of complicated for someone just starting java, as your post suggests).
Don't mix Eclipse window with files, you can understand a .java file as a container for a java class. It's the standard way and it would help you to have a more clear project when it becomes bigger.
You can have more information about this here
If you want 2 classes in the screen you can split the eclipse editor window by dragging the opened tab file and drop it on the tabs zone.
Unfortunately you do have to do this the long way, as everyone else has suggested / insisted. If the problem is a matter of clicking around through tabs, though, eclipse does allow you to drag tabs into new windows on the screen, which lets you view potentially all of them at once.
You also end up with an "overview" of the classes in the file explorer on the left of the screen, if that's more along the lines of what you're looking for.
Good luck (:

How to refactor a static inner class to a top level class in Eclipse?

I am having trouble finding the correct refactor option for the following scenario:
I have code like this (in Outer.java):
public class Outer {
// ... class stuff
public static class Inner {
// ... inner class stuff
}
}
I am looking for a way to select Inner, and have it converted to a top level class, in it's own .java source file. I know this is pretty easy to do manually with copy/paste etc., but the inner class is referenced in a lot of places, and I would like the refactor option to handle the change everywhere it is referenced.
I have tried the option Refactor -> Extract Class... but that does something weird with a field called data that I don't quite understand.
Eclipse version is 3.5.1 Galileo.
How do I refactor a static inner class to be a top level class?
Edit: Can't believe I overlooked the option to do this. Thanks for all your correct answers, +1 to you all. I'll still need to accept an answer, so if there is any more useful info, e.g. gotchas with the script, there is still a purpose to leaving an answer.
This is so easy I can't believe I missed it:
With the cursor anywhere within the inner type, right click and select:
Refactor -> Convert Member Type to Top Level...
(There is no shortcut in the default settings)
This automatically extracts the inner type, and places it in it's own file, in the same package and directory as the outer type.
Update
In later versions of Eclipse this refactoring has been renamed "Move Type to New File"
For completeness, version 4.x of Eclipse has changed terminology and now they call that operation as Move Type to New File...
Extract Class is not what you want.
I just did this using Eclipse 3.4.2
Select Inner
Right click 'Refactor->Convert Member Type To Top Level...'
I have 3.4.1, and if I right-click on Inner, >refactor, I have the option "Covert member Type to top level..", which does exactly what you want.
Use Refactor -> Convert Member to Top Level Type
For Intellj2018.1.
right click in the inner class
Refactor
Move
Move to upper level

Can eclipse extract a second class in class file to its own file

I often refactor code first by creating an inner class inside the class I'm working on--When I'm done, I move the entire thing into a new class file. This makes refactoring code into the new class extremely easy because A) I'm only dealing with a single file, and B) I don't create new files until I have a pretty good idea of the name/names (Sometimes it ends up as more than one class).
Is there any way Eclipse can help me with the final move? I should just be able to tell it what package I want the class in, it can figure out the filename from the class name and the directory from the package.
This seems like a trivial refactor and really obvious, but I can't figure out the keystrokes/gestures/whatever to make it happen. I've tried dragging, menus, context menus, and browsing through the keyboard shortcuts.
Anyone know this one?
[edit] These are already "Top Level" classes in this file, not inner classes, and "Move" doesn't seem to want to create a new class for me. This is the hard way that I usually do it--involves going out, creating an empty class, coming back and moving. I would like to do the whole thing in a single step.
I'm sorry I gave the wrong answer before. I rechecked, and it didn't do quite want you want. I did find a solution for you though, again, in 3.4.
Highlight the class, do a copy CTRL-C or cut CTRL-X, click on the package you want the class do go into, and do a paste, CTRL-V. Eclipse will auto generate the class for you.
Convert Member Type to Top Level doesn't quite work. Doing that will create a field of the outer class and generate a constructor that takes the outer class as a parameter.
In Eclipse 3.6, you can do: Refactor -> Move type to new file
Right-click the class name (in the source code) and choose Refactor -> Convert Member Type to Top Level. It doesn't let you choose the package, though.
For IntelliJ IDEA / Android Studio:
Refactor -> Move -> Move inner class MyInnerClass to upper level
Can be done in 2 refactorings :
Convert Member type to top level
Move

Categories

Resources