use of org.jf.util.IndentingWriter in project atlas by alibaba.
the class ClassDefinition method writeDirectMethods.
private Set<String> writeDirectMethods(IndentingWriter writer) throws IOException {
boolean wroteHeader = false;
Set<String> writtenMethods = new HashSet<String>();
Iterable<? extends Method> directMethods;
Set<? extends Method> modifieds = null;
if (classDef instanceof DexBackedClassDef) {
directMethods = ((DexBackedClassDef) classDef).getDirectMethods(false);
modifieds = (Set<? extends Method>) DexDiffInfo.modifiedMethods;
} else {
directMethods = classDef.getDirectMethods();
}
MethodReplaceAnnotation replaceAnnotaion;
for (Method method : directMethods) {
if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
if (!modifieds.contains(method) && !DexDiffInfo.addedMethods.contains(method)) {
continue;
}
}
if (!wroteHeader) {
writer.write("\n\n");
writer.write("# direct methods");
wroteHeader = true;
}
writer.write('\n');
// TODO: check for method validation errors
String methodString = ReferenceUtil.getMethodDescriptor(method, true);
IndentingWriter methodWriter = writer;
if (!writtenMethods.add(methodString)) {
writer.write("# duplicate method ignored\n");
methodWriter = new CommentingIndentingWriter(writer);
}
MethodImplementation methodImpl = method.getImplementation();
if (methodImpl == null) {
MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
} else {
MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
methodDefinition.setFullMethod(fullMethod);
methodDefinition.writeTo(methodWriter);
}
}
return writtenMethods;
}
use of org.jf.util.IndentingWriter in project atlas by alibaba.
the class ClassDefinition method writeVirtualMethods.
private void writeVirtualMethods(IndentingWriter writer, Set<String> directMethods) throws IOException {
boolean wroteHeader = false;
Set<String> writtenMethods = new HashSet<String>();
Iterable<? extends Method> virtualMethods;
Set<? extends Method> modifieds = null;
if (classDef instanceof DexBackedClassDef) {
virtualMethods = ((DexBackedClassDef) classDef).getVirtualMethods(false);
modifieds = (Set<? extends Method>) DexDiffInfo.modifiedMethods;
} else {
virtualMethods = classDef.getVirtualMethods();
}
MethodReplaceAnnotation replaceAnnotaion;
for (Method method : virtualMethods) {
if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
if (!modifieds.contains(method) && !DexDiffInfo.addedMethods.contains(method)) {
continue;
}
}
if (!wroteHeader) {
writer.write("\n\n");
writer.write("# virtual methods");
wroteHeader = true;
}
writer.write('\n');
// TODO: check for method validation errors
String methodString = ReferenceUtil.getMethodDescriptor(method, true);
IndentingWriter methodWriter = writer;
if (!writtenMethods.add(methodString)) {
writer.write("# duplicate method ignored\n");
methodWriter = new CommentingIndentingWriter(writer);
} else if (directMethods.contains(methodString)) {
writer.write("# There is both a direct and virtual method with this signature.\n" + "# You will need to rename one of these methods, including all references.\n");
System.err.println(String.format("Duplicate direct+virtual method found: %s->%s", classDef.getType(), methodString));
System.err.println("You will need to rename one of these methods, including all references.");
}
MethodImplementation methodImpl = method.getImplementation();
if (methodImpl == null) {
MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
} else {
MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
methodDefinition.writeTo(methodWriter);
}
}
}
use of org.jf.util.IndentingWriter in project smali by JesusFreke.
the class AnalysisTest method runTest.
public void runTest(String test, boolean registerInfo, boolean isArt) throws IOException, URISyntaxException {
String dexFilePath = String.format("%s%sclasses.dex", test, File.separatorChar);
DexFile dexFile = DexFileFactory.loadDexFile(findResource(dexFilePath), Opcodes.getDefault());
BaksmaliOptions options = new BaksmaliOptions();
if (registerInfo) {
options.registerInfo = BaksmaliOptions.ALL | BaksmaliOptions.FULLMERGE;
if (isArt) {
options.classPath = new ClassPath(new ArrayList<ClassProvider>(), true, 56);
} else {
options.classPath = new ClassPath();
}
}
options.implicitReferences = false;
for (ClassDef classDef : dexFile.getClasses()) {
StringWriter stringWriter = new StringWriter();
IndentingWriter writer = new IndentingWriter(stringWriter);
ClassDefinition classDefinition = new ClassDefinition(options, classDef);
classDefinition.writeTo(writer);
writer.close();
String className = classDef.getType();
String smaliPath = String.format("%s%s%s.smali", test, File.separatorChar, className.substring(1, className.length() - 1));
String smaliContents = readResource(smaliPath);
Assert.assertEquals(TextUtils.normalizeWhitespace(smaliContents), TextUtils.normalizeWhitespace((stringWriter.toString())));
}
}
use of org.jf.util.IndentingWriter in project smali by JesusFreke.
the class ClassDefinition method writeStaticFields.
private Set<String> writeStaticFields(IndentingWriter writer) throws IOException {
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.util.IndentingWriter in project otertool by wuntee.
the class SmaliWorkshop method getSmaliSource.
public static Map<String, File> getSmaliSource(File sourceSmaliOrDexFile, File destinationDirectory) throws IOException {
Map<String, File> ret = new HashMap<String, File>();
DexFile dexFile = new DexFile(sourceSmaliOrDexFile);
IndentingWriter idWriter;
for (ClassDefItem c : dexFile.ClassDefsSection.getItems()) {
File classFile = SmaliWorkshop.createSmaliClassFile(destinationDirectory, c);
String className = SmaliWorkshop.classDefItemToFilename(c);
logger.debug("Got class: " + className + " [" + classFile + "]");
BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(classFile)));
idWriter = new IndentingWriter(fileWriter);
ClassDefinition cd = new ClassDefinition(c);
cd.writeTo(idWriter);
ret.put(className, classFile);
idWriter.close();
}
return (sortMapByKey(ret));
}
Aggregations