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

User defined imports #507

Closed
jherkel opened this Issue Sep 24, 2016 · 6 comments

Comments

3 participants
@jherkel
Copy link

jherkel commented Sep 24, 2016

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 :
I have a library that contains some common classes (for example in a path org.common) and I have a maven plugin that generates some classes from xml files (classes are generated in a different package for example org.test)

This XML file contains "field" elements :
<Field Name="SemicolonToken" Type="SyntaxToken">

where "Type" attribute contains a java type. From this element I generate something like this :

private final SyntaxToken semicolonToken;

My problem is that I also want to add this import :

import org.common.*;

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".

@kenzierocks

This comment has been minimized.

Copy link
Contributor

kenzierocks commented Sep 24, 2016

Why not just use ClassName.get(packageName, simpleName)? It sounds like exactly what you need.

@jherkel

This comment has been minimized.

Copy link
Author

jherkel commented Sep 24, 2016

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 :

  1. In a common library there are some abstract classes like ASTNode for a parser generator
  2. User wants to create his own parser, he extends ASTNode from common lib and saves it in his package, also he can extend some basic types from the common lib.
  3. In maven pom he calls a plugin that generates AST nodes from xml file, these files will be generated to his package

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.

@kenzierocks

This comment has been minimized.

Copy link
Contributor

kenzierocks commented Sep 24, 2016

So, could you just detect if there's . in the path and change what version of ClassName.get you use based on that?

@jherkel

This comment has been minimized.

Copy link
Author

jherkel commented Sep 25, 2016

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.

@swankjesse

This comment has been minimized.

Copy link
Member

swankjesse commented Sep 25, 2016

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.

@swankjesse swankjesse closed this Sep 25, 2016

@jherkel

This comment has been minimized.

Copy link
Author

jherkel commented Sep 25, 2016

How can you know class name if there is a chain of tools and javapoet is not the last one? But ok then , I will fork and change it myself.

jherkel added a commit to jherkel/javapoet that referenced this issue Sep 26, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.