Bug 549952 - [evaluation] Add support for evaluating expressions with anonymous class instances.
Summary: [evaluation] Add support for evaluating expressions with anonymous class inst...
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Debug (show other bugs)
Version: 4.12   Edit
Hardware: PC Linux
: P3 enhancement (vote)
Target Milestone: 4.17 M1   Edit
Assignee: Gayan Perera CLA
QA Contact:
URL:
Whiteboard:
Keywords: bugday, helpwanted, noteworthy
Depends on:
Blocks:
 
Reported: 2019-08-09 22:15 EDT by Gayan Perera CLA
Modified: 2021-02-16 12:17 EST (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gayan Perera CLA 2019-08-09 22:15:27 EDT
public static void main(String[] args) {
(A)		int calculate = calculate(new IntSupplier() {

			@Override
			public int getAsInt() {
				return 10 * 20;
			}
		});

(B)		int calc = calculate(new IntSup());
	}

	public static int calculate(IntSupplier supplier) {
		return supplier.getAsInt() * 2;
	}

	private static class IntSup implements IntSupplier {

		@Override
		public int getAsInt() {
			return 10 * 20;
		}
	}

In the above code if i try to evaluate line (A) i get a evaluation error, but if i try to evaluate line (B) it evaluates without any issues. We need to add support for evaluating expressions with anonymous classes.

May be the fix for this code can be used to solve the https://bugs.eclipse.org/bugs/show_bug.cgi?id=448473 issue as well with a workaround solution.
Comment 1 Sarika Sinha CLA 2019-08-12 07:13:36 EDT
If someone can work on this, we can guide and review.
We have lot of inputs in Bug 448473 to start with.

I am occupied with Java 13 work and will not be able to work on this right now.
Comment 2 Gayan Perera CLA 2020-06-06 15:27:24 EDT
@Sarika

Seems like the evaluation engine receiving a wrong snippet because of this method implementation. org.eclipse.jdt.internal.debug.eval.ast.engine.EvaluationSourceGenerator.getCompleteSnippet(String)

But trying to hack it and get it to return the correct snippet cause two new errors that the evaluation cannot have anonymous classes and evaluations cannot have constructor calls of local types. Anonymous classes are written to disc as class files, right? Why the ASTEngine cannot load those class byte code?
Comment 3 Gayan Perera CLA 2020-06-07 05:52:34 EDT
I looked a bit more and found that in org.eclipse.jdt.internal.debug.eval.ast.engine.ASTInstructionCompiler.visit(ClassInstanceCreation) we skip the anonymous class creations. I think the reason is this anonymous class is not yet loaded into the debugging VM. 

For this do we need to take the same approach that the RemoteEvaluator has taken? Or can we just load the anonymous class bytes with a random class name and rewrite the expression with that class name. If so how to do this rewrite operation? any hints?
Comment 4 Sarika Sinha CLA 2020-06-08 01:15:23 EDT
(In reply to Gayan Perera from comment #3)
> I looked a bit more and found that in
> org.eclipse.jdt.internal.debug.eval.ast.engine.ASTInstructionCompiler.
> visit(ClassInstanceCreation) we skip the anonymous class creations. I think
> the reason is this anonymous class is not yet loaded into the debugging VM. 
> 
> For this do we need to take the same approach that the RemoteEvaluator has
> taken? Or can we just load the anonymous class bytes with a random class
> name and rewrite the expression with that class name. If so how to do this
> rewrite operation? any hints?

RemoteEvaluator approach might be better in this case.
Comment 5 Gayan Perera CLA 2020-06-20 12:55:26 EDT
I tried, but seems like i need to catch up lot on the internal working of debugger and how the remote evaluator supposed to work. Seems like it lot to take with the limited help i can get from the JDT members :(.
Comment 6 Gayan Perera CLA 2020-06-21 13:58:21 EDT
I tried again, i think i found out how the remote evaluator works like in the movie "A Beautiful Mind" :). I got it working for anonymous classes the evaluation and instructions are correctly getting executed with the remote evaluator. But i'm missing the return statement of the complete snippet which is generated from org.eclipse.jdt.internal.debug.eval.ast.engine.EvaluationSourceGenerator.getCompleteSnippet(String).

seems like it is hard to change this method with the way it has been written. because a inner class is a complex structure. Is there a way i can create a AST tree for the selected source text and traverse that and complete the statement to a compilable one ?
Comment 7 Sarika Sinha CLA 2020-06-22 00:14:26 EDT
(In reply to Gayan Perera from comment #6)
> I tried again, i think i found out how the remote evaluator works like in
> the movie "A Beautiful Mind" :). I got it working for anonymous classes the
> evaluation and instructions are correctly getting executed with the remote
> evaluator. But i'm missing the return statement of the complete snippet
> which is generated from
> org.eclipse.jdt.internal.debug.eval.ast.engine.EvaluationSourceGenerator.
> getCompleteSnippet(String).
> 
> seems like it is hard to change this method with the way it has been
> written. because a inner class is a complex structure. Is there a way i can
> create a AST tree for the selected source text and traverse that and
> complete the statement to a compilable one ?

You mean, you want to enhance "getCompleteSnippet" for inner class ?
Comment 8 Gayan Perera CLA 2020-06-22 02:24:40 EDT
@Sarika yes, today the getCompleteSnippet doesn’t support anonymous classes declarations at all. It tries to insert the return statement prematurely. I tried to fix it but with the current algorithm its hard to get the context of the code block. If we can create a small syntax tree it will be very much easy and simpler. But I’m not sure if it is possible to create a syntax tree for adhoc statement. 

At least that was the initial thought i have. Please let me know how we should handle this ?
Comment 9 Sarika Sinha CLA 2020-07-04 12:14:43 EDT
We should and an entry to N&N also, Thanks Gayan!
Comment 10 Eclipse Genie CLA 2020-07-05 05:38:42 EDT
New Gerrit change created: https://git.eclipse.org/r/c/www.eclipse.org/eclipse/news/+/165846
Comment 12 Sarika Sinha CLA 2020-07-09 07:06:33 EDT
Gayan can you add the entry in N&N?
Comment 13 Gayan Perera CLA 2020-07-09 08:09:49 EDT
Already added
Comment 14 Sarika Sinha CLA 2020-07-09 08:28:23 EDT
(In reply to Gayan Perera from comment #13)
> Already added

Where is it? Can't see the gerrit and the content? 
https://www.eclipse.org/eclipse/news/4.17/jdt.php#Debug
Comment 15 Gayan Perera CLA 2020-07-10 03:59:23 EDT
@Sarika the Gerrit is not approved on eclipse news repo. The gerrit is there in this issue.