How to add automatically the imports in a Java project?
I generate (from a progam java) some Java classes but actually, I don't add the import in each class. I'd like to have a kind of parser that is able to see which import needs to be added in a class. Maybe, it's possible to reuse the fonctionnality of Eclipse because it's able to do that. However, I don't know wher开发者_Python百科e I can find that.
Do you have any idea?
Many thanks,
Bat
What about using only Full Qualified Names for classes when creating the code for the generated Java classes?
What are you going to do about name collisions? You use a "Date" - is it java.util.Date or java.sql.Date? You use a "XMLFoo", is it com.foomatics.XMLFoo or is it org.openfoo.XMLFoo? It's better to add the imports as you add the code.
Maybe you can create a HashSet of all the imports you're going to need as you generate the code, and then add them in at the top when you're done?
I see now... the problem is definitely on your generator. Work on that because even if you can extract the functionality from Eclipse, you won't get a 100% working solution every time. So my suggestion is just forget it and fix your generator.
As Paul said, you have to decide the imported class yourself. I usually type on Eclipse and at the end of the class I press Ctrl+Space to get some suggestions. So, for example I want to have java.util.Date
, I will write Date
and then Ctrl+Space and select java.util.Date
and Eclipse will automatically import java.util.Date
for me.
If the code is already there, I will do quick fix (F1 on Eclipse) and it will also suggest some fixes to the code I have (one of them is to import the suitable class).
精彩评论