eclipse 3.4 (ganymede)包与类型的冲突。

时间:2023-02-05 11:45:14

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:

我们的代码库直到eclipse 3.3才出现问题,但是当我们转移到eclipse 3.4时,它开始出现与这个包相关的错误:

"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?

当我将包名称重构为a.b.c时。例外,没有问题。这是由于eclipse 3.4中的bug,还是有一些设置来纠正这种行为?

6 个解决方案

#1


5  

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.

这是因为在a.b中有一个名为exception的类(小写“e”)。c包和一个名为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?

它会导致名称冲突,因为如果你有代码a.b.c。exception.dosomething ();这是否意味着您想在a.b.c中调用静态doSomething()方法。异常类?或者它是否意味着有一个类叫做a.b.c。exception。你要调用的构造函数?

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.

坚持使用Java命名约定——所有小写的包,从大写字母开始的类,然后是camel-case——你将永远不会看到这个问题。

==========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'.

它不需要直接在您的项目中,它可以在您的项目依赖的另一个项目或库中。这应该会显示在构建路径或项目中任何地方出现的任何类:在Eclipse工具栏中点击“闪光”按钮——>选择“Java搜索”——>进入a.b.c。搜索字段的异常->选择“区分大小写”->在“搜索”中选择“类型”->确保所有选项都被选中进行“搜索”。

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'?

您是否使用了生成类的工具?他们是否可以将它们放到项目的构建目录中?当您看到错误时,如果您转到项目的构建目录,并进入到a/b/c/目录中,您会看到一个“异常”的.class文件吗?

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.).

当然,Eclipse通常会有一个bug(尽管我预计Eclipse 3.4中会有一个bug报告,如果它是……,您将会发现更多的抱怨),您的Eclipse安装可能会以某种方式被破坏(其他人可以在Eclipse 3.4中打开您的项目吗?)您是否可以在另一个目录中进行一个干净的Eclipse 3.4安装?或者您的项目可能会以某种方式被打乱(创建一个除了JDK之外没有依赖关系的新项目,创建a.b.c。您的新项目中的异常包,在您的项目中创建一个导入a.b.c异常的类。并检查错误是否发生。

#2


4  

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

在Java中,不能有与包名相同的类名。

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

这意味着JDT包必须在3.4中强制执行该规则。

See bug 63668 for instance.

参见bug 63668。


As Nate comments:

内特的评论:

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

一个名为Exception的类不会阻止您创建包异常。情况很重要。

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.

还记得一个类的全名,包括它所在的包。所以a.b。SomeClass(类名)不同于x.y。SomeClass(包名)。这里没有名字碰撞。

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

类名和包名必须匹配,以导致此错误。

See his more accurate answer.

看他更准确的答案。

#3


1  

I encountered a similar problem in a huge code base that I inherited. It turns out that the * was caused by an partially qualified class name in a JavaDoc link.

我在我继承的一个巨大的代码库中遇到了类似的问题。事实证明,冲突是由JavaDoc链接中部分限定的类名引起的。

To paraphrase, Eclipse was telling me that I had a package/type * for a.b.c.d. when compiling a.b.c.d.London. Doing a java search on the code for a.b.c.d revealed that Eclipse thought that a JavaDoc comment in a.b.c.Paris was a match. The JavaDoc comment contained {@ link d.NewYork}. When I changed the it to read {@link a.b.c.d.NewYork} the compilation error was resolved.

换句话说,Eclipse告诉我,我在编译a.b.c.d.时遇到了一个包/类型的冲突。对a.b.c的代码进行java搜索。d揭示了Eclipse认为在a.b.c中有一个JavaDoc注释。巴黎是一个匹配。JavaDoc注释包含{@ link . newyork}。当我修改它时,读取{@link a.b.c.d。编辑错误被解决了。

It should also be noted that NewYork was not imported into the Paris class as it only appeared in the JavaDoc comment. This also made it un-resolved in its abbreviated form and clicking on the link in the comment did not work. Making it an absolute reference also makes the JavaDoc link work.

还应该注意到,纽约并没有被导入到巴黎的类中,因为它只出现在JavaDoc注释中。这也使得它在其缩略形式中没有得到解决,并且点击评论中的链接不起作用。使它成为绝对引用也使JavaDoc链接工作。

#4


1  

I know this will sound silly, and possibly too simple to be true, but I solved this exact same error message by:

我知道这听起来很傻,可能也太简单了,但我解决了同样的错误信息:

  • Deleting the entire line of the package name causing the error message.
  • 删除导致错误消息的包名称的整个行。
  • Saving the .java file(this triggers a new error on the same line stating "The declared package "" does not match the expected package"), which it should do.
  • 保存.java文件(这会在相同的行上触发一个新的错误,声明“声明的包”“与预期的包不匹配”),应该这样做。
  • Re-typing the original package name onto the same line.
  • 将原始包名重新输入到同一行。
  • Saving the .java file.
  • 拯救. java文件。

Could not tell you why this worked, but it did, and Eclipse stopped throwing a tantrum on the spot.

我不能告诉你为什么会这样,但它做到了,而且Eclipse停止了在现场发脾气。

Safe typing and speedy coding.

安全打字和快速编码。

-Goodge

古奇

#5


0  

I changed one of the compilation option in eclipse and the problem disappeared. Under workspace properties: Java Compiler -> Errors/Warnings -> Change 'Unused import' from 'Warning' to 'Ignore'.

我在eclipse中更改了一个编译选项,问题就消失了。在工作区属性下:Java编译器->错误/警告->将“未使用导入”从“警告”改为“忽略”。

#6


0  

If you have a class Foo, you cannot have a package that ends with Foo, such as com.my.Foo.
Also if you are using maven style, you have resources in your project under something like src/main/resources
The folders in your resources also have a package style and there, also, you cannot have a folder that contains the name of your class.

如果你有一个类Foo,你不能有一个以Foo结尾的包,比如com.my.Foo。另外,如果您使用的是maven样式,那么您的项目中有一些资源,比如src/main/resources,您的资源中的文件夹也有一个包样式,而且,您也不能有一个包含类名的文件夹。

you will definitely encounter this problem when developing a Jenkins plugin according to the recommended conventions.
if you follow the Jenkins conventions, and you create a builder in a class named MyBuilder in package x.y then you are also supposed to place your .jelly in a resource folder named x.y.MyBuilder. This will result in the above problem.
However, if you name your resource folder x.y.myBuilder (notice lower case 'm' in myBuilder), unlike the recommended convention, the plugin will still work as you intended

根据推荐的约定,在开发Jenkins插件时,您一定会遇到这个问题。如果您遵循Jenkins的约定,并且在package x中创建一个名为MyBuilder的类中创建一个构建器。然后,您还应该将.jelly放在名为x.y.MyBuilder的资源文件夹中。这将导致上述问题。但是,如果您指定资源文件夹x.y。myBuilder(在myBuilder中,请注意小写的m),与推荐的约定不同,插件仍然按照您的意愿工作。

#1


5  

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.

这是因为在a.b中有一个名为exception的类(小写“e”)。c包和一个名为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?

它会导致名称冲突,因为如果你有代码a.b.c。exception.dosomething ();这是否意味着您想在a.b.c中调用静态doSomething()方法。异常类?或者它是否意味着有一个类叫做a.b.c。exception。你要调用的构造函数?

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.

坚持使用Java命名约定——所有小写的包,从大写字母开始的类,然后是camel-case——你将永远不会看到这个问题。

==========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'.

它不需要直接在您的项目中,它可以在您的项目依赖的另一个项目或库中。这应该会显示在构建路径或项目中任何地方出现的任何类:在Eclipse工具栏中点击“闪光”按钮——>选择“Java搜索”——>进入a.b.c。搜索字段的异常->选择“区分大小写”->在“搜索”中选择“类型”->确保所有选项都被选中进行“搜索”。

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'?

您是否使用了生成类的工具?他们是否可以将它们放到项目的构建目录中?当您看到错误时,如果您转到项目的构建目录,并进入到a/b/c/目录中,您会看到一个“异常”的.class文件吗?

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.).

当然,Eclipse通常会有一个bug(尽管我预计Eclipse 3.4中会有一个bug报告,如果它是……,您将会发现更多的抱怨),您的Eclipse安装可能会以某种方式被破坏(其他人可以在Eclipse 3.4中打开您的项目吗?)您是否可以在另一个目录中进行一个干净的Eclipse 3.4安装?或者您的项目可能会以某种方式被打乱(创建一个除了JDK之外没有依赖关系的新项目,创建a.b.c。您的新项目中的异常包,在您的项目中创建一个导入a.b.c异常的类。并检查错误是否发生。

#2


4  

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

在Java中,不能有与包名相同的类名。

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

这意味着JDT包必须在3.4中强制执行该规则。

See bug 63668 for instance.

参见bug 63668。


As Nate comments:

内特的评论:

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

一个名为Exception的类不会阻止您创建包异常。情况很重要。

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.

还记得一个类的全名,包括它所在的包。所以a.b。SomeClass(类名)不同于x.y。SomeClass(包名)。这里没有名字碰撞。

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

类名和包名必须匹配,以导致此错误。

See his more accurate answer.

看他更准确的答案。

#3


1  

I encountered a similar problem in a huge code base that I inherited. It turns out that the * was caused by an partially qualified class name in a JavaDoc link.

我在我继承的一个巨大的代码库中遇到了类似的问题。事实证明,冲突是由JavaDoc链接中部分限定的类名引起的。

To paraphrase, Eclipse was telling me that I had a package/type * for a.b.c.d. when compiling a.b.c.d.London. Doing a java search on the code for a.b.c.d revealed that Eclipse thought that a JavaDoc comment in a.b.c.Paris was a match. The JavaDoc comment contained {@ link d.NewYork}. When I changed the it to read {@link a.b.c.d.NewYork} the compilation error was resolved.

换句话说,Eclipse告诉我,我在编译a.b.c.d.时遇到了一个包/类型的冲突。对a.b.c的代码进行java搜索。d揭示了Eclipse认为在a.b.c中有一个JavaDoc注释。巴黎是一个匹配。JavaDoc注释包含{@ link . newyork}。当我修改它时,读取{@link a.b.c.d。编辑错误被解决了。

It should also be noted that NewYork was not imported into the Paris class as it only appeared in the JavaDoc comment. This also made it un-resolved in its abbreviated form and clicking on the link in the comment did not work. Making it an absolute reference also makes the JavaDoc link work.

还应该注意到,纽约并没有被导入到巴黎的类中,因为它只出现在JavaDoc注释中。这也使得它在其缩略形式中没有得到解决,并且点击评论中的链接不起作用。使它成为绝对引用也使JavaDoc链接工作。

#4


1  

I know this will sound silly, and possibly too simple to be true, but I solved this exact same error message by:

我知道这听起来很傻,可能也太简单了,但我解决了同样的错误信息:

  • Deleting the entire line of the package name causing the error message.
  • 删除导致错误消息的包名称的整个行。
  • Saving the .java file(this triggers a new error on the same line stating "The declared package "" does not match the expected package"), which it should do.
  • 保存.java文件(这会在相同的行上触发一个新的错误,声明“声明的包”“与预期的包不匹配”),应该这样做。
  • Re-typing the original package name onto the same line.
  • 将原始包名重新输入到同一行。
  • Saving the .java file.
  • 拯救. java文件。

Could not tell you why this worked, but it did, and Eclipse stopped throwing a tantrum on the spot.

我不能告诉你为什么会这样,但它做到了,而且Eclipse停止了在现场发脾气。

Safe typing and speedy coding.

安全打字和快速编码。

-Goodge

古奇

#5


0  

I changed one of the compilation option in eclipse and the problem disappeared. Under workspace properties: Java Compiler -> Errors/Warnings -> Change 'Unused import' from 'Warning' to 'Ignore'.

我在eclipse中更改了一个编译选项,问题就消失了。在工作区属性下:Java编译器->错误/警告->将“未使用导入”从“警告”改为“忽略”。

#6


0  

If you have a class Foo, you cannot have a package that ends with Foo, such as com.my.Foo.
Also if you are using maven style, you have resources in your project under something like src/main/resources
The folders in your resources also have a package style and there, also, you cannot have a folder that contains the name of your class.

如果你有一个类Foo,你不能有一个以Foo结尾的包,比如com.my.Foo。另外,如果您使用的是maven样式,那么您的项目中有一些资源,比如src/main/resources,您的资源中的文件夹也有一个包样式,而且,您也不能有一个包含类名的文件夹。

you will definitely encounter this problem when developing a Jenkins plugin according to the recommended conventions.
if you follow the Jenkins conventions, and you create a builder in a class named MyBuilder in package x.y then you are also supposed to place your .jelly in a resource folder named x.y.MyBuilder. This will result in the above problem.
However, if you name your resource folder x.y.myBuilder (notice lower case 'm' in myBuilder), unlike the recommended convention, the plugin will still work as you intended

根据推荐的约定,在开发Jenkins插件时,您一定会遇到这个问题。如果您遵循Jenkins的约定,并且在package x中创建一个名为MyBuilder的类中创建一个构建器。然后,您还应该将.jelly放在名为x.y.MyBuilder的资源文件夹中。这将导致上述问题。但是,如果您指定资源文件夹x.y。myBuilder(在myBuilder中,请注意小写的m),与推荐的约定不同,插件仍然按照您的意愿工作。