Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
main.com.xml.guard.utils.Helper.kt Maven / Gradle / Ivy
package com.xml.guard.utils
import java.io.File
import java.util.regex.Pattern
import kotlin.random.Random
/**
* User: ljx
* Date: 2022/3/22
* Time: 10:31
*/
private val packageNameBlackList by lazy {
hashSetOf(
"in", "is", "as", "if", "do", "by",
"new", "try", "int", "for", "out", "var", "val", "fun",
"byte", "void", "this", "else", "case", "open", "enum", "true", "unit", "goto", "null", "char", "todo",
"false", "inner", "long", "super", "while", "break", "float", "final", "short", "const", "throw", "class", "catch",
"return", "static", "import", "assert", "inline", "object", "sealed", "vararg", "double", "native",
"suspend", "reified", "extends", "switch", "public", "throws", "package", "default", "finally", "private", "boolean",
"continue", "noinline", "lateinit", "internal", "abstract", "strictfp", "volatile",
"companion", "protected", "interface", "transient",
"implements", "instanceof", "constructor", "synchronized",
)
}
private val classNameBlackList by lazy { hashSetOf("R", "BR") }
private val packagePattern by lazy { Pattern.compile("\\s*package\\s+(.*)") }
private val importRPattern by lazy { Pattern.compile("\\s*import\\s+(.*)\\.R;?\$") }
fun String.inClassNameBlackList() = this in classNameBlackList
fun String.inPackageNameBlackList() = this in packageNameBlackList
// 随机生成数(from - until)
fun generateNumbers(
from: Int = 1,
until: Int = 26
): Int {
return Random.nextInt(from, until)
}
//插入 import xx.xx.xx.R import xx.xx.xx.BuildConfig 语句,
fun File.insertImportXxxIfAbsent(newPackage: String): String {
val text = readText()
val importR = "import $newPackage.R"
val importBR = "import $newPackage.BR"
val importBuildConfig = "import $newPackage.BuildConfig"
//如果使用了R类但没有导入,则需要导入
val importedR = importRPattern.matcher(text).find()
val needImportR = text.findWord("R.") != -1 && text.findWord(importR) == -1 && !importedR
//如果使用了BR类但没有导入,则需要导入
val needImportBR = text.findWord("BR.") != -1 && text.findWord(importBR) == -1
//如果使用了BuildConfig类但没有导入,则需要导入
val needImportBuildConfig = text.findWord("BuildConfig.") != -1 &&
text.findWord(importBuildConfig) == -1
if (!needImportR && !needImportBR && !needImportBuildConfig) return text
return insertNeedImport(text) {
if (needImportR) {
//import xx.xx.xx.R
append(importR)
if (name.endsWith(".java")) {
append(";")
}
append("\n")
}
if (needImportBR) {
//import xx.xx.xx.BR
append(importBR)
if (name.endsWith(".java")) {
append(";")
}
append("\n")
}
if (needImportBuildConfig) {
//import xx.xx.xx.BuildConfig
append(importBuildConfig)
if (name.endsWith(".java")) {
append(";")
}
append("\n")
}
}.also { writeText(it) }
}
// 插入字符串混淆器的路劲
fun File.insertImportStringFog(fogPackage: String): String {
val text = readText()
val importFog = "import $fogPackage"
val needImport = text.findWord(importFog) == -1
if (!needImport) return text
return insertNeedImport(text) {
append(importFog)
if (name.endsWith(".java")) {
append(";")
}
append("\n")
}.also { writeText(it) }
}
private fun insertNeedImport(
text: String,
block: StringBuilder.() -> Unit
): String {
val builder = StringBuilder()
val matcher = packagePattern.matcher(text)
if (matcher.find()) {
val packageStatement = matcher.group()
val packageIndex = text.indexOf(packageStatement)
if (packageIndex > 0) {
builder.append(text.substring(0, packageIndex))
}
builder.append(text.substring(packageIndex, packageIndex + packageStatement.length))
builder.append("\n\n")
builder.apply(block)
builder.append(text.substring(packageIndex + packageStatement.length))
}
return builder.toString()
}