Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
User defined imports #507
I want to add some imports with wildcard but I cannot find any method like addStaticImport. Is there any other way how to do it?
Here is my scenario :
This XML file contains "field" elements :
where "Type" attribute contains a java type. From this element I generate something like this :
My problem is that I also want to add this import :
because I would like to import all classes from common library so that the end user wouldn't need to specify full path for type i.e Type="org.common.SyntaxToken".
Because a "Type" in a "Field" element can have different forms i.e. a simple class name and also a full class name. Also the user can use classes from a package where maven plugin generates java source files because same package he uses for its own files. So it is not easy to know which package should be used.
Maybe a little better example :
So I would like the user to decide if he will use a type with a full path or he will define some packages in plugin configuration and these packages will be added to generated files.
If i understand also java poet does something very similar with skipJavaLangImports method.
For a class with a full path I could split a string to the name and the package. But the real problem are classes without the full path. You cannot simply decide which package you should use because you are in the middle and you don't have necessary information. Because you know nothing about user's classes and also about common library classes. For illustration, imagine that you have javasource and someone removed all imports and there are some types without fullpath. How do you decide which import for this type you have to use? From my point of view, a code generator should allow to add some custom code when the user decides, because the user has got necessary information.
If you don't know the full name of the class you're referencing then JavaPoet isn't going to work for you. I recommend you fix your sources to be unambiguous. If that's not possible you can use JavaPoet and then manually add the imports by doing string manipulation on JavaPoet’s output.