-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement dynamic code loading using code generator x reflection
- Loading branch information
1 parent
3096f22
commit ccd5642
Showing
18 changed files
with
187 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
package com.wisnu.ordertiket | ||
|
||
import android.app.Application | ||
import com.wisnu.feature.order.flight.OrderFlightFeatureApplication | ||
import com.wisnu.feature.order.hotel.OrderHotelFeatureApplication | ||
import com.wisnu.feature.order.train.OrderTrainFeatureApplication | ||
import com.wisnu.feature.setting.SettingFeatureApplication | ||
import com.wisnu.feature.transaction.TransactionFeatureApplication | ||
import com.wisnu.launcher.annotations.RegisterFeature | ||
import com.wisnu.launcher.main.BaseLauncher | ||
import com.wisnu.launcher.main.FeatureApplicationClassName | ||
|
||
@RegisterFeature( | ||
FeatureApplicationClassName.ORDER_FLIGHT, | ||
FeatureApplicationClassName.ORDER_HOTEL, | ||
FeatureApplicationClassName.ORDER_TRAIN, | ||
FeatureApplicationClassName.TRANSACTION, | ||
FeatureApplicationClassName.SETTING | ||
) | ||
class MainLauncher(application: Application) : BaseLauncher(application) { | ||
init { | ||
registerFeatureApplication(OrderFlightFeatureApplication()) | ||
registerFeatureApplication(OrderHotelFeatureApplication()) | ||
registerFeatureApplication(OrderTrainFeatureApplication()) | ||
registerFeatureApplication(TransactionFeatureApplication()) | ||
registerFeatureApplication(SettingFeatureApplication()) | ||
MainLauncherUtils.buildFeatures() | ||
.forEach { registerFeatureApplication(it) } | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
apply plugin: 'java-library' | ||
|
||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
} | ||
|
||
sourceCompatibility = "7" | ||
targetCompatibility = "7" |
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
launcher/annotations/src/main/java/com/wisnu/launcher/annotations/RegisterFeature.java
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,12 @@ | ||
package com.wisnu.launcher.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.CLASS) | ||
public @interface RegisterFeature { | ||
String[] value(); | ||
} |
This file was deleted.
Oops, something went wrong.
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
14 changes: 14 additions & 0 deletions
14
launcher/compiler/src/main/java/com/wisnu/launcher/compiler/ClassExtensions.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,14 @@ | ||
package com.wisnu.launcher.compiler | ||
|
||
private val classMap = mutableMapOf<String, Class<*>>() | ||
|
||
private inline fun <reified T : Any> Any.castOrNull() = this as? T | ||
|
||
internal fun <T> String.loadClassOrNull(): Class<out T>? = | ||
classMap.getOrPut(this) { | ||
try { | ||
Class.forName(this) | ||
} catch (e: ClassNotFoundException) { | ||
return null | ||
} | ||
}.castOrNull() |
32 changes: 32 additions & 0 deletions
32
launcher/compiler/src/main/java/com/wisnu/launcher/compiler/FeatureAnnotatedClass.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,32 @@ | ||
package com.wisnu.launcher.compiler | ||
|
||
import com.squareup.javapoet.ClassName | ||
import com.wisnu.launcher.annotations.RegisterFeature | ||
import javax.lang.model.element.TypeElement | ||
import javax.lang.model.util.Elements | ||
|
||
class FeatureAnnotatedClass(element: TypeElement, elementUtils: Elements) { | ||
|
||
val className: ClassName | ||
val features: Array<String> | ||
|
||
init { | ||
val packageName = getPackageName(elementUtils, element) | ||
val originalClassName = getClassName(element, packageName) | ||
this.className = ClassName.get(packageName, originalClassName) | ||
this.features = element.getAnnotation(RegisterFeature::class.java).value | ||
} | ||
|
||
private fun getPackageName(elementUtils: Elements, type: TypeElement): String { | ||
return elementUtils.getPackageOf(type).qualifiedName.toString() | ||
} | ||
|
||
private fun getClassName(element: TypeElement, packageName: String): String { | ||
return element.qualifiedName.toString().substring(packageName.length + 1).replace('.', '$') | ||
} | ||
|
||
override fun toString(): String { | ||
return className.toString() | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
launcher/compiler/src/main/java/com/wisnu/launcher/compiler/FeatureListWriter.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,42 @@ | ||
package com.wisnu.launcher.compiler | ||
|
||
import com.squareup.javapoet.* | ||
import java.io.IOException | ||
import java.util.* | ||
import javax.annotation.processing.Filer | ||
import javax.lang.model.element.Modifier | ||
|
||
class FeatureListWriter(private val featureAnnotatedClass: FeatureAnnotatedClass) { | ||
|
||
@Throws(IOException::class) | ||
fun write(filer: Filer) { | ||
val classBuilder = TypeSpec.classBuilder("${featureAnnotatedClass.className.simpleName()}Utils") | ||
classBuilder.addModifiers(Modifier.PUBLIC, Modifier.FINAL) | ||
classBuilder.addMethod(createBuildFeaturesMethod()) | ||
JavaFile.builder(featureAnnotatedClass.className.packageName(), classBuilder.build()).build().writeTo(filer) | ||
} | ||
|
||
private fun createBuildFeaturesMethod(): MethodSpec { | ||
val methodName = "buildFeatures" | ||
val localVariableName = "Features" | ||
val featureType = ClassName.get("com.wisnu.launcher.main", "FeatureApplication") | ||
val listType = ClassName.get(List::class.java) | ||
val arrayListType = ClassName.get(ArrayList::class.java) | ||
val returnType = ParameterizedTypeName.get(listType, featureType) | ||
|
||
val buildFeatures = MethodSpec.methodBuilder(methodName) | ||
.addModifiers(Modifier.STATIC) | ||
.addModifiers(Modifier.FINAL) | ||
.returns(returnType) | ||
.addStatement("\$T $localVariableName = new \$T<>()", returnType, arrayListType) | ||
|
||
featureAnnotatedClass.features | ||
.filter { it.loadClassOrNull<Any>() != null } | ||
.forEach { buildFeatures.addStatement("$localVariableName.add(new $it())") } | ||
|
||
buildFeatures.addStatement("return $localVariableName") | ||
|
||
return buildFeatures.build() | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
launcher/compiler/src/main/java/com/wisnu/launcher/compiler/FeatureProcessor.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,49 @@ | ||
package com.wisnu.launcher.compiler | ||
|
||
import com.wisnu.launcher.annotations.RegisterFeature | ||
import java.io.IOException | ||
import javax.annotation.processing.* | ||
import javax.lang.model.SourceVersion | ||
import javax.lang.model.element.TypeElement | ||
import javax.lang.model.util.Elements | ||
import javax.tools.Diagnostic | ||
import kotlin.properties.Delegates | ||
import javax.annotation.processing.ProcessingEnvironment | ||
|
||
class FeatureProcessor : AbstractProcessor() { | ||
private var filer: Filer by Delegates.notNull() | ||
private var messager: Messager by Delegates.notNull() | ||
private var elementUtils: Elements by Delegates.notNull() | ||
|
||
@Synchronized | ||
override fun init(processingEnv: ProcessingEnvironment) { | ||
super.init(processingEnv) | ||
filer = processingEnv.filer | ||
messager = processingEnv.messager | ||
elementUtils = processingEnv.elementUtils | ||
} | ||
|
||
override fun getSupportedSourceVersion(): SourceVersion = SourceVersion.latestSupported() | ||
|
||
override fun getSupportedAnnotationTypes(): Set<String> = hashSetOf(RegisterFeature::class.java.canonicalName) | ||
|
||
override fun process(annotations: Set<TypeElement>, roundEnv: RoundEnvironment): Boolean { | ||
parseEnv(roundEnv, elementUtils) | ||
.map { FeatureListWriter(it) } | ||
.forEach { | ||
try { | ||
it.write(filer) | ||
} catch (e: IOException) { | ||
messager.printMessage(Diagnostic.Kind.ERROR, e.message) | ||
} | ||
} | ||
return true | ||
} | ||
|
||
private fun parseEnv(roundEnv: RoundEnvironment, elementUtils: Elements): List<FeatureAnnotatedClass> { | ||
return roundEnv.getElementsAnnotatedWith(RegisterFeature::class.java).map { | ||
FeatureAnnotatedClass(it as TypeElement, elementUtils) | ||
} | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
launcher/compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor
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 @@ | ||
com.wisnu.launcher.compiler.FeatureProcessor |
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