I have the following code:
A class that models the mapping between a ValueContainer<P>
and an Entity E
. (Eg. a checkbox (ValueContainer) and something that has a Boolean value):
public abstract class ObjValContainerMapper<E, P> {
private ValueContainer<P> provider;
public ObjValContainerMapper(ValueContainer<P> provider) {
this.provider = provider;
}
public abstract P getValue(E entity);
public abstract void setValue(E entity, P value);
...
}
An interface ValueContainer<P>
:
public interface ValueContainer<T> {
T getValue();
void setValue(T value);
}
A custom Checkbox:
public class AdvancedCheckBox extends JCheckBox implements ValueContainer<Boolean>
And some code that unexpectedly doesn't work:
AdvancedCheckBox chckbxBindToDrive = new AdvancedCheckBox(
"Bind to Drive");
ObjValContainerMapper<IndexSpec, Boolean> bindToDriveMapper =
new ObjValContainerMapper<IndexSpec, Boolean>(chckbxBindToDrive) {
@Override
public Boolean getValue(IndexSpec entity) {
if (entity == null) {
return false;
}
return entity.isBindToDrive();
}
@Override
public void setValue(IndexSpec entity, Boolean value) {
entity.setBindToDrive(value);
}
};
The code does not compile. The error shown is "The constructor ObjValContainerMapper(AdvancedCheckBox) is undefined". Eclipse suggests, among other options, to let AdvancedCheckBox implement ValueContainer or cast the argument chckbxBindToDrive to ValueContainer, despite AdvancedCheckBox explicitly declares implements ValueContainer<Boolean>
.
Strangely enough I have reused code from an old project that was built with Java 6, in which case the approx. same code worked fine. Has something changed with Java 7 or am I missing something?
Environment:
Thx for all the troubleshooting proposals in the comments.
The cause for this behaviour was that I copied the ValueContainer interface twice into two differnt packages as I assumed it would be missing given that Eclipse didn't seem to find it first (thx @sasankad for the proposal to clean the project). I hope this is still helpful for others as the error message and solution-proposals by the compiler and Eclipse respectively are not and are rather misleading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With