fix: correct method arg name if unused
This commit is contained in:
@@ -238,10 +238,11 @@ public class MethodGen {
|
||||
classGen.useType(code, argType);
|
||||
}
|
||||
code.add(' ');
|
||||
if (code.isMetadataSupported() && ssaVar != null) {
|
||||
String varName = nameGen.assignArg(var);
|
||||
if (code.isMetadataSupported() && ssaVar != null /* for fallback mode */) {
|
||||
code.attachDefinition(VarDeclareRef.get(mth, var));
|
||||
}
|
||||
code.add(nameGen.assignArg(var));
|
||||
code.add(varName);
|
||||
|
||||
i++;
|
||||
if (it.hasNext()) {
|
||||
|
||||
@@ -99,9 +99,6 @@ public class MethodNode extends NotificationAttrNode implements IMethodDetails,
|
||||
@Override
|
||||
public void unload() {
|
||||
loaded = false;
|
||||
if (noCode) {
|
||||
return;
|
||||
}
|
||||
// don't unload retType, argTypes, typeParameters
|
||||
thisArg = null;
|
||||
argsList = null;
|
||||
|
||||
@@ -26,9 +26,6 @@ public class InitCodeVariables extends AbstractVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(MethodNode mth) throws JadxException {
|
||||
if (mth.isNoCode()) {
|
||||
return;
|
||||
}
|
||||
initCodeVars(mth);
|
||||
}
|
||||
|
||||
@@ -42,16 +39,24 @@ public class InitCodeVariables extends AbstractVisitor {
|
||||
private static void initCodeVars(MethodNode mth) {
|
||||
RegisterArg thisArg = mth.getThisArg();
|
||||
if (thisArg != null) {
|
||||
initCodeVar(thisArg.getSVar());
|
||||
initCodeVar(mth, thisArg);
|
||||
}
|
||||
for (RegisterArg mthArg : mth.getArgRegs()) {
|
||||
initCodeVar(mthArg.getSVar());
|
||||
initCodeVar(mth, mthArg);
|
||||
}
|
||||
for (SSAVar ssaVar : mth.getSVars()) {
|
||||
initCodeVar(ssaVar);
|
||||
}
|
||||
}
|
||||
|
||||
public static void initCodeVar(MethodNode mth, RegisterArg regArg) {
|
||||
SSAVar ssaVar = regArg.getSVar();
|
||||
if (ssaVar == null) {
|
||||
ssaVar = mth.makeNewSVar(regArg);
|
||||
}
|
||||
initCodeVar(ssaVar);
|
||||
}
|
||||
|
||||
public static void initCodeVar(SSAVar ssaVar) {
|
||||
if (ssaVar.isCodeVarSet()) {
|
||||
return;
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package jadx.tests.integration.variables;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jadx.api.data.annotations.VarDeclareRef;
|
||||
import jadx.core.dex.nodes.ClassNode;
|
||||
import jadx.core.dex.nodes.MethodNode;
|
||||
import jadx.tests.api.IntegrationTest;
|
||||
|
||||
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
|
||||
|
||||
public class TestVariablesDeclAnnotation extends IntegrationTest {
|
||||
|
||||
public abstract static class TestCls {
|
||||
public int test(String str, int i) {
|
||||
return i;
|
||||
}
|
||||
|
||||
public abstract int test2(String str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
noDebugInfo();
|
||||
ClassNode cls = getClassNode(TestCls.class);
|
||||
assertThat(cls).code()
|
||||
.containsOne("public int test(String str, int i) {")
|
||||
.containsOne("public abstract int test2(String str);");
|
||||
|
||||
checkArgNamesInMethod(cls, "test", "[str, i]");
|
||||
checkArgNamesInMethod(cls, "test2", "[str]");
|
||||
}
|
||||
|
||||
private static void checkArgNamesInMethod(ClassNode cls, String mthName, String expectedVars) {
|
||||
MethodNode testMth = cls.searchMethodByShortName(mthName);
|
||||
assertThat(testMth).isNotNull();
|
||||
|
||||
int mthLine = testMth.getDecompiledLine();
|
||||
List<String> argNames = cls.getCode().getAnnotations().entrySet().stream()
|
||||
.filter(e -> e.getKey().getLine() == mthLine && e.getValue() instanceof VarDeclareRef)
|
||||
.sorted(Comparator.comparingInt(e -> e.getKey().getPos()))
|
||||
.map(e -> ((VarDeclareRef) e.getValue()).getName())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertThat(argNames).doesNotContainNull();
|
||||
assertThat(argNames.toString()).isEqualTo(expectedVars);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user