views:

650

answers:

2

There are already some discussions here on stackoverflow about Java generics, but I am too stupid to solve this specific question. I have defined an interface in a project, and its implementation in another one. They are in different packages. Instead of implementing the method, the compiler complains in some workspaces:

Name clash: The method highlight(EnumSet, int, int) of type SuperDuperHighlightable has the same erasure as highlight(EnumSet, int, int) of type IHighlightable but does not override it

The same code does not complain in other workspaces, and I cannot find the relevant differences. All projects and workspaces use JRE SE 1.5.

HighlightingStyle.java

package org.my.api;

public enum HighlightingStyle {
 NONE, FIELD, SELECTION, TEST
}

IHighlightable.java

package org.my.api;

import java.util.EnumSet;
import javax.swing.text.BadLocationException;

public interface IHighlightable {
   void highlight(EnumSet<HighlightingStyle> style, int start, int length)
     throws BadLocationException, IllegalArgumentException;
}

Implementation:

package org.my.impl;

import java.util.EnumSet;
import javax.swing.text.BadLocationException;
import org.my.api.HighlightingStyle;
import org.my.api.IHighlightable;

public class SuperDuperHighlightable implements IHighlightable {

 public void highlight(EnumSet<HighlightingStyle> styleSet, int start, int length)
   throws BadLocationException, IllegalArgumentException {
  for (HighlightingStyle style : styleSet) {
   DoSomething(style, start, length);
  }
 }

 private void DoSomething(HighlightingStyle style, int start, int length) {
  // TODO Auto-generated method stub

 }

}

Do you have any insights if this is a source-code problem, a workspace problem, or some problem with Eclipse?

+2  A: 

No solution yet, but a few tips and tests:

  1. Clean all projects in the workspace, this helps sometimes with eclipse based problems
  2. Make sure, that you only have on classfile 'HighlightingStyle'. Maybe, the implementation gets HighlightingStyle from a different source / classloader. Maybe there's a library on a classpath that includes the enum and/or the interface
  3. Rename the enum and check, if the refactoring changes the name both in the interface and the implementation.

Hope it helps to solve your issue..

Edit

Didn't get correctly, that it works in some workspaces. So I'm sure, this is a workspace configuration issue or maybe even a bug. At this point, I personally would not continue fixing that problem but just zip up all projects into a single archive, move or rename the entire workspace, create a new one (same name, original location) and import all archived projects. If it's OK, delete the moved or renamed workspace.

Sure, you loose your workspace settings, and I can't tell, if this is a problem in your case. I did it before on some occasions (I had an ugly issue with subversion and mercurial plugins....) and it's pretty easy.

Andreas_D
Checked all your 3 suggestions without success. Clean does not help, I have a single class named HighlightingStyle in all the projects, and the rename refactoring affects all code. The workspace that did not compile, still does not compile. The workspaces which did compile, still compile.
wigy
Any new workspaces I set up with any method: they work. I tried to isolate the setting that causes the problem in the old workspaces - without success. Thanks for your kind help, originally I was not really sure if the source code was OK.
wigy
A: 

I see that the class definitions are decidedly modified for the purpose of the question. Do either of those take a parametrized type and you are not using it? If yes, then that could explain the problem. I had a similar problem, and that was the root cause.

Yishai
Yes, the code is simplified for testing. No, the original classes were not generic.My basic goal is to define an interface, where one of the methods may accept a set of values from a given enumeration type. Pretty much something like a bitfield in C++ or an enum with a [Flags] attribute in C#. I just do not have enough Java 1.5 experience to express that without running into issues
wigy
@wigy, can you post the exact real method signatures where you have a problem. It is likely a Generics issue, where you need an extends or a type parameter in the right place, and instead your interface and implementation define two different generic types. Generics are not co variant, so it might seem like it should work but doesn't.
Yishai
I have not changed the signature of the highlight method. It was really the same in the original code. And there was no type parameters to the classes involved. And when I recreated the workspace with the same code, there was no compilation error, so it seems to me to be some configuration issue or an Eclipse bug.
wigy