Option to manually add an import

See original GitHub issue

Hi, I am aware that JavaPoet is automatically managing imports, but I would appreciate possibility to manually add non-static import.

The need occurs when I copy method implementation from an existing class to a class I build with JavaPoet. I don’t really know what the method implementation looks like, nor I care. But the implementation may be depending on an import statement, which JavaPoet is not and can’t be aware of.

Note that parsing method implementation is heroic task, which I want to avoid. Thanks

poor man’s workaround

This one is for field initializers, but same applies to code in methods.

initializerString = initializerString.replaceAll("HashSet", "java.util.HashSet"); // etc.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:21 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
JakeWhartoncommented, Sep 7, 2017

This feature would not help your issue. To add an import you need the package name and the class name which means you could call ClassName.get(packageName, className) and emit the reference normally.

On Thu, Sep 7, 2017 at 7:12 AM Thomas Bigger notifications@github.com wrote:

+1 for the feature

My use case is that I’m generating an entity pojo class into another project (not in the current java poet project) then creating an interface directly after which has a super-interface with a generic type of this entity. The import cannot be resolved because it resides in another project. At the moment I’m stuck using ClassName.bestGuess(entityName) then letting the IDE resolve the import within the other project which is not optimal.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/square/javapoet/issues/512#issuecomment-327769135, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEXi_lJPW2mu4PQtud7F_gJUJiXe9ks5sf8-FgaJpZM4KdA-i .

3reactions
octylFractalcommented, Sep 20, 2018

You get a TypeName for it like so: ClassName.get("android.graphics.drawable", "ColorDrawable")

Then you can use that wherever $T is accepted, and it will import it for you. If you want to explicitly reference the Class for it, you can do $T.class and pass the ClassName.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Auto import | IntelliJ IDEA Documentation - JetBrains
In the Settings/Preferences dialog ( Ctrl+Alt+S ), click Editor | General | Auto Import. Select the Add unambiguous imports on the fly checkbox ......
Read more >
Text Import Wizard - Microsoft Support
Once enabled, go to the Data tab > Get & Transform Data > Get Data > Legacy Wizards > From Text (Legacy). Then,...
Read more >
Manually Add or Import Users - SocialChorus Knowledge Base
Hover over Add User to expand the menu and select Import Users. If you have a file prepared, confirm that it has the...
Read more >
Any way (or shortcut) to auto import the classes in IntelliJ IDEA ...
Save this answer. Show activity on this post. Use Alt + Enter for importing a single package or use Alt + Shift +...
Read more >
Import photos and videos on iPhone - Apple Support
Insert an SD memory card into the card reader: Don't force the card into the slot on the reader; it fits only one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found