Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: ever seen a compiler or tool that REJECTS a final comma in array initializer?

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!

like image 924
Chapman Flack Avatar asked Jul 19 '15 23:07

Chapman Flack


1 Answers

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.

  • Related question
  • JDK bug report

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

like image 139
Wander Nauta Avatar answered Oct 14 '22 11:10

Wander Nauta