use of org.jf.dexlib2.immutable.ImmutableField in project atlas by alibaba.
the class PatchFieldTool method addField.
public static void addField(String inFile, String outFile, DexBackedClassDef dexBackedClassDef, ImmutableField immutableField) throws IOException {
DexFile dexFile = readDexFile(inFile);
for (ClassDef classDef : dexFile.getClasses()) {
if (dexBackedClassDef != null && dexBackedClassDef.getType().equals(classDef.getType())) {
dexFile = addField(dexFile, classDef.getType(), immutableField);
} else if (dexBackedClassDef == null) {
dexFile = addField(dexFile, classDef.getType(), immutableField);
}
}
reDexFile(dexFile);
DexFileFactory.writeDexFile(outFile, new DexFile() {
@Nonnull
@Override
public Set<? extends ClassDef> getClasses() {
return new AbstractSet<ClassDef>() {
@Nonnull
@Override
public Iterator<ClassDef> iterator() {
return classes.iterator();
}
@Override
public int size() {
return classes.size();
}
};
}
});
}
use of org.jf.dexlib2.immutable.ImmutableField 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());
}
}
use of org.jf.dexlib2.immutable.ImmutableField in project soot by Sable.
the class DexPrinter method addAsClassDefItem.
private void addAsClassDefItem(SootClass c) {
// add source file tag if any
String sourceFile = null;
if (c.hasTag("SourceFileTag")) {
SourceFileTag sft = (SourceFileTag) c.getTag("SourceFileTag");
sourceFile = sft.getSourceFile();
}
String classType = SootToDexUtils.getDexTypeDescriptor(c.getType());
int accessFlags = c.getModifiers();
String superClass = c.hasSuperclass() ? SootToDexUtils.getDexTypeDescriptor(c.getSuperclass().getType()) : null;
List<String> interfaces = null;
if (!c.getInterfaces().isEmpty()) {
interfaces = new ArrayList<String>();
for (SootClass ifc : c.getInterfaces()) interfaces.add(SootToDexUtils.getDexTypeDescriptor(ifc.getType()));
}
List<Field> fields = null;
if (!c.getFields().isEmpty()) {
fields = new ArrayList<Field>();
for (SootField f : c.getFields()) {
// We do not want to write out phantom fields
if (f.isPhantom())
continue;
// Look for a static initializer
EncodedValue staticInit = null;
for (Tag t : f.getTags()) {
if (t instanceof ConstantValueTag) {
if (staticInit != null) {
LOGGER.warn("More than one constant tag for field \"{}\": \"{}\"", f, t);
} else {
staticInit = makeConstantItem(f, t);
}
}
}
if (staticInit == null)
staticInit = BuilderEncodedValues.defaultValueForType(SootToDexUtils.getDexTypeDescriptor(f.getType()));
// Build field annotations
Set<Annotation> fieldAnnotations = buildFieldAnnotations(f);
ImmutableField field = new ImmutableField(classType, f.getName(), SootToDexUtils.getDexTypeDescriptor(f.getType()), f.getModifiers(), staticInit, fieldAnnotations);
fields.add(field);
}
}
Collection<Method> methods = toMethods(c);
ClassDef classDef = new ImmutableClassDef(classType, accessFlags, superClass, interfaces, sourceFile, buildClassAnnotations(c), fields, methods);
dexBuilder.internClass(classDef);
}
Aggregations