Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eclipse 3.4 (ganymede) package collision with type

We have a package that ends with exception e.g.

package a.b.c.exception;

Our code base had no issues up till eclipse 3.3, however when we shifted to eclipse 3.4, it started giving errors related to this package:

"The package a.b.c.exception collides with a type"

When I refactor the package name to a.b.c.exceptions, there are no issues. Is this due to a bug in eclipse 3.4 or is there some setting to rectify this behavior?

like image 636
Monis Iqbal Avatar asked Dec 13 '22 22:12

Monis Iqbal


2 Answers

It's because you have a class named exception (with a lower case "e") in the a.b.c package and a package named a.b.c.exception.

It causes a name collision because if you have the code a.b.c.exception.doSomething(); - does that mean you want to call the static doSomething() method in the a.b.c.exception class? Or does it mean there's a class called a.b.c.exception.doSomething that you're trying to invoke the constructor of?

Stick with the Java naming conventions - packages all lowercase, classes starting with an uppercase and camel-case after - and you'll never see this problem.

==========EDIT==========

This is the only legitimate reason this error should be showing up...

It doesn't have to be in your project directly, it could be in another project or library that your project depends on. This should show you any occurrences of the class anywhere on the build path or your project : Hit the Flashlight looking button in the Eclipse toolbar -> Choose 'Java Search' -> enter a.b.c.exception in search field -> select 'Case sensitive' -> select 'Type' in 'Search For' -> make sure all options are selected for 'Search In'.

Are you using any tools that generate classes? Could they be putting them into the build directory of your project? When you see the error, if you go to the project's build directory, and go down into the a/b/c/ directory do you see a .class file for 'exception'?

Of course Eclipse in general could have a bug (though I'd expect there would be a bug report in Eclipse 3.4 and you'd be able to find more complaints if it was...), your Eclipse install could be broken in some way (Can anyone else open your project in Eclipse 3.4? Could you do a clean Eclipse 3.4 install in another directory? Does the error appear there?), or your project could be messed up in some way (Create a new project with no dependencies other than the JDK, create the a.b.c.exception package in your new project, create a class in your project to import a.b.c.exception.*; and see if the error occurs.).

like image 127
Nate Avatar answered Dec 26 '22 20:12

Nate


In Java you can not have a class name that is the same as a package name.

That means the JDT package must have enforced that rule only in 3.4

See bug 63668 for instance.


As Nate comments:

A class named Exception won't prevent you from creating package exception.
Case matters.

Also remember the full name of a class includes the package it's in.
So a.b.SomeClass (class name) is different from x.y.SomeClass (package name).
There would be no name collision here.

The class name and the package name have to match in both case and package to cause this error.

See his more accurate answer.

like image 34
VonC Avatar answered Dec 26 '22 21:12

VonC