Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse - How to "Change package declaration to ...." across an entire project

I've just imported a large amount of source code into Eclipse and basically the package name is no longer valid (the code has been moved folders). Is there a way to select all the source code in the Package Explorer and hit a hotkey so that all package declarations are correctly resolved for me? I know you can do this with imports by selecting the source and hitting ctl-shift-o, but is also possible for the package declaration?

Update: Refactoring the packages doesn't work as I don't want to change the name or location of the packages, I just need to adjust the package declaration in the Java source code.

like image 925
Stephane Grenier Avatar asked Jul 28 '11 21:07

Stephane Grenier


People also ask

How do I move a package from one package to another in Eclipse?

Right-click the package you want to move, and select " Refactor/Rename..." -- NOT " Move "! Modify the name of the package to reflect its desired new position in the package hierarchy, e.g. Prepend any new parent package name(s) if you are moving the package to downwards in the package hierarchy.

How do I arrange packages in Eclipse?

Click the little down pointing triangle in the package manager and go to "package presentation". From there select "hierarchal" and that should take care of it.

What is the difference between package and project in Eclipse?

An Eclipse project has nothing to do with Java. It is a feature of Eclipse to organize and configure your different projects. A Java package is a language feature of Java. You can use them to structure your project and control visibility between different classes.


2 Answers

If the package declarations are no longer valid, then all such invalid declarations would appear in the Problems view in Eclipse. If you do not see this view, you can open it from Window-> Show View -> Other... -> Problems (under the General tab).

You can filter on problems in the Problems view and correct easily correctable ones, by choosing the Quick fix option in the context menu (available on a right-click). In your case you should see something similar to the screenshot posted below:

Quick fix for incorrect packages

Applying the quick fix options is trivial, as long as you know which one is correct - you would either have to change the package declaration in the class, or the location of the class itself. Unfortunately there is no option to fix the issue across multiple units at one go; you will have to apply the quick fix for every problem.

If you want to filter on problems of only this variety, consider configuring the Problems view to show all errors that have the text content "does not match the expected package" in the error text, as demonstrated in the following screenshots:

Configure Problem Contents

Eclipse Configure Package Problems

like image 97
Vineet Reynolds Avatar answered Oct 18 '22 05:10

Vineet Reynolds


For this particular problem (which usually comes with auto generated artifact files), I found a neat solution.

So if the issue is that your package declarations is "package abc;" in 200 files, and you want it to be "package com.aa.bb.cc.abc;"

Then in eclipse, Search->File for "package abc;" in required folder or pkg or whole workspace. Don't select Search option but select "Replace" and then put "package com.aa.bb.cc.abc;" when it asks for the replacement after search. Should do the trick.

like image 33
Manish Avatar answered Oct 18 '22 04:10

Manish