Skip to content

Commit fb75b3a

Browse files
committed
fix: 重构问题修复
1 parent 17e7a9b commit fb75b3a

File tree

11 files changed

+72
-45
lines changed

11 files changed

+72
-45
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
buildscript {
44

55
ext {
6-
kotlin_version = '1.3.72'
6+
kotlin_version = '1.4.0'
77
ANDROID_COMPILE_SDK = 28
88
ANDROID_MIN_SDK = 16
99
ANDROID_TARGET_SDK = 28
@@ -19,7 +19,7 @@ buildscript {
1919
}
2020

2121
dependencies {
22-
classpath 'com.android.tools.build:gradle:4.0.0'
22+
classpath 'com.android.tools.build:gradle:4.0.1'
2323
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
2424
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}"
2525
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ siteUrl=https://github.com/wzbos/Android-Rudolph-Router
1818
gitUrl=https://github.com/wzbos/Android-Rudolph-Router.git
1919
licenseName=The Apache Software License, Version 2.0
2020
licenseUrl=http://www.apache.org/licenses/LICENSE-2.0.txt
21-
rudolph_version=2.0.0-rc1
21+
rudolph_version=2.0.1
2222
developerId=wzbos
2323
developerName=zongbo.wu
2424
developerEmail=sckoo@163.com

rudolph-compiler/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ compileJava {
1818

1919
dependencies {
2020
implementation 'com.squareup:javapoet:1.11.1'
21-
implementation 'com.google.auto.service:auto-service-annotations:1.0-rc7'
22-
kapt 'com.google.auto.service:auto-service:1.0-rc7'
2321
implementation 'org.apache.commons:commons-lang3:3.5'
2422
implementation 'org.apache.commons:commons-collections4:4.1'
2523
implementation project(':rudolph-annotations')

rudolph-compiler/src/main/java/cn/wzbos/android/rudolph/RudolphProcessor.kt

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import cn.wzbos.android.rudolph.annotations.Component
66
import cn.wzbos.android.rudolph.annotations.Export
77
import cn.wzbos.android.rudolph.annotations.Extra
88
import cn.wzbos.android.rudolph.annotations.Route
9-
import com.google.auto.service.AutoService
109
import com.squareup.javapoet.*
1110
import org.apache.commons.collections4.CollectionUtils
1211
import org.apache.commons.lang3.StringUtils
@@ -28,7 +27,6 @@ import javax.tools.StandardLocation
2827
* Router Processor
2928
* Created by wuzongbo on 2017/5/30.
3029
*/
31-
@AutoService(Processor::class)
3230
@SupportedSourceVersion(SourceVersion.RELEASE_8)
3331
@SupportedAnnotationTypes(Constant.ANNOTATION_TYPE_ROUTE, Constant.ANNOTATION_TYPE_COMPONENT)
3432
class RudolphProcessor : AbstractProcessor() {
@@ -54,7 +52,7 @@ class RudolphProcessor : AbstractProcessor() {
5452
private val Gson = ClassName.get("com.google.gson", "Gson")
5553
private val Base64 = ClassName.get("android.util", "Base64")
5654
private val TypeToken = ClassName.get("com.google.gson.reflect", "TypeToken")
57-
private val clsApplication = ClassName.get("android.app", "Application")
55+
private val clsContext = ClassName.get("android.content", "Context")
5856
private val Bundle = ClassName.get("android.os", "Bundle")
5957
}
6058

@@ -95,17 +93,31 @@ class RudolphProcessor : AbstractProcessor() {
9593
override fun process(annotations: Set<TypeElement>, roundEnvironment: RoundEnvironment): Boolean {
9694
if (CollectionUtils.isNotEmpty(annotations)) {
9795
try {
98-
//构件路由表类的初始化方法 init(Application application)
99-
val builder = MethodSpec.methodBuilder("init")
96+
97+
val clsName = routeClsName
98+
val superInterfaceType = elements!!.getTypeElement(Constant.ROUTE_TABLE)
99+
100+
val clsBuilder = TypeSpec.classBuilder(clsName)
101+
.addJavadoc(Constant.WARNING_TIPS)
102+
.addSuperinterface(ClassName.get(superInterfaceType))
103+
.addModifiers(Modifier.PUBLIC);
104+
105+
//构件路由表类的初始化方法 init(Context context)
106+
val initMethodBuilder = MethodSpec.methodBuilder("init")
100107
.addAnnotation(Override::class.java)
101108
.addModifiers(Modifier.PUBLIC)
102-
.addParameter(ParameterSpec.builder(clsApplication, "application").build())
109+
.addParameter(ParameterSpec.builder(clsContext, "context").build())
103110
val components = roundEnvironment.getElementsAnnotatedWith(Component::class.java)
104111
if (CollectionUtils.isNotEmpty(components)) {
105112
for (element in components) {
106-
builder.addStatement("new \$T().init(application)", element)
113+
initMethodBuilder.addStatement("new \$T().init(context)", element)
107114
}
108115
}
116+
117+
//构件路由表类的初始化方法 register()
118+
val registerMethodBuilder = MethodSpec.methodBuilder("register")
119+
.addAnnotation(Override::class.java)
120+
.addModifiers(Modifier.PUBLIC)
109121
val routers = roundEnvironment.getElementsAnnotatedWith(Route::class.java)
110122
if (CollectionUtils.isNotEmpty(routers)) {
111123
var routetype: RouteType
@@ -144,18 +156,13 @@ class RudolphProcessor : AbstractProcessor() {
144156
logger.error("UnKnown route type:$kind")
145157
continue
146158
}
147-
generateRouteTable(builder, element, target, route, routetype)
159+
generateRouteTable(registerMethodBuilder, element, target, route, routetype)
148160
}
149161
}
150-
val superInterfaceType = elements!!.getTypeElement(Constant.ROUTE_TABLE)
151-
val clsName = routeClsName
152-
JavaFile.builder(Constant.PACKAGE_NAME,
153-
TypeSpec.classBuilder(clsName)
154-
.addJavadoc(Constant.WARNING_TIPS)
155-
.addSuperinterface(ClassName.get(superInterfaceType))
156-
.addModifiers(Modifier.PUBLIC)
157-
.addMethod(builder.build())
158-
.build()
162+
JavaFile.builder(Constant.PACKAGE_NAME, clsBuilder
163+
.addMethod(registerMethodBuilder.build())
164+
.addMethod(initMethodBuilder.build())
165+
.build()
159166
).build().writeTo(mFiler)
160167
writeClsNameToAssets(clsName)
161168
logger.info(">>> Generated $clsName <<<")
@@ -344,13 +351,13 @@ class RudolphProcessor : AbstractProcessor() {
344351
.addJavadoc("create new instance\n")
345352
.returns(interfaceClsName)
346353
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
347-
.addStatement("return builder()")
354+
.addStatement("return (\$T) builder().build().open()", interfaceClsName)
348355
.build())
349356
}
350357
}
351358

352359

353-
if (routeType != RouteType.SERVICE && !route.singleton) {
360+
if (routeType != RouteType.SERVICE || !route.singleton) {
354361
val routerBuilderClsName = ClassName.get(clsName, "Builder")
355362
val builderTypeSpec = generate(interfaceClsName, routerBuilderClsName, element, routeType)
356363
clsRouterBuilder.addType(builderTypeSpec)

rudolph-compiler/src/main/res/META-INF/services/javax.annotation.processing.Processor renamed to rudolph-compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor

File renamed without changes.

rudolph/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ dependencies {
3939
api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
4040
api "com.android.support:appcompat-v7:${appcompat_v7_version}"
4141
api "com.google.code.gson:gson:${gson_version}"
42-
// api "${publishedGroupId}:rudolph-annotations:${rudolph_version}"
4342
api project(':rudolph-annotations')
43+
// api "${publishedGroupId}:rudolph-annotations:${rudolph_version}"
4444
}
4545

4646
apply from: '../install.gradle'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.wzbos.android.rudolph
2+
3+
import android.content.Context
4+
5+
/**
6+
* 路由表接口
7+
*/
8+
interface IRouteComponent {
9+
/**
10+
* 初始化组件
11+
*
12+
* @param context Context
13+
*/
14+
fun init(context: Context)
15+
}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package cn.wzbos.android.rudolph
22

3-
import android.app.Application
4-
53
/**
64
* 路由表接口
75
*/
8-
interface IRouteTable {
6+
interface IRouteTable : IRouteComponent {
7+
98
/**
10-
* 初始化方法
11-
*
12-
* @param application Application
9+
* 注册路由
1310
*/
14-
fun init(application: Application?)
11+
fun register()
1512
}

rudolph/src/main/java/cn/wzbos/android/rudolph/Rudolph.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,24 @@ object Rudolph {
5151
try {
5252
val list = assetManager.list("rudolph")
5353
if (null != list && list.isNotEmpty()) {
54+
val tables: MutableList<IRouteTable> = ArrayList()
5455
for (className in list) {
5556
try {
5657
val clazz = Class.forName("cn.wzbos.android.rudolph.routes.$className")
5758
if (IRouteTable::class.java.isAssignableFrom(clazz)) {
58-
val iGroupInstance = clazz.newInstance() as IRouteTable
59-
iGroupInstance.init(application)
59+
val iRouteTable = clazz.newInstance() as IRouteTable
60+
iRouteTable.register()
61+
tables.add(iRouteTable)
6062
}
6163
} catch (e: ClassNotFoundException) {
6264
RLog.e(TAG, "初始化\"$className\"组件失败,请检查包名是否正确!", e)
6365
}
6466
}
67+
68+
//待所有组件注册完成后再进行初始化,防止组件之间相互访问
69+
for (iRouteTable in tables) {
70+
iRouteTable.init(application)
71+
}
6572
}
6673
this.isInitialized = true
6774
} catch (e: Exception) {
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package cn.wzbos.samplea;
22

3-
import android.app.Application;
3+
import android.content.Context;
44
import android.widget.Toast;
55

6+
import org.jetbrains.annotations.NotNull;
7+
8+
import cn.wzbos.android.rudolph.IRouteComponent;
69
import cn.wzbos.android.rudolph.annotations.Component;
7-
import cn.wzbos.android.rudolph.IRouteTable;
810

911
@Component
10-
public class TestComponent implements IRouteTable {
12+
public class TestComponent implements IRouteComponent {
1113

1214
@Override
13-
public void init(Application application) {
14-
Toast.makeText(application.getApplicationContext(), "Module A initialized!", Toast.LENGTH_SHORT).show();
15+
public void init(@NotNull Context context) {
16+
Toast.makeText(context.getApplicationContext(), "Module A initialized!", Toast.LENGTH_SHORT).show();
1517
}
1618
}

0 commit comments

Comments
 (0)