You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by "Ben Miller (JIRA)" <ji...@apache.org> on 2019/08/15 18:50:00 UTC

[jira] [Comment Edited] (NETBEANS-2914) Fix imports not working properly

    [ https://issues.apache.org/jira/browse/NETBEANS-2914?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16908406#comment-16908406 ] 

Ben Miller edited comment on NETBEANS-2914 at 8/15/19 6:49 PM:
---------------------------------------------------------------

I've been wrestling with this one for some time as well. The issue is that the import fixer can't distinguish between these two types of token:
 * The first token of a package name, such as the io in io.reactivex.schedulers
 * The outer class of a nested class, such as the Map in Map.Entry

Only the second case should be detected as an unresolved import.

This isn't a serious problem most of the time, because it's looking for something importable named "com" or "java" or "org", which usually doesn't exist, so it assumes the import is unfixable and ignores it. But it gets very irritating when you have a dependency that does contain a class matching the first token of an import statement - io in your case. That token doesn't have to be part of the same import statement, you would get the same error if you were importing io.Foo and a different dependency contained com.Bar.io.

In my case it's ca, not io, causing problems, but the same cause.

 

I've got a hacky fix: Only attempt to fix imports on a package-type token if it begins with an upper-case letter. This distinguishes between classes and packages as long as they're named in the usual way, but will break the fixer sometimes when they're not. Hopefully someone can come up with a better solution. For now, if this is driving you as crazy as it's driving me, the "fix" is simple - change ComputeImports.java:630 (NetBeans 11.1) from this:
{code:java}
if (type != null && type.getKind() == TypeKind.PACKAGE) {
{code}
to this:
{code:java}
if (type != null && type.getKind() == TypeKind.PACKAGE 
        && !el.getSimpleName().toString().isEmpty() 
        && Character.isUpperCase(el.getSimpleName().toString().charAt(0))) {

{code}


was (Author: ben.r.miller):
I've been wrestling with this one for some time as well. The issue is that the import fixer can't distinguish between these two types of token:
 * The first token of a package name, such as the io in io.reactivex.schedulers
 * The outer class of a nested class, such as the Map in Map.Entry

Only the second case should be detected as an unresolved import.

This isn't a serious problem most of the time, because it's looking for something importable named "com" or "java" or "org", which usually doesn't exist, so it assumes the import is unfixable and ignores it. But it gets very irritating when you have a dependency that does contain a class matching the first token of an import statement - io in your case. That token doesn't have to be part of the same import statement, you would get the same error if you were importing io.Foo and a different dependency contained com.Bar.io.

In my case it's ca, not io, causing problems, but the same cause.

 

I've got a hacky fix: Only attempt to fix imports on a package-type token if it begins with an upper-case letter. This distinguishes between classes and packages as long as they're named in the usual way, but will break the fixer sometimes when they're not. Hopefully someone can come up with a better solution. For now, if this is driving you as crazy as it's driving me, the "fix" is simple - change ComputeImports.java:630 (NetBeans 11.1) from this:
{code:java}
if (type != null && type.getKind() == TypeKind.PACKAGE) {
{code}
to this:
{code:java}
if (type != null && type.getKind() == TypeKind.PACKAGE && !el.getSimpleName().toString().isEmpty() && Character.isUpperCase(el.getSimpleName().toString().charAt(0))) {

{code}

> Fix imports not working properly
> --------------------------------
>
>                 Key: NETBEANS-2914
>                 URL: https://issues.apache.org/jira/browse/NETBEANS-2914
>             Project: NetBeans
>          Issue Type: Bug
>          Components: java - Editor
>    Affects Versions: 11.1
>            Reporter: Eduard Catala
>            Priority: Major
>         Attachments: Captura de pantalla de 2019-07-29 18-55-06.png
>
>
> Steps to reproduce:
> Create a simple project (maven)
> Include a dependency:
> {\{ <dependency>}}
>  {{   <groupId>io.reactivex.rxjava2</groupId>}}
>  {{   <artifactId>rxjava</artifactId>}}
>  {{   <version>2.2.10</version>}}
>  \{{ </dependency>}}
>  
> Create a main method:
>  
> {{public class Main {}}
>  {{   public static void main(String[] args) {}}
>  {{     Single<String> s;}}
>  {{   }}}
>  {{}}}
>  
> Fix imports (works)
> Fix imports twice: The fix imports dialog is shown with incorrect import proposal (view attached screenshot).
>  
> Having the option "fix imports on save" is a nigthmare!
>  
>  



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists