use of org.jf.dexlib2.iface.MethodImplementation in project atlas by alibaba.
the class PatchMethodTool method modifyMethodAndFix.
private static MethodImplementation modifyMethodAndFix(@Nonnull MethodImplementation implementation, Method method) {
MutableMethodImplementation mutableImplementation = new MutableMethodImplementation(implementation);
System.out.println(mutableImplementation.getRegisterCount());
List<BuilderInstruction> instructions = mutableImplementation.getInstructions();
mutableImplementation.addInstruction(0, new BuilderInstruction21c(Opcode.CONST_STRING, 0, new ImmutableStringReference("AndFix:" + method.getDefiningClass().replace("/", "."))));
mutableImplementation.addInstruction(1, new BuilderInstruction35c(Opcode.INVOKE_STATIC, 1, 0, 0, 0, 0, 0, new ImmutableMethodReference("Landroid/util/Log;", "e", Lists.newArrayList("Ljava/lang/String;", "Ljava/lang/String;"), "I")));
return mutableImplementation;
}
use of org.jf.dexlib2.iface.MethodImplementation in project atlas by alibaba.
the class PatchMethodTool method modifyMethodTpatch.
private static MethodImplementation modifyMethodTpatch(@Nonnull MethodImplementation implementation, Method method) {
MutableMethodImplementation mutableImplementation = new MutableMethodImplementation(implementation);
System.out.println(mutableImplementation.getRegisterCount());
List<BuilderInstruction> instructions = mutableImplementation.getInstructions();
boolean isModified = false;
for (int i = 0; i < instructions.size(); i++) {
isModified = false;
if (instructions.get(i).getOpcode() == Opcode.INVOKE_DIRECT) {
if (!isModified) {
mutableImplementation.addInstruction(i++, new BuilderInstruction21c(Opcode.CONST_STRING, 0, new ImmutableStringReference("tpatch:" + method.getDefiningClass().replace("/", "."))));
mutableImplementation.addInstruction(i++, new BuilderInstruction35c(Opcode.INVOKE_STATIC, 1, 0, 0, 0, 0, 0, new ImmutableMethodReference("Landroid/util/Log;", "e", Lists.newArrayList("Ljava/lang/String;", "Ljava/lang/String;"), "I")));
isModified = true;
break;
}
}
// mutableImplementation.addInstruction(instructions.get(i));
}
return mutableImplementation;
}
use of org.jf.dexlib2.iface.MethodImplementation in project smali by JesusFreke.
the class SmalideaMethod method getImplementation.
@Nullable
@Override
public MethodImplementation getImplementation() {
if (psiMethod instanceof SmaliMethod) {
final SmaliMethod smaliMethod = (SmaliMethod) this.psiMethod;
List<SmaliInstruction> instructions = smaliMethod.getInstructions();
if (instructions.size() == 0) {
return null;
}
// TODO: cache this?
return new MethodImplementation() {
@Override
public int getRegisterCount() {
return smaliMethod.getRegisterCount();
}
@Nonnull
@Override
public Iterable<? extends Instruction> getInstructions() {
return Lists.transform(smaliMethod.getInstructions(), new Function<SmaliInstruction, Instruction>() {
@Override
public Instruction apply(SmaliInstruction smaliInstruction) {
return SmalideaInstruction.of(smaliInstruction);
}
});
}
@Nonnull
@Override
public List<? extends TryBlock<? extends ExceptionHandler>> getTryBlocks() {
return Lists.transform(smaliMethod.getCatchStatements(), new Function<SmaliCatchStatement, TryBlock<? extends ExceptionHandler>>() {
@Override
public TryBlock<? extends ExceptionHandler> apply(SmaliCatchStatement smaliCatchStatement) {
assert smaliCatchStatement != null;
return new SmalideaTryBlock(smaliCatchStatement);
}
});
}
@Nonnull
@Override
public Iterable<? extends DebugItem> getDebugItems() {
// TODO: implement this
return ImmutableList.of();
}
};
}
return null;
}
use of org.jf.dexlib2.iface.MethodImplementation in project smali by JesusFreke.
the class SmalideaMethodTest method testSparseSwitch.
public void testSparseSwitch() {
String text = ".class public LFormat31t;\n" + ".super Ljava/lang/Object;\n" + ".source \"Format31t.smali\"" + "\n" + ".method public test_sparse-switch()V\n" + " .registers 1\n" + " .annotation runtime Lorg/junit/Test;\n" + " .end annotation\n" + "\n" + " const v0, 13\n" + "\n" + ":switch\n" + " sparse-switch v0, :SparseSwitch\n" + "\n" + ":Label10\n" + " invoke-static {}, Lorg/junit/Assert;->fail()V\n" + " return-void\n" + "\n" + ":Label20\n" + " invoke-static {}, Lorg/junit/Assert;->fail()V\n" + " return-void\n" + "\n" + ":Label15\n" + " invoke-static {}, Lorg/junit/Assert;->fail()V\n" + " return-void\n" + "\n" + ":Label13\n" + " return-void\n" + "\n" + ":Label99\n" + " invoke-static {}, Lorg/junit/Assert;->fail()V\n" + " return-void\n" + "\n" + ":SparseSwitch\n" + " .sparse-switch\n" + " 10 -> :Label10\n" + " 13 -> :Label13\n" + " 15 -> :Label15\n" + " 20 -> :Label20\n" + " 99 -> :Label99\n" + " .end sparse-switch\n" + ".end method";
SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", text);
SmaliClass smaliClass = file.getPsiClass();
SmaliMethod smaliMethod = smaliClass.getMethods()[0];
SmalideaMethod method = new SmalideaMethod(smaliMethod);
MethodImplementation impl = method.getImplementation();
Assert.assertNotNull(impl);
List<Instruction> instructions = Lists.newArrayList(impl.getInstructions());
SparseSwitchPayload sparseSwitchPayload = (SparseSwitchPayload) instructions.get(11);
List<? extends SwitchElement> switchElements = sparseSwitchPayload.getSwitchElements();
Assert.assertEquals(5, switchElements.size());
checkSwitchElement(switchElements.get(0), 10, 6);
checkSwitchElement(switchElements.get(1), 13, 30);
checkSwitchElement(switchElements.get(2), 15, 22);
checkSwitchElement(switchElements.get(3), 20, 14);
checkSwitchElement(switchElements.get(4), 99, 32);
}
use of org.jf.dexlib2.iface.MethodImplementation in project smali by JesusFreke.
the class ClassDefinition method writeDirectMethods.
private Set<String> writeDirectMethods(IndentingWriter writer) throws IOException {
boolean wroteHeader = false;
Set<String> writtenMethods = new HashSet<String>();
Iterable<? extends Method> directMethods;
if (classDef instanceof DexBackedClassDef) {
directMethods = ((DexBackedClassDef) classDef).getDirectMethods(false);
} else {
directMethods = classDef.getDirectMethods();
}
for (Method method : directMethods) {
if (!wroteHeader) {
writer.write("\n\n");
writer.write("# direct methods");
wroteHeader = true;
}
writer.write('\n');
// TODO: check for method validation errors
String methodString = ReferenceUtil.getMethodDescriptor(method, true);
IndentingWriter methodWriter = writer;
if (!writtenMethods.add(methodString)) {
writer.write("# duplicate method ignored\n");
methodWriter = new CommentingIndentingWriter(writer);
}
MethodImplementation methodImpl = method.getImplementation();
if (methodImpl == null) {
MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
} else {
MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
methodDefinition.writeTo(methodWriter);
}
}
return writtenMethods;
}
Aggregations