|
| 1 | +package org.ollide.java2smali |
| 2 | + |
| 3 | +import com.intellij.openapi.command.WriteCommandAction |
| 4 | +import com.intellij.openapi.diagnostic.Logger |
| 5 | +import com.intellij.openapi.fileEditor.OpenFileDescriptor |
| 6 | +import com.intellij.openapi.module.Module |
| 7 | +import com.intellij.openapi.project.Project |
| 8 | +import com.intellij.openapi.roots.* |
| 9 | +import com.intellij.openapi.vfs.LocalFileSystem |
| 10 | +import com.intellij.openapi.vfs.VirtualFile |
| 11 | +import com.intellij.openapi.vfs.VirtualFileManager |
| 12 | +import com.intellij.psi.PsiClassOwner |
| 13 | +import com.intellij.psi.PsiManager |
| 14 | +import com.intellij.task.ProjectTaskManager |
| 15 | +import com.intellij.util.SmartList |
| 16 | +import com.intellij.util.containers.OrderedSet |
| 17 | +import com.intellij.util.io.URLUtil |
| 18 | +import java.io.File |
| 19 | +import java.io.IOException |
| 20 | +import java.nio.file.Paths |
| 21 | + |
| 22 | +class DexCompiler(private val vFile: VirtualFile, private val project: Project, private val module: Module) { |
| 23 | + |
| 24 | + fun run() { |
| 25 | + buildModule { |
| 26 | + onProjectBuildComplete() |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * To create a dex or smali file from the virtual file, we ne a compiled .class file |
| 32 | + * of the given virtual file to be present. |
| 33 | + * |
| 34 | + * Project structures and builds vary a lot (Android, Java, Kotlin, directory structure), |
| 35 | + * so instead of using the CompileManager, we trigger a general build task. |
| 36 | + */ |
| 37 | + private fun buildModule(callback: () -> Unit) { |
| 38 | + val projectTaskManager = ProjectTaskManager.getInstance(project) |
| 39 | + val buildTask = projectTaskManager.createModulesBuildTask(module, true, true, true) |
| 40 | + |
| 41 | + val supportProjectTaskManager = SupportProjectTaskManager(projectTaskManager) |
| 42 | + supportProjectTaskManager.run(buildTask).onSuccess { |
| 43 | + if (it.hasErrors()) { |
| 44 | + LOG.warn("Module build failed, aborting dex/smali build.") |
| 45 | + } else { |
| 46 | + callback() |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private fun onProjectBuildComplete() { |
| 52 | + val file = PsiManager.getInstance(project).findFile(vFile) as PsiClassOwner |
| 53 | + |
| 54 | + val fileOutputDirectory = getFileOutputDirectory(file) |
| 55 | + fileOutputDirectory.refresh(false, false) |
| 56 | + |
| 57 | + val fileName = vFile.nameWithoutExtension |
| 58 | + val dexFilePath = Paths.get(fileOutputDirectory.path, fileName + DEX_EXTENSION).toString() |
| 59 | + |
| 60 | + // CLASS -> DEX |
| 61 | + val targetFiles = getClassFiles(fileOutputDirectory, fileName) |
| 62 | + compileDexFile(targetFiles, dexFilePath) |
| 63 | + |
| 64 | + // DEX -> SMALI |
| 65 | + val outputDir = getSourceRootFile().path |
| 66 | + WriteCommandAction.runWriteCommandAction(project) { |
| 67 | + Dex2SmaliHelper.disassembleDexFile(dexFilePath, outputDir) |
| 68 | + |
| 69 | + // we've created the smali file(s) in our source file's directory |
| 70 | + // refresh directory synchronously and access children to let IDEA detect the file(s) |
| 71 | + val parent = vFile.parent |
| 72 | + parent.refresh(false, false) |
| 73 | + parent.children |
| 74 | + } |
| 75 | + |
| 76 | + // get a VirtualFile by the IO path |
| 77 | + val smaliPath = vFile.path.substringBeforeLast('.') + SMALI_EXTENSION |
| 78 | + val virtualDexFile = LocalFileSystem.getInstance().findFileByIoFile(File(smaliPath)) ?: return |
| 79 | + |
| 80 | + // use the VirtualFile to show the smali file in IDEA editor |
| 81 | + val openFileDescriptor = OpenFileDescriptor(project, virtualDexFile) |
| 82 | + openFileDescriptor.navigate(true) |
| 83 | + } |
| 84 | + |
| 85 | + private fun getClassFiles(fileOutputDirectory: VirtualFile, fileName: String): Array<String> { |
| 86 | + val children = fileOutputDirectory.children ?: arrayOf() |
| 87 | + return children.filter { |
| 88 | + val baseName = it.nameWithoutExtension |
| 89 | + (baseName == fileName || baseName.startsWith("$fileName$")) && it.extension == CLASS |
| 90 | + }.map { |
| 91 | + it.path |
| 92 | + }.toTypedArray() |
| 93 | + } |
| 94 | + |
| 95 | + private fun getFileOutputDirectory(file: PsiClassOwner): VirtualFile { |
| 96 | + // determine whether this is a production or test file |
| 97 | + val isProduction = module.getModuleScope(false).contains(vFile) |
| 98 | + |
| 99 | + val pkg = file.packageName.replace('.', File.separatorChar) |
| 100 | + |
| 101 | + // find the general output directory of the file's module (target, app/build/intermediates/javac/$variant/classes, ...) |
| 102 | + val possibleOutputDirectories = findModuleOutputDirectories(isProduction) |
| 103 | + LOG.debug("Possible output directories: ", possibleOutputDirectories.joinToString(",")) |
| 104 | + |
| 105 | + val virtualFileManager = VirtualFileManager.getInstance().getFileSystem(URLUtil.FILE_PROTOCOL) |
| 106 | + |
| 107 | + val fileOutputDirectory = possibleOutputDirectories |
| 108 | + .asSequence() |
| 109 | + .map { |
| 110 | + val classFile = vFile.nameWithoutExtension + CLASS_EXTENSION |
| 111 | + val path = Paths.get(it, pkg, classFile).toString() |
| 112 | + virtualFileManager.refreshAndFindFileByPath(path)?.parent |
| 113 | + } |
| 114 | + .firstOrNull { it != null } |
| 115 | + |
| 116 | + LOG.debug("Found output directory: $fileOutputDirectory") |
| 117 | + return fileOutputDirectory ?: throw IllegalStateException("Output directory not found") |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @see <a href="https://github.com/JetBrains/intellij-community/blob/master/java/compiler/openapi/src/com/intellij/openapi/compiler/CompilerPaths.java">intellij-community/CompilerPaths.java</a> |
| 122 | + */ |
| 123 | + private fun findModuleOutputDirectories(production: Boolean): OrderedSet<String> { |
| 124 | + val outputPaths: MutableList<String> = mutableListOf() |
| 125 | + |
| 126 | + val compilerExtension = CompilerModuleExtension.getInstance(module) |
| 127 | + if (production) { |
| 128 | + compilerExtension?.compilerOutputPath?.path?.let { outputPaths.add(it) } |
| 129 | + } else { |
| 130 | + compilerExtension?.compilerOutputPathForTests?.path?.let { outputPaths.add(it) } |
| 131 | + } |
| 132 | + |
| 133 | + val moduleRootManager = ModuleRootManager.getInstance(module) |
| 134 | + for (handlerFactory in OrderEnumerationHandler.EP_NAME.extensions) { |
| 135 | + if (handlerFactory.isApplicable(module)) { |
| 136 | + val handler = handlerFactory.createHandler(module) |
| 137 | + val outputUrls: List<String> = SmartList() |
| 138 | + handler.addCustomModuleRoots(OrderRootType.CLASSES, moduleRootManager, outputUrls, production, !production) |
| 139 | + for (outputUrl in outputUrls) { |
| 140 | + outputPaths.add(VirtualFileManager.extractPath(outputUrl).replace('/', File.separatorChar)) |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + return OrderedSet(outputPaths) |
| 145 | + } |
| 146 | + |
| 147 | + private fun compileDexFile(compiledPaths: Array<String>, dexFile: String) { |
| 148 | + try { |
| 149 | + Class2DexHelper.dexClassFile(compiledPaths, dexFile) |
| 150 | + } catch (e: IOException) { |
| 151 | + e.printStackTrace() |
| 152 | + return |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private fun getSourceRootFile(): VirtualFile { |
| 157 | + return ProjectRootManager.getInstance(project).fileIndex.getSourceRootForFile(vFile) as VirtualFile |
| 158 | + } |
| 159 | + |
| 160 | + companion object { |
| 161 | + |
| 162 | + private val LOG = Logger.getInstance(DexCompiler::class.java) |
| 163 | + |
| 164 | + const val CLASS_EXTENSION = ".class" |
| 165 | + const val DEX_EXTENSION = ".dex" |
| 166 | + const val SMALI_EXTENSION = ".smali" |
| 167 | + const val CLASS = "class" |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments