use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project atlas by alibaba.
the class ClassDefinition method writeInstanceFields.
private void writeInstanceFields(IndentingWriter writer, Set<String> staticFields) throws IOException {
if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
return;
}
boolean wroteHeader = false;
Set<String> writtenFields = new HashSet<String>();
Iterable<? extends Field> instanceFields;
if (classDef instanceof DexBackedClassDef) {
instanceFields = ((DexBackedClassDef) classDef).getInstanceFields(false);
} else {
instanceFields = classDef.getInstanceFields();
}
for (Field field : instanceFields) {
if (!wroteHeader) {
writer.write("\n\n");
writer.write("# instance fields");
wroteHeader = true;
}
writer.write('\n');
IndentingWriter fieldWriter = writer;
String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
if (!writtenFields.add(fieldString)) {
writer.write("# duplicate field ignored\n");
fieldWriter = new CommentingIndentingWriter(writer);
System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
} else if (staticFields.contains(fieldString)) {
System.err.println(String.format("Duplicate static+instance field found: %s->%s", classDef.getType(), fieldString));
System.err.println("You will need to rename one of these fields, including all references.");
writer.write("# There is both a static and instance field with this signature.\n" + "# You will need to rename one of these fields, including all references.\n");
}
FieldDefinition.writeTo(options, fieldWriter, field, false);
}
}
use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project atlas by alibaba.
the class ClassDefinition method writeStaticFields.
private Set<String> writeStaticFields(IndentingWriter writer) throws IOException {
if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
return null;
}
boolean wroteHeader = false;
Set<String> writtenFields = new HashSet<String>();
Iterable<? extends Field> staticFields;
if (classDef instanceof DexBackedClassDef) {
staticFields = ((DexBackedClassDef) classDef).getStaticFields(false);
} else {
staticFields = classDef.getStaticFields();
}
for (Field field : staticFields) {
if (!wroteHeader) {
writer.write("\n\n");
writer.write("# static fields");
wroteHeader = true;
}
writer.write('\n');
boolean setInStaticConstructor;
IndentingWriter fieldWriter = writer;
String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
if (!writtenFields.add(fieldString)) {
writer.write("# duplicate field ignored\n");
fieldWriter = new CommentingIndentingWriter(writer);
System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
setInStaticConstructor = false;
} else {
setInStaticConstructor = fieldsSetInStaticConstructor.contains(fieldString);
}
FieldDefinition.writeTo(options, fieldWriter, field, setInStaticConstructor);
}
return writtenFields;
}
use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project atlas by alibaba.
the class DexDiffer method compareFields.
/**
* 比较二个dex中的类的字段差异性
*
* @param baseClassDef
* @param newClassDef
* @param classDiffInfo
*/
private boolean compareFields(DexBackedClassDef baseClassDef, DexBackedClassDef newClassDef, ClassDiffInfo classDiffInfo) throws PatchException {
Map<String, DexBackedField> baseFieldMaps = Maps.newHashMap();
for (DexBackedField backedField : baseClassDef.getFields()) {
baseFieldMaps.put(ReferenceUtil.getFieldDescriptor(backedField), backedField);
}
for (DexBackedField newField : newClassDef.getFields()) {
FieldDiffInfo fieldDiffInfo = new FieldDiffInfo();
fieldDiffInfo.setBackedField(newField);
String fieldDesc = ReferenceUtil.getFieldDescriptor(newField);
DexBackedField baseField = baseFieldMaps.get(fieldDesc);
if (null == baseField) {
fieldDiffInfo.setType(DiffType.ADD);
classDiffInfo.getModifyFields().add(fieldDiffInfo);
continue;
}
// 首先判断字段的声明,包括access flag等是否一致
// 判断初始化值
EncodedValue baseInitaValue = baseField.getInitialValue();
EncodedValue newInitaValue = newField.getInitialValue();
if (!Objects.equal(baseInitaValue, newInitaValue)) {
fieldDiffInfo.setType(DiffType.MODIFY);
classDiffInfo.getModifyFields().add(fieldDiffInfo);
baseFieldMaps.remove(fieldDesc);
continue;
}
//注解
Set<? extends DexBackedAnnotation> backedAnnotations = baseField.getAnnotations();
if (!equalsImpl(backedAnnotations, newField.getAnnotations())) {
fieldDiffInfo.setType(DiffType.MODIFY);
classDiffInfo.getModifyFields().add(fieldDiffInfo);
baseFieldMaps.remove(fieldDesc);
continue;
}
//type
if (!baseField.getType().equals(newField.getType())) {
fieldDiffInfo.setType(DiffType.MODIFY);
classDiffInfo.getModifyFields().add(fieldDiffInfo);
baseFieldMaps.remove(fieldDesc);
continue;
}
if (baseField.getAccessFlags() != newField.getAccessFlags()) {
fieldDiffInfo.setType(DiffType.MODIFY);
classDiffInfo.getModifyFields().add(fieldDiffInfo);
baseFieldMaps.remove(fieldDesc);
continue;
}
baseFieldMaps.remove(fieldDesc);
}
// 如果字段已经移除
if (baseFieldMaps.size() > 0) {
for (Map.Entry<String, DexBackedField> entry : baseFieldMaps.entrySet()) {
FieldDiffInfo fieldDiffInfo = new FieldDiffInfo();
fieldDiffInfo.setBackedField(entry.getValue());
fieldDiffInfo.setType(DiffType.REMOVE);
classDiffInfo.getModifyFields().add(fieldDiffInfo);
}
}
if (classDiffInfo.getModifyFields().size() > 0) {
return true;
}
return false;
}
use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project atlas by alibaba.
the class SmaliDiffUtils method scanClasses.
public static Set<DexBackedClassDef> scanClasses(File smaliDir, List<File> newFiles) throws PatchException {
Set<DexBackedClassDef> classes = Sets.newHashSet();
try {
for (File newFile : newFiles) {
DexBackedDexFile newDexFile = DexFileFactory.loadDexFile(newFile, 19, true);
Set<? extends DexBackedClassDef> dexClasses = newDexFile.getClasses();
classes.addAll(dexClasses);
}
final ClassFileNameHandler outFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
for (DexBackedClassDef classDef : classes) {
String className = classDef.getType();
ApkPatch.currentClassType = null;
AfBakSmali.disassembleClass(classDef, outFileNameHandler, getBuildOption(classes, 19), true, true);
File smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
}
} catch (Exception e) {
throw new PatchException(e);
}
return classes;
}
use of org.jf.dexlib2.dexbacked.DexBackedClassDef in project dex2jar by pxb1988.
the class SmaliTest method baksmali.
private static String baksmali(DexBackedClassDef def) throws IOException {
baksmaliOptions opts = new baksmaliOptions();
opts.outputDebugInfo = false;
opts.syntheticAccessorResolver = new SyntheticAccessorResolver(Collections.EMPTY_LIST);
ClassDefinition classDefinition = new ClassDefinition(opts, def);
StringWriter bufWriter = new StringWriter();
IndentingWriter writer = new IndentingWriter(bufWriter);
classDefinition.writeTo((IndentingWriter) writer);
writer.flush();
return bufWriter.toString();
}
Aggregations