Add support for renaming methods

This commit is contained in:
Ben Gruver 2015-03-31 18:51:48 -07:00
parent 923cc91ea2
commit 48aacd2c01
5 changed files with 121 additions and 5 deletions

View File

@ -225,8 +225,12 @@ public class SmaliMethod extends SmaliStubBasedPsiElement<SmaliMethodStub>
} }
@Override public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException { @Override public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
// TODO: implement this SmaliMemberName smaliMemberName = getNameIdentifier();
throw new IncorrectOperationException(); if (smaliMemberName == null) {
throw new IncorrectOperationException();
}
smaliMemberName.setName(name);
return this;
} }
@NotNull @Override public HierarchicalMethodSignature getHierarchicalMethodSignature() { @NotNull @Override public HierarchicalMethodSignature getHierarchicalMethodSignature() {
@ -253,7 +257,11 @@ public class SmaliMethod extends SmaliStubBasedPsiElement<SmaliMethodStub>
} }
@Nullable @Override public SmaliClass getContainingClass() { @Nullable @Override public SmaliClass getContainingClass() {
return (SmaliClass)getStubOrPsiParent(); PsiElement parent = getStubOrPsiParent();
if (parent instanceof SmaliClass) {
return (SmaliClass) parent;
}
return null;
} }
@Override public boolean hasModifierProperty(@ModifierConstant @NonNls @NotNull String name) { @Override public boolean hasModifierProperty(@ModifierConstant @NonNls @NotNull String name) {

View File

@ -177,8 +177,12 @@ public class SmaliMethodReference extends SmaliCompositeElement implements PsiRe
} }
@Override public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { @Override public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
//TODO: implement this SmaliMemberName memberName = getMemberName();
throw new IncorrectOperationException(); if (memberName == null) {
throw new IncorrectOperationException();
}
memberName.setName(newElementName);
return this;
} }
@Override public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException { @Override public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {

View File

@ -0,0 +1,50 @@
package org.jf.smalidea;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.refactoring.MultiFileTestCase;
import com.intellij.refactoring.rename.RenameProcessor;
import org.jetbrains.annotations.NotNull;
public class MethodRenameTest extends MultiFileTestCase {
@Override
protected String getTestDataPath() {
return "testData";
}
@NotNull
@Override
protected String getTestRoot() {
return "/methodRename/";
}
public void testMethodRename() {
doTest("blah", "blah", "blort");
}
private void doTest(@NotNull final String containingClass, @NotNull final String oldMethodName,
@NotNull final String newMethodName) {
doTest(new PerformAction() {
@Override
public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception {
doRename(containingClass, oldMethodName, newMethodName);
}
});
}
private void doRename(String containingClass, String oldMethodName, String newMethodName) throws Exception {
PsiClass testClass = myJavaFacade.findClass(containingClass, GlobalSearchScope.allScope(getProject()));
PsiMethod method = testClass.findMethodsByName(oldMethodName, false)[0];
RenameProcessor processor = new RenameProcessor(getProject(), method, newMethodName, false, false);
processor.run();
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
FileDocumentManager.getInstance().saveAllDocuments();
}
}

View File

@ -0,0 +1,27 @@
.class public Lblah;
.super Ljava/lang/Object;
.annotation runtime Lblah;
element = Lblah;->blort()V;
.end annotation
.method public blort()V
.registers 2
invoke-direct {v0}, Lblah;->blort()V
invoke-direct/empty {v0}, Lblah;->blort()V
invoke-direct/range {v0}, Lblah;->blort()V
invoke-interface {v0}, Lblah;->blort()V
invoke-interface/range {v0}, Lblah;->blort()V
invoke-object-init/range {v0}, Lblah;->blort()V
invoke-static {v0}, Lblah;->blort()V
invoke-static/range {v0}, Lblah;->blort()V
invoke-super {v0}, Lblah;->blort()V
invoke-super/range {v0}, Lblah;->blort()V
invoke-virtual {v0}, Lblah;->blort()V
invoke-virtual/range {v0}, Lblah;->blort()V
throw-verification-error generic-error, Lblah;->blort()V
return-void
.end method

View File

@ -0,0 +1,27 @@
.class public Lblah;
.super Ljava/lang/Object;
.annotation runtime Lblah;
element = Lblah;->blah()V;
.end annotation
.method public blah()V
.registers 2
invoke-direct {v0}, Lblah;->blah()V
invoke-direct/empty {v0}, Lblah;->blah()V
invoke-direct/range {v0}, Lblah;->blah()V
invoke-interface {v0}, Lblah;->blah()V
invoke-interface/range {v0}, Lblah;->blah()V
invoke-object-init/range {v0}, Lblah;->blah()V
invoke-static {v0}, Lblah;->blah()V
invoke-static/range {v0}, Lblah;->blah()V
invoke-super {v0}, Lblah;->blah()V
invoke-super/range {v0}, Lblah;->blah()V
invoke-virtual {v0}, Lblah;->blah()V
invoke-virtual/range {v0}, Lblah;->blah()V
throw-verification-error generic-error, Lblah;->blah()V
return-void
.end method