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();

2
25 comments

Make sure this option is not enabled:

3
Avatar
Permanently deleted user

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

0

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

0
Avatar
Permanently deleted user

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

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

0

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.

 

1

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

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
Avatar
Permanently deleted user

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.

1
Avatar
Permanently deleted user

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

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

-1

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.

1

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

-1

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
Avatar
Permanently deleted user

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
Avatar
Permanently deleted user

Same problem here.

2

Henrik, could you please also share a sample project to show the problem that you have? There were multiple different problems mentioned in this thread so it's not clear which one you have.

0

I had the same problem, too. It turned out to be a cache problem. After clearing the cache, it became normal again...

0

I am facing same issue. When I use Alt+enter or mouse click (on import tool. tip) to import the required class, Intellij imports fully qualified class name in the code. This makes it so ugly and frustrating. 

Version: Intellij IDEA 2021.1.3 (Ultimate Edition)

I have checked in the settings and made sure "Use fully qualified class names" check box is not clicked in the Java language settings. 

I have tried to clear cache and restart the IDE multiple times and made sure the class hierarchy is right. 

Its kinda a pain to get over this problem and not sure why this simple issue cannot be fixed.

0

Sadeshpande Do you have any plug-ins installed that override IDE formatting, such as Eclipse Code Formatter? Does it help if you disable it? See also if disabling Settings (Preferences on macOS) | Editor | Code Style | Enable EditorConfig support helps.

0

Serge Baranov No, I don't have Eclipse Code Formatter. Only plugin installed additionally is Bazel plugin. Other than that all are the ones that come bundled with IDE. 

Yes, I tried disabling EditorConfig as well now, it makes no difference.

 

0

Sadeshpande

Please upload a sample project to reproduce the issue and your code style settings (File | Manage IDE Settings | Export Settings) at https://uploads.jetbrains.com, provide the ID here.

0

Hi,

I faced the same problem but i've found a solution

My projet was misconfigured. My kotlin source directory was declared as a "resource" directory, not a source directory, in projet structure / modules

Setting the folder as a source directory have fixed the issue

1

Please sign in to leave a comment.