-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fee18cd
commit c6499b0
Showing
6 changed files
with
83 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
booster-gradle-plugin/src/main/kotlin/com/didiglobal/booster/gradle/TransformerLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.didiglobal.booster.gradle | ||
|
||
import com.didiglobal.booster.transform.Transformer | ||
import java.net.URL | ||
import java.nio.charset.StandardCharsets | ||
import java.util.ServiceConfigurationError | ||
|
||
private const val PREFIX = "META-INF/services/" | ||
|
||
/** | ||
* Load [Transformer]s with the specified [classLoader] | ||
*/ | ||
@Throws(ServiceConfigurationError::class) | ||
internal fun loadTransformers(classLoader: ClassLoader): List<Transformer> = classLoader.getResources(PREFIX + Transformer::class.java.name)?.asSequence()?.map(::parse)?.flatten()?.toSet()?.mapNotNull { provider -> | ||
try { | ||
val transformerClass = Class.forName(provider, false, classLoader) | ||
if (!Transformer::class.java.isAssignableFrom(transformerClass)) { | ||
throw ServiceConfigurationError("Provider $provider not a subtype") | ||
} | ||
|
||
try { | ||
transformerClass.getConstructor(ClassLoader::class.java).newInstance(classLoader) as Transformer | ||
} catch (e: NoSuchMethodException) { | ||
transformerClass.newInstance() as Transformer | ||
} | ||
} catch (e: ClassNotFoundException) { | ||
throw ServiceConfigurationError("Provider $provider not found") | ||
} | ||
} ?: emptyList() | ||
|
||
@Throws(ServiceConfigurationError::class) | ||
private fun parse(u: URL) = try { | ||
u.openStream().bufferedReader(StandardCharsets.UTF_8).readLines().filter { | ||
it.isNotEmpty() && it.isNotBlank() && !it.startsWith('#') | ||
}.map(String::trim).filter(::isJavaClassName) | ||
} catch (e: Throwable) { | ||
emptyList<String>() | ||
} | ||
|
||
private fun isJavaClassName(text: String): Boolean { | ||
if (!Character.isJavaIdentifierStart(text[0])) { | ||
throw ServiceConfigurationError("Illegal provider-class name: $text") | ||
} | ||
|
||
for (i in 1 until text.length) { | ||
val cp = text.codePointAt(i) | ||
if (!Character.isJavaIdentifierPart(cp) && cp != '.'.toInt()) { | ||
throw ServiceConfigurationError("Illegal provider-class name: $text") | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters