mirror of
https://github.com/revanced/smali.git
synced 2025-05-29 12:20:11 +02:00
Start adding better support for invalid classes/methods
This tweaks the nullability of various methods/parameters, and ensures they are accessed appropriately
This commit is contained in:
parent
a227c0b142
commit
0a5f278d5f
@ -38,6 +38,7 @@ import com.intellij.psi.PsiElement;
|
|||||||
import com.intellij.psi.stubs.StubElement;
|
import com.intellij.psi.stubs.StubElement;
|
||||||
import com.intellij.util.IncorrectOperationException;
|
import com.intellij.util.IncorrectOperationException;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jf.smalidea.SmaliFileType;
|
import org.jf.smalidea.SmaliFileType;
|
||||||
import org.jf.smalidea.SmaliLanguage;
|
import org.jf.smalidea.SmaliLanguage;
|
||||||
import org.jf.smalidea.psi.SmaliElementTypes;
|
import org.jf.smalidea.psi.SmaliElementTypes;
|
||||||
@ -50,26 +51,37 @@ public class SmaliFile extends PsiFileBase implements PsiClassOwner {
|
|||||||
@NotNull @Override public SmaliFileType getFileType() {
|
@NotNull @Override public SmaliFileType getFileType() {
|
||||||
return SmaliFileType.INSTANCE;
|
return SmaliFileType.INSTANCE;
|
||||||
}
|
}
|
||||||
@NotNull
|
|
||||||
|
@Nullable
|
||||||
public SmaliClass getPsiClass() {
|
public SmaliClass getPsiClass() {
|
||||||
StubElement<? extends PsiElement> stub = (StubElement<? extends PsiElement>)getStub();
|
StubElement<? extends PsiElement> stub = (StubElement<? extends PsiElement>)getStub();
|
||||||
if (stub != null) {
|
if (stub != null) {
|
||||||
StubElement<SmaliClass> classElement = stub.findChildStubByType(SmaliElementTypes.CLASS);
|
StubElement<SmaliClass> classElement = stub.findChildStubByType(SmaliElementTypes.CLASS);
|
||||||
assert classElement != null;
|
if (classElement != null) {
|
||||||
return classElement.getPsi();
|
return classElement.getPsi();
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
SmaliClass smaliClass = findChildByClass(SmaliClass.class);
|
return findChildByClass(SmaliClass.class);
|
||||||
assert smaliClass != null;
|
|
||||||
return smaliClass;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull @Override public SmaliClass[] getClasses() {
|
@NotNull @Override public SmaliClass[] getClasses() {
|
||||||
return new SmaliClass[] {getPsiClass()};
|
SmaliClass smaliClass = getPsiClass();
|
||||||
|
if (smaliClass == null) {
|
||||||
|
return new SmaliClass[] {};
|
||||||
|
} else {
|
||||||
|
return new SmaliClass[] { smaliClass };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public String getPackageName() {
|
@NotNull @Override public String getPackageName() {
|
||||||
return getPsiClass().getPackageName();
|
SmaliClass smaliClass = getPsiClass();
|
||||||
|
if (smaliClass == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return smaliClass.getPackageName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void setPackageName(String packageName) throws IncorrectOperationException {
|
@Override public void setPackageName(String packageName) throws IncorrectOperationException {
|
||||||
|
@ -72,10 +72,19 @@ public class SmaliMethod extends SmaliStubBasedPsiElement<SmaliMethodStub>
|
|||||||
|
|
||||||
@NotNull @Override public String getName() {
|
@NotNull @Override public String getName() {
|
||||||
SmaliMethodStub stub = getStub();
|
SmaliMethodStub stub = getStub();
|
||||||
|
String name = null;
|
||||||
if (stub != null) {
|
if (stub != null) {
|
||||||
return stub.getName();
|
name = stub.getName();
|
||||||
|
} else {
|
||||||
|
SmaliMemberName nameIdentifier = getNameIdentifier();
|
||||||
|
if (nameIdentifier != null) {
|
||||||
|
name = nameIdentifier.getText();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return getNameIdentifier().getText();
|
if (name == null || name.isEmpty()) {
|
||||||
|
name = "<unnamed>";
|
||||||
|
}
|
||||||
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public boolean hasTypeParameters() {
|
@Override public boolean hasTypeParameters() {
|
||||||
@ -88,17 +97,24 @@ public class SmaliMethod extends SmaliStubBasedPsiElement<SmaliMethodStub>
|
|||||||
return getRequiredStubOrPsiChild(SmaliElementTypes.METHOD_PROTOTYPE);
|
return getRequiredStubOrPsiChild(SmaliElementTypes.METHOD_PROTOTYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull @Override public PsiType getReturnType() {
|
@Nullable @Override public PsiType getReturnType() {
|
||||||
SmaliMethodStub stub = getStub();
|
SmaliMethodStub stub = getStub();
|
||||||
if (stub != null) {
|
if (stub != null) {
|
||||||
String returnType = stub.getReturnType();
|
String returnType = stub.getReturnType();
|
||||||
|
if (returnType == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
|
PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
|
||||||
return factory.createTypeByFQClassName(returnType, getResolveScope());
|
return factory.createTypeByFQClassName(returnType, getResolveScope());
|
||||||
}
|
}
|
||||||
return getReturnTypeElement().getType();
|
PsiTypeElement returnTypeElement = getReturnTypeElement();
|
||||||
|
if (returnTypeElement == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return returnTypeElement.getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull @Override public PsiTypeElement getReturnTypeElement() {
|
@Nullable @Override public PsiTypeElement getReturnTypeElement() {
|
||||||
return getMethodPrototype().getReturnType();
|
return getMethodPrototype().getReturnType();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,10 +198,8 @@ public class SmaliMethod extends SmaliStubBasedPsiElement<SmaliMethodStub>
|
|||||||
return MethodSignatureBackedByPsiMethod.create(this, substitutor);
|
return MethodSignatureBackedByPsiMethod.create(this, substitutor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull @Override public SmaliMemberName getNameIdentifier() {
|
@Nullable @Override public SmaliMemberName getNameIdentifier() {
|
||||||
SmaliMemberName memberName = findChildByClass(SmaliMemberName.class);
|
return findChildByClass(SmaliMemberName.class);
|
||||||
assert memberName != null;
|
|
||||||
return memberName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull @Override public PsiMethod[] findSuperMethods() {
|
@NotNull @Override public PsiMethod[] findSuperMethods() {
|
||||||
|
@ -34,6 +34,7 @@ package org.jf.smalidea.psi.impl;
|
|||||||
import com.intellij.lang.ASTNode;
|
import com.intellij.lang.ASTNode;
|
||||||
import com.intellij.psi.PsiTypeElement;
|
import com.intellij.psi.PsiTypeElement;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jf.smalidea.psi.SmaliElementTypes;
|
import org.jf.smalidea.psi.SmaliElementTypes;
|
||||||
import org.jf.smalidea.psi.stub.SmaliMethodPrototypeStub;
|
import org.jf.smalidea.psi.stub.SmaliMethodPrototypeStub;
|
||||||
|
|
||||||
@ -46,18 +47,13 @@ public class SmaliMethodPrototype extends SmaliStubBasedPsiElement<SmaliMethodPr
|
|||||||
super(node);
|
super(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@Nullable
|
||||||
public PsiTypeElement getReturnType() {
|
public PsiTypeElement getReturnType() {
|
||||||
PsiTypeElement element = findChildByClass(PsiTypeElement.class);
|
return findChildByClass(PsiTypeElement.class);
|
||||||
assert element != null;
|
|
||||||
return element;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
@NotNull
|
|
||||||
public SmaliMethodParamList getParameterList() {
|
public SmaliMethodParamList getParameterList() {
|
||||||
SmaliMethodParamList paramList = findChildByClass(SmaliMethodParamList.class);
|
return findChildByClass(SmaliMethodParamList.class);
|
||||||
assert paramList != null;
|
|
||||||
return paramList;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,21 +37,23 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jf.smalidea.psi.SmaliElementTypes;
|
import org.jf.smalidea.psi.SmaliElementTypes;
|
||||||
import org.jf.smalidea.psi.impl.SmaliMethod;
|
import org.jf.smalidea.psi.impl.SmaliMethod;
|
||||||
|
|
||||||
public class SmaliMethodStub extends StubBase<SmaliMethod> {
|
import javax.annotation.Nullable;
|
||||||
@NotNull private final String name;
|
|
||||||
@NotNull private final String returnType;
|
|
||||||
|
|
||||||
public SmaliMethodStub(@NotNull StubElement parent, @NotNull String name, @NotNull String returnType) {
|
public class SmaliMethodStub extends StubBase<SmaliMethod> {
|
||||||
|
@Nullable private final String name;
|
||||||
|
@Nullable private final String returnType;
|
||||||
|
|
||||||
|
public SmaliMethodStub(@NotNull StubElement parent, @Nullable String name, @Nullable String returnType) {
|
||||||
super(parent, SmaliElementTypes.METHOD);
|
super(parent, SmaliElementTypes.METHOD);
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.returnType = returnType;
|
this.returnType = returnType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull public String getName() {
|
@Nullable public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull public String getReturnType() {
|
@Nullable public String getReturnType() {
|
||||||
return returnType;
|
return returnType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,10 +32,12 @@
|
|||||||
package org.jf.smalidea.psi.stub.element;
|
package org.jf.smalidea.psi.stub.element;
|
||||||
|
|
||||||
import com.intellij.lang.ASTNode;
|
import com.intellij.lang.ASTNode;
|
||||||
|
import com.intellij.psi.PsiType;
|
||||||
import com.intellij.psi.stubs.IndexSink;
|
import com.intellij.psi.stubs.IndexSink;
|
||||||
import com.intellij.psi.stubs.StubElement;
|
import com.intellij.psi.stubs.StubElement;
|
||||||
import com.intellij.psi.stubs.StubInputStream;
|
import com.intellij.psi.stubs.StubInputStream;
|
||||||
import com.intellij.psi.stubs.StubOutputStream;
|
import com.intellij.psi.stubs.StubOutputStream;
|
||||||
|
import com.intellij.util.io.StringRef;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jf.smalidea.psi.impl.SmaliMethod;
|
import org.jf.smalidea.psi.impl.SmaliMethod;
|
||||||
import org.jf.smalidea.psi.stub.SmaliMethodStub;
|
import org.jf.smalidea.psi.stub.SmaliMethodStub;
|
||||||
@ -62,7 +64,12 @@ public class SmaliMethodElementType extends SmaliStubElementType<SmaliMethodStub
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public SmaliMethodStub createStub(@NotNull SmaliMethod psi, StubElement parentStub) {
|
@Override public SmaliMethodStub createStub(@NotNull SmaliMethod psi, StubElement parentStub) {
|
||||||
return new SmaliMethodStub(parentStub, psi.getName(), psi.getReturnType().getCanonicalText());
|
String returnTypeText = null;
|
||||||
|
PsiType returnType = psi.getReturnType();
|
||||||
|
if (returnType != null) {
|
||||||
|
returnTypeText = returnType.getCanonicalText();
|
||||||
|
}
|
||||||
|
return new SmaliMethodStub(parentStub, psi.getName(), returnTypeText);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -73,7 +80,13 @@ public class SmaliMethodElementType extends SmaliStubElementType<SmaliMethodStub
|
|||||||
|
|
||||||
@NotNull @Override
|
@NotNull @Override
|
||||||
public SmaliMethodStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
|
public SmaliMethodStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
|
||||||
return new SmaliMethodStub(parentStub, dataStream.readName().getString(), dataStream.readName().getString());
|
StringRef methodNameRef = dataStream.readName();
|
||||||
|
StringRef returnTypeRef = dataStream.readName();
|
||||||
|
String returnType = null;
|
||||||
|
if (returnTypeRef != null) {
|
||||||
|
returnType = returnTypeRef.getString();
|
||||||
|
}
|
||||||
|
return new SmaliMethodStub(parentStub, methodNameRef.getString(), returnType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void indexStub(@NotNull SmaliMethodStub stub, @NotNull IndexSink sink) {
|
@Override public void indexStub(@NotNull SmaliMethodStub stub, @NotNull IndexSink sink) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user