Structurally Replace with Static Import



I'm trying to create a Structurally Replace template for all the static calls (methods or constants) and while I did the proper search pattern and the replacement one, it finds all that I want it to find, and when I click the replace option, it is replaces the string correctly but it is usually does not create the static import that I need it to create, thus, the replacement of course does not compile.


If I hit "Alt + enter" and choose the "Add static import" from the menu, it imports the static import correctly, but I need it to work with the replacement option without the need to search for the correct option from the built ones. 

* It did not help when I checked the "Shorten fully..." checkbox.


template for static constants:


template for static method:

Please help.


P.S: I've edited that posts with both templates (method and constants search, and not only constants as was before).


Official comment

If all you want to is add a static import for the code structure you found, use a replace template identical to the search template. When "Use static imports" is enabled, Structural Search tries to shorten any code in the replacement that can be statically imported. It can't do this is the class qualifier is already removed.


Hello Marat,

Can you provide a sample with the code that successfully matches your search template where static import is not used on performing replacement?


I'm not sure how to provide it, but I'll try:

Here is the case which is found with the template:


That is the case after I clicked replace, as you can see that assertEO is red (uncompiled).

That's the import list, as you see I do have the Debug import but that's not the static one, since it was imported before when the Debug.assertEO was added to the code.


Thanks Bas,

Didn't realize that it looks at the replace statement and removes the class from it, thought that it uses the search one. 

While it works, I've noticed 3 things:

1. The message that the replace statement generates is wrong, it says: "Replace with ClassName.methodName" when it actually removes the ClassName and should only say "Replace with methodName". Is there a way to configure that?

2. It is not always works. I've notices that for synchronized methods it doesn't. And it does not even give you the predefined "Add on-demand static import" or "Add static import" when you hit Alt+Enter while it does for unsynchronized methods. 

3. That's a weird one.... The IDEA decided on that specific inspection the severity of it, and by that I mean that I changed the severity to a custom one, and until I restart my IDEA it has that severity level, but after restart, it changed it to "Warning". I've changed to some other NON custom severity, and after restart it still changes it to "Warning". And the strangest thing that I've changed the severity to a few inspections/Structural search inspections and it's only for that one. I've even deleted it, and created a new one, and it still the same.

I believe that it will be very hard to reproduce (issue #3), but maybe if you'll try the one I've created it will act the same in your case.


1. No, there is currently no way to change this. You could file an issue for this problem if you want to see it fixed.

2. That seems like a bug. Could you give a concrete code example?

3. Unfortunately I am unable to reproduce this thus far. Is there any more information you can provide? Maybe you can paste the exact template you use here? (Export Template to Clipboard action under the tool menu in the Structural Search dialog)


2. It looks that I don't have that issue anymore, as I've checked now with some other synchronized method, and it seems to work with those, as I don't remember the method that I've seen it with. 


3.This inspection has 2 templates:

<replaceConfiguration name="Replace static calls with static import" description="Replace this occurance with static import, or hit Alt+Enter and choose &quot;Add/Add on demand static import...&quot; to replace all such occurances" problemDescriptor="Replace this with static import or alt+Enter and &quot;Add/Add on demand static import&quot; to replace all" text="$ClassName$.$MethodCall$($Parameter$)" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default" reformatAccordingToStyle="true" shortenFQN="false" useStaticImport="true" replacement="$ClassName$.$MethodCall$($Parameter$)">
<constraint name="__context__" within="" contains="" />
<constraint name="MethodCall" within="" contains="" />
<constraint name="Parameter" minCount="0" maxCount="2147483647" within="" contains="" />
<constraint name="ClassName" reference="classes" within="" contains="" />
<replaceConfiguration name="Replace static calls with static import" text="$ClassName$.$Constant$" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default" reformatAccordingToStyle="true" shortenFQN="false" useStaticImport="true" replacement="$ClassName$.$Constant$">
<constraint name="__context__" within="" contains="" />
<constraint name="ClassName" reference="classes" within="" contains="" />
<constraint name="Constant" reference="all static fields" within="" contains="" />

initial state of inspection defined as recommended:

After restart of IDEA, the inspection saying it's a warning:

By the way, when I exported the profile with 2 new severities and structural inspections, and imported this XML file, the IDEA gave me a message that there are new severity levels, so I want to include them. I said yes, and it imported them, but without any properties (no highlighting, color etc.).

I've defined the highlighting again, but after each IDEA restart, it shows as gray, and only if I go to edit those severities, and hit save, it gives me the colors that defined.


Please sign in to leave a comment.