This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 244055

Summary: Introduce a variable doesn't work
Product: java Reporter: ma.schroed
Component: HintsAssignee: Svata Dedic <sdedic>
Status: RESOLVED WONTFIX    
Severity: normal CC: ralphbenjamin
Priority: P3    
Version: 8.0   
Hardware: PC   
OS: Windows 7   
Issue Type: DEFECT Exception Reporter:

Description ma.schroed 2014-04-24 20:59:41 UTC
I'm working on a Java Project using Java 1.8.0. The refactoring feature 'Introduce Variable' sometimes doesn't work.

With the following sample it works:

int a = 2;
int b = 2;
System.out.println(a/b);

If I want to introduce a new variable 'r', based on 'a/b', I get what I expect:

int a = 2;
int b = 2;
int r = a/b;
System.out.println(r);

But when I add a multiplier before the division I get an error 'Invalid selection'. So I can't extract 'a/b':

System.out.println(2 * a/b);

However, when brackets are added around the division the refactoring works:

System.out.println(2 * (a/b));
Comment 1 Svata Dedic 2014-04-25 10:54:16 UTC
Not a P2 defect.
Yes, it's a known issue, sadly. The reason is that the selection is (currently) implemented supposing that a region for a complete AST subtree is selected. This is the case of a/b selection. In 2 * a / b, the * associates left, so the expression is (in the tree form) actually (2 * a) / b; so a/b selection spans two branches of the AST treee, but not their common root -> invalid selection.

It's unfortunate, since the internal form does not make sense to the user. The way how we work with selection must change.
Comment 2 Martin Balin 2016-07-07 07:18:04 UTC
This old bug may not be relevant anymore. If you can still reproduce it in 8.2 development builds please reopen this issue.

Thanks for your cooperation,
NetBeans IDE 8.2 Release Boss