Bug 361938 - Formerly working JLS3 parser not working -- Scanner reports com.sun.jdi.InvocationException occurred invoking method.
Summary: Formerly working JLS3 parser not working -- Scanner reports com.sun.jdi.Invoc...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7.1   Edit
Hardware: All All
: P3 major (vote)
Target Milestone: 3.6.2+J7   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-25 10:49 EDT by Missing name CLA
Modified: 2012-01-19 19:29 EST (History)
5 users (show)

See Also:


Attachments
The source that cannot be parsed any more by JLS3 but is parsed by JLS4. (2.53 KB, application/octet-stream)
2011-10-25 10:51 EDT, Missing name CLA
no flags Details
Workspace log in case of using JLS3 (3.49 KB, text/plain)
2011-10-26 03:38 EDT, Missing name CLA
no flags Details
Workspace log in case of using JLS4 (3.49 KB, text/plain)
2011-10-26 03:39 EDT, Missing name CLA
no flags Details
Contains an example Eclipse project that demonstrates the problem. (8.77 KB, application/zip)
2011-11-03 06:14 EDT, Missing name CLA
no flags Details
Patch under test (1.88 KB, patch)
2011-11-10 06:50 EST, Srikanth Sankaran CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Missing name CLA 2011-10-25 10:49:58 EDT
Build Identifier: 20110916-0149

I am parsing sourcecode with the help of the ASTParser the following way:

String source = ...

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source.toCharArray());

Up to Eclipse 3.7 this worked like a charme. Since the last update which introduced JLS4, the parser cannot parse a class it could before. When I open the AST in debugger mode, the scanner variable reports

com.sun.jdi.InvocationException occurred invoking method.

When I switch to JLS4, it parses the sourcecode without any problems and I get my compilation unit just as I did before with JLS3. Since I want my plugin to be used as well by people who haven't already switched to Eclipse 3.7.1, I cannot use JLS4 but would like to stay with JLS3.

I should note, that no Exception is thrown at the console. The exception is only reported as value of the Scanner. I provide the class on which it fails (this does not happen for all classes) as well.

Reproducible: Always

Steps to Reproduce:
1. Update Eclipse 3.7 to 3.7.1 (Indigo Service Release 1) that brings JLS4
2. Parse source that worked with JLS3 again with JLS3
3. Get InvocationException message in scanner of AST
Comment 1 Missing name CLA 2011-10-25 10:51:31 EDT
Created attachment 205923 [details]
The source that cannot be parsed any more by JLS3 but is parsed by JLS4.

The source that cannot be parsed any more by JLS3 but is parsed by JLS4.
Comment 2 Olivier Thomann CLA 2011-10-25 12:56:22 EDT
Could you please provide the .log file of your workspace ?
Comment 3 Missing name CLA 2011-10-26 03:38:58 EDT
Created attachment 205967 [details]
Workspace log in case of using JLS3
Comment 4 Missing name CLA 2011-10-26 03:39:15 EDT
Created attachment 205968 [details]
Workspace log in case of using JLS4
Comment 5 Missing name CLA 2011-10-26 03:43:09 EDT
The workspace logs did not reveal anything to me. By the way I noticed, that the Scanner object in the AST very often contains the "com.sun.jdi.InvocationException occurred invoking method" string, also when the class itself is parsed correctly, i.e. the CompilationUnit object created with

compilationUnit = (CompilationUnit) parser.createAST(null);

is shown as the parsed sourcecode in the debugger, while the attached source is shown as something like:

!org.eclipse.jdt.core.dom.CompilationUnit@37423658

When using JLS4, it is shown as sourcecode in the debugger as well.

Hope this helps.
Comment 6 Srikanth Sankaran CLA 2011-10-26 07:34:20 EDT
(In reply to comment #5)
> The workspace logs did not reveal anything to me. By the way I noticed, that
> the Scanner object in the AST very often contains the
> "com.sun.jdi.InvocationException occurred invoking method" string,

This probably means that the debug print invocation of toString on
the scanner object failed probably due to bad state. May be some
field is null which would result in this message while viewing the
object during debugging and could also indicate the underlying problem
for the subject matter at hand.
Comment 7 Missing name CLA 2011-10-26 08:05:28 EDT
I don't think the Scanner value is related to this particular problem, since it occurs as well with JLS4. Nevertheless, the source parsed with JLS4 is available as output of the CompilationUnit in debugger while not any more with JLS3.

By the way .. when I walk through the AST of the CompilationUnit, I get the methods contained in the class. Very strange...
Comment 8 Missing name CLA 2011-10-27 06:56:12 EDT
The same problem occurs with Eclipse 4 considering JLS3 and JLS4.
Comment 9 Srikanth Sankaran CLA 2011-11-03 05:21:33 EDT
(In reply to comment #5)
> The workspace logs did not reveal anything to me. By the way I noticed, that
> the Scanner object in the AST very often contains the
> "com.sun.jdi.InvocationException occurred invoking method" string, also when
> the class itself is parsed correctly, i.e. the CompilationUnit object created
> with

It doesn't "contain" that string, as I said earlier the debug print toString
method is encountering an NPE due the fact that scanner.source comes out to
be null. You can expand the scanner object and you will be able to view the
members.

> When using JLS4, it is shown as sourcecode in the debugger as well.

I see the same behavior here too, i.e the com.sun.jdi* message shows up
when you view the value of the scanner object in the debugger even in JLS4
using HEAD version.

Will continue to investigate.
Comment 10 Srikanth Sankaran CLA 2011-11-03 05:31:52 EDT
Just so we are on the same page, can you paste a larger fragment of the
code that you are stepping through and indicating where exactly you attempt
to view the scanner object. Once I have this, I'll study the behavior
difference between pre-JLS4 days and now to see what brought about this
behavior.

It is not clear to me (yet) that there is a bug here. You are peeking at
internal state of objects while debugging and that visualization operation
is failing due to partially cooked state of objects.

I agree if things can be restored to the earlier state that enabled debugging
then that would be better.

(In reply to comment #0)

> Up to Eclipse 3.7 this worked like a charme. Since the last update which
> introduced JLS4, the parser cannot parse a class it could before. When I open
> the AST in debugger mode, the scanner variable reports
> 
> com.sun.jdi.InvocationException occurred invoking method.

Do you have some clearer evidence that "the parser could not parse a class
it could before" other than this debug print failure.

May be if you paste the larger snippet you are stepping through and indicate
clearly where you are able print the scanner object that you aren't anymore
able to, I can follow up - Thanks.
Comment 11 Missing name CLA 2011-11-03 05:51:07 EDT
> Do you have some clearer evidence that "the parser could not parse a class
> it could before" other than this debug print failure.
> 
> May be if you paste the larger snippet you are stepping through and indicate
> clearly where you are able print the scanner object that you aren't anymore
> able to, I can follow up - Thanks.

Hello!

Well -- the main failure for me was not the debug print failure (earlier I just thought it may indicate the problem's source), but that if I create the 

compilationUnit = (CompilationUnit) parser.createAST(null);

and then take a type from it with

BodyDeclaration t = compilationUnit.types().get(0);

the execution of

t.toString();

normally returned the source of the BodyDeclaration object as a string. Since JLS4 is in, with JLS3 it sometimes returns sth. like

"!org.eclipse.jdt.core.dom.CompilationUnit@37423658"

instead of the sourcecode. If I use JLS4, the same code returns me the sourcecode of the declaration as JLS3 did before. This happens e.g. with the earlier attached java file.
Comment 12 Missing name CLA 2011-11-03 06:14:49 EDT
Created attachment 206392 [details]
Contains an example Eclipse project that demonstrates the problem.

This example project demonstrates the problem on the attached source code. With JLS3 it shows only an object-referece string, while JLS4 shows the source.
Comment 13 Srikanth Sankaran CLA 2011-11-03 06:16:39 EDT
(In reply to comment #11)

> Well -- the main failure for me was not the debug print failure (earlier I just
> thought it may indicate the problem's source), but that if I create the 

I see, I understand better now, 

(In reply to comment #12)
> Created attachment 206392 [details]
> Contains an example Eclipse project that demonstrates the problem.

Excellent, thanks, I'll see what is going on here.
Comment 14 Srikanth Sankaran CLA 2011-11-10 05:58:01 EST
org.eclipse.jdt.core.dom.TryStatement.resources() reads:

	public List resources() {
		// more efficient than just calling unsupportedIn2_3() to check
		if (this.resources == null) {
			unsupportedIn2_3();
		}
		return this.resources;
	}

I think this should be:

	public List resources() {
		// more efficient than just calling unsupportedIn2_3() to check
		if (this.resources != null) {
			unsupportedIn2_3();
		}
		return this.resources;
	}
Comment 15 Srikanth Sankaran CLA 2011-11-10 06:24:58 EST
All cients of org.eclipse.jdt.internal.core.dom.NaiveASTFlattener are prone to
this bug, which at the moment is fortunately all of org.eclipse.jdt.core.dom.ASTNode.toString() :)

Minimal test case that shows the problem:


public class X {
  X(){
    try {
    } finally {
    }
  }
}

Patch will follow shortly.

If the one character fix stands scrutiny will backport to 3.7.2 also.
Comment 16 Srikanth Sankaran CLA 2011-11-10 06:50:06 EST
Created attachment 206781 [details]
Patch under test
Comment 17 Srikanth Sankaran CLA 2011-11-10 08:17:51 EST
(In reply to comment #11)
> > Do you have some clearer evidence that "the parser could not parse a class
> > it could before" other than this debug print failure.
> > 
> > May be if you paste the larger snippet you are stepping through and indicate
> > clearly where you are able print the scanner object that you aren't anymore
> > able to, I can follow up - Thanks.
> 
> Hello!
> 
> Well -- the main failure for me was not the debug print failure (earlier I just
> thought it may indicate the problem's source), but that if I create the 
> 
> compilationUnit = (CompilationUnit) parser.createAST(null);

Hello, the patch posted fixes this problem which you identify as the main
failure for you.

For the inability to view the scanner object, as requested in comment# 10
please provide a snippet and also identify precisely where you view the
value of the scanner object. Do you notice a difference between JLS3 and
JLS4 in this exercise ? Do you notice a regression compared to earlier
versions ? I am not sure yet there is a problem there.

toString() methods are not always coded defensively, so if they run into
an NPE, you will see the com.sun.jdi.InvocationException.
Comment 18 Srikanth Sankaran CLA 2011-11-11 02:07:21 EST
Released for 3.8 M4 via commits: 8ab95da8a0ea0d6e3c8c0619022e89253da6eb42 and
45bb778e6ee242f361e8397463f6e1b0b9b9dfa0 (the latter removing a sysout needlessly
left in)

If you still suspect the com.sun.jdi.InvocationException issue is real problem/
regression, please open a separate bug with clear steps - Thanks.
Comment 19 Missing name CLA 2011-11-11 02:19:41 EST
(In reply to comment #18)
> Released for 3.8 M4 via commits: 8ab95da8a0ea0d6e3c8c0619022e89253da6eb42 and
> 45bb778e6ee242f361e8397463f6e1b0b9b9dfa0 (the latter removing a sysout
> needlessly
> left in)
> 
> If you still suspect the com.sun.jdi.InvocationException issue is real problem/
> regression, please open a separate bug with clear steps - Thanks.

I don't think the InvocationException issue is a real problem; as I said, it was just a first suspect but obviously not the one responsible.

Thank you for investigating the bug and fixing it.

Cheers!
Comment 20 Srikanth Sankaran CLA 2011-11-11 03:01:43 EST
As the fix is a single character change that fixes a blatantly wrong
piece of code, I have also arranged to release this into 3.7.2 and 3.6.2+java7
branches.
Comment 21 Dani Megert CLA 2011-11-14 03:26:43 EST
(In reply to comment #20)
> As the fix is a single character change that fixes a blatantly wrong
> piece of code, I have also arranged to release this into 3.7.2 and 3.6.2+java7
> branches.

+1.
Comment 22 Satyam Kandula CLA 2011-12-05 08:33:07 EST
I found some more incorrect code which shouldn't impact any functionality. However, I have opened another bug 365582 to track the incorrect code.
Comment 23 Satyam Kandula CLA 2011-12-05 08:33:35 EST
Verified for 3.8M4 using build I20111202-0800
Comment 24 Satyam Kandula CLA 2012-01-19 00:56:06 EST
Verified for 3.7.2RC2 using build M20120118-0800
Comment 25 Olivier Thomann CLA 2012-01-19 14:33:25 EST
(In reply to comment #14)
> org.eclipse.jdt.core.dom.TryStatement.resources() reads:
> 
>     public List resources() {
>         // more efficient than just calling unsupportedIn2_3() to check
>         if (this.resources == null) {
>             unsupportedIn2_3();
>         }
>         return this.resources;
>     }
This is actually fine. resources is null only if the ast level is 2 or 3 as this is initialized in the constructor for ast level >= 4.
The real problem is that the resources() call is not within the if that checks for the jls level being >= JLS4 in the naive flattener as Satyam mentioned in bug 365582.
Comment 26 Srikanth Sankaran CLA 2012-01-19 16:41:02 EST
(In reply to comment #25)

> This is actually fine. resources is null only if the ast level is 2 or 3 as
> this is initialized in the constructor for ast level >= 4.
> The real problem is that the resources() call is not within the if that checks
> for the jls level being >= JLS4 in the naive flattener as Satyam mentioned in
> bug 365582.

You are right of course, we'll adopt the fix for bug 365582 at M6 time.
Thanks Olivier.
Comment 27 Srikanth Sankaran CLA 2012-01-19 16:58:34 EST
(In reply to comment #26)

> You are right of course, we'll adopt the fix for bug 365582 at M6 time.

I meant M5 time.
Comment 28 Srikanth Sankaran CLA 2012-01-19 19:29:21 EST
(In reply to comment #27)
> (In reply to comment #26)
> 
> > You are right of course, we'll adopt the fix for bug 365582 at M6 time.
> 
> I meant M5 time.

Released the proper fix for M5.

(In reply to comment #25)

> This is actually fine. resources is null only if the ast level is 2 or 3 as
> this is initialized in the constructor for ast level >= 4.

Yes, indeed, I was led astray by the hang over memory of the compiler
AST having resources set to valid ones even in (ill formed) Java 6
programs.

For the maintenance streams, I'll leave things as they are as the
probability of this situation acting up are very low, realistically
speaking.