use of org.jf.dexlib2.util.SyntheticAccessorResolver 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();
}
use of org.jf.dexlib2.util.SyntheticAccessorResolver in project atlas by alibaba.
the class SmaliDiffUtils method getBuildOption.
public static BaksmaliOptions getBuildOption(Iterable<? extends ClassDef> collection, int apiLevel) {
BaksmaliOptions options = new BaksmaliOptions();
options.deodex = false;
options.parameterRegisters = false;
options.localsDirective = true;
options.sequentialLabels = true;
options.debugInfo = true;
options.codeOffsets = false;
options.accessorComments = false;
// 128
options.registerInfo = 0;
options.inlineResolver = null;
options.apiLevel = apiLevel;
if (!options.accessorComments) {
options.syntheticAccessorResolver = new SyntheticAccessorResolver(Opcodes.getDefault(), collection);
}
return options;
}
use of org.jf.dexlib2.util.SyntheticAccessorResolver in project atlas by alibaba.
the class SmaliCodeUtils method createBaksmaliOptions.
/**
* 生成解析成smali代码的选项
*
* @return
*/
private static BaksmaliOptions createBaksmaliOptions(ClassDef classDef) {
BaksmaliOptions options = new BaksmaliOptions();
options.deodex = false;
options.parameterRegisters = false;
options.localsDirective = true;
options.sequentialLabels = true;
options.debugInfo = false;
options.codeOffsets = false;
options.accessorComments = false;
// 128
options.registerInfo = 0;
options.inlineResolver = null;
options.apiLevel = DEFAULT_API_LEVEL;
List<ClassDef> classDefs = Lists.newArrayList();
classDefs.add(classDef);
options.syntheticAccessorResolver = new SyntheticAccessorResolver(Opcodes.getDefault(), classDefs);
return options;
}
use of org.jf.dexlib2.util.SyntheticAccessorResolver in project atlas by alibaba.
the class BakSmali method disassembleDexFile.
public static boolean disassembleDexFile(DexFile dexFile, final baksmaliOptions options) {
if (options.registerInfo != 0 || options.deodex) {
try {
Iterable<String> extraClassPathEntries;
if (options.extraClassPathEntries != null) {
extraClassPathEntries = options.extraClassPathEntries;
} else {
extraClassPathEntries = ImmutableList.of();
}
options.classPath = ClassPath.fromClassPath(options.bootClassPathDirs, Iterables.concat(options.bootClassPathEntries, extraClassPathEntries), dexFile, options.apiLevel, options.checkPackagePrivateAccess, options.experimental);
if (options.customInlineDefinitions != null) {
options.inlineResolver = new CustomInlineMethodResolver(options.classPath, options.customInlineDefinitions);
}
} catch (Exception ex) {
System.err.println("\n\nError occurred while loading boot class path files. Aborting.");
ex.printStackTrace(System.err);
return false;
}
}
if (options.resourceIdFileEntries != null) {
class PublicHandler extends DefaultHandler {
String prefix = null;
public PublicHandler(String prefix) {
super();
this.prefix = prefix;
}
public void startElement(String uri, String localName, String qName, Attributes attr) throws SAXException {
if (qName.equals("public")) {
String type = attr.getValue("type");
String name = attr.getValue("name").replace('.', '_');
Integer public_key = Integer.decode(attr.getValue("id"));
String public_val = new StringBuffer().append(prefix).append(".").append(type).append(".").append(name).toString();
options.resourceIds.put(public_key, public_val);
}
}
}
;
for (Map.Entry<String, String> entry : options.resourceIdFileEntries.entrySet()) {
try {
SAXParser saxp = SAXParserFactory.newInstance().newSAXParser();
String prefix = entry.getValue();
saxp.parse(entry.getKey(), new PublicHandler(prefix));
} catch (ParserConfigurationException e) {
continue;
} catch (SAXException e) {
continue;
} catch (IOException e) {
continue;
}
}
}
File outputDirectoryFile = new File(options.outputDirectory);
if (!outputDirectoryFile.exists()) {
if (!outputDirectoryFile.mkdirs()) {
System.err.println("Can't create the output directory " + options.outputDirectory);
return false;
}
}
//sort the classes, so that if we're on a case-insensitive file system and need to handle classes with file
//name collisions, then we'll use the same name for each class, if the dex file goes through multiple
//baksmali/smali cycles for some reason. If a class with a colliding name is added or removed, the filenames
//may still change of course
List<? extends ClassDef> classDefs = Ordering.natural().sortedCopy(dexFile.getClasses());
if (!options.noAccessorComments) {
options.syntheticAccessorResolver = new SyntheticAccessorResolver(classDefs);
}
final ClassFileNameHandler fileNameHandler = new ClassFileNameHandler(outputDirectoryFile, ".smali");
ExecutorService executor = Executors.newFixedThreadPool(options.jobs);
List<Future<Boolean>> tasks = Lists.newArrayList();
for (final ClassDef classDef : classDefs) {
tasks.add(executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return disassembleClass(classDef, fileNameHandler, options);
}
}));
}
boolean errorOccurred = false;
try {
for (Future<Boolean> task : tasks) {
while (true) {
try {
if (!task.get()) {
errorOccurred = true;
}
} catch (InterruptedException ex) {
continue;
} catch (ExecutionException ex) {
throw new RuntimeException(ex);
}
break;
}
}
} finally {
executor.shutdown();
}
return !errorOccurred;
}
use of org.jf.dexlib2.util.SyntheticAccessorResolver in project smali by JesusFreke.
the class AccessorTest method testAccessors.
@Test
public void testAccessors() throws IOException {
URL url = AccessorTest.class.getClassLoader().getResource("accessorTest.dex");
Assert.assertNotNull(url);
DexFile f = DexFileFactory.loadDexFile(url.getFile(), Opcodes.getDefault());
SyntheticAccessorResolver sar = new SyntheticAccessorResolver(f.getOpcodes(), f.getClasses());
ClassDef accessorTypesClass = null;
ClassDef accessorsClass = null;
for (ClassDef classDef : f.getClasses()) {
String className = classDef.getType();
if (className.equals("Lorg/jf/dexlib2/AccessorTypes;")) {
accessorTypesClass = classDef;
} else if (className.equals("Lorg/jf/dexlib2/AccessorTypes$Accessors;")) {
accessorsClass = classDef;
}
}
Assert.assertNotNull(accessorTypesClass);
Assert.assertNotNull(accessorsClass);
for (Method method : accessorsClass.getMethods()) {
Matcher m = accessorMethodPattern.matcher(method.getName());
if (!m.matches()) {
continue;
}
String type = m.group(1);
String operation = m.group(2);
MethodImplementation methodImpl = method.getImplementation();
Assert.assertNotNull(methodImpl);
for (Instruction instruction : methodImpl.getInstructions()) {
Opcode opcode = instruction.getOpcode();
if (opcode == Opcode.INVOKE_STATIC || opcode == Opcode.INVOKE_STATIC_RANGE) {
MethodReference accessorMethod = (MethodReference) ((ReferenceInstruction) instruction).getReference();
SyntheticAccessorResolver.AccessedMember accessedMember = sar.getAccessedMember(accessorMethod);
Assert.assertNotNull(String.format("Could not resolve accessor for %s_%s", type, operation), accessedMember);
int operationType = operationTypes.get(operation);
Assert.assertEquals(operationType, accessedMember.accessedMemberType);
Assert.assertEquals(String.format("%s_val", type), ((FieldReference) accessedMember.accessedMember).getName());
}
}
}
}
Aggregations