Structural Replace script to rename class renames class and deletes file content
I'd like to rename all classes that extend Spring's JUnit helper classes from `xxxTest` to `xxxIT`.
For instance, if I have
@SpringJUnitConfig(locations="...")
@Transactional
public abstract class AbstractBaseTest extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private SomeInjection someInjection;
// more code elided
}
after running the replace I would have
@SpringJUnitConfig(locations="...")
@Transactional
public abstract class AbstractBaseIT extends AbstractTransactionalJUnit4SpringContextTests {
@Autowired
private SomeInjection someInjection;
// more code elided
}
And the same would happen for all the classes that extend this base class.
To do this I've come up with the following template:
<replaceConfiguration name="Misnamed Integration Test" text="class $Class$ {}" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default" reformatAccordingToStyle="false" shortenFQN="false" replacement="class $NewName$ {}">
<constraint name="__context__" within="" contains="" />
<constraint name="Class" script=""if (__context__.interface || __context__.enum) { return false } def p = __context__.getSuperClass() while (p != null) { println("parent: " + p) if (p.getName().endsWith("JUnit4SpringContextTests")) return true; p = p.getSuperClass() } return false "" regexp="\b(.*)((?:Integration)?Test)(.*)\b" target="true" within="" contains="" />
<variableDefinition name="NewName" script=""__context__.name.replaceAll("((?:Integration)?Test)", "IT")"" />
</replaceConfiguration>
The problem is, the replacement produces the following:
class AbstractBaseIT {}
That's it. It strips all the annotations, the modifiers, the extends and implements, as well as the entire contents of the class.
The find part works fine. It's correctly identifying all the classes that should be integration tests.
How do I get the replace to just change the name and not the rest?
UPDATE:
I got closer with the following:
<replaceConfiguration name="Misnamed Integration Test" text="class $Class$ {}" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default" reformatAccordingToStyle="false" shortenFQN="false" replacement="class $NewName$ {}">
<constraint name="__context__" within="" contains="" />
<constraint name="Class" script=""if (__context__.interface || __context__.enum) { return false } println("inital context: " + __context__) def p = __context__.getSuperClass() while (p != null) { if (p.getName().endsWith("JUnit4SpringContextTests")) return true; p = p.getSuperClass() } return false "" regexp="\b(.*)((?:Integration)?Test)(.*)\b" target="true" within="" contains="" />
<variableDefinition name="NewName" script=""__context__.setName( __context__.name.replaceAll("((?:Integration)Test)", "IT")) __context__.text"" />
</replaceConfiguration>
which, while admittedly still wrong, is closer, but throws the following exception:
INFO - tcher.predicates.ScriptSupport - Exception thrown by Structural Search Groovy Script
com.intellij.util.IncorrectOperationException: Must not change PSI outside command or undo-transparent action. See com.intellij.openapi.command.WriteCommandAction or com.intellij.openapi.command.CommandProcessor
Please sign in to leave a comment.
Something like the following should get you a little closer to your desired result:
This should rename the class and keep all its contents. But it does not rename the files containing the classes.
I am working on improving structural replacement for the next versions of IntelliJ IDEA, so the situation should improve. Please also vote for https://youtrack.jetbrains.com/issue/IDEA-12246
Bas
UPDATE 2:
I know this is overkill, but I've now got the replace script to
and this fails with:
So it's looking like I've got the script (possibly) doing close to what I want it to do (I noticed in editing this post I'm not actually changing the name), it's just impossible to do in just Structural Replace.
Is there a way to create an Intention based on a Structural Find that has a "Rename Refactor" as the suggested fix instead of the replace template?
>Is there a way to create an Intention based on a Structural Find that has a "Rename Refactor" as the suggested fix instead of the replace template?
You can create an inspection, based on the created SSR, that you may run and apply the fixes from the results.
I've created the following SSR template:
but it works only when there is single class annotation. I have asked the responsible developer to comment about this case.
Andrey Dernov Thanks! That's very similar to something I came up with along the way, but it unfortunately still misses the boat. There are a few things missing (like JavaDoc and class modifiers). It also suffers the same issue that while the class is renamed, the file is not, and there is no bulk-way to run the "rename file" inspection for mismatched file and class names.
Bas Leijdekkers Thanks! I got to very much the same place via slightly different means. I had upvoted that issue previously, and hope this gets resolved before I need it again ;)
I think what I'll have to do is export the "Find" results and write a Perl script to do the actual renaming.
That's very similar to something I came up with along the way, but it unfortunately still misses the boat. There are a few things missing (like JavaDoc and class modifiers).