10

My mystery begins like this. Consider this bit of code:

import java.util.Set;
import javax.annotation.processing.*;
import javax.lang.model.element.TypeElement;

@SupportedOptions({
  "thing1",
  "thing2",
})
public class fc extends AbstractProcessor
{
  @Override
  public boolean process( Set<? extends TypeElement> anns, RoundEnvironment re)
  {
    return false;
  }
}

If you look past most of the scaffolding (I just wanted to make sure it's minimally complete and you can run your compiler on it), you'll see in the middle there's an annotation, and it takes a String array initializer, and there's a comma after "thing2". Now, if you're the sort who takes the Java Language Specification to bed with you at night, you'll remember that a final comma is perfectly valid, "may appear after the last expression in an array initializer and is ignored." So if you try this in your favorite javac, you will not be surprised that it compiles perfectly.

So here's the mystery. That example above is condensed straight from a real patch that was requested in a real project because of a real "illegal start of expression" compiler message that somebody got while building that project, and went away when he deleted the final comma.

So clearly that person was using a braindamaged version of javac, or had some other whizbang source munging tool in his toolchain that hasn't got the Java grammar quite right, and while he gave otherwise complete information in his bug report, in this case the only information that really matters would be whose compiler and toolchain and what version he was using, and he didn't supply any of that! So not only was a spurious patch made to code that didn't need it, but there isn't enough information to file a bug report where it really needs to go, the tool vendor that gives spurious errors on valid Java.

So, I'm sort of crowdsourcing this :) ... can anybody find a Java compiler or other related tool that doesn't compile the above code successfully, but flags an error similar to what was reported in this example? Then maybe we'll know what the culprit was.

I'm only partly peeved 'cause it was my code that got spuriously patched ;) ... it bugs me at least as much that there may be some tool still out there that hasn't been fixed, and will be confusing more people about what's Java and what isn't, and leading to more weird patches to code that's ok.

Thanks!

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Chapman Flack
  • 604
  • 5
  • 13

1 Answers1

6

IntelliJ IDEA compiles your code correctly (of course), but it does show a warning in the 'Javac quirks' category (badly drawn freehand emphasis mine):

Screenshot

So it seems it's not so much braindead or exotic javacs that struggle with trailing comma's, just old ones.

It has been fixed in JDK 7 and the fix has been backported to newer versions of JDK 6.

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • Wow ... who'd have thought it was Oracle's own? I've only been using Sun/Oracle `javac` on the code myself, I guess I just luckily had versions either before or after that bug? _I_ never saw it not compile. Crazy. Thanks for the definitive links! – Chapman Flack Jul 19 '15 at 23:53
  • Yeah, you'd expect the JDK team to be able to recite the entire JLS by heart by now. Everyone makes mistakes I guess. – Wander Nauta Jul 19 '15 at 23:54
  • I'm surprised an IDE gave a full description about such a thing. IntelliJ is more advanced than I thought.. This makes me wonder what other "quirks" exist – Vince Jul 20 '15 at 01:08
  • @EJP No, it's an IntelliJ inspection warning (correctly) about a bug in Oracle javac. – Wander Nauta Jul 20 '15 at 01:18