Created
February 18, 2013 17:16
-
-
Save xhanin/4978948 to your computer and use it in GitHub Desktop.
Remove all setters or getters in a class (IntellijEval plugin).
Example use case: move from regular getters/setters to fluent interface: - use this plugin to remove getters / setters - use http://code.google.com/p/idea-generate-fluent-interface/ to generate fluent interface methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.intellij.openapi.actionSystem.AnActionEvent | |
import com.intellij.psi.PsiClass | |
import com.intellij.psi.PsiElement | |
import com.intellij.psi.PsiModifier | |
import com.intellij.psi.util.PsiTreeUtil | |
import static intellijeval.PluginUtil.* | |
def findContextClass = { project -> | |
def editor = currentEditorIn(event.project); | |
def psiFile = currentPsiFileIn(event.project); | |
PsiElement context = psiFile.findElementAt(editor.caretModel.offset); | |
if (context == null) { | |
return null; | |
} | |
PsiClass clazz = PsiTreeUtil.getParentOfType(context, PsiClass.class, false); | |
if (clazz == null || clazz.isInterface()) { | |
return null; | |
} | |
return clazz; | |
} | |
def setters = { method-> | |
return (!method.hasModifierProperty(PsiModifier.STATIC) | |
&& method.name.startsWith('set') | |
&& method.parameterList.parametersCount == 1) | |
} | |
def getters = { method-> | |
return (!method.hasModifierProperty(PsiModifier.STATIC) | |
&& method.name.startsWith('get') | |
&& method.parameterList.parametersCount == 0) | |
} | |
def deleteMethods = { methods -> | |
def deleted = []; | |
methods.each { method -> | |
deleted.add( method.name ) | |
method.delete() | |
} | |
show("deleted $deleted") | |
} | |
registerAction("RemoveSetters", "alt shift S") { AnActionEvent event -> | |
runDocumentWriteAction(event.project) { | |
def clazz = findContextClass(event.project) | |
if (clazz == null) return | |
deleteMethods clazz.methods.findAll(setters) | |
} | |
} | |
registerAction("RemoveGetters", "alt shift G") { AnActionEvent event -> | |
runDocumentWriteAction(event.project) { | |
def clazz = findContextClass(event.project) | |
if (clazz == null) return | |
deleteMethods clazz.methods.findAll(getters) | |
} | |
} | |
show("Loaded 'RemoveSetters' action<br/>Use 'Alt+Shift+S' to run it") | |
show("Loaded 'RemoveGetters' action<br/>Use 'Alt+Shift+G' to run it") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just noticed your plugin :) Do you mind if add a link to intellijeval readme?
(also noticed that in findContextClass it should probably be "project" instead of "event.project")