mirror of
https://github.com/revanced/smali.git
synced 2025-05-28 20:00:13 +02:00
Add support for renaming fields
This commit is contained in:
parent
07e6ade7fe
commit
923cc91ea2
@ -188,8 +188,7 @@ public class SmaliClassTypeElement extends SmaliTypeElement implements PsiJavaCo
|
||||
}
|
||||
|
||||
@Nullable @Override public PsiElement getQualifier() {
|
||||
// TODO: implement this if needed
|
||||
throw new UnsupportedOperationException();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable @Override public String getReferenceName() {
|
||||
|
@ -130,8 +130,9 @@ public class SmaliField extends SmaliStubBasedPsiElement<SmaliFieldStub> impleme
|
||||
}
|
||||
|
||||
@Override public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
|
||||
// TODO: implement this
|
||||
return null;
|
||||
SmaliMemberName smaliMemberName = getNameIdentifier();
|
||||
smaliMemberName.setName(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public boolean hasModifierProperty(@ModifierConstant @NonNls @NotNull String name) {
|
||||
|
@ -32,7 +32,10 @@
|
||||
package org.jf.smalidea.psi.impl;
|
||||
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -138,7 +141,11 @@ public class SmaliFieldReference extends SmaliCompositeElement implements PsiRef
|
||||
}
|
||||
|
||||
@Override public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
|
||||
//TODO: implement this
|
||||
throw new IncorrectOperationException();
|
||||
SmaliMemberName memberName = getMemberName();
|
||||
if (memberName == null) {
|
||||
throw new IncorrectOperationException();
|
||||
}
|
||||
memberName.setName(newElementName);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -32,9 +32,12 @@
|
||||
package org.jf.smalidea.psi.impl;
|
||||
|
||||
import com.intellij.psi.PsiIdentifier;
|
||||
import com.intellij.psi.impl.source.codeStyle.CodeEditUtil;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jf.smalidea.psi.SmaliCompositeElementFactory;
|
||||
import org.jf.smalidea.psi.SmaliElementTypes;
|
||||
import org.jf.smalidea.psi.leaf.SmaliSimpleName;
|
||||
|
||||
public class SmaliMemberName extends SmaliCompositeElement implements PsiIdentifier {
|
||||
public static final SmaliCompositeElementFactory FACTORY = new SmaliCompositeElementFactory() {
|
||||
@ -50,4 +53,17 @@ public class SmaliMemberName extends SmaliCompositeElement implements PsiIdentif
|
||||
@Override public IElementType getTokenType() {
|
||||
return getElementType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return this.getText();
|
||||
}
|
||||
|
||||
public void setName(@NotNull String newElementName) {
|
||||
removeAllChildren();
|
||||
SmaliSimpleName newNameElement = new SmaliSimpleName(newElementName);
|
||||
CodeEditUtil.setNodeGenerated(newNameElement, true);
|
||||
|
||||
addChild(newNameElement);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,10 @@
|
||||
package org.jf.smalidea.psi.leaf;
|
||||
|
||||
import com.intellij.psi.impl.source.tree.LeafPsiElement;
|
||||
import org.jf.smalidea.SmaliTokens;
|
||||
|
||||
public class SmaliSimpleName extends LeafPsiElement {
|
||||
public SmaliSimpleName(CharSequence text) {
|
||||
super(SmaliTokens.SIMPLE_NAME, text);
|
||||
}
|
||||
}
|
50
smalidea/src/test/java/org/jf/smalidea/FieldRenameTest.java
Normal file
50
smalidea/src/test/java/org/jf/smalidea/FieldRenameTest.java
Normal 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.PsiField;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.refactoring.MultiFileTestCase;
|
||||
import com.intellij.refactoring.rename.RenameProcessor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class FieldRenameTest extends MultiFileTestCase {
|
||||
@Override
|
||||
protected String getTestDataPath() {
|
||||
return "testData";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getTestRoot() {
|
||||
return "/fieldRename/";
|
||||
}
|
||||
|
||||
public void testFieldRename() {
|
||||
doTest("blah", "blah", "blort");
|
||||
}
|
||||
|
||||
private void doTest(@NotNull final String containingClass, @NotNull final String oldFieldName,
|
||||
@NotNull final String newFieldName) {
|
||||
doTest(new PerformAction() {
|
||||
@Override
|
||||
public void performAction(VirtualFile rootDir, VirtualFile rootAfter) throws Exception {
|
||||
doRename(containingClass, oldFieldName, newFieldName);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void doRename(String containingClass, String oldFieldName, String newFieldName) throws Exception {
|
||||
PsiClass testClass = myJavaFacade.findClass(containingClass, GlobalSearchScope.allScope(getProject()));
|
||||
|
||||
PsiField field = testClass.findFieldByName(oldFieldName, false);
|
||||
|
||||
RenameProcessor processor = new RenameProcessor(getProject(), field, newFieldName, false, false);
|
||||
processor.run();
|
||||
|
||||
PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
}
|
||||
}
|
59
smalidea/testData/fieldRename/fieldRename/after/blah.smali
Normal file
59
smalidea/testData/fieldRename/fieldRename/after/blah.smali
Normal file
@ -0,0 +1,59 @@
|
||||
.class public Lblah;
|
||||
.super Ljava/lang/Object;
|
||||
|
||||
.annotation runtime Lblah;
|
||||
element = Lblah;->blort:Lblah;
|
||||
element2 = .enum Lblah;->blort:Lblah;
|
||||
.end annotation
|
||||
|
||||
.field public blort:Lblah;
|
||||
|
||||
.method public blah(Lblah;)Lblah;
|
||||
.registers 2
|
||||
|
||||
iget v0, v0, Lblah;->blort:Lblah;
|
||||
iget-object v0, v0, Lblah;->blort:Lblah;
|
||||
iget-byte v0, v0, Lblah;->blort:Lblah;
|
||||
iget-char v0, v0, Lblah;->blort:Lblah;
|
||||
iget-object v0, v0, Lblah;->blort:Lblah;
|
||||
iget-object-volatile v0, v0, Lblah;->blort:Lblah;
|
||||
iget-short v0, v0, Lblah;->blort:Lblah;
|
||||
iget-volatile v0, v0, Lblah;->blort:Lblah;
|
||||
iget-wide v0, v0, Lblah;->blort:Lblah;
|
||||
iget-wide-volatile v0, v0, Lblah;->blort:Lblah;
|
||||
sget v0, Lblah;->blort:Lblah;
|
||||
sget-boolean v0, Lblah;->blort:Lblah;
|
||||
sget-byte v0, Lblah;->blort:Lblah;
|
||||
sget-char v0, Lblah;->blort:Lblah;
|
||||
sget-object v0, Lblah;->blort:Lblah;
|
||||
sget-object-volatile v0, Lblah;->blort:Lblah;
|
||||
sget-short v0, Lblah;->blort:Lblah;
|
||||
sget-volatile v0, Lblah;->blort:Lblah;
|
||||
sget-wide v0, Lblah;->blort:Lblah;
|
||||
sget-wide-volatile v0, Lblah;->blort:Lblah;
|
||||
|
||||
iput v0, v0, Lblah;->blort:Lblah;
|
||||
iput-object v0, v0, Lblah;->blort:Lblah;
|
||||
iput-byte v0, v0, Lblah;->blort:Lblah;
|
||||
iput-char v0, v0, Lblah;->blort:Lblah;
|
||||
iput-object v0, v0, Lblah;->blort:Lblah;
|
||||
iput-object-volatile v0, v0, Lblah;->blort:Lblah;
|
||||
iput-short v0, v0, Lblah;->blort:Lblah;
|
||||
iput-volatile v0, v0, Lblah;->blort:Lblah;
|
||||
iput-wide v0, v0, Lblah;->blort:Lblah;
|
||||
iput-wide-volatile v0, v0, Lblah;->blort:Lblah;
|
||||
sput v0, Lblah;->blort:Lblah;
|
||||
sput-boolean v0, Lblah;->blort:Lblah;
|
||||
sput-byte v0, Lblah;->blort:Lblah;
|
||||
sput-char v0, Lblah;->blort:Lblah;
|
||||
sput-object v0, Lblah;->blort:Lblah;
|
||||
sput-object-volatile v0, Lblah;->blort:Lblah;
|
||||
sput-short v0, Lblah;->blort:Lblah;
|
||||
sput-volatile v0, Lblah;->blort:Lblah;
|
||||
sput-wide v0, Lblah;->blort:Lblah;
|
||||
sput-wide-volatile v0, Lblah;->blort:Lblah;
|
||||
|
||||
throw-verification-error generic-error, Lblah;->blort:Lblah;
|
||||
|
||||
return-void
|
||||
.end method
|
59
smalidea/testData/fieldRename/fieldRename/before/blah.smali
Normal file
59
smalidea/testData/fieldRename/fieldRename/before/blah.smali
Normal file
@ -0,0 +1,59 @@
|
||||
.class public Lblah;
|
||||
.super Ljava/lang/Object;
|
||||
|
||||
.annotation runtime Lblah;
|
||||
element = Lblah;->blah:Lblah;
|
||||
element2 = .enum Lblah;->blah:Lblah;
|
||||
.end annotation
|
||||
|
||||
.field public blah:Lblah;
|
||||
|
||||
.method public blah(Lblah;)Lblah;
|
||||
.registers 2
|
||||
|
||||
iget v0, v0, Lblah;->blah:Lblah;
|
||||
iget-object v0, v0, Lblah;->blah:Lblah;
|
||||
iget-byte v0, v0, Lblah;->blah:Lblah;
|
||||
iget-char v0, v0, Lblah;->blah:Lblah;
|
||||
iget-object v0, v0, Lblah;->blah:Lblah;
|
||||
iget-object-volatile v0, v0, Lblah;->blah:Lblah;
|
||||
iget-short v0, v0, Lblah;->blah:Lblah;
|
||||
iget-volatile v0, v0, Lblah;->blah:Lblah;
|
||||
iget-wide v0, v0, Lblah;->blah:Lblah;
|
||||
iget-wide-volatile v0, v0, Lblah;->blah:Lblah;
|
||||
sget v0, Lblah;->blah:Lblah;
|
||||
sget-boolean v0, Lblah;->blah:Lblah;
|
||||
sget-byte v0, Lblah;->blah:Lblah;
|
||||
sget-char v0, Lblah;->blah:Lblah;
|
||||
sget-object v0, Lblah;->blah:Lblah;
|
||||
sget-object-volatile v0, Lblah;->blah:Lblah;
|
||||
sget-short v0, Lblah;->blah:Lblah;
|
||||
sget-volatile v0, Lblah;->blah:Lblah;
|
||||
sget-wide v0, Lblah;->blah:Lblah;
|
||||
sget-wide-volatile v0, Lblah;->blah:Lblah;
|
||||
|
||||
iput v0, v0, Lblah;->blah:Lblah;
|
||||
iput-object v0, v0, Lblah;->blah:Lblah;
|
||||
iput-byte v0, v0, Lblah;->blah:Lblah;
|
||||
iput-char v0, v0, Lblah;->blah:Lblah;
|
||||
iput-object v0, v0, Lblah;->blah:Lblah;
|
||||
iput-object-volatile v0, v0, Lblah;->blah:Lblah;
|
||||
iput-short v0, v0, Lblah;->blah:Lblah;
|
||||
iput-volatile v0, v0, Lblah;->blah:Lblah;
|
||||
iput-wide v0, v0, Lblah;->blah:Lblah;
|
||||
iput-wide-volatile v0, v0, Lblah;->blah:Lblah;
|
||||
sput v0, Lblah;->blah:Lblah;
|
||||
sput-boolean v0, Lblah;->blah:Lblah;
|
||||
sput-byte v0, Lblah;->blah:Lblah;
|
||||
sput-char v0, Lblah;->blah:Lblah;
|
||||
sput-object v0, Lblah;->blah:Lblah;
|
||||
sput-object-volatile v0, Lblah;->blah:Lblah;
|
||||
sput-short v0, Lblah;->blah:Lblah;
|
||||
sput-volatile v0, Lblah;->blah:Lblah;
|
||||
sput-wide v0, Lblah;->blah:Lblah;
|
||||
sput-wide-volatile v0, Lblah;->blah:Lblah;
|
||||
|
||||
throw-verification-error generic-error, Lblah;->blah:Lblah;
|
||||
|
||||
return-void
|
||||
.end method
|
Loading…
x
Reference in New Issue
Block a user