-
-
Notifications
You must be signed in to change notification settings - Fork 355
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
Comments
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)
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).
Could you elaborate more about your usecase then? |
Hi @surli, An example of my use case is Original source
Then it is supposed to insert new Objects/ClassReferences which will require new import statement.
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. |
In that kind of transformation, Spoon should add automatically the new needed imports: you don't have to add them manually.
Yes that's the normal behaviour of Spoon.
Indeed. The idea is to keep the original imports that are still used. For example, if you had the code of your class |
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. |
Thanks @tempuser1909 how do you use this code together with Spoon? |
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. |
Interesting, thanks. Why do you need to rewrite the imports since the code is written in fully-qualified mode ( |
First, I have to say that I did not know that 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 |
Why? |
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. |
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. |
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.
The text was updated successfully, but these errors were encountered: