Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 361039

Summary: NPE in FieldReference.optimizedBooleanConstant
Product: [Eclipse Project] JDT Reporter: Sergey Prigogin <eclipse.sprigogin>
Component: CoreAssignee: Stephan Herrmann <stephan.herrmann>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: amj87.iitr, eclipse.sprigogin, jarthana, manoj.palat, stephan.herrmann, sxenos, tparker
Version: 3.7   
Target Milestone: 4.6 M3   
Hardware: PC   
OS: Linux   
See Also: https://git.eclipse.org/r/46957
https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=48ceb5ac4a314c1ce7f6900fe3c64b02824b66f2
Whiteboard:

Description Sergey Prigogin CLA 2011-10-14 21:29:48 EDT
java.lang.NullPointerException
       at org.eclipse.jdt.internal.compiler.ast.FieldReference.optimizedBooleanConstant(FieldReference.java:488)
       at org.eclipse.jdt.internal.compiler.ast.CastExpression.optimizedBooleanConstant(CastExpression.java:458)
       at org.eclipse.jdt.internal.compiler.ast.UnaryExpression.resolveType(UnaryExpression.java:287)
       at org.eclipse.jdt.internal.compiler.ast.AllocationExpression.resolveType(AllocationExpression.java:317)
       at org.eclipse.jdt.internal.compiler.ast.ReturnStatement.resolve(ReturnStatement.java:225)
       at org.eclipse.jdt.internal.compiler.ast.IfStatement.resolve(IfStatement.java:260)
       at org.eclipse.jdt.internal.compiler.ast.Block.resolve(Block.java:90)
       at org.eclipse.jdt.internal.compiler.ast.Block.resolve(Block.java:90)
       at org.eclipse.jdt.internal.compiler.ast.IfStatement.resolve(IfStatement.java:260)
       at org.eclipse.jdt.internal.compiler.ast.Block.resolve(Block.java:90)
       at org.eclipse.jdt.internal.compiler.ast.IfStatement.resolve(IfStatement.java:260)
       at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.resolveStatements(AbstractMethodDeclaration.java:463)
       at org.eclipse.jdt.internal.compiler.ast.MethodDeclaration.resolveStatements(MethodDeclaration.java:252)
       at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.resolve(AbstractMethodDeclaration.java:422)
       at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.java:1148)
       at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.java:1258)
       at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.resolve(CompilationUnitDeclaration.java:538)
       at org.eclipse.jdt.internal.compiler.Compiler.process(Compiler.java:763)
       at org.eclipse.jdt.internal.compiler.ProcessTaskManager.run(ProcessTaskManager.java:137)
       at java.lang.Thread.run(Thread.java:679)

The problem is 100% reproducible but, unfortunately, only on proprietary code.
Comment 1 Stephan Herrmann CLA 2011-10-15 10:39:57 EDT
It seems I can reconstruct just from the stack trace:

public class Bug361039 {
	public Bug361039(boolean b) {
	}
	private Object foo() {
		return new Bug361039(!((Boolean)this.f));
	}
}

Here the issue is caused by an unresolved field f.
Can you confirm that you had a compiler error like:
"f cannot be resolved or is not a field"?
Comment 2 Sergey Prigogin CLA 2011-10-17 13:15:34 EDT
(In reply to comment #1)
> Here the issue is caused by an unresolved field f.
> Can you confirm that you had a compiler error like:
> "f cannot be resolved or is not a field"?

The code compiles fine with javac. Eclipse compiler doesn't produce any errors either, but I don't know if it's because of the NPE or not.
Comment 3 Ayushman Jain CLA 2011-10-17 13:41:21 EDT
Thanks for the test Stephan!
Seems like a long standing issue. Looks like optimizedBooleanConstant() needs a null check. (Assigning to myself for now, let me know if you have a patch and want to release it).
Comment 4 Stephan Herrmann CLA 2011-10-17 17:20:40 EDT
(In reply to comment #3)
> ... (Assigning to myself for now, let me know if you have a patch and
> want to release it).

Go ahead, I don't have a patch.

(In reply to comment #2)
> The code compiles fine with javac. Eclipse compiler doesn't produce any errors
> either, but I don't know if it's because of the NPE or not.

This actually worries me a bit: from the stacktrace we have pretty strong
indication that a field reference was unresolvable or had some other problem.
The example in comment 1 shows the same characteristics as your stacktrace.

If indeed javac compiles the original code and if the compiled code runs
fine, I'd very much appreciate a small example program that exhibits this
issue. Would it be possible to isolate the problem into s.t. you can share?
Comment 5 Sergey Prigogin CLA 2011-10-17 19:40:51 EDT
(In reply to comment #4)
> Would it be possible to isolate the problem into s.t. you can share?

Is there a tracing option that can be used to narrow it down to a particular file? Right now the only thing we know that it happens on a large body of code that compiles fine with javac.
Comment 6 Stephan Herrmann CLA 2011-10-18 17:31:08 EDT
(In reply to comment #5)
> (In reply to comment #4)
> > Would it be possible to isolate the problem into s.t. you can share?
> 
> Is there a tracing option that can be used to narrow it down to a particular
> file? Right now the only thing we know that it happens on a large body of code
> that compiles fine with javac.

Mh, doesn't the exception also show up as an error in the Problems view?
That should take you to the offending class, no?

Inside that class you might want to search using the following regex [1]:

  return new .*\(.*!.*\(.oolean\)

If you don't know the class searching all files in the project might still
help, since the thing we are looking for is pretty specific already.


[1] Please use a File Search and tick [x] Regular expression
Comment 7 Eclipse Genie CLA 2015-05-01 09:48:19 EDT
New Gerrit change created: https://git.eclipse.org/r/46957
Comment 8 Stephan Herrmann CLA 2015-05-01 11:54:41 EDT
(In reply to Eclipse Genie from comment #7)
> New Gerrit change created: https://git.eclipse.org/r/46957

Thanks!

Would you also have a JUnit for this? I'd suggest to put it in /org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/FieldAccessTest.java

I'm still puzzled by the difference that my repro in comment 1 requires a compile error to trigger the bug whereas Sergey is seeing the exception with no compile error around ...

The reason for being pedantic: if the situation indeed arises without a compile error, then the provided patch might actually be wrong.
Comment 9 Sergey Prigogin CLA 2015-05-01 13:26:41 EDT
(In reply to Stephan Herrmann from comment #8)
> The reason for being pedantic: if the situation indeed arises without a
> compile error, then the provided patch might actually be wrong.

The NPE happens with a code that compiles with javac without errors, but the patch may still be beneficial because it would allow Eclipse compiler to proceed and to report an error if it thinks that the code is invalid. If the NPE happens without an error, the patch seems harmless.
Comment 10 Stephan Herrmann CLA 2015-05-01 14:40:24 EDT
(In reply to Sergey Prigogin from comment #9)
> If the NPE happens without an error, the patch seems harmless.

Why do you think so?

If the bug is still reproducible on your side we should really identify the source location. If the search mentioned in comment 6 yields no hit, then we clearly haven't understood the bug. In this case I suggest to remote-debug Eclipse:
+ launch main Eclipse with -Xdebug and friends into your main workspace
+ launch second Eclipse with just JDT/Core in the workspace and a breakpoint on NPE, connect to the main Eclipse, wait for breakpoint hit and inspect the AST elements available from stack frames near the top.
Comment 11 Sergey Prigogin CLA 2015-05-01 15:02:28 EDT
(In reply to Stephan Herrmann from comment #10)
> (In reply to Sergey Prigogin from comment #9)
> > If the NPE happens without an error, the patch seems harmless.
> 
> Why do you think so?

The null check is still needed in the scenario explained in comment #1. There is likely another problem behind this bug that still has to be investigated, but the the investigation can be done with or without the null check.

> If the bug is still reproducible on your side we should really identify the
> source location. If the search mentioned in comment 6 yields no hit, then we
> clearly haven't understood the bug. In this case I suggest to remote-debug
> Eclipse:
> + launch main Eclipse with -Xdebug and friends into your main workspace
> + launch second Eclipse with just JDT/Core in the workspace and a breakpoint
> on NPE, connect to the main Eclipse, wait for breakpoint hit and inspect the
> AST elements available from stack frames near the top.

Good point. We'll try to do something.
Comment 12 Stefan Xenos CLA 2015-08-03 17:00:29 EDT
I've asked the original reporter of the bug for more information about his workspace and project.

Does the gerrit patch address the issue described in comment 1?
Comment 13 Jay Arthanareeswaran CLA 2015-08-14 01:56:53 EDT
Tentatively targeting 4.6. 

Stephan, hope you are okay with being the QA contact for this one.
Comment 14 Stephan Herrmann CLA 2015-10-03 18:05:02 EDT
Since we don't have information about the source code that originally triggered the exception all we have is a stacktrace.

I created a JUnit from my reverse-engineered example in comment 1.

Final Hudson tests are running (patch set 6).
Comment 15 Stephan Herrmann CLA 2015-10-03 18:09:09 EDT
I should add that once we release this fix we'll probably never learn what caused the NPE on code without compile errors (if that was the case) - that bug will just go into hiding.
Comment 17 Stephan Herrmann CLA 2015-10-04 04:56:35 EDT
(In reply to Eclipse Genie from comment #16)
> Gerrit change https://git.eclipse.org/r/46957 was merged to [master].
> Commit:
> http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=48ceb5ac4a314c1ce7f6900fe3c64b02824b66f2
> 

Released the fix for the problem witnessed by comment 1 for 4.6 M3
Comment 18 Jay Arthanareeswaran CLA 2015-10-28 12:54:34 EDT
(In reply to Stephan Herrmann from comment #17)
> Released the fix for the problem witnessed by comment 1 for 4.6 M3

Is there more to the fix? Should this be moved out to M4?
Comment 19 Stephan Herrmann CLA 2015-10-28 18:39:24 EDT
(In reply to Jay Arthanareeswaran from comment #18)
> (In reply to Stephan Herrmann from comment #17)
> > Released the fix for the problem witnessed by comment 1 for 4.6 M3
> 
> Is there more to the fix? Should this be moved out to M4?

No, let's just close it at this.

I just wanted to give Sergey a chance to follow-up on his claim that the bug can be triggered without a compile error :)
Comment 20 Sergey Prigogin CLA 2015-10-28 19:11:21 EDT
(In reply to Stephan Herrmann from comment #19)
Unfortunately the problem is not easily reproducible so I wasn't able to get additional information. There is a possibility that a bug in the code was introduced by an annotation processor that produced bad code under ecj but not under javac.
Comment 21 Stephan Herrmann CLA 2015-10-28 20:00:14 EDT
(In reply to Sergey Prigogin from comment #20)
> (In reply to Stephan Herrmann from comment #19)
> Unfortunately the problem is not easily reproducible so I wasn't able to get
> additional information. There is a possibility that a bug in the code was
> introduced by an annotation processor that produced bad code under ecj but
> not under javac.

Perhaps our fix swept that annotation processor bug under the rug. So may the rug contain it :)
Comment 22 Manoj N Palat CLA 2015-10-29 04:27:34 EDT
Verified for Eclipse Neon (4.6) Build id: I20151028-2000