2

So here I was trying this "Effective Java 2nd edition" exercise about writing extensible enums.

I faced a really strange issue though - I had each of the enum instances implement the interface methods and all looked good in eclipse. But when I tried to compile it through maven, it failed, although eclipse didn't throw any compilation errors previously at all (both my eclipse and maven use the same JDK: 1.6.0_33-b05). It turned out that I had to override the interface method(s) [I'll call it method X from now] inside the enum (outside of all enum instances) too for fixing this!

Here's a sample explaining this:

Interface:

public interface IDay
{
  public boolean isHoliday(); 
}

Enum:

public enum Day implements IDay
{
  SATURDAY
  {
    @Override
    public boolean isHoliday()
    {
      return true;
    }
  },
  SUNDAY
  {
    @Override
    public boolean isHoliday()
    {
      return true;
    }
  },
  MONDAY
  {
    @Override
    public boolean isHoliday()
    {
      return false;
    }
  };

  // Method X: the project won't compile for me without this method!
  public boolean isHoliday()
  {
    return false;
  }

}

Invocation:

public class DayTester
{
  public static void main(final String[] args)
  {
    // line Y: this line won't maven compile if I got rid of the method X
    System.out.println("Sunday: " + Day.SUNDAY.isHoliday());
  }
}

Strange enough, eclipse was completely ok without the method 'X' yet the maven compilation would fail on the line Y saying

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project X: Compilation failure [ERROR] DayTester.java:[7,46] cannot find symbol [ERROR] symbol : method isHoliday() [ERROR] location: class Day

And my eclipse save actions automatically inserts @Override if I had the method X in place. Removing the method X throws compilation errors in my eclipse aon those Override annotations I had previously.

Here are my questions: 1. Why won't maven compile in this case when eclipse does? 2. What do the Override annotation errors mean here? 3. Is the method X even accessible somehow? What am I missing to understand?

mystarrocks
  • 4,040
  • 2
  • 36
  • 61

1 Answers1

0

@Override annotation

@Override - Checks that the method is an override. Causes a compile error if the method is not found in one of the parent classes or implemented interface

Eclipse configuration

Properties > Java Compiler > Compiler compliance level = 1.6

Check Compiler compliance level at project level too.

Properties > Java Compiler > Errors/Warnings

The @Override annotation is under Annotations and is ignored by default.

Maven configuration

Add this properties to pom.xml

<properties>
   <maven.compiler.source>1.6</maven.compiler.source>
   <maven.compiler.target>1.6</maven.compiler.target>
</properties>

This is issued by maven-compile-plugin

note that at present the default source setting is 1.5 and the default target setting is 1.5, independently of the JDK you run Maven with. If you want to change these defaults, you should set source and target as described in Setting the -source and -target of the Java Compiler.

Read more

Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • The compilation failure on line Y is the main concern that I'm trying to understand here. Sorry if I wasn't clear on that. Anyway, I'm setting the source and target settings to see if the override annotation issue is gone. EDIT: and the override annotation error was reported in the eclipse itself, not when I compile through maven. – mystarrocks Nov 22 '13 at 18:05
  • And what source level is configured in Eclipse? http://stackoverflow.com/questions/1736730/eclipse-magic-are-only-available-if-source-level-is-1-5-yesterday-everythi – MariuszS Nov 22 '13 at 18:07
  • If you mean the compiler compliance level, it's set to 1.6 – mystarrocks Nov 22 '13 at 18:08
  • Project is refreshed and rebuilded? – MariuszS Nov 22 '13 at 18:09
  • Yes. Many times too. Hitting F3/ctrl+click on line Y would take me to the method definition on the interface too! It's reproducible on a different PC with similar settings btw. – mystarrocks Nov 22 '13 at 18:10
  • Check `Properties > Java Compiler > Errors/Warnings` @Override – MariuszS Nov 22 '13 at 18:23
  • If I'm rightly understanding it, this setting is only for missing override annotations. Which I think is not relevant to the problem in hand? – mystarrocks Nov 22 '13 at 18:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41723/discussion-between-mariuszs-and-mystarrocks) – MariuszS Nov 22 '13 at 18:28