Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there are any settings to retain import statements? #2397

Closed
tempuser1909 opened this issue Aug 20, 2018 · 11 comments
Closed

Is there are any settings to retain import statements? #2397

tempuser1909 opened this issue Aug 20, 2018 · 11 comments

Comments

@tempuser1909
Copy link
Contributor

Hi,

Can I ask how do I add import statement?

Or how do I ensure that the original import statements remain intact?

I only know about setAutoImports() but it does not seem help in my use case.

Thank you.

@surli
Copy link
Collaborator

surli commented Aug 20, 2018

Hi @tempuser1909

Can I ask how do I add import statement?

There is no easy API to do that for now: I'm working on the whole import mechanism for Spoon but it's still a work in progress. In the meantime I provided in this message some insight to add an import statement manually: #1267 (comment)

Or how do I ensure that the original import statements remain intact?

The original import statements should remain intact: in fact, they might change depending on the transformation you make in your code (e.g. if you add a new field with an external type for example).
Besides that, our strategy is to keep the original imports.

I only know about setAutoImports() but it does not seem help in my use case.

Could you elaborate more about your usecase then?

@tempuser1909
Copy link
Contributor Author

tempuser1909 commented Aug 21, 2018

Hi @surli,

An example of my use case is

Original source

public class A {
}

Then it is supposed to insert new Objects/ClassReferences which will require new import statement.

public class A {
    Scanner scan = new Scanner();
}

On another note, I noticed that for me when i do prettyprint() with setAutoImports(false), it does not generate any import statements.

And when I do prettyprint() with setAutoImports(true), it does generate some import statements but it is not the original import statements. Because I am testing on an example Java file, I was thinking if unused imports will be deleted. Can I ask if that is true?

Thank you.

@surli
Copy link
Collaborator

surli commented Aug 25, 2018

Then it is supposed to insert new Objects/ClassReferences which will require new import statement.

In that kind of transformation, Spoon should add automatically the new needed imports: you don't have to add them manually.
Now don't hesitate to tell us if it seems to not working properly.

when i do prettyprint() with setAutoImports(false), it does not generate any import statements.

Yes that's the normal behaviour of Spoon.

And when I do prettyprint() with setAutoImports(true), it does generate some import statements but it is not the original import statements. Because I am testing on an example Java file, I was thinking if unused imports will be deleted. Can I ask if that is true?

Indeed. The idea is to keep the original imports that are still used. For example, if you had the code of your class A with a Scanner and you do a transformation to remove the scanner type, then you don't want anymore the import in your class. That's why we decided to automatically remove the unused imports, even if they were in the original java file and you did not change its code.

@monperrus monperrus changed the title How to insert import statement or if there are any settings to retain import statements? Is there are any settings to retain import statements? Aug 27, 2018
@tempuser1909
Copy link
Contributor Author

tempuser1909 commented Sep 17, 2018

Not sure if it will help anyone but I will put the class I wrote that helps inserts import statements easier here.

It is a direct way of inserting the import statements where it reads and writes directly to the Java file.

ImportWriter.java

@monperrus
Copy link
Collaborator

Thanks @tempuser1909 how do you use this code together with Spoon?

@tempuser1909
Copy link
Contributor Author

tempuser1909 commented Sep 19, 2018

Hi @monperrus, I have added Main.java in the gist to show an example of how I used it with Spoon.

The idea is to get the files you want from Spoon's CtModel and add the defined import statements.

However, some improvements could have been made.

For example, I could have implemented a way to define different "MUST-HAVE" import statements for different files.

In my use case, I am not really too concerned with having unused imports so I did not implement that.

@monperrus
Copy link
Collaborator

Interesting, thanks. Why do you need to rewrite the imports since the code is written in fully-qualified mode (setAutoImports(false))?

@tempuser1909
Copy link
Contributor Author

tempuser1909 commented Sep 20, 2018

First, I have to say that I did not know that setAutoImports(false) means to convert all type references to fully-qualified identifier, I just thought that it probably means no import statements are added (looking back now, it makes sense)

Second, I do not want to disturb the existing code as much as possible. I only wanted to have access to all the elements via an API and modify what I need to modify. The code that I am working with has been written to be as readable as possible. So with fully-qualified identifiers, I think it will make the code less readable.

Also, I have to add that I wrote that class during my phase of seeing what Spoon can do, so I only used setAutoImports(false). And only recently when I have been testing with complete code that I have used setAutoImports(true) which works fine (but I still wanted to retain the imports from the original source as it is just part of my criteria).

@monperrus
Copy link
Collaborator

I still wanted to retain the imports from the original source

Why?

@tempuser1909
Copy link
Contributor Author

It is as a convenient way of having all the essential packages imported without having to face the trouble of importing them when you are coding halfway.

Even though this practice is not really recommended, convenience is more of my priority for what I am working on.

@pvojtechovsky
Copy link
Collaborator

Spoon actually doesn't support forcing of printing of import if the import is not used.

Workaround: Add javadoc which contains links to all by default imported types. That forces spoon to keep the import there even if it is not used in java code.

Real solution would need a change in ImportScannerImpl#usedImport Map values has to be set to TRUE.

I have found no way how to do it without changing Spoon code, because all fields are private.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants