use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class MethodAnalyzerTest method testInstanceOfNarrowingAfterMove_dalvik.
@Test
public void testInstanceOfNarrowingAfterMove_dalvik() throws IOException {
MethodImplementationBuilder builder = new MethodImplementationBuilder(3);
builder.addInstruction(new BuilderInstruction12x(Opcode.MOVE_OBJECT, 1, 2));
builder.addInstruction(new BuilderInstruction22c(Opcode.INSTANCE_OF, 0, 1, new ImmutableTypeReference("Lmain;")));
builder.addInstruction(new BuilderInstruction21t(Opcode.IF_EQZ, 0, builder.getLabel("not_instance_of")));
builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
builder.addLabel("not_instance_of");
builder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
MethodImplementation methodImplementation = builder.getMethodImplementation();
Method method = new ImmutableMethod("Lmain;", "narrowing", Collections.singletonList(new ImmutableMethodParameter("Ljava/lang/Object;", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, methodImplementation);
ClassDef classDef = new ImmutableClassDef("Lmain;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, null, null, Collections.singletonList(method));
DexFile dexFile = new ImmutableDexFile(Opcodes.getDefault(), Collections.singletonList(classDef));
ClassPath classPath = new ClassPath(new DexClassProvider(dexFile));
MethodAnalyzer methodAnalyzer = new MethodAnalyzer(classPath, method, null, false);
List<AnalyzedInstruction> analyzedInstructions = methodAnalyzer.getAnalyzedInstructions();
Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(3).getPreInstructionRegisterType(1).type.getType());
Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(3).getPreInstructionRegisterType(2).type.getType());
Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(4).getPreInstructionRegisterType(1).type.getType());
Assert.assertEquals("Ljava/lang/Object;", analyzedInstructions.get(4).getPreInstructionRegisterType(2).type.getType());
}
use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class SuperclassChainTest method testGetSuperclassChain_Unresolved.
@Test
public void testGetSuperclassChain_Unresolved() throws IOException {
// Ltest/one; isn't defined
ClassDef twoClassDef = TestUtils.makeClassDef("Ltest/two;", "Ltest/one;");
ClassDef threeClassDef = TestUtils.makeClassDef("Ltest/three;", "Ltest/two;");
ImmutableSet<ClassDef> classes = ImmutableSet.<ClassDef>of(twoClassDef, threeClassDef);
ClassPath classPath = new ClassPath(new DexClassProvider(new ImmutableDexFile(Opcodes.getDefault(), classes)));
TypeProto unknownClassProto = classPath.getUnknownClass();
TypeProto oneClassProto = classPath.getClass("Ltest/one;");
TypeProto twoClassProto = classPath.getClass("Ltest/two;");
TypeProto threeClassProto = classPath.getClass("Ltest/three;");
Assert.assertEquals(ImmutableList.<TypeProto>of(oneClassProto, unknownClassProto), ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(twoClassProto)));
Assert.assertEquals(ImmutableList.<TypeProto>of(twoClassProto, oneClassProto, unknownClassProto), ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(threeClassProto)));
}
use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class SuperclassChainTest method testGetSuperclassChain.
@Test
public void testGetSuperclassChain() throws IOException {
ClassDef objectClassDef = TestUtils.makeClassDef("Ljava/lang/Object;", null);
ClassDef oneClassDef = TestUtils.makeClassDef("Ltest/one;", "Ljava/lang/Object;");
ClassDef twoClassDef = TestUtils.makeClassDef("Ltest/two;", "Ltest/one;");
ClassDef threeClassDef = TestUtils.makeClassDef("Ltest/three;", "Ltest/two;");
ImmutableSet<ClassDef> classes = ImmutableSet.<ClassDef>of(objectClassDef, oneClassDef, twoClassDef, threeClassDef);
ClassPath classPath = new ClassPath(new DexClassProvider(new ImmutableDexFile(Opcodes.getDefault(), classes)));
TypeProto objectClassProto = classPath.getClass("Ljava/lang/Object;");
TypeProto oneClassProto = classPath.getClass("Ltest/one;");
TypeProto twoClassProto = classPath.getClass("Ltest/two;");
TypeProto threeClassProto = classPath.getClass("Ltest/three;");
Assert.assertEquals(ImmutableList.<TypeProto>of(), ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(objectClassProto)));
Assert.assertEquals(ImmutableList.<TypeProto>of(objectClassProto), ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(oneClassProto)));
Assert.assertEquals(ImmutableList.<TypeProto>of(oneClassProto, objectClassProto), ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(twoClassProto)));
Assert.assertEquals(ImmutableList.<TypeProto>of(twoClassProto, oneClassProto, objectClassProto), ImmutableList.copyOf(TypeProtoUtils.getSuperclassChain(threeClassProto)));
}
use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class DexPool method writeTo.
public static void writeTo(@Nonnull String path, @Nonnull org.jf.dexlib2.iface.DexFile input) throws IOException {
DexPool dexPool = new DexPool(input.getOpcodes());
for (ClassDef classDef : input.getClasses()) {
dexPool.internClass(classDef);
}
dexPool.writeTo(new FileDataStore(new File(path)));
}
use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class AccessorTest method testAccessors.
@Test
public void testAccessors() throws IOException {
URL url = AccessorTest.class.getClassLoader().getResource("accessorTest.dex");
Assert.assertNotNull(url);
DexFile f = DexFileFactory.loadDexFile(url.getFile(), Opcodes.getDefault());
SyntheticAccessorResolver sar = new SyntheticAccessorResolver(f.getOpcodes(), f.getClasses());
ClassDef accessorTypesClass = null;
ClassDef accessorsClass = null;
for (ClassDef classDef : f.getClasses()) {
String className = classDef.getType();
if (className.equals("Lorg/jf/dexlib2/AccessorTypes;")) {
accessorTypesClass = classDef;
} else if (className.equals("Lorg/jf/dexlib2/AccessorTypes$Accessors;")) {
accessorsClass = classDef;
}
}
Assert.assertNotNull(accessorTypesClass);
Assert.assertNotNull(accessorsClass);
for (Method method : accessorsClass.getMethods()) {
Matcher m = accessorMethodPattern.matcher(method.getName());
if (!m.matches()) {
continue;
}
String type = m.group(1);
String operation = m.group(2);
MethodImplementation methodImpl = method.getImplementation();
Assert.assertNotNull(methodImpl);
for (Instruction instruction : methodImpl.getInstructions()) {
Opcode opcode = instruction.getOpcode();
if (opcode == Opcode.INVOKE_STATIC || opcode == Opcode.INVOKE_STATIC_RANGE) {
MethodReference accessorMethod = (MethodReference) ((ReferenceInstruction) instruction).getReference();
SyntheticAccessorResolver.AccessedMember accessedMember = sar.getAccessedMember(accessorMethod);
Assert.assertNotNull(String.format("Could not resolve accessor for %s_%s", type, operation), accessedMember);
int operationType = operationTypes.get(operation);
Assert.assertEquals(operationType, accessedMember.accessedMemberType);
Assert.assertEquals(String.format("%s_val", type), ((FieldReference) accessedMember.accessedMember).getName());
}
}
}
}
Aggregations