NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Netbeans fix all imports classes, AND methods+variables

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
rwilson352



Joined: 12 Jul 2010
Posts: 21

PostPosted: Sun Feb 19, 2017 7:45 pm    Post subject: Netbeans fix all imports classes, AND methods+variables Reply with quote

Hi, since I've been using Netbeans the fix all imports feature (ctrl+shift+I) has always just worked for classes. But, a few years ago, I updated Netbeans and fix imports started working for methods and variables as well. Basically, if I have a variable that is not declared in the code, Netbeans will try to find that variable even searching through class libraries for the variable. Worse, if there is an undeclared method, it will look for that method even if the auto import would fix it.

Here's an illustration. If I copy/paste this code into Netbeans:

Code:

    class Task extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() throws Exception {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        protected void done() {
            try {
                get();
            } catch (ExecutionException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


And then I run the fix-import feature, I will be greeted with this dialog:



As you can see, the assumptions that Netbeans made about the classes are correct, but the `get()` method assumption is totally wrong. But, if Netbeans wouldn't worry about methods/variables, everything would be fine once the classes are imported (because `get()` is a method in the `SwingWorker` class).

Netbeans is almost perfectly accurate when it comes to figuring out the classes to import, but it is almost completely inaccurate when it comes to deciding which methods/variables to import.

Is there a way to tell Netbeans to import classes only?

Note: I know that there is a pull-down option on the get() method that is "none" so I can tell it to skip that. But, this is tedious when you have a lot of things to import.
Back to top
Sean Carrick
Posted via mailing list.





PostPosted: Mon Feb 20, 2017 2:26 am    Post subject: Netbeans fix all imports classes, AND methods+variables Reply with quote

rwilson,


This is perfectly normal and acceptable behavior. What NB is doing is giving you suggestions on where the method resides on the Fix Imports command.


Notice in your example image that "oracle.jrockit.jfr.JFR.get()" is in a drop down list. NB just locates all classes that have that method available, based on the signature you used, and presents the dialog box to you for you to make the correct choice.


Prior to this functionality, NB wouldn't fix the methods, but would suggest that the method be created in your current class. By searching the classes on the classpath for the method, NB is able to intelligently present options to you. If the method only resides in one class, NB would automatically create the `import ...' statement, but if the method resides in multiple classes, NB allows you to choose which of the available methods you are actually referring to.


I hope this helps you to better understand what NB is doing by presenting this dialog. If you were to click the drop-down list that shows the method above, you would find the `get()' method that you are actually desiring to use in the list, if the `get()' method you want is in your classpath. Therefore, if you were to drop down that list, you would find `javax.swing.*.SwingWorker.get()' listed, provided Swing is on your classpath. You could then choose that `get()' method import and NB would create the `import...' statement at the top of your class.


Quote:
--
[img]https://s3.amazonaws.com/ucwebapp.wisestamp.com/13aab24c-4877-4ae8-97d8-e5b95d5767a8/IntegritySolutionsSplash.format_png.resize_200x.png#logo[/img]
Sean Carrick
VP Software Development, Integrity Solutions
address-removed ([email]address-removed[/email]) [/url]



On Sun, 2017-02-19 at 19:45 +0000, rwilson352 wrote:
Quote:
Quote:
Hi, since I've been using Netbeans the fix all imports feature (ctrl+shift+I) has always just worked for classes. But, a few years ago, I updated Netbeans and fix imports started working for methods and variables as well. Basically, if I have a variable that is not declared in the code, Netbeans will try to find that variable even searching through class libraries for the variable. Worse, if there is an undeclared method, it will look for that method even if the auto import would fix it.

Here's an illustration. If I copy/paste this code into Netbeans:


Code:

class Task extends SwingWorker<Void, Void> {

@Override
protected Void doInBackground() throws Exception {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
protected void done() {
try {
get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
}



And then I run the fix-import feature, I will be greeted with this dialog:

[Image: [url=http://www.vantagecp.com/images/netbeans%20auto%20import.PNG]http://www.vantagecp.com/images/netbeans%20auto%20import.PNG ]

As you can see, the assumptions that Netbeans made about the classes are correct, but the `get()` method assumption is totally wrong. But, if Netbeans wouldn't worry about methods/variables, everything would be fine once the classes are imported (because `get()` is a method in the `SwingWorker` class).

Netbeans is almost perfectly accurate when it comes to figuring out the classes to import, but it is almost completely inaccurate when it comes to deciding which methods/variables to import.

Is there a way to tell Netbeans to import classes only?




Back to top
rwilson352



Joined: 12 Jul 2010
Posts: 21

PostPosted: Mon Feb 20, 2017 12:55 pm    Post subject: Reply with quote

Sean,

I understand all that, but (perhaps this is just my programming style) it is very rare to import a 'method' that is not a static method (e.g., 'Math.cos()') or an object method (e.g., 'Dog::wagTail()') but a standalone method with no class reference. That would be pretty bad code style, honestly, if I saw a method that was referencing another class but was by itself with no class descriptor near it.

Further, it can hardly be said that the Netbeans search is intelligent if after 'fix all imports' is run, there is an unused import (because the compiler uses the 'get()' in the 'SwingWorker' class before it uses the 'get' in the 'pracle.jrockit' package (see attached pic)

Lastly, I can't think of a single instance in my experience where Netbeans got it right when trying to import a method or variable from another class. It is outstandingly accurate when it is picking the classes to import, but it is outstandingly inaccurate when it is picking the methods/variables to import.

If I wanted to use the 'get()' in jrockit, I would have written 'oracle.jrockit.jfr.JFR.get()' in my code. If Netbeans would just stick to importing classes, there would be no confusion here.

The question "can this be turned off?" seems to be no. Is that right?



netbeans auto import2.PNG
 Description:
 Filesize:  3.95 KB
 Viewed:  3183 Time(s)

netbeans auto import2.PNG


Back to top
Sean Carrick
Posted via mailing list.





PostPosted: Mon Feb 20, 2017 5:28 pm    Post subject: Netbeans fix all imports classes, AND methods+variables Reply with quote

rwilson,


AFAIK, this behavior cannot be turned off, but I may not be aware of the option if it can be.


Personally, I don't believe that there's really an issue with the Fix Imports, because if there is a collision (the same method in multiple classes), NB gives you the option to choose which of the available class methods you are wanting to use. To my way of thinking, that is the proper way for the IDE to handle the situation. It's not like the IDE could be programmed to read a developer's mind, right? (Boy, wouldn't we get rich if we could figure out a way to do THAT!)


I understand the gist of what you are saying, but if you want to have Fix Imports get it right all of the time, when it comes to importing the classes that contain the method you want to use, each and every method would have to be uniquely named so that no collisions took place. However, that would kill the polymorphism of the language, because no one would ever be able to @Override any methods to provide additional functionality, which is where the power of Java resides.


Most developers, however, have the need to @Override methods in classes, and then those @Overrides become valuable to other developers, so that we end up with the same method name in multiple classes. Therefore, NB handles this situation in the best possible manner...by providing the developer the ability to select the class that contains the method they want to use. Truly, I cannot fathom any other way to handle such a situation in a better manner.


Remember back when you started programming in Java, in any IDE, and try to recall how many times you wanted to use a method, but could not remember which package and class that particular strain of the method resided in. At least with NB prompting you for which package and class you are referring, you don't have to try to remember them all. That's just my opinion.
Quote:
--
[img]https://s3.amazonaws.com/ucwebapp.wisestamp.com/13aab24c-4877-4ae8-97d8-e5b95d5767a8/IntegritySolutionsSplash.format_png.resize_200x.png#logo[/img]
Sean Carrick
VP Software Development, Integrity Solutions
address-removed ([email]address-removed[/email]) [url=http://facebook.com/PekinSOFT][/url]



On Mon, 2017-02-20 at 12:55 +0000, rwilson352 wrote:
Quote:
Quote:
Sean,

I understand all that, but (perhaps this is just my programming style) it is very rare to import a 'method' that is not a static method (e.g., 'Math.cos()') or an object method (e.g., 'Dog::wagTail()') but a standalone method with no class reference. That would be pretty bad code style, honestly, if I saw a method that was referencing another class but was by itself with no class descriptor near it.

Further, it can hardly be said that the Netbeans search is intelligent if after 'fix all imports' is run, there is an unused import (because the compiler uses the 'get()' in the 'SwingWorker' class before it uses the 'get' in the 'pracle.jrockit' package:

[img]www.vantagecp.com/images/netbeans%20auto%20import2.PNG[/img]

Lastly, I can't think of a single instance in my experience where Netbeans got it right when trying to import a method or variable from another class. It is outstandingly accurate when it is picking the classes to import, but it is outstandingly inaccurate when it is picking the methods/variables to import.

If I wanted to use the 'get()' in jrockit, I would have written 'oracle.jrockit.jfr.JFR.get()' in my code. If Netbeans would just stick to importing classes, there would be no confusion here.

The question "can this be turned off?" seems to be no. Is that right?




Back to top
Sean Carrick
Posted via mailing list.





PostPosted: Mon Feb 20, 2017 5:33 pm    Post subject: Netbeans fix all imports classes, AND methods+variables Reply with quote

rwilson,


Sorry to give another reply so quickly, but I just noticed in your image that the oracle.jrockit.JFR class that you are referencing is @Deprecated, anyway. It's fairly bad programming style to want to use deprecated classes or methods anyway. I mean, there was just an article regarding Java 9 that made mention of the fact that Oracle is going to start removing deprecated classes and methods out of the Java language, which I would imagine would trace down to their other API packages, as well. Just a thought...
Quote:
--
[img]https://s3.amazonaws.com/ucwebapp.wisestamp.com/13aab24c-4877-4ae8-97d8-e5b95d5767a8/IntegritySolutionsSplash.format_png.resize_200x.png#logo[/img]
Sean Carrick
VP Software Development, Integrity Solutions
address-removed ([email]address-removed[/email]) [url=http://facebook.com/PekinSOFT][/url]



On Mon, 2017-02-20 at 12:55 +0000, rwilson352 wrote:
Quote:
www.vantagecp.com/images/netbeans%20auto%20import2.PNG
Back to top
Eirik Bakke
Posted via mailing list.





PostPosted: Tue Feb 21, 2017 2:39 am    Post subject: Netbeans fix all imports classes, AND methods+variables Reply with quote

+1. I'd love an option to tell "Fix Imports" to avoid generating static
imports, or even make it the default. My project's code style is to never
use static imports at all, so I'd prefer if Fix Imports just left any
unresolveable methods as errors (which can then be reviewed in the editor).

-- Eirik

On 2/19/17, 2:45 PM, "rwilson352" <address-removed> wrote:

Quote:
Hi, since I've been using Netbeans the fix all imports feature
(ctrl+shift+I) has always just worked for classes. But, a few years ago,
I updated Netbeans and fix imports started working for methods and
variables as well. Basically, if I have a variable that is not declared
in the code, Netbeans will try to find that variable even searching
through class libraries for the variable. Worse, if there is an
undeclared method, it will look for that method even if the auto import
would fix it.

Here's an illustration. If I copy/paste this code into Netbeans:


Code:

class Task extends SwingWorker<Void, Void> {

@Override
protected Void doInBackground() throws Exception {
throw new UnsupportedOperationException("Not supported
yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
protected void done() {
try {
get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
}



And then I run the fix-import feature, I will be greeted with this dialog:

[Image: http://www.vantagecp.com/images/netbeans%20auto%20import.PNG ]

As you can see, the assumptions that Netbeans made about the classes are
correct, but the `get()` method assumption is totally wrong. But, if
Netbeans wouldn't worry about methods/variables, everything would be fine
once the classes are imported (because `get()` is a method in the
`SwingWorker` class).

Netbeans is almost perfectly accurate when it comes to figuring out the
classes to import, but it is almost completely inaccurate when it comes
to deciding which methods/variables to import.

Is there a way to tell Netbeans to import classes only?



Back to top
tkellerer



Joined: 15 Aug 2008
Posts: 737
Location: Bavaria

PostPosted: Tue Feb 21, 2017 6:54 am    Post subject: Re: Netbeans fix all imports classes, AND methods+variables Reply with quote

Eirik Bakke wrote:
+1. I'd love an option to tell "Fix Imports" to avoid generating static
imports, or even make it the default. My project's code style is to never
use static imports at all, so I'd prefer if Fix Imports just left any
unresolveable methods as errors (which can then be reviewed in the editor).

For one you could simply stop using "Fix Imports", and do a manual import of the needed class while you write the code using the "Fast Import" shortcut (Alt-Shift-I)

Did you disable the "Prefer static import" option? I have hardly seen any static imports when using Fix Imports (I think it only happens with JUnit methods) - but then I usually prefix the method call with the class anyway, so that the method never needs to be imported.
Back to top
Sean Carrick
Posted via mailing list.





PostPosted: Wed Feb 22, 2017 1:53 pm    Post subject: Netbeans fix all imports classes, AND methods+variables Reply with quote

rwilson,


I apologize for responding to your question regarding imports without fully understanding what you were saying.


After rereading your message thread a couple of times, I see what you are saying more clearly. The issue is in using the proper tool for the job. When you are referencing a method in a class and not wanting to import the class, instead of using the "Fix Imports..." or "Fix All Imports" tools, you should right-click on the little yellow lightbulb in the left margin of the editor that shows up on the line in question. This will present you with some options of how to handle the issue.


Going back to your example of the 'get()' method, instead of using the "Fix Imports..." tool, if you place your cursor on the line that contains that method, you will see a little yellow lightbulb in the left margin of the editor (LightbulbTips.png). When you click or right-click on this icon, you get multiple options, depending on what the IDE determines to be the task you are trying to create. Usually, you will see some standard actions, like "Create method get() in <package-name>.<class-name>..." and also some other options such as importing a class that contains the method or placing the fully-qualified <package>.<class>.<method> in the line.


When coding, if you simply want to use the fully-qualified method name, use the lightbulb icon on that line first, then do your "Fix Imports..." work. This should solve the issue that you are having. Best of luck to you!
Quote:
--
[img]https://s3.amazonaws.com/ucwebapp.wisestamp.com/13aab24c-4877-4ae8-97d8-e5b95d5767a8/IntegritySolutionsSplash.format_png.resize_200x.png#logo[/img]
Sean Carrick
VP Software Development, Integrity Solutions
address-removed ([email]address-removed[/email]) [url=http://facebook.com/PekinSOFT][/url]



On Mon, 2017-02-20 at 12:55 +0000, rwilson352 wrote:
Quote:
Quote:
Sean,

I understand all that, but (perhaps this is just my programming style) it is very rare to import a 'method' that is not a static method (e.g., 'Math.cos()') or an object method (e.g., 'Dog::wagTail()') but a standalone method with no class reference. That would be pretty bad code style, honestly, if I saw a method that was referencing another class but was by itself with no class descriptor near it.

Further, it can hardly be said that the Netbeans search is intelligent if after 'fix all imports' is run, there is an unused import (because the compiler uses the 'get()' in the 'SwingWorker' class before it uses the 'get' in the 'pracle.jrockit' package:

[img]www.vantagecp.com/images/netbeans%20auto%20import2.PNG[/img]

Lastly, I can't think of a single instance in my experience where Netbeans got it right when trying to import a method or variable from another class. It is outstandingly accurate when it is picking the classes to import, but it is outstandingly inaccurate when it is picking the methods/variables to import.

If I wanted to use the 'get()' in jrockit, I would have written 'oracle.jrockit.jfr.JFR.get()' in my code. If Netbeans would just stick to importing classes, there would be no confusion here.

The question "can this be turned off?" seems to be no. Is that right?




Back to top
rwilson352



Joined: 12 Jul 2010
Posts: 21

PostPosted: Fri Feb 24, 2017 1:11 pm    Post subject: Re: Netbeans fix all imports classes, AND methods+variables Reply with quote

Sean Carrick wrote:
rwilson,


Personally, I don't believe that there's really an issue with the Fix Imports, because if there is a collision (the same method in multiple classes), NB gives you the option to choose which of the available class methods you are wanting to use. To my way of thinking, that is the proper way for the IDE to handle the situation. It's not like the IDE could be programmed to read a developer's mind, right? (Boy, wouldn't we get rich if we could figure out a way to do THAT!)


Sean,

The risk of collision on Classes is far less than the risk of collision on methods/variables. My example is an excellent illustration: when finding the two classes, there is no collision, but when finding the method, there are half a dozen (see attached image).

So, if Netbeans weren't trying to search for methods/variables, I wouldn't even need the collisions prompt to begin with. Again, if I wanted to use a method or variable from an outside class, I would've manually written the class name.

For sure there will be collisions, and I am very happy to go through and fix the collisions when they occur. But before Netbeans started searching for methods/variables, this process was quick and easy. Now that it searches for methods/variables, this process is long. And I -always- select "do nothing" for the methods/variables. Always.

Quote:
I understand the gist of what you are saying, but if you want to have Fix Imports get it right all of the time


I'm not trying to argue with you, but when I point out to you that this is a strawman I hope you realize that you aren't understanding my request. I don't expect Netbeans to get it right "all of the time." But, if methods/variables weren't part of the fix imports, there would be far far far fewer collisions in the fix imports.

Quote:
when it comes to importing the classes that contain the method you want to use, each and every method would have to be uniquely named so that no collisions took place.


Noooo, not at all. It needs to simply stop looking for methods in outside classes. I -never- reference a method that needs to be imported without manually adding the class name next to it (if it's static). I have never met a programmer who codes in a way that they expect to -import- a -method-.

Quote:
After rereading your message thread a couple of times, I see what you are saying more clearly. The issue is in using the proper tool for the job. When you are referencing a method in a class and not wanting to import the class, instead of using the "Fix Imports..." or "Fix All Imports" tools, you should right-click on the little yellow lightbulb in the left margin of the editor that shows up on the line in question. This will present you with some options of how to handle the issue.


This would only be relevant as you are typing your code. But if you copy/paste code into a new file with no imports and do a 'fix all imports' and you have dozens of things to import, that is usually the only time I use fix all imports.



netbeans auto import3.PNG
 Description:
 Filesize:  18.05 KB
 Viewed:  3042 Time(s)

netbeans auto import3.PNG


Back to top
rwilson352



Joined: 12 Jul 2010
Posts: 21

PostPosted: Fri Feb 24, 2017 1:17 pm    Post subject: Reply with quote

THIS is bad code:

Code:
import static java.lang.Math.cos;

public class Test {
    public static void main(String[] args) {
        cos(2);
    }
}


This is the proper way to write the code above:

Code:
public class Test {
    public static void main(String[] args) {
        Math.cos(2);
    }
}


Using the imports for static methods is awful practice, in my opinion. I have never seen a programmer with that style. I doubt I would be long suffering toward them
Back to top
Sean Carrick
Posted via mailing list.





PostPosted: Sun Feb 26, 2017 1:39 pm    Post subject: Netbeans fix all imports classes, AND methods+variables Reply with quote

rwilson,


Quote:
Quote:
THIS is bad code:


Code:
import static java.lang.Math.cos;

public class Test {
public static void main(String[] args) {
cos(2);
}
}


I agree with you that this is bad code. No one should *ever* access static methods in this manner.


However, in the example that you've been using, the get() method that is popped up in the selection dialog is not static, so, cannot be accessed as Math.cos() is able to be. Looking at each of the get() methods in the screenshot you posted, looking up the JavaDocs of each of them in turn (as best I could), I saw none of those get() methods as being static.


So, I guess I am still a little confused about what the issue to which you are referring is. One moment you are referencing non-static methods and then you are comparing that with static methods. It's like an attempt to compare apples to oranges...unless, of course, I'm missing something (which is most likely).
Quote:
--
[img]https://s3.amazonaws.com/ucwebapp.wisestamp.com/13aab24c-4877-4ae8-97d8-e5b95d5767a8/IntegritySolutionsSplash.format_png.resize_200x.png#logo[/img]
Sean Carrick
VP Software Development, Integrity Solutions
address-removed ([email]address-removed[/email]) [url=http://facebook.com/PekinSOFT][/url]



On Fri, 2017-02-24 at 13:17 +0000, rwilson352 wrote:
Quote:
Quote:
THIS is bad code:


Code:
import static java.lang.Math.cos;

public class Test {
public static void main(String[] args) {
cos(2);
}
}



This is the proper way to write the code above:


Code:
public class Test {
public static void main(String[] args) {
Math.cos(2);
}
}



Using the imports for static methods is awful practice, in my opinion. I have never seen a programmer with that style. I doubt I would be long suffering toward them




Back to top
rwilson352



Joined: 12 Jul 2010
Posts: 21

PostPosted: Mon Oct 16, 2017 12:48 pm    Post subject: Re: Netbeans fix all imports classes, AND methods+variables Reply with quote

Sean Carrick wrote:
Looking at each of the get() methods in the screenshot you posted, looking up the JavaDocs of each of them in turn (as best I could), I saw none of those get() methods as being static.


Sean, sorry for this bump of an old post.

The fact that none of the methods in that image were static goes to this question: why on earth is the importer suggesting them? If I didn't instantiate any object of those classes, why why why would it suggest I might be trying to use those methods? I assumed these methods were static because that's the only logical reason I can see the importer suggesting them as possibilities. Truth is stranger than fiction, it seems.

I have never desired to import a method and I have never met anyone who has. Methods are either a) members of classes or b) static and in -both- cases I will specifically tell Netbeans what I want.

Don't worry about importing methods for me, just focus on classes. This is what I wish I could tell Netbeans.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo