9

I was in middle of coding and accidentally put the following line of code at the part of class where we declare instance variables. but i checked and it gives the same error anywhere by anywhere i mean : inside a static block, inside constructor, inside any class method. except when private; is put as the first line of the class it gives Syntax error, insert "EnumBody" to complete ClassBodyDeclarations (as written by @chaitanya10 in comments below and also verified by me on my workspace) error in eclipse tooltip when we hover cursor over it.

I understand there is error.
but I dont understand the error message when i hover cursor over the error. what is the meaning of this message?

why does it expecting EnumBody ?

Below is the screenshot.

enter image description here

Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
  • is this piece of the code from an Enum ?? – PermGenError Nov 01 '12 at 08:11
  • @chaitanya10 Nope this screenshot is from instance variable declaration section of a class. But Its the same error if i put it in some function or any other place. – Mukul Goel Nov 01 '12 at 08:13
  • i think its cuz you put a semicolon at the end and the compiler thinks that its the end of the line , and its obviously an syntax error cuz you never gave the type and variable name in the declaration – PermGenError Nov 01 '12 at 08:15
  • @chaitanya10 , that I understand. that there is error. what i dont understand is the error message. why is it expecting `EnumBody` to complete `EnumDeclaration`. – Mukul Goel Nov 01 '12 at 08:17
  • i trid to reproduce in eclipse juno and got `Syntax error, insert "EnumBody" to complete ClassBodyDeclarations` when i do the same instance variable declaration . – PermGenError Nov 01 '12 at 08:22
  • @chaitanya10 , I got `insert "EnumBody" to complete "enumDeclaration"` on eclipse indigo – Mukul Goel Nov 01 '12 at 08:23
  • @chaitanya10 , you must have wrote `private;` as first statement of class, in that case it gives the `classBodyDeclaration` error. Try giving something (a constructor/staticblocl/any othervariable) before writing `private;` – Mukul Goel Nov 01 '12 at 08:26
  • add more context to the line. Is it in a class or method, or what? – Simulant Nov 01 '12 at 11:53
  • @Simulant , please look at the updated question – Mukul Goel Nov 01 '12 at 12:03
  • 2
    I'll put this in a comment rather than an answer, because I don't know whether it's correct: My guess is that Eclipse is using an LR parser to parse the Java source. That means it's trying to determine the right-most symbol of a production that can match at that point. The things that it can ambiguously match are method definitions, fields, etc., and type definitions like enums. Of the list of things that can match, an enum definition is probably listed first in the grammar. So, Eclipse thinks that it's missing the last symbol of an enum declaration, which is EnumBody. – Nathan Ryan Nov 08 '12 at 23:31
  • This is probably a better question for an Eclipse mailing list rather than Stack Overflow. – Dennis Nov 10 '12 at 13:49
  • @Dennis , yea as fellow members have answered, probably a bug in eclipse. I agree with you. When asking question , i thought there would be some explanatiom for it. But its turning out to be some bug with eclipse. – Mukul Goel Nov 10 '12 at 14:13

3 Answers3

11

For a real explanation, someone would need to do a detailed analysis of the syntax checking, and the syntax error recovery and reporting components of the Eclipse Java compiler front end. But the most likely explanation is that your "unusual" syntax error has "confused" the compiler's syntax error recovery code.

In short it is a minor compiler bug (or feature).

It is possible that the developers know about this, but have refrained from fixing it because of one or more of the following:

  • It doesn't happen often enough to be prioritized.
  • It might be hard to do a better job without affecting other error recovery cases.
  • Any change could break1 a number of compiler error regression tests.
  • There are other higher priority issues to deal with.

1 - Break ... in the sense of causing tests to fail because of assumptions in the test rather than problems in the code being tested. Fixing these regressions could be burdensome.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
5

For what it's worth, the following:

public class Foo;

Error: Syntax error, insert "ClassBody" to complete ClassBodyDeclarations

public enum Foo;

Error: Syntax error, insert "EnumBody" to complete ClassBodyDeclarations

public interface Foo;

Error Syntax error, insert "InterfaceBody" to complete ClassBodyDeclarations

Anyway, I think what happens is, in this org.eclipse.jdt.internal.compiler.parser.Parser.consumeEnumConstantHeader(), it tries to recover when it gets the parsing error with this code:

if(this.currentToken == TokenNameSEMICOLON) {
    RecoveredType currentType = currentRecoveryType();
    if(currentType != null) {
        currentType.insideEnumConstantPart = false;
    }
}

So it correctly figures out that we're not inside an enumeration, so we get an error like above, but it doesn't realize that we don't even know if it is an enumeration. You can look at the source here:

In other words, it's probably a bug that the Eclipse team introduced when they added Enumerations, it's handled slightly differently than some of the other tokens, which causes it to be Recovered in a different way and therefore shows up in the compiler errors.

durron597
  • 31,968
  • 17
  • 99
  • 158
1

This was happening to me in Eclipse too even though my code was correct. Happened because I wrote some code without adding a jar first (aspectJ JARs) and after I added the JAR this issue came about.

At first I copied the code, deleted it, and pasted it back in and thought that worked as the red line went away but errors were thrown at runtime. I did however get it to work by deleting the class, recreating it, and typing the same exact code again and it worked. Copy paste probably would have worked after deleting the class, but I typed it out to be sure.

Definitely a weird problem. I knew the code was right because I was copying it out of a book (Spring in action 4th edition - a good read btw). Anyway, hope this helps you or someone in the future! drove me crazy for about an hour

Mason
  • 1,662
  • 1
  • 9
  • 10
  • *"I knew the code was right because I was copying it out of a book ..."* - Not necessarily. 1) Books can be wrong. Even good ones. 2) It is possible that the text of a machine-readable version of a book contains odd characters. Things like sloping quote characters, long-dash, non-breaking space, etc that might *look* write on paper but give a compiler indigestion. – Stephen C Aug 31 '19 at 06:57
  • Either way, the Question is about an incorrect error message for a real error, not an error for something that wasn't ... or may not have been ... a real error. Unless you can actually *reproduce* the problem and tell us how to do that too, it is not possible to say what you were experiencing. (Sure. What you did "fixed it for you". But we don't know what it fixed, or why.) – Stephen C Aug 31 '19 at 07:02