Home > Software engineering >  How to add correct import when using Structural replace on Intellij?
How to add correct import when using Structural replace on Intellij?

Time:01-04

When adding elements to Java code using Intellij Structural find/replace, how can i add the missing imports?

For example, on doing the following Structural (not text, not regex) search:

find:

@$Annotation [Text=Autowired]$
$FieldType$ $Field [Text=X]$ = $Init$;

Replace:

@Lazy
@$Annotation$
$FieldType$ $Field$ = $Init$;

It correctly adds the annotation on the right place

@Autowired
@Getter
Private X x;
// Is replaced with:
@Getter
@Lazy
@Autowired
Private X x;

But, it won't add the import as there are more than one option for Lazy class. How can I instruct Intellij to also add import org.springframework.context.annotation.Lazy; to the replaced file?

CodePudding user response:

You can use fully qualified names in the replacement and enable the Shorten fully qualified names checkbox. This will add the imports when replacing.

  •  Tags:  
  • Related