auto import adding full package name to the line

Answered

When using auto-import via the Alt-Enter button, it gives me list of possible classes.  When I click on one I expect it to add the class to the list of imports at the top of the page.  Instead if changes the reference to the fully qualified name.  Is there a way to switch this?  

For example:

    Foo foo = new Foo();

I want this to become:

    import ack.bar.Foo;

    Foo foo = new Foo();

but instead I get this:

    ack.bar.Foo foo = new ack.bar.Foo();

17 comments
Comment actions Permalink

Make sure this option is not enabled:

2
Comment actions Permalink

That's not checked.  My settings look similar except I have 5 and 3 respective for the counts.  

0
Comment actions Permalink

Please share a sample project with the exact steps to reproduce.

0
Comment actions Permalink

What's the easiest way to do that?  You want the actual IDEA project?  

Here's the repository: https://github.com/strattonbrazil/munch

I'm using the sbt plugin, which may or may not be related.  I get these kind of warnings for classes that are in the same package.  

Before alt-enter: 

after alt-enter:

 

0
Comment actions Permalink

Package statement doesn't correspond to the class location (package), hence the problem. Please fix it and imports will work.

0
Comment actions Permalink

I having the same problem with 2017.3 EAP.

alt-enter generates:

import org.slf4j.*;
import uk.co.magdalene.migrationtool.interfaces.*;

import javax.ejb.*;
import javax.persistence.*;
import java.util.*;

The package name matches the directory structure.

 

0
Comment actions Permalink

Hello Roger,

Could you please clarify what is wrong with the import? Also it would be great if you could provide sample project example.

 

0
Comment actions Permalink

Updating my IntelliJ seems to have fixed the problem. I was using:

IntelliJ IDEA 2018.2.8 (Ultimate Edition)
Build #IU-182.5262.2, built on March 28, 2019

I upgraded to:

IntelliJ IDEA 2019.1.2 (Ultimate Edition)
Build #IU-191.7141.44, built on May 7, 2019

0
Comment actions Permalink

IntelliJ IDEA 2019.2.2 (Ultimate Edition)
Build #IU-192.6603.28, built on September 6, 2019


IDEA does not put a path to the import section if dependency is higher in the structure. 

full_path

src_folder

    ╠ dependency_class

    ╚ inner_folder

        ╚ file_with_dependecy_class

file_with_dependecy_class will have a variable with type <full_path.src_folder.dependency_class> and highlighted 'Unnecessarry qualified reference'. Action 'replace qualified name with import' do nothing. 

Put import manually does not see src_folder, but as variable type sees correct.

0
Comment actions Permalink

This is an acquired behavior because the project was obtained from the git repository with the correct view, but IDEA itself made changes to the import.

Same behavior for structure :

project

╚ src

   ╚ main

      ╚ java

         ╚ org

            ╚ project

               ╚ subfolder

                  ╠ dependency_class

                  ╚ inner_folder

                     ╚ file_with_dependecy_class

 

                 

0
Comment actions Permalink

Please upload the sample project at https://uploads.services.jetbrains.com/ and specify the file name.

0
Comment actions Permalink

I have the same problem on Ubuntu 19.04 with 

IntelliJ IDEA 2019.2.3 (Ultimate Edition)
Build #IU-192.6817.14, built on September 24, 2019

 

For me it is when importing generated classes.

0
Comment actions Permalink

Fs, we'd appreciate a sample project to reproduce it, thanks.

0
Comment actions Permalink

I found the error. For some reason, Idea had messed up the folder structure. On disk the folder structure and package structure was identical for the generated classes, but in Idea the package structure skipped two levels of folders. Starting over and doing a new clone of the repo sorted it out.

I have no sample project to send in.

0
Comment actions Permalink

Hi, I have same issue

I already upload the demo project to https://uploads.services.jetbrains.com/

demo-spring-protobuf.zip

 

you can check AddPerson.java, and try to fix the import 

here is correct import:

import com.example.tutorial.AddressBookProtos.AddressBook;
import com.example.tutorial.AddressBookProtos.Person;
Person

 

 

but IDEA always generate this

import com.example.tutorial.AddressBookProtos;
AddressBookProtos.Person

 

btw, I use Alt+enter to import.

0

Please sign in to leave a comment.