Skip to content

Commit

Permalink
fix #157 : service implementations could not be loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonlee authored and neighbWang committed Apr 16, 2020
1 parent e5ce227 commit 546266e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.didiglobal.booster.build

import java.util.ServiceLoader

/**
* @author johnsonlee
*/
class BoosterServiceLoader {

companion object {

fun <S : Any> load(service: Class<S>, classLoader: ClassLoader): List<S> = listOf(
ServiceLoader.load(service),
ServiceLoader.load(service, classLoader)
).flatten().distinctBy {
it.javaClass
}

}

}
2 changes: 1 addition & 1 deletion booster-command/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ apply from: '../gradle/booster.gradle'

dependencies {
api gradleApi()
api project(":booster-kotlinx")
api project(":booster-build")
api "commons-io:commons-io:2.6"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.didiglobal.booster.command

import com.didiglobal.booster.build.BoosterServiceLoader
import java.io.File
import java.util.ServiceLoader

/**
* Represents a command service
Expand All @@ -10,7 +10,7 @@ class CommandService {

companion object {

private val commands = ServiceLoader.load(CommandProvider::class.java, Command::class.java.classLoader).map {
private val commands = BoosterServiceLoader.load(CommandProvider::class.java, Command::class.java.classLoader).map {
it.get()
}.flatten().map {
it.name to it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.didiglobal.booster.gradle
import com.android.build.gradle.AppExtension
import com.android.build.gradle.LibraryExtension
import com.didiglobal.booster.annotations.Priority
import com.didiglobal.booster.build.BoosterServiceLoader
import com.didiglobal.booster.task.spi.VariantProcessor
import org.gradle.api.Plugin
import org.gradle.api.Project
import java.util.ServiceLoader

/**
* Represents the booster gradle plugin
Expand Down Expand Up @@ -47,7 +47,7 @@ class BoosterPlugin : Plugin<Project> {
}

private val variantProcessors: Collection<VariantProcessor>
get() = ServiceLoader.load(VariantProcessor::class.java, javaClass.classLoader).sortedBy {
get() = BoosterServiceLoader.load(VariantProcessor::class.java, javaClass.classLoader).sortedBy {
it.javaClass.getAnnotation(Priority::class.java)?.value ?: 0
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import com.android.build.api.transform.TransformInvocation
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.internal.pipeline.TransformManager
import com.android.build.gradle.internal.pipeline.TransformManager.SCOPE_FULL_PROJECT
import com.didiglobal.booster.build.BoosterServiceLoader
import com.didiglobal.booster.transform.AbstractKlassPool
import com.didiglobal.booster.transform.Transformer
import org.gradle.api.Project
import java.util.ServiceLoader

/**
* Represents the transform base
Expand All @@ -21,7 +21,7 @@ open class BoosterTransform(val project: Project) : Transform() {
/*
* Preload transformers as List to fix NoSuchElementException caused by ServiceLoader in parallel mode
*/
internal val transformers = ServiceLoader.load(Transformer::class.java, javaClass.classLoader).toList()
internal val transformers = BoosterServiceLoader.load(Transformer::class.java, javaClass.classLoader).toList()

private val android: BaseExtension = project.getAndroid()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.didiglobal.booster.transform.asm

import com.didiglobal.booster.annotations.Priority
import com.didiglobal.booster.build.BoosterServiceLoader
import com.didiglobal.booster.transform.TransformContext
import com.didiglobal.booster.transform.Transformer
import com.google.auto.service.AutoService
Expand All @@ -11,7 +12,6 @@ import java.io.File
import java.io.InputStream
import java.lang.management.ManagementFactory
import java.lang.management.ThreadMXBean
import java.util.ServiceLoader
import java.util.jar.JarFile

/**
Expand All @@ -31,7 +31,7 @@ class AsmTransformer : Transformer {
/*
* Preload transformers as List to fix NoSuchElementException caused by ServiceLoader in parallel mode
*/
constructor() : this(*ServiceLoader.load(ClassTransformer::class.java, AsmTransformer::class.java.classLoader).toList().toTypedArray())
constructor() : this(*BoosterServiceLoader.load(ClassTransformer::class.java, AsmTransformer::class.java.classLoader).toList().toTypedArray())

/**
* For unit test only
Expand Down
2 changes: 1 addition & 1 deletion booster-transform-javassist/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply from: '../gradle/booster.gradle'
dependencies {
kapt "com.google.auto.service:auto-service:1.0-rc6"
api project(':booster-annotations')
api project(':booster-kotlinx')
api project(':booster-build')
api project(':booster-transform-spi')
api 'org.javassist:javassist:3.25.0-GA'
api 'com.google.auto.service:auto-service:1.0-rc6'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.didiglobal.booster.transform.javassist

import com.didiglobal.booster.annotations.Priority
import com.didiglobal.booster.build.BoosterServiceLoader
import com.didiglobal.booster.transform.TransformContext
import com.didiglobal.booster.transform.Transformer
import com.google.auto.service.AutoService
Expand All @@ -9,7 +10,6 @@ import java.io.ByteArrayOutputStream
import java.io.DataOutputStream
import java.lang.management.ManagementFactory
import java.lang.management.ThreadMXBean
import java.util.ServiceLoader

/**
* Represents bytecode transformer using Javassist
Expand All @@ -30,7 +30,7 @@ class JavassistTransformer : Transformer {
/*
* Preload transformers as List to fix NoSuchElementException caused by ServiceLoader in parallel mode
*/
constructor() : this(*ServiceLoader.load(ClassTransformer::class.java, JavassistTransformer::class.java.classLoader).toList().toTypedArray())
constructor() : this(*BoosterServiceLoader.load(ClassTransformer::class.java, JavassistTransformer::class.java.classLoader).toList().toTypedArray())

/**
* For unit test only
Expand Down

0 comments on commit 546266e

Please sign in to comment.