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.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jf.smalidea.SmaliFileType;
|
||||
import org.jf.smalidea.SmaliLanguage;
|
||||
import org.jf.smalidea.psi.SmaliElementTypes;
|
||||
@ -50,26 +51,37 @@ public class SmaliFile extends PsiFileBase implements PsiClassOwner {
|
||||
@NotNull @Override public SmaliFileType getFileType() {
|
||||
return SmaliFileType.INSTANCE;
|
||||
}
|
||||
@NotNull
|
||||
|
||||
@Nullable
|
||||
public SmaliClass getPsiClass() {
|
||||
StubElement<? extends PsiElement> stub = (StubElement<? extends PsiElement>)getStub();
|
||||
if (stub != null) {
|
||||
StubElement<SmaliClass> classElement = stub.findChildStubByType(SmaliElementTypes.CLASS);
|
||||
assert classElement != null;
|
||||
if (classElement != null) {
|
||||
return classElement.getPsi();
|
||||
} else {
|
||||
SmaliClass smaliClass = findChildByClass(SmaliClass.class);
|
||||
assert smaliClass != null;
|
||||
return smaliClass;
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return findChildByClass(SmaliClass.class);
|
||||
}
|
||||
}
|
||||
|
||||
@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() {
|
||||
return getPsiClass().getPackageName();
|
||||
@NotNull @Override public String getPackageName() {
|
||||
SmaliClass smaliClass = getPsiClass();
|
||||
if (smaliClass == null) {
|
||||
return "";
|
||||
}
|
||||
return smaliClass.getPackageName();
|
||||
}
|
||||
|
||||
@Override public void setPackageName(String packageName) throws IncorrectOperationException {
|
||||
|
@ -72,10 +72,19 @@ public class SmaliMethod extends SmaliStubBasedPsiElement<SmaliMethodStub>
|
||||
|
||||
@NotNull @Override public String getName() {
|
||||
SmaliMethodStub stub = getStub();
|
||||
String name = 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() {
|
||||
@ -88,17 +97,24 @@ public class SmaliMethod extends SmaliStubBasedPsiElement<SmaliMethodStub>
|
||||
return getRequiredStubOrPsiChild(SmaliElementTypes.METHOD_PROTOTYPE);
|
||||
}
|
||||
|
||||
@NotNull @Override public PsiType getReturnType() {
|
||||
@Nullable @Override public PsiType getReturnType() {
|
||||
SmaliMethodStub stub = getStub();
|
||||
if (stub != null) {
|
||||
String returnType = stub.getReturnType();
|
||||
if (returnType == null) {
|
||||
return null;
|
||||
}
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
|
||||
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();
|
||||
}
|
||||
|
||||
@ -182,10 +198,8 @@ public class SmaliMethod extends SmaliStubBasedPsiElement<SmaliMethodStub>
|
||||
return MethodSignatureBackedByPsiMethod.create(this, substitutor);
|
||||
}
|
||||
|
||||
@NotNull @Override public SmaliMemberName getNameIdentifier() {
|
||||
SmaliMemberName memberName = findChildByClass(SmaliMemberName.class);
|
||||
assert memberName != null;
|
||||
return memberName;
|
||||
@Nullable @Override public SmaliMemberName getNameIdentifier() {
|
||||
return findChildByClass(SmaliMemberName.class);
|
||||
}
|
||||
|
||||
@NotNull @Override public PsiMethod[] findSuperMethods() {
|
||||
|
@ -34,6 +34,7 @@ package org.jf.smalidea.psi.impl;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.PsiTypeElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jf.smalidea.psi.SmaliElementTypes;
|
||||
import org.jf.smalidea.psi.stub.SmaliMethodPrototypeStub;
|
||||
|
||||
@ -46,18 +47,13 @@ public class SmaliMethodPrototype extends SmaliStubBasedPsiElement<SmaliMethodPr
|
||||
super(node);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Nullable
|
||||
public PsiTypeElement getReturnType() {
|
||||
PsiTypeElement element = findChildByClass(PsiTypeElement.class);
|
||||
assert element != null;
|
||||
return element;
|
||||
return findChildByClass(PsiTypeElement.class);
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@Nullable
|
||||
public SmaliMethodParamList getParameterList() {
|
||||
SmaliMethodParamList paramList = findChildByClass(SmaliMethodParamList.class);
|
||||
assert paramList != null;
|
||||
return paramList;
|
||||
return findChildByClass(SmaliMethodParamList.class);
|
||||
}
|
||||
}
|
||||
|
@ -37,21 +37,23 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jf.smalidea.psi.SmaliElementTypes;
|
||||
import org.jf.smalidea.psi.impl.SmaliMethod;
|
||||
|
||||
public class SmaliMethodStub extends StubBase<SmaliMethod> {
|
||||
@NotNull private final String name;
|
||||
@NotNull private final String returnType;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
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);
|
||||
this.name = name;
|
||||
this.returnType = returnType;
|
||||
}
|
||||
|
||||
@NotNull public String getName() {
|
||||
@Nullable public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@NotNull public String getReturnType() {
|
||||
@Nullable public String getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
}
|
||||
|
@ -32,10 +32,12 @@
|
||||
package org.jf.smalidea.psi.stub.element;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.PsiType;
|
||||
import com.intellij.psi.stubs.IndexSink;
|
||||
import com.intellij.psi.stubs.StubElement;
|
||||
import com.intellij.psi.stubs.StubInputStream;
|
||||
import com.intellij.psi.stubs.StubOutputStream;
|
||||
import com.intellij.util.io.StringRef;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jf.smalidea.psi.impl.SmaliMethod;
|
||||
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) {
|
||||
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
|
||||
@ -73,7 +80,13 @@ public class SmaliMethodElementType extends SmaliStubElementType<SmaliMethodStub
|
||||
|
||||
@NotNull @Override
|
||||
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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user