use of org.jf.dexlib2.iface.ClassDef in project android-classyshark by google.
the class RootBuilder method fillFromDex.
private void fillFromDex(File file, ClassNode rootNode) {
try {
DexFile dxFile = DexlibLoader.loadDexFile(file);
Set<? extends ClassDef> classSet = dxFile.getClasses();
for (ClassDef o : classSet) {
int methodCount = 0;
for (Method method : o.getMethods()) {
methodCount++;
}
String translatedClassName = o.getType().replaceAll("\\/", "\\.").substring(1, o.getType().length() - 1);
ClassInfo classInfo = new ClassInfo(translatedClassName, methodCount);
rootNode.add(classInfo);
}
} catch (Exception ex) {
System.err.println("Error parsing Dexfile: " + file.getName() + ": " + ex.getMessage());
ex.printStackTrace(System.err);
}
}
use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class SyntheticAccessorResolver method getAccessedMember.
@Nullable
public AccessedMember getAccessedMember(@Nonnull MethodReference methodReference) {
String methodDescriptor = ReferenceUtil.getMethodDescriptor(methodReference);
AccessedMember accessedMember = resolvedAccessors.get(methodDescriptor);
if (accessedMember != null) {
return accessedMember;
}
String type = methodReference.getDefiningClass();
ClassDef classDef = classDefMap.get(type);
if (classDef == null) {
return null;
}
Method matchedMethod = null;
MethodImplementation matchedMethodImpl = null;
for (Method method : classDef.getMethods()) {
MethodImplementation methodImpl = method.getImplementation();
if (methodImpl != null) {
if (methodReferenceEquals(method, methodReference)) {
matchedMethod = method;
matchedMethodImpl = methodImpl;
break;
}
}
}
if (matchedMethod == null) {
return null;
}
//A synthetic accessor will be marked synthetic
if (!AccessFlags.SYNTHETIC.isSet(matchedMethod.getAccessFlags())) {
return null;
}
List<Instruction> instructions = ImmutableList.copyOf(matchedMethodImpl.getInstructions());
int accessType = syntheticAccessorFSM.test(instructions);
if (accessType >= 0) {
AccessedMember member = new AccessedMember(accessType, ((ReferenceInstruction) instructions.get(0)).getReference());
resolvedAccessors.put(methodDescriptor, member);
return member;
}
return null;
}
use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class ImplicitReferenceTest method testImplicitMethodLiteral.
@Test
public void testImplicitMethodLiteral() throws RecognitionException, IOException {
ClassDef classDef = SmaliTestUtils.compileSmali("" + ".class public LHelloWorld;\n" + ".super Ljava/lang/Object;\n" + ".field public static field1:Ljava/lang/reflect/Method; = toString()V\n" + ".field public static field2:Ljava/lang/reflect/Method; = V()V\n" + ".field public static field3:Ljava/lang/reflect/Method; = I()V\n" + ".field public static field4:Ljava/lang/Class; = I");
Map<String, Field> fields = Maps.newHashMap();
for (Field field : classDef.getFields()) {
fields.put(field.getName(), field);
}
Field field = fields.get("field1");
Assert.assertNotNull(field);
Assert.assertNotNull(field.getInitialValue());
Assert.assertEquals(ValueType.METHOD, field.getInitialValue().getValueType());
MethodEncodedValue methodEncodedValue = (MethodEncodedValue) field.getInitialValue();
Assert.assertEquals(classDef.getType(), methodEncodedValue.getValue().getDefiningClass());
Assert.assertEquals("toString", methodEncodedValue.getValue().getName());
field = fields.get("field2");
Assert.assertNotNull(field);
Assert.assertNotNull(field.getInitialValue());
Assert.assertEquals(ValueType.METHOD, field.getInitialValue().getValueType());
methodEncodedValue = (MethodEncodedValue) field.getInitialValue();
Assert.assertEquals(classDef.getType(), methodEncodedValue.getValue().getDefiningClass());
Assert.assertEquals("V", methodEncodedValue.getValue().getName());
field = fields.get("field3");
Assert.assertNotNull(field);
Assert.assertNotNull(field.getInitialValue());
Assert.assertEquals(ValueType.METHOD, field.getInitialValue().getValueType());
methodEncodedValue = (MethodEncodedValue) field.getInitialValue();
Assert.assertEquals(classDef.getType(), methodEncodedValue.getValue().getDefiningClass());
Assert.assertEquals("I", methodEncodedValue.getValue().getName());
field = fields.get("field4");
Assert.assertNotNull(field);
Assert.assertNotNull(field.getInitialValue());
Assert.assertEquals(ValueType.TYPE, field.getInitialValue().getValueType());
TypeEncodedValue typeEncodedValue = (TypeEncodedValue) field.getInitialValue();
Assert.assertEquals("I", typeEncodedValue.getValue());
}
use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class ImplicitReferenceTest method testImplicitFieldReference.
@Test
public void testImplicitFieldReference() throws RecognitionException, IOException {
ClassDef classDef = SmaliTestUtils.compileSmali("" + ".class public LHelloWorld;\n" + ".super Ljava/lang/Object;\n" + ".method public static main([Ljava/lang/String;)V\n" + " .registers 1\n" + " sget-object v0, someField:I\n" + " sget-object v0, V:I\n" + " sget-object v0, I:I\n" + " return-void\n" + ".end method");
Method mainMethod = null;
for (Method method : classDef.getMethods()) {
if (method.getName().equals("main")) {
mainMethod = method;
}
}
Assert.assertNotNull(mainMethod);
MethodImplementation methodImpl = mainMethod.getImplementation();
Assert.assertNotNull(methodImpl);
List<Instruction> instructions = Lists.newArrayList(methodImpl.getInstructions());
Instruction21c instruction = (Instruction21c) instructions.get(0);
Assert.assertNotNull(instruction);
Assert.assertEquals(Opcode.SGET_OBJECT, instruction.getOpcode());
FieldReference field = (FieldReference) instruction.getReference();
Assert.assertEquals(classDef.getType(), field.getDefiningClass());
Assert.assertEquals("someField", field.getName());
instruction = (Instruction21c) instructions.get(1);
Assert.assertNotNull(instruction);
Assert.assertEquals(Opcode.SGET_OBJECT, instruction.getOpcode());
field = (FieldReference) instruction.getReference();
Assert.assertEquals(classDef.getType(), field.getDefiningClass());
Assert.assertEquals("V", field.getName());
instruction = (Instruction21c) instructions.get(2);
Assert.assertNotNull(instruction);
Assert.assertEquals(Opcode.SGET_OBJECT, instruction.getOpcode());
field = (FieldReference) instruction.getReference();
Assert.assertEquals(classDef.getType(), field.getDefiningClass());
Assert.assertEquals("I", field.getName());
}
use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class RollbackTest method testRollback.
@Test
public void testRollback() throws IOException {
ClassDef class1 = new ImmutableClassDef("Lcls1;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, Lists.newArrayList(new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lannotation;", null)), Lists.<Field>newArrayList(new ImmutableField("Lcls1;", "field1", "I", AccessFlags.PUBLIC.getValue(), null, null)), Lists.<Method>newArrayList(new ImmutableMethod("Lcls1", "method1", Lists.<MethodParameter>newArrayList(new ImmutableMethodParameter("L", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, null)));
ClassDef class2 = new ImmutableClassDef("Lcls2;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, Lists.newArrayList(new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lannotation2;", null)), Lists.<Field>newArrayList(new ImmutableField("Lcls2;", "field2", "D", AccessFlags.PUBLIC.getValue(), null, null)), Lists.<Method>newArrayList(new ImmutableMethod("Lcls2;", "method2", Lists.<MethodParameter>newArrayList(new ImmutableMethodParameter("D", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, null)));
RawDexFile dexFile1;
{
MemoryDataStore dataStore = new MemoryDataStore();
DexPool dexPool = new DexPool(Opcodes.getDefault());
dexPool.internClass(class1);
dexPool.mark();
dexPool.internClass(class2);
dexPool.reset();
dexPool.writeTo(dataStore);
dexFile1 = new RawDexFile(Opcodes.getDefault(), dataStore.getData());
}
RawDexFile dexFile2;
{
MemoryDataStore dataStore = new MemoryDataStore();
DexPool dexPool = new DexPool(Opcodes.getDefault());
dexPool.internClass(class1);
dexPool.writeTo(dataStore);
dexFile2 = new RawDexFile(Opcodes.getDefault(), dataStore.getData());
}
List<MapItem> mapItems1 = dexFile1.getMapItems();
List<MapItem> mapItems2 = dexFile2.getMapItems();
for (int i = 0; i < mapItems1.size(); i++) {
Assert.assertEquals(mapItems1.get(i).getType(), mapItems2.get(i).getType());
Assert.assertEquals(mapItems1.get(i).getItemCount(), mapItems2.get(i).getItemCount());
}
}
Aggregations