use of org.jf.dexlib2.iface.ClassDef 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.iface.ClassDef 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.iface.ClassDef 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.iface.ClassDef in project atlas by alibaba.
the class BakSmali method disassembleClass.
public static boolean disassembleClass(ClassDef classDef, ClassFileNameHandler fileNameHandler, baksmaliOptions options) {
/**
* The path for the disassembly file is based on the package name
* The class descriptor will look something like:
* Ljava/lang/Object;
* Where the there is leading 'L' and a trailing ';', and the parts of the
* package name are separated by '/'
*/
String classDescriptor = classDef.getType();
//validate that the descriptor is formatted like we expect
if (classDescriptor.charAt(0) != 'L' || classDescriptor.charAt(classDescriptor.length() - 1) != ';') {
System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class");
return false;
}
File smaliFile = fileNameHandler.getUniqueFilenameForClass(classDescriptor);
//create and initialize the top level string template
ClassDefinition classDefinition = new ClassDefinition(options, classDef);
//write the disassembly
Writer writer = null;
try {
File smaliParent = smaliFile.getParentFile();
if (!smaliParent.exists()) {
if (!smaliParent.mkdirs()) {
// check again, it's likely it was created in a different thread
if (!smaliParent.exists()) {
System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class");
return false;
}
}
}
if (!smaliFile.exists()) {
if (!smaliFile.createNewFile()) {
System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class");
return false;
}
}
BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(smaliFile), "UTF8"));
writer = new IndentingWriter(bufWriter);
classDefinition.writeTo((IndentingWriter) writer);
} catch (Exception ex) {
System.err.println("\n\nError occurred while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class");
ex.printStackTrace();
// noinspection ResultOfMethodCallIgnored
smaliFile.delete();
return false;
} finally {
if (writer != null) {
try {
writer.close();
} catch (Throwable ex) {
System.err.println("\n\nError occurred while closing file " + smaliFile.toString());
ex.printStackTrace();
}
}
}
return true;
}
use of org.jf.dexlib2.iface.ClassDef in project atlas by alibaba.
the class AbIClassDef method reClassDef.
@Override
public ClassDef reClassDef(ClassDef classDef) {
Iterable<? extends Method> methods = classDef.getMethods();
LinkedHashSet<Method> newMethods = new LinkedHashSet<Method>();
Iterable<? extends Field> fields = classDef.getFields();
LinkedHashSet<Field> newFields = new LinkedHashSet<Field>();
Set<? extends Annotation> annotations = classDef.getAnnotations();
Set<String> interfaces = classDef.getInterfaces();
Set<String> newInterfaces = new HashSet<String>();
Set<Annotation> immutableAnnotations = new HashSet<Annotation>();
String type = classDef.getType();
reType = reType(type);
String superClass = classDef.getSuperclass();
for (String inter : interfaces) {
newInterfaces.add(reInterface(inter));
}
String reSuperClass = reSuperClass(superClass);
for (Annotation annotation : annotations) {
if (type.startsWith("Landroid/taobao/atlas/bundleInfo/AtlasBundleInfoManager;")) {
System.out.println("xxxx");
}
immutableAnnotations.add(reAnnotation(annotation));
}
for (Field field : fields) {
newFields.add(reField(field));
}
for (Method method : methods) {
if (method.getName().equals("<cinit>") || method.getName().equals("<init>")) {
newMethods.add(reMethod(method));
continue;
}
// if (method.getName().equals("getArchiveFile")) {
newMethods.add(reMethod(method));
// }
}
return new ImmutableClassDef(reType, classDef.getAccessFlags(), reSuperClass, newInterfaces, classDef.getSourceFile(), immutableAnnotations, newFields, newMethods);
}
Aggregations